Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
RedBeanModelRelationsTest.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 
37  /*
38  These tests use classes in the tests/models directory which
39  have this inheritance structure to test the RedBeanModel class.
40 
41  H has a name and is-a RedBeanModel.
42  I, J, K, L are Hs.
43  I has-a J.
44  I has-many Ks.
45  I is many-many with L.
46  */
47 
49  {
50  const MANY_COUNT = 10;
51 
52  public static function getDependentTestModelClassNames()
53  {
54  return array('H', 'I', 'J', 'K', 'L');
55  }
56 
57  public function testSetupAllKindsOfRelations()
58  {
59  // Give an I... a J, a bunch of Ks, and a bunch of Ls.
60  $firstI = new I();
61  $firstI->name = 'The First I!';
62  $firstI->j = new J();
63  $firstI->j->name = 'The J!';
64  for ($i = 0; $i < self::MANY_COUNT; $i++)
65  {
66  $k = new K();
67  $l = new L();
68  $k->name = "K-$i";
69  $l->name = "L-$i";
70  $firstI->ks->add($k);
71  $firstI->ls->add($l);
72  // Both sides of a MANY MANY need to be saved.
73  $this->assertTrue($l->save());
74  }
75  // The ks, being on a ONE MANY get saved along with the ONE.
76  $this->assertTrue($firstI->save());
77 
78  // Give a second I... one of the Ls that the I has.
79  $secondI = new I();
80  $secondI->name = 'The Second I!';
81  $secondI->ls->add($firstI->ls[0]);
82  $this->assertTrue($secondI->save());
83 
84  // So the L is related to two Is - because it is a M:N.
85  }
86 
91  {
92  $firstI = H::getByName('The First I!')->castDown(array('I'));
93  $this->assertEquals ('The First I!', $firstI->name);
94  $this->assertNotNull( $firstI->j);
95  $this->assertEquals ('The J!', $firstI->j->name);
96  $this->assertNotNull( $firstI->j->i);
97  $this->assertTrue ( $firstI->isSame($firstI->j->i));
98  $this->assertEquals ('The First I!', $firstI->j->i->name);
99  $this->assertTrue ($firstI === $firstI->j->i);
100  }
101 
106  {
107  $theI = H::getByName('The First I!')->castDown(array('I'));
108  $this->assertEquals(array(), $theI->getErrors());
109  $this->assertEquals(array(), $theI->j->getErrors());
110  }
111 
116  {
117  $theI = H::getByName('The First I!')->castDown(array('I'));
118  $this->assertTrue($theI->validate());
119  $this->assertEquals(array(), $theI->getErrors());
120  $this->assertTrue($theI->j->validate());
121  $this->assertEquals(array(), $theI->j->getErrors());
122  }
123 
128  {
129  $theI = H::getByName('The First I!')->castDown(array('I'));
130  $theI->name = 'Tooooooo looooooong';
131  $this->assertFalse($theI->validate());
132  $this->assertEquals(
133  array('name' => array('Name is too long (maximum is 15 characters).')),
134  $theI->getErrors());
135  $this->assertTrue ($theI->j->validate());
136  $this->assertEquals(array(), $theI->j->getErrors());
137  $theI->name = 'The First I!';
138  $theI->j->name = 'Tooooooo looooooong';
139  $this->assertFalse($theI->validate());
140  $this->assertEquals(
141  array('j' => array('name' => array('Name is too long (maximum is 15 characters).'))),
142  $theI->getErrors());
143  $this->assertFalse($theI->j->validate());
144  $this->assertEquals(
145  array('name' => array('Name is too long (maximum is 15 characters).')),
146  $theI->j->getErrors());
147  $theI->j->name = 'The J!';
148  $this->assertTrue($theI->validate());
149  $this->assertTrue($theI->j->validate());
150  }
151 
156  {
157  $theI = H::getByName('The First I!')->castDown(array('I'));
158  $this->assertTrue ($theI->save());
159 
160  $theI->name = 'Tooooooo looooooong';
161  $this->assertFalse($theI->save());
162  $this->assertTrue ($theI->save(false));
163  $theI->name = 'The First I!';
164  $this->assertTrue ($theI->save());
165 
166  $theI->j->name = 'Tooooooo looooooong';
167  $this->assertFalse($theI->save());
168  $this->assertTrue ($theI->save(false));
169  $theI->j->name = 'The J!';
170  $this->assertTrue ($theI->save());
171 
172  $theI->name = 'Tooooooo looooooong';
173  $this->assertTrue ($theI->j->save());
174  $this->assertTrue ($theI->j->save(false));
175  $theI->name = 'The First I!';
176  $this->assertTrue ($theI->j->save());
177  $this->assertTrue ($theI->j->save(false));
178  }
179 
184  {
185  $firstI = H::getByName('The First I!')->castDown(array('I'));
186  $this->assertEquals(self::MANY_COUNT, $firstI->ks->count());
187  for ($i = 0; $i < self::MANY_COUNT; $i++)
188  {
189  $this->assertEquals("K-$i", $firstI->ks[$i]->name);
190  $this->assertEquals("The First I!", $firstI->ks[$i]->i->name);
191  $this->assertTrue ($firstI->isSame($firstI->ks[$i]->i));
192  $this->assertTrue ($firstI === $firstI->ks[$i]->i);
193  }
194  }
195 
200  {
201  $theI = H::getByName('The First I!')->castDown(array('I'));
202  $this->assertEquals(array(), $theI->getErrors());
203  $this->assertEquals(array(), $theI->ks->getErrors());
204  }
205 
210  {
211  $theI = H::getByName('The First I!')->castDown(array('I'));
212  $this->assertTrue($theI->validate());
213  $this->assertEquals(array(), $theI->getErrors());
214  $this->assertTrue($theI->ks->validate());
215  $this->assertEquals(array(), $theI->ks->getErrors());
216  }
217 
222  {
223  $theI = H::getByName('The First I!')->castDown(array('I'));
224  $theI->name = 'Tooooooo looooooong';
225  $this->assertFalse($theI->validate());
226  $this->assertEquals(
227  array('name' => array('Name is too long (maximum is 15 characters).')),
228  $theI->getErrors());
229  $this->assertTrue ($theI->ks->validate());
230  $this->assertEquals(array(), $theI->ks->getErrors());
231  $theI->name = 'The First I!';
232  $theI->ks[0]->name = 'Tooooooo looooooong';
233  $this->assertFalse($theI->validate());
234  $this->assertEquals(
235  array('ks' => array(0 => array('name' => array('Name is too long (maximum is 15 characters).')))),
236  $theI->getErrors());
237  $this->assertFalse($theI->ks[0]->validate());
238  $this->assertEquals(
239  array(0 => array('name' => array('Name is too long (maximum is 15 characters).'))),
240  $theI->ks->getErrors());
241  $theI->ks[0]->name = 'K-0';
242  $this->assertTrue($theI->validate());
243  $this->assertTrue($theI->ks->validate());
244  }
245 
250  {
251  $theI = H::getByName('The First I!')->castDown(array('I'));
252  $this->assertTrue ($theI->save());
253 
254  $theI->name = 'Tooooooo looooooong';
255  $this->assertFalse($theI->save());
256  $this->assertTrue ($theI->save(false));
257  $theI->name = 'The First I!';
258  $this->assertTrue ($theI->save());
259 
260  $theI->ks[0]->name = 'Tooooooo looooooong';
261  $this->assertFalse($theI->save());
262  $this->assertTrue ($theI->save(false));
263  $theI->ks[0]->name = 'K-0';
264  $this->assertTrue ($theI->save());
265 
266  $this->assertTrue ($theI->ks->save());
267  }
268 
272  public function testManyToManyRelations()
273  {
274  $firstI = H::getByName('The First I!')->castDown(array('I'));
275  $this->assertEquals(self::MANY_COUNT, $firstI->ls->count());
276  for ($i = 0; $i < self::MANY_COUNT; $i++)
277  {
278  $this->assertEquals("L-$i", $firstI->ls[$i]->name);
279  }
280  for ($i = 1; $i < self::MANY_COUNT; $i++)
281  {
282  $this->assertEquals(1, $firstI->ls[$i]->is->count());
283  }
284  $this->assertEquals(2, $firstI->ls[0]->is->count());
285 
286  $secondI = H::getByName('The Second I!')->castDown(array('I'));
287  $this->assertEquals(1, $secondI->ls->count());
288  $this->assertEquals("L-0", $secondI->ls[0]->name);
289  $this->assertEquals(2, $secondI->ls[0]->is->count());
290  $this->assertEquals("The First I!", $secondI->ls[0]->is[0]->name);
291  $this->assertEquals("The Second I!", $secondI->ls[0]->is[1]->name);
292  $this->assertTrue ($firstI ->isSame($secondI->ls[0]->is[0]));
293  $this->assertTrue ($secondI->isSame($secondI->ls[0]->is[1]));
294  $this->assertTrue ($firstI === $secondI->ls[0]->is[0]);
295  $this->assertTrue ($secondI === $secondI->ls[0]->is[1]);
296  }
297 
302  {
303  $theI = H::getByName('The First I!')->castDown(array('I'));
304  $this->assertEquals(array(), $theI->getErrors());
305  $this->assertEquals(array(), $theI->ls->getErrors());
306  }
307 
312  {
313  $theI = H::getByName('The First I!')->castDown(array('I'));
314  $this->assertTrue($theI->validate());
315  $this->assertEquals(array(), $theI->getErrors());
316  $this->assertTrue($theI->ls->validate());
317  $this->assertEquals(array(), $theI->ks->getErrors());
318  }
319 
324  {
325  $theI = H::getByName('The First I!')->castDown(array('I'));
326  $theI->name = 'Tooooooo looooooong';
327  $this->assertFalse($theI->validate());
328  $this->assertEquals(
329  array('name' => array('Name is too long (maximum is 15 characters).')),
330  $theI->getErrors());
331  $this->assertTrue ($theI->ls->validate());
332  $this->assertEquals(array(), $theI->ls->getErrors());
333  $theI->name = 'The First I!';
334  $theI->ls[0]->name = 'Tooooooo looooooong';
335  $this->assertTrue ($theI->validate()); // Validate does not follow M:N relations.
336  $this->assertTrue ($theI->ls->validate()); // Because the many many collection doesn't validate.
337  $this->assertEquals(array(), $theI->getErrors());
338  $this->assertFalse($theI->ls[0]->validate());
339  $this->assertEquals(
340  array(0 => array('name' => array('Name is too long (maximum is 15 characters).'))),
341  $theI->ls->getErrors());
342  $theI->ls[0]->name = 'L-0';
343  $this->assertTrue ($theI->validate());
344  $this->assertTrue ($theI->ls->validate());
345  $this->assertTrue ($theI->ls[0]->validate());
346  }
347 
352  {
353  $theI = H::getByName('The First I!')->castDown(array('I'));
354  $this->assertTrue ($theI->save());
355 
356  $theI->name = 'Tooooooo looooooong';
357  $this->assertFalse($theI->save());
358  $this->assertTrue ($theI->save(false));
359  $theI->name = 'The First I!';
360  $this->assertTrue ($theI->save());
361 
362  $theI->ls[0]->name = 'Tooooooo looooooong';
363  $this->assertTrue ($theI->save()); // Validate does not follow M:N relations.
364 
365  $theI->ls[0]->forget();
366  H::getByName('L-0'); // It is still there in the database unsaved.
367 
368  $this->assertFalse($theI->ls[0]->save());
369  $this->assertTrue ($theI->ls[0]->save(false));
370 
371  $theI->ls[0]->name = 'L-0';
372  $this->assertTrue ($theI->ls[0]->save());
373  $this->assertTrue ($theI->ls[0]->save(false));
374 
375  $this->assertTrue ($theI->ls->save());
376  }
377 
383  {
384  $firstIAsH = H::getByName('The First I!');
385  $firstI = $firstIAsH->castDown(array('I'));
386  $this->assertEquals(self::MANY_COUNT, $firstI->ks->count());
387  $this->assertEquals(self::MANY_COUNT, $firstI->ls->count());
388 
389  $firstIAsH->forget();
390  $firstI ->forget();
391  unset($firstIAsH);
392  unset($firstI);
393 
394  $firstIAsH = H::getByName('The First I!');
395  $firstI = $firstIAsH->castDown(array('I'));
396  $this->assertEquals(self::MANY_COUNT, $firstI->ks->count());
397  $this->assertEquals(self::MANY_COUNT, $firstI->ls->count());
398  }
399 
403  public function testMemberMemberOfRelation()
404  {
405  $i1 = new I();
406  $i2 = new I();
407  $i3 = new I();
408  $i1->is->add($i2);
409  $i1->is->add($i3);
410  $this->assertTrue($i1->save());
411  $this->assertTrue($i2->id > 0);
412  $this->assertTrue($i3->id > 0);
413  $this->assertTrue($i2->isSame($i1->is[0]));
414  $this->assertTrue($i3->isSame($i1->is[1]));
415  $i1Id = $i1->id;
416  $i2Id = $i2->id;
417  $i3Id = $i3->id;
418  $i1->forget();
419  $i2->forget();
420  $i3->forget();
421  $i1 = I::getById($i1Id);
422  $i2 = I::getById($i2Id);
423  $i3 = I::getById($i3Id);
424  $this->assertTrue($i1->isSame($i2->i));
425  $this->assertTrue($i1->isSame($i3->i));
426 
427  $this->assertEquals(0, $i3->is->count());
428  $i4 = new I();
429  $i4->i = $i3;
430  $this->assertTrue($i4->save());
431  $i3Id = $i3->id;
432  $i3->forget();
433  $i3 = I::getById($i3Id);
434  $this->assertEquals(1, $i3->is->count());
435  }
436 
437  public function testChangingBelongsToSideOfHasManyRelation()
438  {
439  $k1 = new K();
440  $k2 = new K();
441 
442  $i = new I();
443  $i->ks->add($k1);
444  $i->ks->add($k2);
445  $this->assertTrue($i->save());
446  $this->assertEquals(2, $i->ks->count());
447 
448  $k1->i = null;
449  $this->assertTrue($k1->save());
450 
451  $iId = $i->id;
452  $i->forget();
453  unset($i);
454 
455  $i = I::getById($iId);
456  $this->assertEquals(1, $i->ks->count());
457 
458  $i->ks->removeByIndex(0);
459  $this->assertTrue($i->save());
460  $this->assertEquals(0, $i->ks->count());
461 
462  $k2Id = $k2->id;
463  $k2->forget();
464  unset($k2);
465 
466  $k2 = K::getById($k2Id);
467  $this->assertTrue($k2->i->id < 0);
468  }
469  }
470 ?>
Definition: I.php:37
Definition: K.php:37
Definition: J.php:37
Definition: L.php:37
static getById($id, $modelClassName=null)
Generated on Wed Jul 8 2020 07:10:30
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.