All Data Structures Functions Variables Pages
RedBeanModelToApiDataUtilTest.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 setUpBeforeClass()
43  {
44  parent::setUpBeforeClass();
45  $super = SecurityTestHelper::createSuperAdmin();
46  $multiSelectValues = array(
47  'Multi 1',
48  'Multi 2',
49  'Multi 3',
50  );
51  $customFieldData = CustomFieldData::getByName('ApiTestMultiDropDown');
52  $customFieldData->serializedData = serialize($multiSelectValues);
53  $save = $customFieldData->save();
54  assert('$save'); // Not Coding Standard
55 
56  $tagCloudValues = array(
57  'Cloud 1',
58  'Cloud 2',
59  'Cloud 3',
60  );
61  $customFieldData = CustomFieldData::getByName('ApiTestTagCloud');
62  $customFieldData->serializedData = serialize($tagCloudValues);
63  $save = $customFieldData->save();
64  assert('$save'); // Not Coding Standard
65  }
66 
67  public static function getDependentTestModelClassNames()
68  {
69  return array('ApiTestModelItem', 'ApiTestModelItem2', 'ApiTestModelItem3', 'ApiTestModelItem4');
70  }
71 
72  public function testGetDataWithNoRelationsSet()
73  {
74  $super = User::getByUsername('super');
75  Yii::app()->user->userModel = $super;
76  $testItem = new ApiTestModelItem();
77  $testItem->firstName = 'Bob';
78  $testItem->lastName = 'Bob';
79  $testItem->boolean = true;
80  $testItem->date = '2002-04-03';
81  $testItem->dateTime = '2002-04-03 02:00:43';
82  $testItem->float = 54.22;
83  $testItem->integer = 10;
84  $testItem->phone = '21313213';
85  $testItem->string = 'aString';
86  $testItem->textArea = 'Some Text Area';
87  $testItem->url = 'http://www.asite.com';
88  $testItem->owner = $super;
89  $testItem->language = 'en';
90 
91  $customFieldValue = new CustomFieldValue();
92  $customFieldValue->value = 'Multi 1';
93  $testItem->multiDropDown->values->add($customFieldValue);
94 
95  $customFieldValue = new CustomFieldValue();
96  $customFieldValue->value = 'Multi 3';
97  $testItem->multiDropDown->values->add($customFieldValue);
98 
99  $customFieldValue = new CustomFieldValue();
100  $customFieldValue->value = 'Cloud 2';
101  $testItem->tagCloud->values->add($customFieldValue);
102 
103  $customFieldValue = new CustomFieldValue();
104  $customFieldValue->value = 'Cloud 3';
105  $testItem->tagCloud->values->add($customFieldValue);
106 
107  $createStamp = strtotime(DateTimeUtil::convertTimestampToDbFormatDateTime(time()));
108  $this->assertTrue($testItem->save());
109  $id = $testItem->id;
110  $testItem->forget();
111  unset($testItem);
112 
113  $testItem = ApiTestModelItem::getById($id);
114  $adapter = new RedBeanModelToApiDataUtil($testItem);
115  $data = $adapter->getData();
116 
117  $compareData = array(
118  'id' => $id,
119  'firstName' => 'Bob',
120  'lastName' => 'Bob',
121  'boolean' => '1',
122  'date' => '2002-04-03',
123  'dateTime' => '2002-04-03 02:00:43',
124  'float' => '54.22',
125  'integer' => '10',
126  'phone' => '21313213',
127  'string' => 'aString',
128  'textArea' => 'Some Text Area',
129  'url' => 'http://www.asite.com',
130  'currencyValue' => null,
131  'dropDown' => null,
132  'radioDropDown' => null,
133  'multiDropDown' => array('values' => array('Multi 1', 'Multi 3')),
134  'tagCloud' => array('values' => array('Cloud 2', 'Cloud 3')),
135  'modelItem2' => null,
136  'primaryEmail' => null,
137  'primaryAddress' => null,
138  'secondaryEmail' => null,
139  'language' => 'en',
140  'owner' => array(
141  'id' => $super->id,
142  'username' => 'super'
143  ),
144  'createdByUser' => array(
145  'id' => $super->id,
146  'username' => 'super'
147  ),
148  'modifiedByUser' => array(
149  'id' => $super->id,
150  'username' => 'super'
151  ),
152  );
153 
154  // Because small delay in IO operation, allow tresholds
155  $this->assertEquals($createStamp, strtotime($data['createdDateTime']), '', 2);
156  $this->assertEquals($createStamp, strtotime($data['modifiedDateTime']), '', 2);
157  unset($data['createdDateTime']);
158  unset($data['modifiedDateTime']);
159 
160  $this->assertEquals($compareData, $data);
161  }
162 
163  public function testGetDataWithAllHasOneOrOwnedRelationsSet()
164  {
165  $super = User::getByUsername('super');
166  Yii::app()->user->userModel = $super;
167 
168  $values = array(
169  'Test1',
170  'Test2',
171  'Test3',
172  'Sample',
173  'Demo',
174  );
175  $customFieldData = CustomFieldData::getByName('ApiTestDropDown');
176  $customFieldData->serializedData = serialize($values);
177  $saved = $customFieldData->save();
178  $this->assertTrue($saved);
179 
180  $currencies = Currency::getAll();
181  $currencyValue = new CurrencyValue();
182  $currencyValue->value = 100;
183  $currencyValue->currency = $currencies[0];
184  $this->assertEquals('USD', $currencyValue->currency->code);
185 
186  $testItem = new ApiTestModelItem();
187  $testItem->firstName = 'Bob2';
188  $testItem->lastName = 'Bob2';
189  $testItem->boolean = true;
190  $testItem->date = '2002-04-03';
191  $testItem->dateTime = '2002-04-03 02:00:43';
192  $testItem->float = 54.22;
193  $testItem->integer = 10;
194  $testItem->phone = '21313213';
195  $testItem->string = 'aString';
196  $testItem->textArea = 'Some Text Area';
197  $testItem->url = 'http://www.asite.com';
198  $testItem->owner = $super;
199  $testItem->currencyValue = $currencyValue;
200  $testItem->dropDown->value = $values[1];
201  $createStamp = strtotime(DateTimeUtil::convertTimestampToDbFormatDateTime(time()));
202  $this->assertTrue($testItem->save());
203  $id = $testItem->id;
204  $testItem->forget();
205  unset($testItem);
206 
207  $testItem = ApiTestModelItem::getById($id);
208  $adapter = new RedBeanModelToApiDataUtil($testItem);
209  $data = $adapter->getData();
210  $compareData = array(
211  'id' => $id,
212  'firstName' => 'Bob2',
213  'lastName' => 'Bob2',
214  'boolean' => 1,
215  'date' => '2002-04-03',
216  'dateTime' => '2002-04-03 02:00:43',
217  'float' => 54.22,
218  'integer' => 10,
219  'phone' => '21313213',
220  'string' => 'aString',
221  'textArea' => 'Some Text Area',
222  'url' => 'http://www.asite.com',
223  'currencyValue' => array(
224  'id' => $currencyValue->id,
225  'value' => 100,
226  'rateToBase' => 1,
227  'currency' => array(
228  'id' => $currencies[0]->id,
229  ),
230  ),
231  'dropDown' => array(
232  'id' => $testItem->dropDown->id,
233  'value' => $values[1],
234  ),
235  'radioDropDown' => null,
236  'multiDropDown' => array('values' => null),
237  'tagCloud' => array('values' => null),
238  'modelItem2' => null,
239  'primaryEmail' => null,
240  'primaryAddress' => null,
241  'secondaryEmail' => null,
242  'language' => null,
243  'owner' => array(
244  'id' => $super->id,
245  'username' => 'super'
246  ),
247  'createdByUser' => array(
248  'id' => $super->id,
249  'username' => 'super'
250  ),
251  'modifiedByUser' => array(
252  'id' => $super->id,
253  'username' => 'super'
254  ),
255  );
256 
257  // Because small delay in IO operation, allow tresholds
258  $this->assertEquals($createStamp, strtotime($data['createdDateTime']), '', 2);
259  $this->assertEquals($createStamp, strtotime($data['modifiedDateTime']), '', 2);
260  unset($data['createdDateTime']);
261  unset($data['modifiedDateTime']);
262 
263  $this->assertEquals($compareData, $data);
264  }
265 
266  public function testGetDataWithHasOneRelatedModel()
267  {
268  $super = User::getByUsername('super');
269  Yii::app()->user->userModel = $super;
270 
271  $currencies = Currency::getAll();
272  $currencyValue = new CurrencyValue();
273  $currencyValue->value = 100;
274  $currencyValue->currency = $currencies[0];
275  $this->assertEquals('USD', $currencyValue->currency->code);
276 
277  $testItem2 = new ApiTestModelItem2();
278  $testItem2->name = 'John';
279  $this->assertTrue($testItem2->save());
280 
281  $testItem4 = new ApiTestModelItem4();
282  $testItem4->name = 'John';
283  $this->assertTrue($testItem4->save());
284 
285  //HAS_MANY and MANY_MANY relationships should be ignored.
286  $testItem3_1 = new ApiTestModelItem3();
287  $testItem3_1->name = 'Kevin';
288  $this->assertTrue($testItem3_1->save());
289 
290  $testItem3_2 = new ApiTestModelItem3();
291  $testItem3_2->name = 'Jim';
292  $this->assertTrue($testItem3_2->save());
293 
294  $testItem = new ApiTestModelItem();
295  $testItem->firstName = 'Bob3';
296  $testItem->lastName = 'Bob3';
297  $testItem->boolean = true;
298  $testItem->date = '2002-04-03';
299  $testItem->dateTime = '2002-04-03 02:00:43';
300  $testItem->float = 54.22;
301  $testItem->integer = 10;
302  $testItem->phone = '21313213';
303  $testItem->string = 'aString';
304  $testItem->textArea = 'Some Text Area';
305  $testItem->url = 'http://www.asite.com';
306  $testItem->owner = $super;
307  $testItem->currencyValue = $currencyValue;
308  $testItem->modelItem2 = $testItem2;
309  $testItem->modelItems3->add($testItem3_1);
310  $testItem->modelItems3->add($testItem3_2);
311  $testItem->modelItems4->add($testItem4);
312  $createStamp = strtotime(DateTimeUtil::convertTimestampToDbFormatDateTime(time()));
313  $this->assertTrue($testItem->save());
314  $id = $testItem->id;
315  $testItem->forget();
316  unset($testItem);
317 
318  $testItem = ApiTestModelItem::getById($id);
319  $adapter = new RedBeanModelToApiDataUtil($testItem);
320  $data = $adapter->getData();
321  $compareData = array(
322  'id' => $id,
323  'firstName' => 'Bob3',
324  'lastName' => 'Bob3',
325  'boolean' => '1',
326  'date' => '2002-04-03',
327  'dateTime' => '2002-04-03 02:00:43',
328  'float' => '54.22',
329  'integer' => '10',
330  'phone' => '21313213',
331  'string' => 'aString',
332  'textArea' => 'Some Text Area',
333  'url' => 'http://www.asite.com',
334  'currencyValue' => array(
335  'id' => $currencyValue->id,
336  'value' => 100,
337  'rateToBase' => 1,
338  'currency' => array(
339  'id' => $currencies[0]->id,
340  ),
341  ),
342  'dropDown' => null,
343  'radioDropDown' => null,
344  'multiDropDown' => array('values' => null),
345  'tagCloud' => array('values' => null),
346  'modelItem2' => array('id' => $testItem2->id),
347  'primaryEmail' => null,
348  'primaryAddress' => null,
349  'secondaryEmail' => null,
350  'language' => null,
351  'owner' => array(
352  'id' => $super->id,
353  'username' => 'super'
354  ),
355  'createdByUser' => array(
356  'id' => $super->id,
357  'username' => 'super'
358  ),
359  'modifiedByUser' => array(
360  'id' => $super->id,
361  'username' => 'super'
362  ),
363  );
364  // Because small delay in IO operation, allow tresholds
365  $this->assertEquals($createStamp, strtotime($data['createdDateTime']), '', 2);
366  $this->assertEquals($createStamp, strtotime($data['modifiedDateTime']), '', 2);
367  unset($data['createdDateTime']);
368  unset($data['modifiedDateTime']);
369 
370  $this->assertEquals($compareData, $data);
371  }
372  }
373 ?>
static getByUsername($username)
Definition: User.php:49
static getByName($name, $shouldCache=true)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null, $buildFirstCurrency=true)
Definition: Currency.php:107
static getById($id, $modelClassName=null)
Generated on Fri Jun 5 2020 07:10:30