All Data Structures Functions Variables Pages
ApiRestAccountContactAffiliationTest.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 testGetAccountContactAffiliation()
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::createAccountByNameForOwner('firstAccount', Yii::app()->user->userModel);
56  $account2 = AccountTestHelper::createAccountByNameForOwner('secondAccount', Yii::app()->user->userModel);
57 
58  $contact = ContactTestHelper::createContactByNameForOwner('First', Yii::app()->user->userModel);
59  $contact2 = ContactTestHelper::createContactByNameForOwner('Second', Yii::app()->user->userModel);
60 
61  $accountContactAffiliation = new AccountContactAffiliation();
62  $accountContactAffiliation->account = $account;
63  $accountContactAffiliation->contact = $contact;
64  $this->assertTrue($accountContactAffiliation->save());
65 
66  $accountContactAffiliation2 = new AccountContactAffiliation();
67  $accountContactAffiliation2->account = $account2;
68  $accountContactAffiliation2->contact = $contact2;
69  $this->assertTrue($accountContactAffiliation2->save());
70 
71  $compareData = $this->getModelToApiDataUtilData($accountContactAffiliation);
72  $response = $this->createApiCallWithRelativeUrl('read/' . $accountContactAffiliation->id, 'GET', $headers);
73  $response = json_decode($response, true);
74  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
75  $this->assertEquals($compareData, $response['data']);
76  }
77 
82  {
83  Yii::app()->user->userModel = User::getByUsername('super');
84  $authenticationData = $this->login();
85  $headers = array(
86  'Accept: application/json',
87  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
88  'ZURMO_TOKEN: ' . $authenticationData['token'],
89  'ZURMO_API_REQUEST_TYPE: REST',
90  );
91 
92  $affiliations = AccountContactAffiliation::getAll();
93  $this->assertEquals(2, count($affiliations));
94 
95  $response = $this->createApiCallWithRelativeUrl('delete/' . $affiliations[0]->id, 'DELETE', $headers);
96  $response = json_decode($response, true);
97  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
98 
99  $response = $this->createApiCallWithRelativeUrl('read/' . $affiliations[0]->id, 'GET', $headers);
100  $response = json_decode($response, true);
101  $this->assertEquals(ApiResponse::STATUS_FAILURE, $response['status']);
102  $this->assertEquals('The ID specified was invalid.', $response['message']);
103 
104  $affiliations = AccountContactAffiliation::getAll();
105  $this->assertEquals(1, count($affiliations));
106  }
107 
112  {
113  $super = User::getByUsername('super');
114  Yii::app()->user->userModel = $super;
116  $authenticationData = $this->login();
117  $headers = array(
118  'Accept: application/json',
119  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
120  'ZURMO_TOKEN: ' . $authenticationData['token'],
121  'ZURMO_API_REQUEST_TYPE: REST',
122  );
123  $account = AccountTestHelper::createAccountByNameForOwner('CreateAccount', Yii::app()->user->userModel);
124  $contact = ContactTestHelper::createContactByNameForOwner('CreateContact', Yii::app()->user->userModel);
125 
126  $data['account']['id'] = $account->id;
127  $data['contact']['id'] = $contact->id;
128  $response = $this->createApiCallWithRelativeUrl('create/', 'POST', $headers, array('data' => $data));
129  $response = json_decode($response, true);
130  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
131  $this->assertEquals($account->id, $response['data']['account']['id']);
132  $this->assertEquals($contact->id, $response['data']['contact']['id']);
133 
134  $response = $this->createApiCallWithRelativeUrl('read/' . $response['data']['id'], 'GET', $headers);
135  $response = json_decode($response, true);
136  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
137  $this->assertEquals($account->id, $response['data']['account']['id']);
138  $this->assertEquals($contact->id, $response['data']['contact']['id']);
139  }
140 
145  {
146  $super = User::getByUsername('super');
147  Yii::app()->user->userModel = $super;
148 
149  $authenticationData = $this->login();
150  $headers = array(
151  'Accept: application/json',
152  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
153  'ZURMO_TOKEN: ' . $authenticationData['token'],
154  'ZURMO_API_REQUEST_TYPE: REST',
155  );
156 
157  $account = AccountTestHelper::createAccountByNameForOwner('UpdateAccount', Yii::app()->user->userModel);
158  $contact = ContactTestHelper::createContactByNameForOwner('FirstUpdateContact', Yii::app()->user->userModel);
159  $contact2 = ContactTestHelper::createContactByNameForOwner('SecondUpdateContact', Yii::app()->user->userModel);
160 
161  $accountContactAffiliation = new AccountContactAffiliation();
162  $accountContactAffiliation->account = $account;
163  $accountContactAffiliation->contact = $contact;
164  $this->assertTrue($accountContactAffiliation->save());
165 
166  $data['contact']['id'] = $contact2->id;
167 
168  $response = $this->createApiCallWithRelativeUrl('update/' . $accountContactAffiliation->id, 'PUT', $headers, array('data' => $data));
169  $response = json_decode($response, true);
170  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
171  $this->assertEquals($account->id, $response['data']['account']['id']);
172  $this->assertEquals($contact2->id, $response['data']['contact']['id']);
173 
174  $accountContactAffiliationId = $accountContactAffiliation->id;
175  $accountContactAffiliation->forgetAll();
176  $accountContactAffiliation = AccountContactAffiliation::getById($accountContactAffiliationId);
177  $this->assertEquals($accountContactAffiliation->account->id, $response['data']['account']['id']);
178  $this->assertEquals($accountContactAffiliation->contact->id, $response['data']['contact']['id']);
179  }
180 
181  public function testGetAffiliatedAccountsAndContacts()
182  {
183  $super = User::getByUsername('super');
184  Yii::app()->user->userModel = $super;
185 
186  $authenticationData = $this->login();
187  $headers = array(
188  'Accept: application/json',
189  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
190  'ZURMO_TOKEN: ' . $authenticationData['token'],
191  'ZURMO_API_REQUEST_TYPE: REST',
192  );
193 
194  $account = AccountTestHelper::createAccountByNameForOwner('firstAffAccount', Yii::app()->user->userModel);
195  $account2 = AccountTestHelper::createAccountByNameForOwner('secondAffAccount', Yii::app()->user->userModel);
196  $account3 = AccountTestHelper::createAccountByNameForOwner('thirdAffAccount', Yii::app()->user->userModel);
197  $contact = ContactTestHelper::createContactByNameForOwner('firstAffContact', Yii::app()->user->userModel);
198  $contact2 = ContactTestHelper::createContactByNameForOwner('secondAffContact', Yii::app()->user->userModel);
199  $contact3 = ContactTestHelper::createContactByNameForOwner('thirdAffContact', Yii::app()->user->userModel);
200 
201  $accountContactAffiliation = new AccountContactAffiliation();
202  $accountContactAffiliation->account = $account;
203  $accountContactAffiliation->contact = $contact;
204  $this->assertTrue($accountContactAffiliation->save());
205  sleep(1);
206  $accountContactAffiliation2 = new AccountContactAffiliation();
207  $accountContactAffiliation2->account = $account;
208  $accountContactAffiliation2->contact = $contact2;
209  $this->assertTrue($accountContactAffiliation2->save());
210  sleep(1);
211  $accountContactAffiliation = new AccountContactAffiliation();
212  $accountContactAffiliation->account = $account2;
213  $accountContactAffiliation->contact = $contact2;
214  $this->assertTrue($accountContactAffiliation->save());
215  sleep(1);
216  $accountContactAffiliation = new AccountContactAffiliation();
217  $accountContactAffiliation->account = $account3;
218  $accountContactAffiliation->contact = $contact3;
219  $this->assertTrue($accountContactAffiliation->save());
220 
221  // Get all contacts affiliated with account
222  $data = array(
223  'search' => array(
224  'modelClassName' => 'AccountContactAffiliation',
225  'searchAttributeData' => array(
226  'clauses' => array(
227  1 => array(
228  'attributeName' => 'account',
229  'relatedAttributeName' => 'id',
230  'operatorType' => 'equals',
231  'value' => $account->id,
232  ),
233  ),
234  'structure' => '1',
235  ),
236  ),
237  'pagination' => array(
238  'page' => 1,
239  'pageSize' => 5,
240  ),
241  'sort' => 'id asc',
242  );
243 
244  $response = $this->createApiCallWithRelativeUrl('search/filter/', 'POST', $headers, array('data' => $data));
245  $response = json_decode($response, true);
246  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
247  $this->assertEquals(2, $response['data']['totalCount']);
248  $this->assertEquals(2, count($response['data']['items']));
249  $this->assertEquals($account->id, $response['data']['items'][0]['account']['id']);
250  $this->assertEquals($contact->id, $response['data']['items'][0]['contact']['id']);
251  $this->assertEquals($account->id, $response['data']['items'][1]['account']['id']);
252  $this->assertEquals($contact2->id, $response['data']['items'][1]['contact']['id']);
253 
254  // Get all accounts affiliated with account
255  $data = array(
256  'search' => array(
257  'modelClassName' => 'AccountContactAffiliation',
258  'searchAttributeData' => array(
259  'clauses' => array(
260  1 => array(
261  'attributeName' => 'contact',
262  'relatedAttributeName' => 'id',
263  'operatorType' => 'equals',
264  'value' => $contact2->id,
265  ),
266  ),
267  'structure' => '1',
268  ),
269  ),
270  'pagination' => array(
271  'page' => 1,
272  'pageSize' => 5,
273  ),
274  'sort' => 'id asc',
275  );
276 
277  $response = $this->createApiCallWithRelativeUrl('search/filter/', 'POST', $headers, array('data' => $data));
278  $response = json_decode($response, true);
279  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
280  $this->assertEquals(2, $response['data']['totalCount']);
281  $this->assertEquals(2, count($response['data']['items']));
282  $this->assertEquals($account->id, $response['data']['items'][0]['account']['id']);
283  $this->assertEquals($contact2->id, $response['data']['items'][0]['contact']['id']);
284  $this->assertEquals($account2->id, $response['data']['items'][1]['account']['id']);
285  $this->assertEquals($contact2->id, $response['data']['items'][1]['contact']['id']);
286  }
287 
288  public function testListAccountContactAffiliationAttributes()
289  {
290  $super = User::getByUsername('super');
291  Yii::app()->user->userModel = $super;
292 
293  $authenticationData = $this->login();
294  $headers = array(
295  'Accept: application/json',
296  'ZURMO_SESSION_ID: ' . $authenticationData['sessionId'],
297  'ZURMO_TOKEN: ' . $authenticationData['token'],
298  'ZURMO_API_REQUEST_TYPE: REST',
299  );
301  $response = $this->createApiCallWithRelativeUrl('listAttributes/' , 'GET', $headers);
302  $response = json_decode($response, true);
303  $this->assertEquals(ApiResponse::STATUS_SUCCESS, $response['status']);
304  $this->assertEquals($allAttributes, $response['data']['items']);
305  }
306 
307  protected function getApiControllerClassName()
308  {
309  Yii::import('application.modules.accountContactAffiliations.controllers.AccountContactAffiliationApiController', true);
310  return 'AccountContactAffiliationsAccountContactAffiliationApiController';
311  }
312 
313  protected function getModuleBaseApiUrl()
314  {
315  return 'accountContactAffiliations/AccountContactAffiliation/api/';
316  }
317  }
318 ?>
static rebuild($overwriteExistingTables=true, $forcePhp=false, $messageStreamer=null)
static getByUsername($username)
Definition: User.php:49
static getModelAttributes(RedBeanModel $model)
static getById($id, $modelClassName=null)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
Generated on Tue May 26 2020 07:10:29