Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ApiRestAccountTest.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
41  {
42  public function testGetAccount()
43  {
44  $super = User::getByUsername('super');
45  Yii::app()->user->userModel = $super;
46 
47  $authenticationData = $this->login();
48  $headers = array(
49  'Accept: application/json',
50  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
51  'ZURMO_TOKEN: ' . $authenticationData['token'],
52  'ZURMO_API_REQUEST_TYPE: REST',
53  );
54 
55  $account = AccountTestHelper::createAccountByNameTypeAndIndustryForOwner('First Account', 'Customer',
56  'Automotive', $super);
57  $compareData = $this->getModelToApiDataUtilData($account);
58  $response = $this->createApiCallWithRelativeUrl('read/' . $account->id, 'GET', $headers);
59  $response = json_decode($response, true);
60  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
61  $this->assertEquals($compareData, $response['data']);
62  $this->assertArrayHasKey('owner', $response['data']);
63  $this->assertCount(2, $response['data']['owner']);
64  $this->assertArrayHasKey('id', $response['data']['owner']);
65  $this->assertEquals($super->id, $response['data']['owner']['id']);
66  $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']);
67  $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']);
68  $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']);
69  }
70 
74  public function testDeleteAccount()
75  {
76  Yii::app()->user->userModel = User::getByUsername('super');
77  $authenticationData = $this->login();
78  $headers = array(
79  'Accept: application/json',
80  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
81  'ZURMO_TOKEN: ' . $authenticationData['token'],
82  'ZURMO_API_REQUEST_TYPE: REST',
83  );
84  $accounts = Account::getByName('First Account');
85  $this->assertEquals(1, count($accounts));
86 
87  $response = $this->createApiCallWithRelativeUrl('delete/' . $accounts[0]->id, 'DELETE', $headers);
88  $response = json_decode($response, true);
89  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
90 
91  $response = $this->createApiCallWithRelativeUrl('read/' . $accounts[0]->id, 'GET', $headers);
92  $response = json_decode($response, true);
93  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
94  $this->assertEquals('The ID specified was invalid.', $response['message']);
95  }
96 
97  public function testCreateAccount()
98  {
99  $super = User::getByUsername('super');
100  Yii::app()->user->userModel = $super;
102  $authenticationData = $this->login();
103  $headers = array(
104  'Accept: application/json',
105  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
106  'ZURMO_TOKEN: ' . $authenticationData['token'],
107  'ZURMO_API_REQUEST_TYPE: REST',
108  );
109 
110  $industryValues = array(
111  'Automotive',
112  'Adult Entertainment',
113  'Financial Services',
114  'Mercenaries & Armaments',
115  );
116  $industryFieldData = CustomFieldData::getByName('Industries');
117  $industryFieldData->serializedData = serialize($industryValues);
118  $this->assertTrue($industryFieldData->save());
119 
120  $typeValues = array(
121  'Prospect',
122  'Customer',
123  'Vendor',
124  );
125  $typeFieldData = CustomFieldData::getByName('AccountTypes');
126  $typeFieldData->serializedData = serialize($typeValues);
127  $this->assertTrue($typeFieldData->save());
128 
129  $primaryEmail['emailAddress'] = "a@example.com";
130  $primaryEmail['optOut'] = 1;
131 
132  $secondaryEmail['emailAddress'] = "b@example.com";
133  $secondaryEmail['optOut'] = 0;
134  $secondaryEmail['isInvalid'] = 1;
135 
136  $billingAddress['street1'] = '129 Noodle Boulevard';
137  $billingAddress['street2'] = 'Apartment 6000A';
138  $billingAddress['city'] = 'Noodleville';
139  $billingAddress['postalCode'] = '23453';
140  $billingAddress['country'] = 'The Good Old US of A';
141 
142  $shippingAddress['street1'] = '25 de Agosto 2543';
143  $shippingAddress['street2'] = 'Local 3';
144  $shippingAddress['city'] = 'Ciudad de Los Fideos';
145  $shippingAddress['postalCode'] = '5123-4';
146  $shippingAddress['country'] = 'Latinoland';
147 
148  $account = new Account();
149  $data['name'] = "My Company with no permissions";
150  $data['officePhone'] = "6438238";
151  $data['officeFax'] = "6565465436";
152  $data['employees'] = 100;
153  $data['website'] = "http://www.google.com";
154  $data['annualRevenue'] = "1000000";
155  $data['description'] = "Some Description";
156 
157  $data['industry']['value'] = $industryValues[2];
158  $data['type']['value'] = $typeValues[1];
159 
160  $data['primaryEmail'] = $primaryEmail;
161  $data['secondaryEmail'] = $secondaryEmail;
162  $data['billingAddress'] = $billingAddress;
163  $data['shippingAddress'] = $shippingAddress;
164 
165  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
166  $response = json_decode($response, true);
167  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
168  $this->assertArrayHasKey('id', $response['data']);
169  $accountId = $response['data']['id'];
170 
171  $this->assertArrayHasKey('owner', $response['data']);
172  $this->assertCount(2, $response['data']['owner']);
173  $this->assertArrayHasKey('id', $response['data']['owner']);
174  $this->assertEquals($super->id, $response['data']['owner']['id']);
175  $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']);
176  $this->assertCount(2, $response['data']['explicitReadWriteModelPermissions']);
177  $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']);
178  $this->assertEquals(1, $response['data']['explicitReadWriteModelPermissions']['type']);
179  $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']);
180  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['nonEveryoneGroup']);
181 
182  $data['owner'] = array(
183  'id' => $super->id,
184  'username' => 'super'
185  );
186  $data['createdByUser'] = array(
187  'id' => $super->id,
188  'username' => 'super'
189  );
190  $data['modifiedByUser'] = array(
191  'id' => $super->id,
192  'username' => 'super'
193  );
194 
195  // unset explicit permissions, we won't use these in comparison.
196  unset($response['data']['explicitReadWriteModelPermissions']);
197  // We need to unset some empty values from response.
198  unset($response['data']['createdDateTime']);
199  unset($response['data']['modifiedDateTime']);
200  unset($response['data']['primaryEmail']['id']);
201  unset($response['data']['primaryEmail']['isInvalid']);
202  unset($response['data']['secondaryEmail']['id']);
203  unset($response['data']['billingAddress']['id']);
204  unset($response['data']['billingAddress']['state']);
205  unset($response['data']['billingAddress']['longitude']);
206  unset($response['data']['billingAddress']['latitude']);
207  unset($response['data']['billingAddress']['invalid']);
208 
209  unset($response['data']['shippingAddress']['id']);
210  unset($response['data']['shippingAddress']['state']);
211  unset($response['data']['shippingAddress']['longitude']);
212  unset($response['data']['shippingAddress']['latitude']);
213  unset($response['data']['shippingAddress']['invalid']);
214  unset($response['data']['industry']['id']);
215  unset($response['data']['latestActivityDateTime']);
216  unset($response['data']['type']['id']);
217  unset($response['data']['id']);
218 
219  ksort($data);
220  ksort($response['data']);
221  $this->assertEquals($data, $response['data']);
222 
223  $response = $this->createApiCallWithRelativeUrl('read/' . $accountId, 'GET', $headers);
224  $response = json_decode($response, true);
225  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
226  $this->assertArrayHasKey('data', $response);
227  $this->assertArrayHasKey('owner', $response['data']);
228  $this->assertCount(2, $response['data']['owner']);
229  $this->assertArrayHasKey('id', $response['data']['owner']);
230  $this->assertEquals($super->id, $response['data']['owner']['id']);
231 
232  $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']);
233  $this->assertCount(2, $response['data']['explicitReadWriteModelPermissions']);
234  $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']);
235  $this->assertEquals(1, $response['data']['explicitReadWriteModelPermissions']['type']);
236  $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']);
237  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['nonEveryoneGroup']);
238  }
239 
244  {
245  $super = User::getByUsername('super');
246  Yii::app()->user->userModel = $super;
247  $billy = User::getByUsername('billy');
249  $authenticationData = $this->login();
250  $headers = array(
251  'Accept: application/json',
252  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
253  'ZURMO_TOKEN: ' . $authenticationData['token'],
254  'ZURMO_API_REQUEST_TYPE: REST',
255  );
256 
257  $industryValues = array(
258  'Automotive',
259  'Adult Entertainment',
260  'Financial Services',
261  'Mercenaries & Armaments',
262  );
263  $industryFieldData = CustomFieldData::getByName('Industries');
264  $industryFieldData->serializedData = serialize($industryValues);
265  $this->assertTrue($industryFieldData->save());
266 
267  $typeValues = array(
268  'Prospect',
269  'Customer',
270  'Vendor',
271  );
272  $typeFieldData = CustomFieldData::getByName('AccountTypes');
273  $typeFieldData->serializedData = serialize($typeValues);
274  $this->assertTrue($typeFieldData->save());
275 
276  $primaryEmail['emailAddress'] = "a@example.com";
277  $primaryEmail['optOut'] = 1;
278 
279  $secondaryEmail['emailAddress'] = "b@example.com";
280  $secondaryEmail['optOut'] = 0;
281  $secondaryEmail['isInvalid'] = 1;
282 
283  $billingAddress['street1'] = '129 Noodle Boulevard';
284  $billingAddress['street2'] = 'Apartment 6000A';
285  $billingAddress['city'] = 'Noodleville';
286  $billingAddress['postalCode'] = '23453';
287  $billingAddress['country'] = 'The Good Old US of A';
288 
289  $shippingAddress['street1'] = '25 de Agosto 2543';
290  $shippingAddress['street2'] = 'Local 3';
291  $shippingAddress['city'] = 'Ciudad de Los Fideos';
292  $shippingAddress['postalCode'] = '5123-4';
293  $shippingAddress['country'] = 'Latinoland';
294 
295  $account = new Account();
296  $data['name'] = "My Company with just owner";
297  $data['officePhone'] = "6438238";
298  $data['officeFax'] = "6565465436";
299  $data['employees'] = 100;
300  $data['website'] = "http://www.google.com";
301  $data['annualRevenue'] = "1000000";
302  $data['description'] = "Some Description";
303 
304  $data['industry']['value'] = $industryValues[2];
305  $data['type']['value'] = $typeValues[1];
306 
307  $data['primaryEmail'] = $primaryEmail;
308  $data['secondaryEmail'] = $secondaryEmail;
309  $data['billingAddress'] = $billingAddress;
310  $data['shippingAddress'] = $shippingAddress;
311  $data['owner']['id'] = $billy->id;
312 
313  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
314  $response = json_decode($response, true);
315  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
316  $this->assertArrayHasKey('id', $response['data']);
317  $accountId = $response['data']['id'];
318 
319  $this->assertArrayHasKey('owner', $response['data']);
320  $this->assertCount(2, $response['data']['owner']);
321  $this->assertArrayHasKey('id', $response['data']['owner']);
322  $this->assertEquals($billy->id, $response['data']['owner']['id']);
323  $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']);
324  $this->assertCount(2, $response['data']['explicitReadWriteModelPermissions']);
325  $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']);
326  $this->assertEquals(1, $response['data']['explicitReadWriteModelPermissions']['type']);
327  $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']);
328  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['nonEveryoneGroup']);
329 
330  $data['owner'] = array(
331  'id' => $billy->id,
332  'username' => 'billy'
333  );
334  $data['createdByUser'] = array(
335  'id' => $super->id,
336  'username' => 'super'
337  );
338  $data['modifiedByUser'] = array(
339  'id' => $super->id,
340  'username' => 'super'
341  );
342 
343  // unset explicit permissions, we won't use these in comparison.
344  unset($response['data']['explicitReadWriteModelPermissions']);
345  // We need to unset some empty values from response.
346  unset($response['data']['createdDateTime']);
347  unset($response['data']['modifiedDateTime']);
348  unset($response['data']['primaryEmail']['id']);
349  unset($response['data']['primaryEmail']['isInvalid']);
350  unset($response['data']['secondaryEmail']['id']);
351  unset($response['data']['billingAddress']['id']);
352  unset($response['data']['billingAddress']['state']);
353  unset($response['data']['billingAddress']['longitude']);
354  unset($response['data']['billingAddress']['latitude']);
355  unset($response['data']['billingAddress']['invalid']);
356 
357  unset($response['data']['shippingAddress']['id']);
358  unset($response['data']['shippingAddress']['state']);
359  unset($response['data']['shippingAddress']['longitude']);
360  unset($response['data']['shippingAddress']['latitude']);
361  unset($response['data']['shippingAddress']['invalid']);
362  unset($response['data']['industry']['id']);
363  unset($response['data']['latestActivityDateTime']);
364  unset($response['data']['type']['id']);
365  unset($response['data']['id']);
366 
367  ksort($data);
368  ksort($response['data']);
369  $this->assertEquals($data, $response['data']);
370 
371  $response = $this->createApiCallWithRelativeUrl('read/' . $accountId, 'GET', $headers);
372  $response = json_decode($response, true);
373  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
374  $this->assertArrayHasKey('data', $response);
375  $this->assertArrayHasKey('owner', $response['data']);
376  $this->assertCount(2, $response['data']['owner']);
377  $this->assertArrayHasKey('id', $response['data']['owner']);
378  $this->assertEquals($billy->id, $response['data']['owner']['id']);
379 
380  $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']);
381  $this->assertCount(2, $response['data']['explicitReadWriteModelPermissions']);
382  $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']);
383  $this->assertEquals(1, $response['data']['explicitReadWriteModelPermissions']['type']);
384  $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']);
385  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['nonEveryoneGroup']);
386  }
387 
392  {
393  $super = User::getByUsername('super');
394  Yii::app()->user->userModel = $super;
396  $authenticationData = $this->login();
397  $headers = array(
398  'Accept: application/json',
399  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
400  'ZURMO_TOKEN: ' . $authenticationData['token'],
401  'ZURMO_API_REQUEST_TYPE: REST',
402  );
403 
404  $industryValues = array(
405  'Automotive',
406  'Adult Entertainment',
407  'Financial Services',
408  'Mercenaries & Armaments',
409  );
410  $industryFieldData = CustomFieldData::getByName('Industries');
411  $industryFieldData->serializedData = serialize($industryValues);
412  $this->assertTrue($industryFieldData->save());
413 
414  $typeValues = array(
415  'Prospect',
416  'Customer',
417  'Vendor',
418  );
419  $typeFieldData = CustomFieldData::getByName('AccountTypes');
420  $typeFieldData->serializedData = serialize($typeValues);
421  $this->assertTrue($typeFieldData->save());
422 
423  $primaryEmail['emailAddress'] = "a@example.com";
424  $primaryEmail['optOut'] = 1;
425 
426  $secondaryEmail['emailAddress'] = "b@example.com";
427  $secondaryEmail['optOut'] = 0;
428  $secondaryEmail['isInvalid'] = 1;
429 
430  $billingAddress['street1'] = '129 Noodle Boulevard';
431  $billingAddress['street2'] = 'Apartment 6000A';
432  $billingAddress['city'] = 'Noodleville';
433  $billingAddress['postalCode'] = '23453';
434  $billingAddress['country'] = 'The Good Old US of A';
435 
436  $shippingAddress['street1'] = '25 de Agosto 2543';
437  $shippingAddress['street2'] = 'Local 3';
438  $shippingAddress['city'] = 'Ciudad de Los Fideos';
439  $shippingAddress['postalCode'] = '5123-4';
440  $shippingAddress['country'] = 'Latinoland';
441 
442  $account = new Account();
443  $data['name'] = "My Company with owner only permissions";
444  $data['officePhone'] = "6438238";
445  $data['officeFax'] = "6565465436";
446  $data['employees'] = 100;
447  $data['website'] = "http://www.google.com";
448  $data['annualRevenue'] = "1000000";
449  $data['description'] = "Some Description";
450 
451  $data['industry']['value'] = $industryValues[2];
452  $data['type']['value'] = $typeValues[1];
453 
454  $data['primaryEmail'] = $primaryEmail;
455  $data['secondaryEmail'] = $secondaryEmail;
456  $data['billingAddress'] = $billingAddress;
457  $data['shippingAddress'] = $shippingAddress;
458  // TODO: @Shoaibi/@Ivica: null does not work, empty works. null doesn't send it.
459  $data['explicitReadWriteModelPermissions'] = array('nonEveryoneGroup' => '', 'type' => '');
460 
461  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
462  $response = json_decode($response, true);
463  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
464  $this->assertArrayHasKey('id', $response['data']);
465  $accountId = $response['data']['id'];
466 
467  $this->assertArrayHasKey('owner', $response['data']);
468  $this->assertCount(2, $response['data']['owner']);
469  $this->assertArrayHasKey('id', $response['data']['owner']);
470  $this->assertEquals($super->id, $response['data']['owner']['id']);
471  $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']);
472  $this->assertCount(2, $response['data']['explicitReadWriteModelPermissions']);
473  $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']);
474  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['type']);
475  // following also works. wonder why.
476  //$this->assertTrue(null === $response['data']['explicitReadWriteModelPermissions']['type']);
477  $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']);
478  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['nonEveryoneGroup']);
479 
480  $data['owner'] = array(
481  'id' => $super->id,
482  'username' => 'super'
483  );
484  $data['createdByUser'] = array(
485  'id' => $super->id,
486  'username' => 'super'
487  );
488  $data['modifiedByUser'] = array(
489  'id' => $super->id,
490  'username' => 'super'
491  );
492 
493  // We need to unset some empty values from response.
494  unset($response['data']['createdDateTime']);
495  unset($response['data']['modifiedDateTime']);
496  unset($response['data']['primaryEmail']['id']);
497  unset($response['data']['primaryEmail']['isInvalid']);
498  unset($response['data']['secondaryEmail']['id']);
499  unset($response['data']['billingAddress']['id']);
500  unset($response['data']['billingAddress']['state']);
501  unset($response['data']['billingAddress']['longitude']);
502  unset($response['data']['billingAddress']['latitude']);
503  unset($response['data']['billingAddress']['invalid']);
504 
505  unset($response['data']['shippingAddress']['id']);
506  unset($response['data']['shippingAddress']['state']);
507  unset($response['data']['shippingAddress']['longitude']);
508  unset($response['data']['shippingAddress']['latitude']);
509  unset($response['data']['shippingAddress']['invalid']);
510  unset($response['data']['industry']['id']);
511  unset($response['data']['latestActivityDateTime']);
512  unset($response['data']['type']['id']);
513  unset($response['data']['id']);
514 
515  ksort($data);
516  ksort($response['data']);
517  $this->assertEquals($data, $response['data']);
518 
519  $response = $this->createApiCallWithRelativeUrl('read/' . $accountId, 'GET', $headers);
520  $response = json_decode($response, true);
521  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
522  $this->assertArrayHasKey('data', $response);
523  $this->assertArrayHasKey('owner', $response['data']);
524  $this->assertCount(2, $response['data']['owner']);
525  $this->assertArrayHasKey('id', $response['data']['owner']);
526  $this->assertEquals($super->id, $response['data']['owner']['id']);
527 
528  $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']);
529  $this->assertCount(2, $response['data']['explicitReadWriteModelPermissions']);
530  $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']);
531  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['type']);
532  $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']);
533  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['nonEveryoneGroup']);
534  }
535 
539  public function testUpdateAccount()
540  {
541  $super = User::getByUsername('super');
542  Yii::app()->user->userModel = $super;
543 
544  $authenticationData = $this->login();
545  $headers = array(
546  'Accept: application/json',
547  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
548  'ZURMO_TOKEN: ' . $authenticationData['token'],
549  'ZURMO_API_REQUEST_TYPE: REST',
550  );
551 
552  $accounts = Account::getByName('My Company with just owner');
553  $this->assertEquals(1, count($accounts));
554  $compareData = $this->getModelToApiDataUtilData($accounts[0]);
555  $group = static::$randomNonEveryoneNonAdministratorsGroup;
556  $explicitReadWriteModelPermissions = array('type' => 2, 'nonEveryoneGroup' => $group->id);
557 
558  $data['name'] = "My Company 2";
559  $data['explicitReadWriteModelPermissions'] = $explicitReadWriteModelPermissions;
560  $compareData['name'] = "My Company 2";
561  $compareData['explicitReadWriteModelPermissions'] = $explicitReadWriteModelPermissions;
562 
563  $response = $this->createApiCallWithRelativeUrl('update/' . $compareData['id'], 'PUT', $headers, array('data' => $data));
564  $response = json_decode($response, true);
565  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
566 
567  // We need to unset some empty values from response and dates.
568  unset($response['data']['modifiedDateTime']);
569  unset($compareData['modifiedDateTime']);
570  ksort($compareData);
571  ksort($response['data']);
572  $this->assertEquals($compareData, $response['data']);
573 
574  $response = $this->createApiCallWithRelativeUrl('read/' . $compareData['id'], 'GET', $headers);
575  $response = json_decode($response, true);
576  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
577  unset($response['data']['modifiedDateTime']);
578  ksort($response['data']);
579  $this->assertEquals($compareData, $response['data']);
580  }
581 
585  public function testListAccounts()
586  {
587  $super = User::getByUsername('super');
588  Yii::app()->user->userModel = $super;
589 
590  $authenticationData = $this->login();
591  $headers = array(
592  'Accept: application/json',
593  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
594  'ZURMO_TOKEN: ' . $authenticationData['token'],
595  'ZURMO_API_REQUEST_TYPE: REST',
596  );
597 
598  $accounts = Account::getByName('My Company 2');
599  $this->assertEquals(1, count($accounts));
600  $compareData = $this->getModelToApiDataUtilData($accounts[0]);
601 
602  $response = $this->createApiCallWithRelativeUrl('list/' , 'GET', $headers);
603  $response = json_decode($response, true);
604  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
605  $this->assertEquals(3, count($response['data']['items']));
606  $this->assertEquals(1, $response['data']['currentPage']);
607  $this->assertEquals(3, $response['data']['totalCount']);
608  $this->assertEquals($compareData, $response['data']['items'][0]);
609  }
610 
611  public function testListAccountAttributes()
612  {
613  $super = User::getByUsername('super');
614  Yii::app()->user->userModel = $super;
615 
616  $authenticationData = $this->login();
617  $headers = array(
618  'Accept: application/json',
619  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
620  'ZURMO_TOKEN: ' . $authenticationData['token'],
621  'ZURMO_API_REQUEST_TYPE: REST',
622  );
623  $allAttributes = ApiRestTestHelper::getModelAttributes(new Account());
624  $response = $this->createApiCallWithRelativeUrl('listAttributes/' , 'GET', $headers);
625  $response = json_decode($response, true);
626  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
627  $this->assertEquals($allAttributes, $response['data']['items']);
628  }
629 
634  {
635  Yii::app()->user->userModel = User::getByUsername('super');
636  $notAllowedUser = UserTestHelper::createBasicUser('Steven');
637  $notAllowedUser->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB_API);
638  $this->assertTrue($notAllowedUser->save());
639 
640  // Test with unprivileged user to view, edit and delete account.
641  $authenticationData = $this->login('steven', 'steven');
642  $headers = array(
643  'Accept: application/json',
644  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
645  'ZURMO_TOKEN: ' . $authenticationData['token'],
646  'ZURMO_API_REQUEST_TYPE: REST',
647  );
648  $everyoneGroup = Group::getByName(Group::EVERYONE_GROUP_NAME);
649  $this->assertTrue($everyoneGroup->save());
650 
651  $accounts = Account::getByName('My Company 2');
652  $this->assertEquals(1, count($accounts));
653  $data['name'] = "My Company 3";
654 
655  // Check first if user doesn't have rights.
656  $response = $this->createApiCallWithRelativeUrl('read/' . $accounts[0]->id, 'GET', $headers);
657  $response = json_decode($response, true);
658  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
659  $this->assertEquals('You do not have rights to perform this action.', $response['message']);
660 
661  $response = $this->createApiCallWithRelativeUrl('update/' . $accounts[0]->id, 'PUT', $headers, array('data' => $data));
662  $response = json_decode($response, true);
663  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
664  $this->assertEquals('You do not have rights to perform this action.', $response['message']);
665 
666  $response = $this->createApiCallWithRelativeUrl('delete/' . $accounts[0]->id, 'DELETE', $headers);
667  $response = json_decode($response, true);
668  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
669  $this->assertEquals('You do not have rights to perform this action.', $response['message']);
670 
671  //now check if user have rights, but no permissions.
672  $notAllowedUser->setRight('AccountsModule', AccountsModule::getAccessRight());
673  $notAllowedUser->setRight('AccountsModule', AccountsModule::getCreateRight());
674  $notAllowedUser->setRight('AccountsModule', AccountsModule::getDeleteRight());
675  $saved = $notAllowedUser->save();
676  $this->assertTrue($saved);
677 
678  $response = $this->createApiCallWithRelativeUrl('read/' . $accounts[0]->id, 'GET', $headers);
679  $response = json_decode($response, true);
680  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
681  $this->assertEquals('You do not have permissions for this action.', $response['message']);
682 
683  $response = $this->createApiCallWithRelativeUrl('update/' . $accounts[0]->id, 'PUT', $headers, array('data' => $data));
684  $response = json_decode($response, true);
685  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
686  $this->assertEquals('You do not have permissions for this action.', $response['message']);
687 
688  $response = $this->createApiCallWithRelativeUrl('delete/' . $accounts[0]->id, 'DELETE', $headers);
689  $response = json_decode($response, true);
690  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
691  $this->assertEquals('You do not have permissions for this action.', $response['message']);
692 
693  // Test with privileged user
694  $authenticationData = $this->login();
695  $headers = array(
696  'Accept: application/json',
697  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
698  'ZURMO_TOKEN: ' . $authenticationData['token'],
699  'ZURMO_API_REQUEST_TYPE: REST',
700  );
701 
702  unset($data);
703  $data['explicitReadWriteModelPermissions'] = array(
704  'type' => ExplicitReadWriteModelPermissionsUtil::MIXED_TYPE_EVERYONE_GROUP
705  );
706  $response = $this->createApiCallWithRelativeUrl('update/' . $accounts[0]->id, 'PUT', $headers, array('data' => $data));
707  $response = json_decode($response, true);
708  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
709 
710  $authenticationData = $this->login('steven', 'steven');
711  $headers = array(
712  'Accept: application/json',
713  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
714  'ZURMO_TOKEN: ' . $authenticationData['token'],
715  'ZURMO_API_REQUEST_TYPE: REST',
716  );
717  $response = $this->createApiCallWithRelativeUrl('read/' . $accounts[0]->id, 'GET', $headers);
718  $response = json_decode($response, true);
719  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
720 
721  unset($data);
722  $data['name'] = "My Company 3";
723  $response = $this->createApiCallWithRelativeUrl('update/' . $accounts[0]->id, 'PUT', $headers, array('data' => $data));
724  $response = json_decode($response, true);
725  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
726  $this->assertEquals('My Company 3', $response['data']['name']);
727 
728  // Test with privileged user
729  $authenticationData = $this->login();
730  $headers = array(
731  'Accept: application/json',
732  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
733  'ZURMO_TOKEN: ' . $authenticationData['token'],
734  'ZURMO_API_REQUEST_TYPE: REST',
735  );
736 
737  //Test Delete
738  $response = $this->createApiCallWithRelativeUrl('delete/' . $accounts[0]->id, 'DELETE', $headers);
739  $response = json_decode($response, true);
740  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
741 
742  $response = $this->createApiCallWithRelativeUrl('read/' . $accounts[0]->id, 'GET', $headers);
743  $response = json_decode($response, true);
744  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
745  }
746 
750  public function testBasicSearchAccounts()
751  {
752  $super = User::getByUsername('super');
753  Yii::app()->user->userModel = $super;
755  $authenticationData = $this->login();
756  $headers = array(
757  'Accept: application/json',
758  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
759  'ZURMO_TOKEN: ' . $authenticationData['token'],
760  'ZURMO_API_REQUEST_TYPE: REST',
761  );
762  AccountTestHelper::createAccountByNameTypeAndIndustryForOwner('First Account', 'Customer', 'Automotive', $super);
763  AccountTestHelper::createAccountByNameTypeAndIndustryForOwner('Second Account', 'Customer', 'Automotive', $super);
764  AccountTestHelper::createAccountByNameTypeAndIndustryForOwner('Third Account', 'Customer', 'Financial Services', $super);
765  AccountTestHelper::createAccountByNameTypeAndIndustryForOwner('Forth Account', 'Vendor', 'Financial Services', $super);
766  AccountTestHelper::createAccountByNameTypeAndIndustryForOwner('Fifth Account', 'Vendor', 'Financial Services', $super);
767 
768  $searchParams = array(
769  'pagination' => array(
770  'page' => 1,
771  'pageSize' => 3,
772  ),
773  'search' => array(
774  'name' => '',
775  ),
776  'sort' => 'name',
777  );
778  $searchParamsQuery = http_build_query($searchParams);
779  $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, 'GET', $headers);
780  $response = json_decode($response, true);
781  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
782  $this->assertEquals(3, count($response['data']['items']));
783  $this->assertEquals(5, $response['data']['totalCount']);
784  $this->assertEquals(1, $response['data']['currentPage']);
785  $this->assertEquals('Fifth Account', $response['data']['items'][0]['name']);
786  $this->assertEquals('First Account', $response['data']['items'][1]['name']);
787  $this->assertEquals('Forth Account', $response['data']['items'][2]['name']);
788 
789  // Second page
790  $searchParams['pagination']['page'] = 2;
791  $searchParamsQuery = http_build_query($searchParams);
792  $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, 'GET', $headers);
793  $response = json_decode($response, true);
794  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
795  $this->assertEquals(2, count($response['data']['items']));
796  $this->assertEquals(5, $response['data']['totalCount']);
797  $this->assertEquals(2, $response['data']['currentPage']);
798  $this->assertEquals('Second Account', $response['data']['items'][0]['name']);
799  $this->assertEquals('Third Account', $response['data']['items'][1]['name']);
800 
801  // Search by name
802  $searchParams['pagination']['page'] = 1;
803  $searchParams['search']['name'] = 'First Account';
804  $searchParamsQuery = http_build_query($searchParams);
805  $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, 'GET', $headers);
806  $response = json_decode($response, true);
807  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
808  $this->assertEquals(1, count($response['data']['items']));
809  $this->assertEquals(1, $response['data']['totalCount']);
810  $this->assertEquals(1, $response['data']['currentPage']);
811  $this->assertEquals('First Account', $response['data']['items'][0]['name']);
812 
813  // No results
814  $searchParams['pagination']['page'] = 1;
815  $searchParams['search']['name'] = 'First Account 2';
816  $searchParamsQuery = http_build_query($searchParams);
817  $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, 'GET', $headers);
818  $response = json_decode($response, true);
819  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
820  $this->assertEquals(0, $response['data']['totalCount']);
821  $this->assertFalse(isset($response['data']['items']));
822 
823  // Search by name desc.
824  $searchParams = array(
825  'pagination' => array(
826  'page' => 1,
827  'pageSize' => 3,
828  ),
829  'search' => array(
830  'name' => '',
831  ),
832  'sort' => 'name.desc',
833  );
834  $searchParamsQuery = http_build_query($searchParams);
835  $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, 'GET', $headers);
836  $response = json_decode($response, true);
837  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
838  $this->assertEquals(3, count($response['data']['items']));
839  $this->assertEquals(5, $response['data']['totalCount']);
840  $this->assertEquals(1, $response['data']['currentPage']);
841  $this->assertEquals('Third Account', $response['data']['items'][0]['name']);
842  $this->assertEquals('Second Account', $response['data']['items'][1]['name']);
843  $this->assertEquals('Forth Account', $response['data']['items'][2]['name']);
844 
845  // Second page
846  $searchParams['pagination']['page'] = 2;
847  $searchParamsQuery = http_build_query($searchParams);
848  $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, 'GET', $headers);
849  $response = json_decode($response, true);
850  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
851  $this->assertEquals(2, count($response['data']['items']));
852  $this->assertEquals(5, $response['data']['totalCount']);
853  $this->assertEquals(2, $response['data']['currentPage']);
854  $this->assertEquals('First Account', $response['data']['items'][0]['name']);
855  $this->assertEquals('Fifth Account', $response['data']['items'][1]['name']);
856 
857  // Search by custom fields, order by name desc
858  $searchParams = array(
859  'pagination' => array(
860  'page' => 1,
861  'pageSize' => 3,
862  ),
863  'search' => array(
864  'industry' => array( 'value' => 'Financial Services'),
865  'type' => array( 'value' => 'Vendor'),
866  'owner' => array( 'id' => $super->id),
867  ),
868  'sort' => 'name.desc',
869  );
870  $searchParamsQuery = http_build_query($searchParams);
871  $response = $this->createApiCallWithRelativeUrl('list/filter/' . $searchParamsQuery, 'GET', $headers);
872  $response = json_decode($response, true);
873  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
874  $this->assertEquals(2, $response['data']['totalCount']);
875  $this->assertEquals(2, count($response['data']['items']));
876  $this->assertEquals(1, $response['data']['currentPage']);
877  $this->assertEquals('Forth Account', $response['data']['items'][0]['name']);
878  $this->assertEquals('Fifth Account', $response['data']['items'][1]['name']);
879  }
880 
884  public function testDynamicSearchAccounts()
885  {
886  $super = User::getByUsername('super');
887  Yii::app()->user->userModel = $super;
888 
889  $authenticationData = $this->login();
890  $headers = array(
891  'Accept: application/json',
892  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
893  'ZURMO_TOKEN: ' . $authenticationData['token'],
894  'ZURMO_API_REQUEST_TYPE: REST',
895  );
896 
897  $data = array(
898  'dynamicSearch' => array(
899  'dynamicClauses' => array(
900  array(
901  'attributeIndexOrDerivedType' => 'owner',
902  'structurePosition' => 1,
903  'owner' => array(
904  'id' => Yii::app()->user->userModel->id,
905  ),
906  ),
907  array(
908  'attributeIndexOrDerivedType' => 'name',
909  'structurePosition' => 2,
910  'name' => 'Fi',
911  ),
912  array(
913  'attributeIndexOrDerivedType' => 'name',
914  'structurePosition' => 3,
915  'name' => 'Se',
916  ),
917  ),
918  'dynamicStructure' => '1 AND (2 OR 3)',
919  ),
920  'pagination' => array(
921  'page' => 1,
922  'pageSize' => 2,
923  ),
924  'sort' => 'name.asc',
925  );
926 
927  $response = $this->createApiCallWithRelativeUrl('list/filter/', 'POST', $headers, array('data' => $data));
928 
929  $response = json_decode($response, true);
930  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
931  $this->assertEquals(2, count($response['data']['items']));
932  $this->assertEquals(3, $response['data']['totalCount']);
933  $this->assertEquals(1, $response['data']['currentPage']);
934  $this->assertEquals('Fifth Account', $response['data']['items'][0]['name']);
935  $this->assertEquals('First Account', $response['data']['items'][1]['name']);
936 
937  // Get second page
938  $data['pagination']['page'] = 2;
939  $response = $this->createApiCallWithRelativeUrl('list/filter/', 'POST', $headers, array('data' => $data));
940 
941  $response = json_decode($response, true);
942  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
943  $this->assertEquals(1, count($response['data']['items']));
944  $this->assertEquals(3, $response['data']['totalCount']);
945  $this->assertEquals(2, $response['data']['currentPage']);
946  $this->assertEquals('Second Account', $response['data']['items'][0]['name']);
947  }
948 
949  public function testNewSearchAccounts()
950  {
951  $super = User::getByUsername('super');
952  Yii::app()->user->userModel = $super;
953 
954  $authenticationData = $this->login();
955  $headers = array(
956  'Accept: application/json',
957  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
958  'ZURMO_TOKEN: ' . $authenticationData['token'],
959  'ZURMO_API_REQUEST_TYPE: REST',
960  );
961 
962  $data = array(
963  'search' => array(
964  'modelClassName' => 'Account',
965  'searchAttributeData' => array(
966  'clauses' => array(
967  1 => array(
968  'attributeName' => 'owner',
969  'relatedAttributeName' => 'id',
970  'operatorType' => 'equals',
971  'value' => Yii::app()->user->userModel->id,
972  ),
973  2 => array(
974  'attributeName' => 'name',
975  'operatorType' => 'startsWith',
976  'value' => 'Fi'
977  ),
978  3 => array(
979  'attributeName' => 'name',
980  'operatorType' => 'startsWith',
981  'value' => 'Se'
982  ),
983  ),
984  'structure' => '1 AND (2 OR 3)',
985  ),
986  ),
987  'pagination' => array(
988  'page' => 1,
989  'pageSize' => 2,
990  ),
991  'sort' => 'name asc',
992  );
993 
994  $response = $this->createApiCallWithRelativeUrl('search/filter/', 'POST', $headers, array('data' => $data));
995  $response = json_decode($response, true);
996  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
997  $this->assertEquals(2, count($response['data']['items']));
998  $this->assertEquals(3, $response['data']['totalCount']);
999  $this->assertEquals(1, $response['data']['currentPage']);
1000  $this->assertEquals('Fifth Account', $response['data']['items'][0]['name']);
1001  $this->assertEquals('First Account', $response['data']['items'][1]['name']);
1002 
1003  // Get second page
1004  $data['pagination']['page'] = 2;
1005  $response = $this->createApiCallWithRelativeUrl('search/filter/', 'POST', $headers, array('data' => $data));
1006 
1007  $response = json_decode($response, true);
1008  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
1009  $this->assertEquals(1, count($response['data']['items']));
1010  $this->assertEquals(3, $response['data']['totalCount']);
1011  $this->assertEquals(2, $response['data']['currentPage']);
1012  $this->assertEquals('Second Account', $response['data']['items'][0]['name']);
1013 
1014  // Check with invalid search options
1015  // Invalid modelClassName
1016  $data['search']['modelClassName'] = 'InvalidClassName';
1017  $response = $this->createApiCallWithRelativeUrl('search/filter/', 'POST', $headers, array('data' => $data));
1018  $response = json_decode($response, true);
1019  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
1020  $this->assertEquals('InvalidClassName class does not exist.', $response['message']);
1021 
1022  $data['search']['modelClassName'] = 'AccountsModule';
1023  $response = $this->createApiCallWithRelativeUrl('search/filter/', 'POST', $headers, array('data' => $data));
1024  $response = json_decode($response, true);
1025  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
1026  $this->assertEquals('AccountsModule should be subclass of RedBeanModel.', $response['message']);
1027  }
1028 
1029  public function testCreateWithRelations()
1030  {
1031  $super = User::getByUsername('super');
1032  Yii::app()->user->userModel = $super;
1033 
1034  $account2 = AccountTestHelper::createAccountByNameForOwner('Faber', $super);
1035  $contact = ContactTestHelper::createContactByNameForOwner('Simon', $super);
1036 
1037  $authenticationData = $this->login();
1038  $headers = array(
1039  'Accept: application/json',
1040  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
1041  'ZURMO_TOKEN: ' . $authenticationData['token'],
1042  'ZURMO_API_REQUEST_TYPE: REST',
1043  );
1044 
1045  $data['name'] = 'Zurmo';
1046  $data['modelRelations'] = array(
1047  'accounts' => array(
1048  array(
1049  'action' => 'add',
1050  'modelId' => $account2->id,
1051  'modelClassName' => 'Account'
1052  ),
1053  ),
1054  'contacts' => array(
1055  array(
1056  'action' => 'add',
1057  'modelId' => $contact->id,
1058  'modelClassName' => 'Contact'
1059  ),
1060  ),
1061  );
1062  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
1063 
1064  $response = json_decode($response, true);
1065  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
1066 
1068  $account = Account::getById($response['data']['id']);
1069  $this->assertEquals(1, count($account->accounts));
1070  $this->assertEquals($account2->id, $account->accounts[0]->id);
1071  $this->assertEquals(1, count($account->contacts));
1072  $this->assertEquals($contact->id, $account->contacts[0]->id);
1073 
1074  $account2 = Account::getById($account2->id);
1075  $this->assertEquals($account->id, $account2->account->id);
1076 
1077  $contact = Contact::getById($contact->id);
1078  $this->assertEquals($account->id, $contact->account->id);
1079  }
1080 
1084  public function testUpdateWithRelations()
1085  {
1086  $super = User::getByUsername('super');
1087  Yii::app()->user->userModel = $super;
1088 
1089  $authenticationData = $this->login();
1090  $headers = array(
1091  'Accept: application/json',
1092  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
1093  'ZURMO_TOKEN: ' . $authenticationData['token'],
1094  'ZURMO_API_REQUEST_TYPE: REST',
1095  );
1096 
1097  $account = AccountTestHelper::createAccountByNameForOwner('Factor X', $super);
1098  $account1 = AccountTestHelper::createAccountByNameForOwner('Miko', $super);
1099  $account2 = AccountTestHelper::createAccountByNameForOwner('Troter', $super);
1100  $contact = ContactTestHelper::createContactByNameForOwner('Simon', $super);
1101 
1102  $compareData = $this->getModelToApiDataUtilData($account);
1103  $account->forget();
1104 
1105  $data['modelRelations'] = array(
1106  'accounts' => array(
1107  array(
1108  'action' => 'add',
1109  'modelId' => $account1->id,
1110  'modelClassName' => 'Account'
1111  ),
1112  array(
1113  'action' => 'add',
1114  'modelId' => $account2->id,
1115  'modelClassName' => 'Account'
1116  ),
1117  ),
1118  'contacts' => array(
1119  array(
1120  'action' => 'add',
1121  'modelId' => $contact->id,
1122  'modelClassName' => 'Contact'
1123  ),
1124  ),
1125  );
1126  $data['name'] = 'Zurmo Inc.';
1127 
1128  $response = $this->createApiCallWithRelativeUrl('update/' . $compareData['id'], 'PUT', $headers, array('data' => $data));
1129  $response = json_decode($response, true);
1130  unset($response['data']['modifiedDateTime']);
1131  unset($compareData['modifiedDateTime']);
1132  $compareData['name'] = 'Zurmo Inc.';
1133  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
1134  $this->assertEquals($compareData, $response['data']);
1135 
1137  $account = Account::getById($compareData['id']);
1138  $this->assertEquals(2, count($account->accounts));
1139  $this->assertEquals($account1->id, $account->accounts[0]->id);
1140  $this->assertEquals($account2->id, $account->accounts[1]->id);
1141  $this->assertEquals(1, count($account->contacts));
1142  $this->assertEquals($contact->id, $account->contacts[0]->id);
1143 
1144  $account1 = Account::getById($account1->id);
1145  $this->assertEquals($account->id, $account1->account->id);
1146  $account2 = Account::getById($account2->id);
1147  $this->assertEquals($account->id, $account2->account->id);
1148  $contact = Contact::getById($contact->id);
1149  $this->assertEquals($account->id, $contact->account->id);
1150 
1151  // Now test remove relations
1152  $data['modelRelations'] = array(
1153  'accounts' => array(
1154  array(
1155  'action' => 'remove',
1156  'modelId' => $account1->id,
1157  'modelClassName' => 'Account'
1158  ),
1159  array(
1160  'action' => 'remove',
1161  'modelId' => $account2->id,
1162  'modelClassName' => 'Account'
1163  ),
1164  ),
1165  'contacts' => array(
1166  array(
1167  'action' => 'remove',
1168  'modelId' => $contact->id,
1169  'modelClassName' => 'Contact'
1170  ),
1171  ),
1172  );
1173 
1174  $response = $this->createApiCallWithRelativeUrl('update/' . $compareData['id'], 'PUT', $headers, array('data' => $data));
1175  $response = json_decode($response, true);
1176  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
1178  $updatedModel = Account::getById($compareData['id']);
1179  $this->assertEquals(0, count($updatedModel->accounts));
1180  $this->assertEquals(0, count($updatedModel->contacts));
1181 
1182  $account1 = Account::getById($account1->id);
1183  $this->assertLessThanOrEqual(0, $account1->account->id);
1184  $account2 = Account::getById($account2->id);
1185  $this->assertLessThanOrEqual(0, $account2->account->id);
1186  $contact = Contact::getById($contact->id);
1187  $this->assertLessThanOrEqual(0, $contact->account->id);
1188  }
1189 
1190  public function testEditAccountWithIncompleteData()
1191  {
1192  $super = User::getByUsername('super');
1193  Yii::app()->user->userModel = $super;
1194  $authenticationData = $this->login();
1195  $headers = array(
1196  'Accept: application/json',
1197  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
1198  'ZURMO_TOKEN: ' . $authenticationData['token'],
1199  'ZURMO_API_REQUEST_TYPE: REST',
1200  );
1201 
1202  AccountTestHelper::createAccountByNameTypeAndIndustryForOwner('New Account', 'Customer', 'Automotive', $super);
1203 
1204  // Provide data without required field
1205  $data['officePhone'] = "6438238";
1206  $data['officeFax'] = "6565465436";
1207 
1208  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
1209  $response = json_decode($response, true);
1210  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
1211 
1212  $accounts = Account::getByName('New Account');
1213  $this->assertEquals(1, count($accounts));
1214  $id = $accounts[0]->id;
1215  $data = array();
1216  $data['name'] = '';
1217  $response = $this->createApiCallWithRelativeUrl('update/' . $id, 'PUT', $headers, array('data' => $data));
1218  $response = json_decode($response, true);
1219  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
1220  $this->assertEquals(1, count($response['errors']));
1221  }
1222 
1223  public function testEditAccountWIthIncorrectDataType()
1224  {
1225  $super = User::getByUsername('super');
1226  Yii::app()->user->userModel = $super;
1227  $authenticationData = $this->login();
1228  $headers = array(
1229  'Accept: application/json',
1230  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
1231  'ZURMO_TOKEN: ' . $authenticationData['token'],
1232  'ZURMO_API_REQUEST_TYPE: REST',
1233  );
1234 
1235  AccountTestHelper::createAccountByNameTypeAndIndustryForOwner('Newest Account', 'Customer', 'Automotive', $super);
1236 
1237  // Provide data with wrong type.
1238  $data['name'] = "AAA";
1239  $data['employees'] = "SS";
1240 
1241  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
1242  $response = json_decode($response, true);
1243  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
1244  $this->assertEquals(1, count($response['errors']));
1245 
1246  $accounts = Account::getByName('Newest Account');
1247  $this->assertEquals(1, count($accounts));
1248  $id = $accounts[0]->id;
1249  $data = array();
1250  $data['employees'] = 'DD';
1251  $response = $this->createApiCallWithRelativeUrl('update/' . $id, 'PUT', $headers, array('data' => $data));
1252  $response = json_decode($response, true);
1253  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
1254  $this->assertEquals(1, count($response['errors']));
1255  }
1256 
1257  public function testNotAllowedGuestAction()
1258  {
1259  $authenticationData = $this->login('st', 'st');
1260  $headers = array(
1261  'Accept: application/json',
1262  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
1263  'ZURMO_TOKEN: ' . $authenticationData['token'],
1264  'ZURMO_API_REQUEST_TYPE: REST',
1265  );
1266  $response = $this->createApiCallWithRelativeUrl('read/1', 'GET', $headers);
1267  $response = json_decode($response, true);
1268  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
1269  $this->assertEquals('Sign in required.', $response['message']);
1270  }
1271 
1272  public function testCreateWithScenario()
1273  {
1274  $super = User::getByUsername('super');
1275  Yii::app()->user->userModel = $super;
1276 
1277  $authenticationData = $this->login();
1278  $headers = array(
1279  'Accept: application/json',
1280  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
1281  'ZURMO_TOKEN: ' . $authenticationData['token'],
1282  'ZURMO_API_REQUEST_TYPE: REST',
1283  );
1284  $data['name'] = 'ABCD';
1285  $data['createdDateTime'] = '2014-06-12 15:22:41';
1286  $data['modifiedDateTime'] = '2014-06-12 15:28:41';
1287  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
1288 
1289  $response = json_decode($response, true);
1290  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
1291  $this->assertEquals('It is not allowed to set read only attribute: createdDateTime.', $response['message']);
1292 
1293  // Now try to use some invalid scenario name
1294  $data['modelScenario'] = 'dummyScenarioName';
1295 
1296  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
1297  $response = json_decode($response, true);
1298  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
1299  $this->assertEquals('It is not allowed to set read only attribute: createdDateTime.', $response['message']);
1300 
1301  // Now use scenario name that allow us to set all values
1302  $data['modelScenario'] = 'importModel';
1303  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
1304  $response = json_decode($response, true);
1305  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
1306 
1308  $account = Account::getById($response['data']['id']);
1309  $this->assertEquals($account->name, $response['data']['name']);
1310  $this->assertEquals($account->createdDateTime, $response['data']['createdDateTime']);
1311  }
1312 
1313  public function testUpdateWithScenario()
1314  {
1315  $super = User::getByUsername('super');
1316  Yii::app()->user->userModel = $super;
1317 
1318  $account = AccountTestHelper::createAccountByNameForOwner('Factor X', $super);
1319  $authenticationData = $this->login();
1320  $headers = array(
1321  'Accept: application/json',
1322  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
1323  'ZURMO_TOKEN: ' . $authenticationData['token'],
1324  'ZURMO_API_REQUEST_TYPE: REST',
1325  );
1326  $data['createdDateTime'] = '2014-06-12 14:26:41';
1327  $data['createdDateTime'] = '2014-06-12 14:28:41';
1328  $response = $this->createApiCallWithRelativeUrl('update/' . $account->id, 'PUT', $headers, array('data' => $data));
1329  $response = json_decode($response, true);
1330  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
1331  $this->assertEquals('It is not allowed to set read only attribute: createdDateTime.', $response['message']);
1332 
1333  // Now try to use some invalid scenario name
1334  $data['modelScenario'] = 'dummyScenarioName';
1335 
1336  $response = $this->createApiCallWithRelativeUrl('update/' . $account->id, 'PUT', $headers, array('data' => $data));
1337  $response = json_decode($response, true);
1338  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
1339  $this->assertEquals('It is not allowed to set read only attribute: createdDateTime.', $response['message']);
1340 
1341  // For update, it is not allowed to set created and modified date time
1342  $data['modelScenario'] = 'importModel';
1343  $response = $this->createApiCallWithRelativeUrl('update/' . $account->id, 'PUT', $headers, array('data' => $data));
1344  $response = json_decode($response, true);
1345  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
1346  }
1347 
1356  public function testGetCreatedAccounts()
1357  {
1358  $timestamp = time();
1359  sleep(1);
1360  $super = User::getByUsername('super');
1361  Yii::app()->user->userModel = $super;
1362  $lisa = UserTestHelper::createBasicUser('Lisa');
1363  $lisa->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB_API);
1364  $lisa->setRight('AccountsModule', AccountsModule::getAccessRight());
1365  $this->assertTrue($lisa->save());
1369 
1370  $account1 = AccountTestHelper::createAccountByNameForOwner('Account1', $super);
1371  sleep(1);
1372  $account2 = AccountTestHelper::createAccountByNameForOwner('Account2', $super);
1373  sleep(1);
1374  $account3 = AccountTestHelper::createAccountByNameForOwner('Account3', $super);
1375  sleep(1);
1376  $this->assertTrue($job->run());
1377 
1378  $authenticationData = $this->login();
1379  $headers = array(
1380  'Accept: application/json',
1381  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
1382  'ZURMO_TOKEN: ' . $authenticationData['token'],
1383  'ZURMO_API_REQUEST_TYPE: REST',
1384  );
1385  $data = array(
1386  'sinceDateTime' => DateTimeUtil::convertTimestampToDbFormatDateTime($timestamp),
1387  'pagination' => array(
1388  'pageSize' => 2,
1389  'page' => 1
1390  )
1391  );
1392 
1393  $response = $this->createApiCallWithRelativeUrl('getCreatedItems/', 'POST', $headers, array('data' => $data));
1394  $response = json_decode($response, true);
1395  $this->assertEquals(3, $response['data']['totalCount']);
1396  $this->assertEquals(2, $response['data']['pageSize']);
1397  $this->assertEquals(1, $response['data']['currentPage']);
1398 
1399  $this->assertEquals($account1->id, $response['data']['items'][0]['id']);
1400  $this->assertEquals($super->id, $response['data']['items'][0]['owner']['id']);
1401  $this->assertEquals($account1->name, $response['data']['items'][0]['name']);
1402 
1403  $this->assertEquals($account2->id, $response['data']['items'][1]['id']);
1404  $this->assertEquals($super->id, $response['data']['items'][1]['owner']['id']);
1405  $this->assertEquals($account2->name, $response['data']['items'][1]['name']);
1406 
1407  $data = array(
1408  'sinceDateTime' => DateTimeUtil::convertTimestampToDbFormatDateTime(0),
1409  'pagination' => array(
1410  'pageSize' => 2,
1411  'page' => 2
1412  )
1413  );
1414  $response = $this->createApiCallWithRelativeUrl('getCreatedItems/', 'POST', $headers, array('data' => $data));
1415  $response = json_decode($response, true);
1416  $this->assertEquals(3, $response['data']['totalCount']);
1417  $this->assertEquals(2, $response['data']['pageSize']);
1418  $this->assertEquals(2, $response['data']['currentPage']);
1419 
1420  $this->assertEquals($account3->id, $response['data']['items'][0]['id']);
1421  $this->assertEquals($super->id, $response['data']['items'][0]['owner']['id']);
1422  $this->assertEquals($account3->name, $response['data']['items'][0]['name']);
1423 
1424  // Change owner of $contact1, it should appear in Lisa's created contacts
1425  $account1->owner = $lisa;
1426  $this->assertTrue($account1->save());
1427  sleep(1);
1428  $this->assertTrue($job->run());
1429 
1430  $data = array(
1431  'sinceDateTime' => DateTimeUtil::convertTimestampToDbFormatDateTime($timestamp),
1432  'pagination' => array(
1433  'pageSize' => 2,
1434  'page' => 1
1435  )
1436  );
1437 
1438  $response = $this->createApiCallWithRelativeUrl('getCreatedItems/', 'POST', $headers, array('data' => $data));
1439  $response = json_decode($response, true);
1440  $this->assertEquals(2, $response['data']['totalCount']);
1441  $this->assertEquals(2, $response['data']['pageSize']);
1442  $this->assertEquals(1, $response['data']['currentPage']);
1443 
1444  $this->assertEquals($account2->id, $response['data']['items'][0]['id']);
1445  $this->assertEquals($super->id, $response['data']['items'][0]['owner']['id']);
1446  $this->assertEquals($account2->name, $response['data']['items'][0]['name']);
1447 
1448  $this->assertEquals($account3->id, $response['data']['items'][1]['id']);
1449  $this->assertEquals($super->id, $response['data']['items'][1]['owner']['id']);
1450  $this->assertEquals($account3->name, $response['data']['items'][1]['name']);
1451 
1452  $authenticationData = $this->login('lisa', 'lisa');
1453  $headers = array(
1454  'Accept: application/json',
1455  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
1456  'ZURMO_TOKEN: ' . $authenticationData['token'],
1457  'ZURMO_API_REQUEST_TYPE: REST',
1458  );
1459 
1460  $data = array(
1461  'sinceDateTime' => DateTimeUtil::convertTimestampToDbFormatDateTime($timestamp),
1462  'pagination' => array(
1463  'pageSize' => 2,
1464  'page' => 1
1465  )
1466  );
1467 
1468  $response = $this->createApiCallWithRelativeUrl('getCreatedItems/', 'POST', $headers, array('data' => $data));
1469  $response = json_decode($response, true);
1470  $this->assertEquals(1, $response['data']['totalCount']);
1471  $this->assertEquals(2, $response['data']['pageSize']);
1472  $this->assertEquals(1, $response['data']['currentPage']);
1473 
1474  $this->assertEquals($account1->id, $response['data']['items'][0]['id']);
1475  $this->assertEquals($lisa->id, $response['data']['items'][0]['owner']['id']);
1476  $this->assertEquals($account1->name, $response['data']['items'][0]['name']);
1477  }
1478 
1485  public function testGetModifiedAccounts()
1486  {
1487  $timestamp = time();
1488  sleep(1);
1489  $super = User::getByUsername('super');
1490  Yii::app()->user->userModel = $super;
1493  $account1 = AccountTestHelper::createAccountByNameForOwner('Account1', $super);
1494  $account2 = AccountTestHelper::createAccountByNameForOwner('Account2', $super);
1495  $account3 = AccountTestHelper::createAccountByNameForOwner('Account3', $super);
1496  $account4 = AccountTestHelper::createAccountByNameForOwner('Account4', $super);
1497 
1498  $this->assertTrue($job->run());
1499  sleep(1);
1500 
1501  $authenticationData = $this->login();
1502  $headers = array(
1503  'Accept: application/json',
1504  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
1505  'ZURMO_TOKEN: ' . $authenticationData['token'],
1506  'ZURMO_API_REQUEST_TYPE: REST',
1507  );
1508 
1509  $data = array(
1510  'sinceDateTime' => DateTimeUtil::convertTimestampToDbFormatDateTime($timestamp),
1511  'pagination' => array(
1512  'pageSize' => 2,
1513  'page' => 1
1514  )
1515  );
1516 
1517  $response = $this->createApiCallWithRelativeUrl('getModifiedItems/', 'POST', $headers, array('data' => $data));
1518  $response = json_decode($response, true);
1519  $this->assertEquals(0, $response['data']['totalCount']);
1520  $this->assertEquals(2, $response['data']['pageSize']);
1521  $this->assertEquals(1, $response['data']['currentPage']);
1522 
1523  $timestamp = time();
1524  sleep(2);
1525  $account1->name = "Micheal Modified";
1526  $this->assertTrue($account1->save());
1527  sleep(1);
1528  $account3->name = "Micheal Modified";
1529  $this->assertTrue($account3->save());
1530  sleep(1);
1531  $account4->name = "Micheal Modified";
1532  $this->assertTrue($account4->save());
1533  sleep(2);
1534 
1535  $data = array(
1536  'sinceDateTime' => DateTimeUtil::convertTimestampToDbFormatDateTime($timestamp),
1537  'pagination' => array(
1538  'pageSize' => 2,
1539  'page' => 1
1540  )
1541  );
1542 
1543  $response = $this->createApiCallWithRelativeUrl('getModifiedItems/', 'POST', $headers, array('data' => $data));
1544  $response = json_decode($response, true);
1545  $this->assertEquals(3, $response['data']['totalCount']);
1546  $this->assertEquals(2, $response['data']['pageSize']);
1547  $this->assertEquals(1, $response['data']['currentPage']);
1548 
1549  $this->assertEquals($account1->id, $response['data']['items'][0]['id']);
1550  $this->assertEquals($super->id, $response['data']['items'][0]['owner']['id']);
1551  $this->assertEquals($account1->name, $response['data']['items'][0]['name']);
1552 
1553  $this->assertEquals($account3->id, $response['data']['items'][1]['id']);
1554  $this->assertEquals($super->id, $response['data']['items'][1]['owner']['id']);
1555  $this->assertEquals($account3->name, $response['data']['items'][1]['name']);
1556 
1557  $data = array(
1558  'sinceDateTime' => DateTimeUtil::convertTimestampToDbFormatDateTime($timestamp),
1559  'pagination' => array(
1560  'pageSize' => 2,
1561  'page' => 2
1562  )
1563  );
1564 
1565  $response = $this->createApiCallWithRelativeUrl('getModifiedItems/', 'POST', $headers, array('data' => $data));
1566  $response = json_decode($response, true);
1567  $this->assertEquals(3, $response['data']['totalCount']);
1568  $this->assertEquals(2, $response['data']['pageSize']);
1569  $this->assertEquals(2, $response['data']['currentPage']);
1570 
1571  $this->assertEquals($account4->id, $response['data']['items'][0]['id']);
1572  $this->assertEquals($super->id, $response['data']['items'][0]['owner']['id']);
1573  $this->assertEquals($account4->name, $response['data']['items'][0]['name']);
1574  }
1575 
1582  public function testGetDeletedAccounts()
1583  {
1584  $timestamp = time();
1585  sleep(1);
1586  $super = User::getByUsername('super');
1587  Yii::app()->user->userModel = $super;
1588  $peter = UserTestHelper::createBasicUser('Peter');
1589  $peter->setRight('UsersModule', UsersModule::RIGHT_LOGIN_VIA_WEB_API);
1590  $peter->setRight('AccountsModule', AccountsModule::getAccessRight());
1591  $this->assertTrue($peter->save());
1594  $account1 = AccountTestHelper::createAccountByNameForOwner('Account1', $super);
1595  $account2 = AccountTestHelper::createAccountByNameForOwner('Account2', $super);
1596  $account3 = AccountTestHelper::createAccountByNameForOwner('Account3', $super);
1597  $this->assertTrue($job->run());
1598  sleep(1);
1599  $accountId1 = $account1->id;
1600  $accountId2 = $account2->id;
1601  $accountId3 = $account3->id;
1602  $account1->delete();
1603  $account2->delete();
1604  $account3->delete();
1605 
1606  $this->assertTrue($job->run());
1607 
1608  $authenticationData = $this->login();
1609  $headers = array(
1610  'Accept: application/json',
1611  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
1612  'ZURMO_TOKEN: ' . $authenticationData['token'],
1613  'ZURMO_API_REQUEST_TYPE: REST',
1614  );
1615  $data = array(
1616  'userId' => $super->id,
1617  'sinceDateTime' => DateTimeUtil::convertTimestampToDbFormatDateTime($timestamp),
1618  'pagination' => array(
1619  'pageSize' => 2,
1620  'page' => 1
1621  )
1622  );
1623 
1624  $response = $this->createApiCallWithRelativeUrl('getDeletedItems/', 'POST', $headers, array('data' => $data));
1625  $response = json_decode($response, true);
1626  $this->assertEquals(3, $response['data']['totalCount']);
1627  $this->assertEquals(2, $response['data']['pageSize']);
1628  $this->assertEquals(1, $response['data']['currentPage']);
1629  $this->assertContains($accountId1, $response['data']['items']);
1630  $this->assertContains($accountId2, $response['data']['items']);
1631 
1632  $data = array(
1633  'userId' => $super->id,
1634  'sinceDateTime' => DateTimeUtil::convertTimestampToDbFormatDateTime(0),
1635  'pagination' => array(
1636  'pageSize' => 2,
1637  'page' => 2
1638  )
1639  );
1640 
1641  $response = $this->createApiCallWithRelativeUrl('getDeletedItems/', 'POST', $headers, array('data' => $data));
1642  $response = json_decode($response, true);
1643  $this->assertEquals(3, $response['data']['totalCount']);
1644  $this->assertEquals(2, $response['data']['pageSize']);
1645  $this->assertEquals(2, $response['data']['currentPage']);
1646  $this->assertContains($accountId3, $response['data']['items']);
1647 
1648  // Check with owner change
1650  $account1 = AccountTestHelper::createAccountByNameForOwner('Account1', $super);
1651  $account2 = AccountTestHelper::createAccountByNameForOwner('Account2', $super);
1652  $this->assertTrue($job->run());
1653  sleep(2);
1654  $account1->owner = $peter;
1655  $this->assertTrue($account1->save());
1656  sleep(1);
1657  $this->assertTrue($job->run());
1658  sleep(2);
1659 
1660  $data = array(
1661  'sinceDateTime' => DateTimeUtil::convertTimestampToDbFormatDateTime($timestamp),
1662  'pagination' => array(
1663  'pageSize' => 2,
1664  'page' => 1
1665  )
1666  );
1667  $response = $this->createApiCallWithRelativeUrl('getDeletedItems/', 'POST', $headers, array('data' => $data));
1668  $response = json_decode($response, true);
1669  $this->assertEquals(0, $response['data']['totalCount']);
1670 
1671  $response = $this->createApiCallWithRelativeUrl('getCreatedItems/', 'POST', $headers, array('data' => $data));
1672  $response = json_decode($response, true);
1673  $this->assertEquals(1, $response['data']['totalCount']);
1674 
1675  $authenticationData = $this->login('peter', 'peter');
1676  $headers = array(
1677  'Accept: application/json',
1678  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
1679  'ZURMO_TOKEN: ' . $authenticationData['token'],
1680  'ZURMO_API_REQUEST_TYPE: REST',
1681  );
1682 
1683  $response = $this->createApiCallWithRelativeUrl('getDeletedItems/', 'POST', $headers, array('data' => $data));
1684  $response = json_decode($response, true);
1685  $this->assertEquals(0, $response['data']['totalCount']);
1686 
1687  $response = $this->createApiCallWithRelativeUrl('getCreatedItems/', 'POST', $headers, array('data' => $data));
1688  $response = json_decode($response, true);
1689  $this->assertEquals(1, $response['data']['totalCount']);
1690  }
1691 
1692  protected function getApiControllerClassName()
1693  {
1694  Yii::import('application.modules.accounts.controllers.AccountApiController', true);
1695  return 'AccountsAccountApiController';
1696  }
1697 
1698  protected function getModuleBaseApiUrl()
1699  {
1700  return 'accounts/account/api/';
1701  }
1702  }
1703 ?>
static rebuild($overwriteExistingTables=true, $forcePhp=false, $messageStreamer=null)
static getByUsername($username)
Definition: User.php:49
static forgetAll()
static getModelAttributes(RedBeanModel $model)
static rebuild($overwriteExistingTables=true, $forcePhp=false, $messageStreamer=null)
deleteAllModelsAndRecordsFromReadPermissionTable($modelClassName)
static getByName($name, $shouldCache=true)
static getByName($name)
Definition: Group.php:57
static deleteAll()
static getById($id, $modelClassName=null)
testCreateAccountWithSpecificExplicitPermissions()
Generated on Sat Jul 11 2020 07:10:28
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.