Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ApiRestCustomModelActionTest.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 static function getDependentTestModelClassNames()
43  {
44  return array('ApiTestModelItem2');
45  }
46 
47  public function testCustomGetAction()
48  {
49  $super = User::getByUsername('super');
50  Yii::app()->user->userModel = $super;
51  $apiTestModelItem2Temp = new ApiTestModelItem2();
52  $apiTestModelItem2Temp->name = 'first';
53  $saved = $apiTestModelItem2Temp->save();
54  $this->assertTrue($saved);
55 
56  $authenticationData = $this->login();
57  $headers = array(
58  'Accept: application/json',
59  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
60  'ZURMO_TOKEN: ' . $authenticationData['token'],
61  'ZURMO_API_REQUEST_TYPE: REST',
62  );
63  $testModels = ApiTestModelItem2::getByName('first');
64  $this->assertEquals(1, count($testModels));
65  $compareData = $this->getModelToApiDataUtilData($testModels[0]);
66 
67  $response = $this->createApiCallWithRelativeUrl('customGet/?id=' . $compareData['id'], 'GET', $headers);
68  $response = json_decode($response, true);
69  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
70  $this->assertEquals($compareData, $response['data']);
71  $this->assertArrayHasKey('owner', $response['data']);
72  $this->assertCount(2, $response['data']['owner']);
73  $this->assertArrayHasKey('id', $response['data']['owner']);
74  $this->assertEquals($super->id, $response['data']['owner']['id']);
75  $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']);
76  $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']);
77  $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']);
78  }
79 
83  public function testCustomDeleteAction()
84  {
85  $super = User::getByUsername('super');
86  Yii::app()->user->userModel = $super;
87 
88  $authenticationData = $this->login();
89  $headers = array(
90  'Accept: application/json',
91  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
92  'ZURMO_TOKEN: ' . $authenticationData['token'],
93  'ZURMO_API_REQUEST_TYPE: REST',
94  );
95 
96  $testModels = ApiTestModelItem2::getByName('first');
97  $this->assertEquals(1, count($testModels));
98 
99  //Test Delete
100  $response = $this->createApiCallWithRelativeUrl('customDelete/?id=' . $testModels[0]->id, 'DELETE', $headers);
101  $response = json_decode($response, true);
102  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
103 
104  $response = $this->createApiCallWithRelativeUrl('customGet/?id=' . $testModels[0]->id, 'GET', $headers);
105  $response = json_decode($response, true);
106  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
107  }
108 
112  public function testCustomPostAction()
113  {
114  $super = User::getByUsername('super');
115  Yii::app()->user->userModel = $super;
116 
117  $authenticationData = $this->login();
118  $headers = array(
119  'Accept: application/json',
120  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
121  'ZURMO_TOKEN: ' . $authenticationData['token'],
122  'ZURMO_API_REQUEST_TYPE: REST',
123  );
124 
125  //Test Create
126  $data['name'] = 'new name with no permissions';
127  $response = $this->createApiCallWithRelativeUrl('customPost/', 'POST', $headers, array('data' => $data));
128  $response = json_decode($response, true);
129  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
130  $this->assertTrue(is_int($response['data']['id']));
131  $this->assertGreaterThan(0, $response['data']['id']);
132  $modelId = $response['data']['id'];
133 
134  $this->assertArrayHasKey('owner', $response['data']);
135  $this->assertCount(2, $response['data']['owner']);
136  $this->assertArrayHasKey('id', $response['data']['owner']);
137  $this->assertEquals($super->id, $response['data']['owner']['id']);
138  $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']);
139  $this->assertCount(2, $response['data']['explicitReadWriteModelPermissions']);
140  $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']);
141  $this->assertEquals(1, $response['data']['explicitReadWriteModelPermissions']['type']);
142  $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']);
143  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['nonEveryoneGroup']);
144 
145  $data['owner'] = array(
146  'id' => $super->id,
147  'username' => 'super'
148  );
149  $data['createdByUser'] = array(
150  'id' => $super->id,
151  'username' => 'super'
152  );
153  $data['modifiedByUser'] = array(
154  'id' => $super->id,
155  'username' => 'super'
156  );
157  $data['modelItem'] = null;
158  // unset explicit permissions, we won't use these in comparison.
159  unset($response['data']['explicitReadWriteModelPermissions']);
160  unset($response['data']['createdDateTime']);
161  unset($response['data']['modifiedDateTime']);
162  unset($response['data']['id']);
163  ksort($data);
164  ksort($response['data']);
165  $this->assertEquals($data, $response['data']);
166 
167  $response = $this->createApiCallWithRelativeUrl('read/' . $modelId, 'GET', $headers);
168  $response = json_decode($response, true);
169  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
170  $this->assertArrayHasKey('data', $response);
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 
176  $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']);
177  $this->assertCount(2, $response['data']['explicitReadWriteModelPermissions']);
178  $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']);
179  $this->assertEquals(1, $response['data']['explicitReadWriteModelPermissions']['type']);
180  $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']);
181  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['nonEveryoneGroup']);
182  }
183 
188  {
189  $super = User::getByUsername('super');
190  Yii::app()->user->userModel = $super;
191  $billy = User::getByUsername('billy');
192  $authenticationData = $this->login();
193  $headers = array(
194  'Accept: application/json',
195  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
196  'ZURMO_TOKEN: ' . $authenticationData['token'],
197  'ZURMO_API_REQUEST_TYPE: REST',
198  );
199 
200  //Test Create
201  $data['name'] = 'new name with just owner';
202  $data['owner'] = array('id' => $billy->id);
203 
204  $response = $this->createApiCallWithRelativeUrl('customPost/', 'POST', $headers, array('data' => $data));
205  $response = json_decode($response, true);
206  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
207  $this->assertTrue(is_int($response['data']['id']));
208  $this->assertGreaterThan(0, $response['data']['id']);
209  $modelId = $response['data']['id'];
210 
211  $this->assertArrayHasKey('owner', $response['data']);
212  $this->assertCount(2, $response['data']['owner']);
213  $this->assertArrayHasKey('id', $response['data']['owner']);
214  $this->assertEquals($billy->id, $response['data']['owner']['id']);
215 
216  $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']);
217  $this->assertCount(2, $response['data']['explicitReadWriteModelPermissions']);
218  $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']);
219  $this->assertEquals(1, $response['data']['explicitReadWriteModelPermissions']['type']);
220  $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']);
221  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['nonEveryoneGroup']);
222 
223  $data['owner'] = array(
224  'id' => $billy->id,
225  'username' => 'billy'
226  );
227  $data['createdByUser'] = array(
228  'id' => $super->id,
229  'username' => 'super'
230  );
231  $data['modifiedByUser'] = array(
232  'id' => $super->id,
233  'username' => 'super'
234  );
235  $data['modelItem'] = null;
236  // unset explicit permissions, we won't use these in comparison.
237  unset($response['data']['explicitReadWriteModelPermissions']);
238  unset($response['data']['createdDateTime']);
239  unset($response['data']['modifiedDateTime']);
240  unset($response['data']['id']);
241  ksort($data);
242  ksort($response['data']);
243  $this->assertEquals($data, $response['data']);
244 
245  $response = $this->createApiCallWithRelativeUrl('read/' . $modelId, 'GET', $headers);
246  $response = json_decode($response, true);
247  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
248  $this->assertArrayHasKey('data', $response);
249  $this->assertArrayHasKey('owner', $response['data']);
250  $this->assertCount(2, $response['data']['owner']);
251  $this->assertArrayHasKey('id', $response['data']['owner']);
252  $this->assertEquals($billy->id, $response['data']['owner']['id']);
253 
254  $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']);
255  $this->assertCount(2, $response['data']['explicitReadWriteModelPermissions']);
256  $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']);
257  $this->assertEquals(1, $response['data']['explicitReadWriteModelPermissions']['type']);
258  $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']);
259  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['nonEveryoneGroup']);
260  }
261 
266  {
267  $super = User::getByUsername('super');
268  Yii::app()->user->userModel = $super;
269  $authenticationData = $this->login();
270  $headers = array(
271  'Accept: application/json',
272  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
273  'ZURMO_TOKEN: ' . $authenticationData['token'],
274  'ZURMO_API_REQUEST_TYPE: REST',
275  );
276 
277  //Test Create
278  $data['name'] = 'new name with owner only';
279  // TODO: @Shoaibi/@Ivica: null does not work, empty works. null doesn't send it.
280  $data['explicitReadWriteModelPermissions'] = array('nonEveryoneGroup' => '', 'type' => '');
281 
282  $response = $this->createApiCallWithRelativeUrl('customPost/', 'POST', $headers, array('data' => $data));
283  $response = json_decode($response, true);
284  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
285  $this->assertTrue(is_int($response['data']['id']));
286  $this->assertGreaterThan(0, $response['data']['id']);
287  $modelId = $response['data']['id'];
288 
289  $this->assertArrayHasKey('owner', $response['data']);
290  $this->assertCount(2, $response['data']['owner']);
291  $this->assertArrayHasKey('id', $response['data']['owner']);
292  $this->assertEquals($super->id, $response['data']['owner']['id']);
293  $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']);
294  $this->assertCount(2, $response['data']['explicitReadWriteModelPermissions']);
295  $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']);
296  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['type']);
297  // following also works. wonder why.
298  //$this->assertTrue(null === $response['data']['explicitReadWriteModelPermissions']['type']);
299  $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']);
300  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['nonEveryoneGroup']);
301 
302  $data['owner'] = array(
303  'id' => $super->id,
304  'username' => 'super'
305  );
306  $data['createdByUser'] = array(
307  'id' => $super->id,
308  'username' => 'super'
309  );
310  $data['modifiedByUser'] = array(
311  'id' => $super->id,
312  'username' => 'super'
313  );
314  $data['modelItem'] = null;
315  unset($response['data']['createdDateTime']);
316  unset($response['data']['modifiedDateTime']);
317  unset($response['data']['id']);
318  ksort($data);
319  ksort($response['data']);
320  $this->assertEquals($data, $response['data']);
321 
322  $response = $this->createApiCallWithRelativeUrl('read/' . $modelId, 'GET', $headers);
323  $response = json_decode($response, true);
324  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
325  $this->assertArrayHasKey('data', $response);
326  $this->assertArrayHasKey('owner', $response['data']);
327  $this->assertCount(2, $response['data']['owner']);
328  $this->assertArrayHasKey('id', $response['data']['owner']);
329  $this->assertEquals($super->id, $response['data']['owner']['id']);
330 
331  $this->assertArrayHasKey('explicitReadWriteModelPermissions', $response['data']);
332  $this->assertCount(2, $response['data']['explicitReadWriteModelPermissions']);
333  $this->assertArrayHasKey('type', $response['data']['explicitReadWriteModelPermissions']);
334  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['type']);
335  $this->assertArrayHasKey('nonEveryoneGroup', $response['data']['explicitReadWriteModelPermissions']);
336  $this->assertEquals('', $response['data']['explicitReadWriteModelPermissions']['nonEveryoneGroup']);
337  }
338 
342  public function testCustomUpdateAction()
343  {
344  $super = User::getByUsername('super');
345  Yii::app()->user->userModel = $super;
346 
347  $authenticationData = $this->login();
348  $headers = array(
349  'Accept: application/json',
350  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
351  'ZURMO_TOKEN: ' . $authenticationData['token'],
352  'ZURMO_API_REQUEST_TYPE: REST',
353  );
354 
355  $testModels = ApiTestModelItem2::getByName('new name with just owner');
356  $this->assertEquals(1, count($testModels));
357  $compareData = $this->getModelToApiDataUtilData($testModels[0]);
358  $data['name'] = 'new name 2 with just owner';
359  $compareData['name'] = 'new name 2 with just owner';
360  $group = static::$randomNonEveryoneNonAdministratorsGroup;
361  $explicitReadWriteModelPermissions = array('type' => 2, 'nonEveryoneGroup' => $group->id);
362  $data['explicitReadWriteModelPermissions'] = $explicitReadWriteModelPermissions;
363  $compareData['explicitReadWriteModelPermissions'] = $explicitReadWriteModelPermissions;
364  $testModels[0]->forget();
365 
366  $response = $this->createApiCallWithRelativeUrl('customUpdate/?id=' . $compareData['id'], 'PUT', $headers, array('data' => $data));
367  $response = json_decode($response, true);
368  unset($response['data']['modifiedDateTime']);
369  unset($compareData['modifiedDateTime']);
370  ksort($compareData);
371  ksort($response['data']);
372  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
373  $this->assertEquals($compareData, $response['data']);
374 
375  $response = $this->createApiCallWithRelativeUrl('customGet/?id=' . $compareData['id'], 'GET', $headers);
376  $response = json_decode($response, true);
377  unset($response['data']['modifiedDateTime']);
378  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
379  $this->assertEquals('new name 2 with just owner', $response['data']['name']);
380  $this->assertEquals($compareData, $response['data']);
381  }
382 
386  public function testCustomListAction()
387  {
388  $super = User::getByUsername('super');
389  Yii::app()->user->userModel = $super;
390 
391  $authenticationData = $this->login();
392  $headers = array(
393  'Accept: application/json',
394  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
395  'ZURMO_TOKEN: ' . $authenticationData['token'],
396  'ZURMO_API_REQUEST_TYPE: REST',
397  );
398 
399  $testModels = ApiTestModelItem2::getByName('new name 2 with just owner');
400  $this->assertEquals(1, count($testModels));
401  $compareData = $this->getModelToApiDataUtilData($testModels[0]);
402 
403  $response = $this->createApiCallWithRelativeUrl('customList/', 'GET', $headers);
404  $response = json_decode($response, true);
405  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
406  $this->assertEquals(3, count($response['data']['items']));
407  $this->assertEquals(3, $response['data']['totalCount']);
408  $this->assertEquals(1, $response['data']['currentPage']);
409  $this->assertEquals($compareData, $response['data']['items'][1]);
410  }
411 
412  public function testListCustomModelAttributes()
413  {
414  $super = User::getByUsername('super');
415  Yii::app()->user->userModel = $super;
416 
417  $authenticationData = $this->login();
418  $headers = array(
419  'Accept: application/json',
420  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
421  'ZURMO_TOKEN: ' . $authenticationData['token'],
422  'ZURMO_API_REQUEST_TYPE: REST',
423  );
425 
426  $response = $this->createApiCallWithRelativeUrl('listAttributes/' , 'GET', $headers);
427  $response = json_decode($response, true);
428  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
429  $this->assertEquals($allAttributes, $response['data']['items']);
430  }
431 
432  protected function getApiControllerClassName()
433  {
434  Yii::import('application.modules.api.controllers.TestModelItem2ApiController', true);
435  return 'ApiTestModelItem2ApiController';
436  }
437 
438  protected function getModuleBaseApiUrl()
439  {
440  return 'api/testModelItem2/api/';
441  }
442  }
443 ?>
static getByUsername($username)
Definition: User.php:49
static getModelAttributes(RedBeanModel $model)
Generated on Fri Jul 3 2020 07:10:30
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.