Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
RedBeanModelTest.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  Key: <- = is-a, | = has-a
42 
43  RedBeanModel <- F
44  |
45  RedBeanModel <- A <- B <- C <- D
46  ^ |
47  | |
48  Y |
49  |
50  RedBeanModel <- E
51  |
52  RedBeanModel <- G
53 
54  RedBeanModel <- Z
55 
56  How RedBeanModel works - the derived classes supply it with the meta data that
57  it needs to correctly represent each class in its own table and link
58  them to related classes, on demand loading the related classes.
59  */
60 
61  class RedBeanModelTest extends BaseTest
62  {
63  public static function getDependentTestModelClassNames()
64  {
65  return array('A',
66  'B',
67  'C',
68  'D',
69  'E',
70  'F',
71  'G',
72  'H',
73  'I',
74  'J',
75  'M',
76  'TestAttributeNamedLikeSqlKeywordModel',
77  'TestBlobModel',
78  'TestBooleanAttributeModel',
79  'TestDateTimeModel',
80  'TestDefaultedAttributeModel',
81  'TestIdentityModel',
82  'TestNameModel',
83  'TestSelfRelatingModel',
84  'TestSimplestModel',
85  'Y',
86  'X',
87  'P'
88  );
89  }
90 
91  public static function tearDownAfterClass()
92  {
93  parent::tearDownAfterClass();
94  }
95 
96  public function testA()
97  {
98  $a = new A();
99  $a->a = 1;
100  $a->uniqueRequiredEmail = 'a@zurmoinc.com';
101  $this->assertTrue($a->save());
102  $id = $a->id;
103  unset($a);
104  $a = A::getById($id);
105  $this->assertEquals(1, $a->a);
106  $this->assertEquals('a@zurmoinc.com', $a->uniqueRequiredEmail);
107  }
108 
113  {
114  $a = new A();
115  $a->a = 'a';
116  $a->uniqueRequiredEmail = 'whatever@zurmoinc.com';
117  $this->assertFalse($a->validate());
118  $this->assertEquals(
119  array('a' =>
120  array(
121  'A must be either 1 or 0.'
122  )
123  ),
124  $a->getErrors()
125  );
126  $this->assertFalse($a->save());
127  }
128 
133  {
134  // Dumb thing to do, but yii allows saving without calling
135  // validate so we're just testing that it is correctly doing
136  // exactly what we're asking it to do. In this example the
137  // column will automatically be changed to a string column
138  // by RedBean.
139  $a = new A();
140  $a->a = 'a';
141  $a->uniqueRequiredEmail = 'whatever@zurmoinc.com';
142  $this->assertTrue($a->save(false));
143  $a->delete();
144  }
145 
149  public function testGetAll()
150  {
151  $data = array(
152  array(1, 'a1@zurmoinc.com'),
153  array(0, 'a2@zurmoinc.com'),
154  array(0, 'a3@zurmoinc.com'),
155  array(1, 'a4@zurmoinc.com'),
156  );
157  foreach ($data as $aAndZ)
158  {
159  $a = new A();
160  $a->a = $aAndZ[0];
161  $a->uniqueRequiredEmail = $aAndZ[1];
162  $a->validate();
163  $this->assertTrue($a->save());
164  }
165  $allAs = A::getAll();
166  $this->assertEquals(5, count($allAs));
167  }
168 
172  public function testGetRange()
173  {
174  $allAs = A::getSubset(null, 0, 1);
175  $this->assertEquals(1, count($allAs));
176  $allAs = A::getSubset(null, 0, 2);
177  $this->assertEquals(2, count($allAs));
178  $allAs = A::getSubset(null, 2, 3);
179  $this->assertEquals(3, count($allAs));
180  }
181 
185  public function testGetAllWithSorting()
186  {
187  $allAs = A::getAll('a');
188  $this->assertEquals(5, count($allAs));
189  $this->assertEquals(0, $allAs[0]->a);
190  $this->assertEquals(0, $allAs[1]->a);
191  $this->assertEquals(1, $allAs[2]->a);
192  $this->assertEquals(1, $allAs[3]->a);
193  $this->assertEquals(1, $allAs[4]->a);
194 
195  $allAs = A::getAll('a', true);
196  $this->assertEquals(5, count($allAs));
197  $this->assertEquals(1, $allAs[0]->a);
198  $this->assertEquals(1, $allAs[1]->a);
199  $this->assertEquals(1, $allAs[2]->a);
200  $this->assertEquals(0, $allAs[3]->a);
201  $this->assertEquals(0, $allAs[4]->a);
202 
203  $allAs = A::getAll('uniqueRequiredEmail');
204  $this->assertEquals(5, count($allAs));
205  if (in_array(RedBeanDatabase::getDatabaseType(), array('sqlite', 'pgsql')))
206  {
207  $this->assertEquals(1, $allAs[0]->a);
208  $this->assertEquals('a1@zurmoinc.com', $allAs[0]->uniqueRequiredEmail);
209  $this->assertEquals(0, $allAs[1]->a);
210  $this->assertEquals('a2@zurmoinc.com', $allAs[1]->uniqueRequiredEmail);
211  $this->assertEquals(0, $allAs[2]->a);
212  $this->assertEquals('a3@zurmoinc.com', $allAs[2]->uniqueRequiredEmail);
213  $this->assertEquals(1, $allAs[3]->a);
214  $this->assertEquals('a4@zurmoinc.com', $allAs[3]->uniqueRequiredEmail);
215  $this->assertEquals(1, $allAs[4]->a);
216  $this->assertEquals('a@zurmoinc.com', $allAs[4]->uniqueRequiredEmail);
217  }
218  else
219  {
220  $this->assertEquals(1, $allAs[0]->a);
221  $this->assertEquals('a@zurmoinc.com', $allAs[0]->uniqueRequiredEmail);
222  $this->assertEquals(1, $allAs[1]->a);
223  $this->assertEquals('a1@zurmoinc.com', $allAs[1]->uniqueRequiredEmail);
224  $this->assertEquals(0, $allAs[2]->a);
225  $this->assertEquals('a2@zurmoinc.com', $allAs[2]->uniqueRequiredEmail);
226  $this->assertEquals(0, $allAs[3]->a);
227  $this->assertEquals('a3@zurmoinc.com', $allAs[3]->uniqueRequiredEmail);
228  $this->assertEquals(1, $allAs[4]->a);
229  $this->assertEquals('a4@zurmoinc.com', $allAs[4]->uniqueRequiredEmail);
230  }
231 
232  $allAs = A::getAll('uniqueRequiredEmail', true);
233  $this->assertEquals(5, count($allAs));
234  if (in_array(RedBeanDatabase::getDatabaseType(), array('sqlite', 'pgsql')))
235  {
236  $this->assertEquals(1, $allAs[4]->a);
237  $this->assertEquals('a1@zurmoinc.com', $allAs[4]->uniqueRequiredEmail);
238  $this->assertEquals(0, $allAs[3]->a);
239  $this->assertEquals('a2@zurmoinc.com', $allAs[3]->uniqueRequiredEmail);
240  $this->assertEquals(0, $allAs[2]->a);
241  $this->assertEquals('a3@zurmoinc.com', $allAs[2]->uniqueRequiredEmail);
242  $this->assertEquals(1, $allAs[1]->a);
243  $this->assertEquals('a4@zurmoinc.com', $allAs[1]->uniqueRequiredEmail);
244  $this->assertEquals(1, $allAs[0]->a);
245  $this->assertEquals('a@zurmoinc.com', $allAs[0]->uniqueRequiredEmail);
246  }
247  else
248  {
249  $this->assertEquals(1, $allAs[4]->a);
250  $this->assertEquals('a@zurmoinc.com', $allAs[4]->uniqueRequiredEmail);
251  $this->assertEquals(1, $allAs[3]->a);
252  $this->assertEquals('a1@zurmoinc.com', $allAs[3]->uniqueRequiredEmail);
253  $this->assertEquals(0, $allAs[2]->a);
254  $this->assertEquals('a2@zurmoinc.com', $allAs[2]->uniqueRequiredEmail);
255  $this->assertEquals(0, $allAs[1]->a);
256  $this->assertEquals('a3@zurmoinc.com', $allAs[1]->uniqueRequiredEmail);
257  $this->assertEquals(1, $allAs[0]->a);
258  $this->assertEquals('a4@zurmoinc.com', $allAs[0]->uniqueRequiredEmail);
259  }
260  }
261 
265  public function testGetRangeWithSorting()
266  {
267  $allAs = A::getSubset(null, 0, 2, null, 'uniqueRequiredEmail');
268  $this->assertEquals(2, count($allAs));
269  if (in_array(RedBeanDatabase::getDatabaseType(), array('sqlite', 'pgsql')))
270  {
271  $this->assertEquals(1, $allAs[0]->a);
272  $this->assertEquals('a1@zurmoinc.com', $allAs[0]->uniqueRequiredEmail);
273  $this->assertEquals(0, $allAs[1]->a);
274  $this->assertEquals('a2@zurmoinc.com', $allAs[1]->uniqueRequiredEmail);
275  }
276  else
277  {
278  $this->assertEquals(1, $allAs[0]->a);
279  $this->assertEquals('a@zurmoinc.com', $allAs[0]->uniqueRequiredEmail);
280  $this->assertEquals(1, $allAs[1]->a);
281  $this->assertEquals('a1@zurmoinc.com', $allAs[1]->uniqueRequiredEmail);
282  }
283  $allAs = A::getSubset(null, 2, 3, null, 'uniqueRequiredEmail');
284  $this->assertEquals(3, count($allAs));
285  if (in_array(RedBeanDatabase::getDatabaseType(), array('sqlite', 'pgsql')))
286  {
287  $this->assertEquals(0, $allAs[0]->a);
288  $this->assertEquals('a3@zurmoinc.com', $allAs[0]->uniqueRequiredEmail);
289  $this->assertEquals(1, $allAs[1]->a);
290  $this->assertEquals('a4@zurmoinc.com', $allAs[1]->uniqueRequiredEmail);
291  $this->assertEquals(1, $allAs[2]->a);
292  $this->assertEquals('a@zurmoinc.com', $allAs[2]->uniqueRequiredEmail);
293  }
294  else
295  {
296  $this->assertEquals(0, $allAs[0]->a);
297  $this->assertEquals('a2@zurmoinc.com', $allAs[0]->uniqueRequiredEmail);
298  $this->assertEquals(0, $allAs[1]->a);
299  $this->assertEquals('a3@zurmoinc.com', $allAs[1]->uniqueRequiredEmail);
300  $this->assertEquals(1, $allAs[2]->a);
301  $this->assertEquals('a4@zurmoinc.com', $allAs[2]->uniqueRequiredEmail);
302  }
303 
304  $allAs = A::getSubset(null, 4, 1, null, 'uniqueRequiredEmail');
305  $this->assertEquals(1, count($allAs));
306  if (in_array(RedBeanDatabase::getDatabaseType(), array('sqlite', 'pgsql')))
307  {
308  $this->assertEquals(1, $allAs[0]->a);
309  $this->assertEquals('a@zurmoinc.com', $allAs[0]->uniqueRequiredEmail);
310  }
311  else
312  {
313  $this->assertEquals(1, $allAs[0]->a);
314  $this->assertEquals('a4@zurmoinc.com', $allAs[0]->uniqueRequiredEmail);
315  }
316 
317  $allAs = A::getSubset(null, 5, 1, null, 'uniqueRequiredEmail');
318  $this->assertEquals(0, count($allAs));
319 
320  $allAs = A::getSubset(null, 500, 123, null, 'uniqueRequiredEmail');
321  $this->assertEquals(0, count($allAs));
322  }
323 
327  public function testB()
328  {
329  $b = new B();
330  $b->a = 0;
331  $b->b = 'b';
332  $b->uniqueRequiredEmail = 'b@zurmoinc.com';
333  $this->assertTrue($b->validate());
334  $this->assertTrue($b->save());
335  $id = $b->id;
336  unset($b);
337  $b = B::getById($id);
338  $this->assertEquals(0, $b->a);
339  $this->assertEquals('b@zurmoinc.com', $b->uniqueRequiredEmail);
340  }
341 
345  public function testC()
346  {
347  $e = new E();
348  $e->e = 'theDefaultE';
349  $this->assertTrue($e->save());
350 
351  $c = new C();
352  $c->a = 0;
353  $c->b = 'b';
354  $c->c = 'c';
355  $c->eRequired = $e;
356  $c->eUnique->e = strval(time());
357  $this->assertNotNull($c->e);
358  $this->assertNotNull($c->e->g);
359  $c->e->e = 'e';
360  $c->e->g->g = 'g';
361  $this->assertTrue($c->save());
362  $id = $c->id;
363  unset($c);
364  $c = C::getById($id);
365  $this->assertEquals(0, $c->a);
366  $this->assertEquals('b', $c->b);
367  $this->assertEquals('c', $c->c);
368  $this->assertEquals('e', $c->e->e);
369  $this->assertEquals('g', $c->e->g->g);
370  $attributeNames = $c->attributeNames();
371  $this->assertEquals(13, count($attributeNames));
372  $this->assertTrue(in_array('a', $attributeNames));
373  $this->assertTrue(in_array('b', $attributeNames));
374  $this->assertTrue(in_array('c', $attributeNames));
375  $this->assertTrue(in_array('e', $attributeNames));
376  $this->assertTrue(in_array('defaultedInt', $attributeNames));
377  $this->assertTrue(in_array('junk', $attributeNames));
378  $this->assertTrue(in_array('uniqueRequiredEmail', $attributeNames));
379  }
380 
384  public function testCWithoutExplicitEAndG()
385  {
386  $c = new C();
387  $c->a = 1;
388  $c->b = 'b';
389  $c->c = 'c';
390  $this->assertTrue($c->save(false));
391  $id = $c->id;
392  unset($c);
393  $c = C::getById($id);
394  $this->assertNotNull($c->e);
395  $this->assertNotNull($c->e->g);
396  $c->e->e = 'e';
397  $c->e->g->g = 'g';
398  $this->assertTrue($c->save(false));
399  unset($c);
400  $c = C::getById($id);
401  $this->assertEquals(1, $c->a);
402  $this->assertEquals('b', $c->b);
403  $this->assertEquals('c', $c->c);
404  $this->assertEquals('e', $c->e->e);
405  $this->assertEquals('g', $c->e->g->g);
406  }
407 
411  public function testD()
412  {
413  $d = new D();
414  $d->a = 1;
415  $d->b = 'b';
416  $d->c = 'c';
417  $d->d = 'd';
418  $d->eRequired->e = 'hello';
419  $this->assertNotNull($d->f);
420  $d->f->f = 'f';
421  $this->assertTrue($d->save());
422  $id = $d->id;
423  unset($d);
424  $d = D::getById($id);
425  $this->assertEquals(1, $d->a);
426  $this->assertEquals('b', $d->b);
427  $this->assertEquals('c', $d->c);
428  $this->assertEquals('d', $d->d);
429  $this->assertEquals('f', $d->f->f);
430  }
431 
432  public function testGetAttributeLabel()
433  {
434  $a = new TestGetAttributeLabelModel();
435  $this->assertEquals('This Is A Member Of Model', $a->getAttributeLabel('thisIsAMemberOfModel'));
436  $this->assertEquals('And Another One', $a->getAttributeLabel('andAnotherOne'));
437  $this->assertEquals('Ooh A Boo Ba Doo', $a->getAttributeLabel('oohABooBaDoo'));
438  $this->assertEquals('Ooh A Boo Ba Doo D', $a->getAttributeLabel('oohABooBaDooD'));
439  }
440 
445  {
446  $a = new A();
447  $a->a = 1;
448  $this->assertTrue ($a->isAttributeRequired('a'));
449  $this->assertFalse($a->isAttributeRequired('junk'));
450  $this->assertFalse($a->isAttributeRequired('uniqueRequiredEmail'));
451  $validators = $a->getValidators('a');
452  $this->assertEquals(3, count($validators));
453  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelRequiredValidator', $validators));
454  $this->assertTrue(TestHelpers::isClassInArray('CBooleanValidator', $validators));
455  $validators = $a->getValidators('junk');
456  $this->assertEquals(2, count($validators));
457  $validators = $a->getValidators('uniqueRequiredEmail');
458  $this->assertEquals(2, count($validators));
459  $this->assertTrue(TestHelpers::isClassInArray('CEmailValidator', $validators));
460  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelUniqueValidator', $validators));
461  $validators = $a->getValidators();
462  $this->assertEquals(8, count($validators));
463  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelRequiredValidator', $validators));
464  $this->assertTrue(TestHelpers::isClassInArray('CBooleanValidator', $validators));
465  $this->assertTrue(TestHelpers::isClassInArray('CEmailValidator', $validators));
466  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelUniqueValidator', $validators));
467  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelTypeValidator', $validators));
468  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelDefaultValueValidator', $validators));
469  $validators = $a->getValidatorList();
470  $this->assertEquals(9, count($validators));
471  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelRequiredValidator', $validators));
472  $this->assertTrue(TestHelpers::isClassInArray('CBooleanValidator', $validators));
473  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelDefaultValueValidator', $validators));
474  $this->assertTrue(TestHelpers::isClassInArray('CEmailValidator', $validators));
475  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelUniqueValidator', $validators));
476  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelTypeValidator', $validators));
477  }
478 
479  /*
480  * @depends testA
481  */
482  public function testGetValidatorsAndGetValidatorListWithScenarios()
483  {
484  $a = new A();
485  $validators = $a->getValidators();
486  $this->assertEquals(8, count($validators));
487  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelRequiredValidator', $validators));
488  $this->assertTrue(TestHelpers::isClassInArray('CBooleanValidator', $validators));
489  $this->assertTrue(TestHelpers::isClassInArray('CEmailValidator', $validators));
490  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelUniqueValidator', $validators));
491  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelTypeValidator', $validators));
492  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelDefaultValueValidator', $validators));
493  $validators = $a->getValidatorList();
494  $this->assertEquals(9, count($validators));
495  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelRequiredValidator', $validators));
496  $this->assertTrue(TestHelpers::isClassInArray('CBooleanValidator', $validators));
497  $this->assertTrue(TestHelpers::isClassInArray('CEmailValidator', $validators));
498  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelUniqueValidator', $validators));
499  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelTypeValidator', $validators));
500  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelDefaultValueValidator', $validators));
501 
502  $validators = $a->getValidators('a');
503  $this->assertEquals(3, count($validators));
504  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelRequiredValidator', $validators));
505  $this->assertTrue(TestHelpers::isClassInArray('CBooleanValidator', $validators));
506 
507  $validators = $a->getValidators('junk');
508  $this->assertEquals(2, count($validators));
509 
510  $validators = $a->getValidators('uniqueRequiredEmail');
511  $this->assertEquals(2, count($validators));
512  $this->assertTrue(TestHelpers::isClassInArray('CEmailValidator', $validators));
513  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelUniqueValidator', $validators));
514 
515  $a->setScenario('Tuesday');
516  $this->assertEquals('Tuesday', $a->getScenario());
517  $validators = $a->getValidators();
518  $this->assertEquals(9, count($validators));
519  $validators = $a->getValidatorList();
520  $this->assertEquals(9, count($validators));
521 
522  $validators = $a->getValidators('a');
523  $this->assertEquals(3, count($validators));
524  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelRequiredValidator', $validators));
525  $this->assertTrue(TestHelpers::isClassInArray('CBooleanValidator', $validators));
526 
527  $validators = $a->getValidators('junk');
528  $this->assertEquals(2, count($validators));
529 
530  $validators = $a->getValidators('uniqueRequiredEmail');
531  $this->assertEquals(3, count($validators));
532  $this->assertTrue(TestHelpers::isClassInArray('CEmailValidator', $validators));
533  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelRequiredValidator', $validators));
534  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelUniqueValidator', $validators));
535 
536  $a->setScenario('Monday');
537  $this->assertEquals('Monday', $a->getScenario());
538  $validators = $a->getValidators();
539  $this->assertEquals(8, count($validators));
540  $validators = $a->getValidatorList();
541  $this->assertEquals(9, count($validators));
542 
543  $validators = $a->getValidators('a');
544  $this->assertEquals(3, count($validators));
545  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelRequiredValidator', $validators));
546  $this->assertTrue(TestHelpers::isClassInArray('CBooleanValidator', $validators));
547 
548  $validators = $a->getValidators('junk');
549  $this->assertEquals(2, count($validators));
550 
551  $validators = $a->getValidators('uniqueRequiredEmail');
552  $this->assertEquals(2, count($validators));
553  $this->assertTrue(TestHelpers::isClassInArray('CEmailValidator', $validators));
554  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelUniqueValidator', $validators));
555 
556  $a->setScenario('');
557  $this->assertEquals('', $a->getScenario());
558  $validators = $a->getValidators();
559  $this->assertEquals(8, count($validators));
560  $validators = $a->getValidatorList();
561  $this->assertEquals(9, count($validators));
562 
563  $validators = $a->getValidators('a');
564  $this->assertEquals(3, count($validators));
565  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelRequiredValidator', $validators));
566  $this->assertTrue(TestHelpers::isClassInArray('CBooleanValidator', $validators));
567 
568  $validators = $a->getValidators('junk');
569  $this->assertEquals(2, count($validators));
570 
571  $validators = $a->getValidators('uniqueRequiredEmail');
572  $this->assertEquals(2, count($validators));
573  $this->assertTrue(TestHelpers::isClassInArray('CEmailValidator', $validators));
574  $this->assertTrue(TestHelpers::isClassInArray('RedBeanModelUniqueValidator', $validators));
575  }
576 
581  {
582  $a = new A();
583  $a->a = 1;
584  $this->assertFalse($a->hasErrors());
585  $this->assertFalse($a->hasErrors('a'));
586 
587  $a->addError('a', 'A is jammed.');
588  $this->assertTrue($a->hasErrors());
589  $this->assertTrue($a->hasErrors('a'));
590  $errors = $a->getErrors('a');
591  $this->assertEquals(1, count($errors));
592  $this->assertEquals('A is jammed.', $errors[0]);
593  $errors = $a->getErrors();
594  $this->assertEquals(1, count($errors));
595  $this->assertTrue(isset($errors['a']));
596  $this->assertEquals(1, count($errors['a']));
597  $this->assertEquals('A is jammed.', $errors['a'][0]);
598 
599  $a->a = 1;
600  $this->assertTrue($a->validate());
601  $this->assertFalse($a->hasErrors());
602  $this->assertFalse($a->hasErrors('a'));
603  $errors = $a->getErrors('a');
604  $this->assertEquals(0, count($errors));
605 
606  $a->addError('uniqueRequiredEmail', 'Unique Required Email is gash.');
607  $this->assertTrue($a->hasErrors());
608  $this->assertFalse($a->hasErrors('a'));
609  $this->assertTrue($a->hasErrors('uniqueRequiredEmail'));
610  $errors = $a->getErrors('a');
611  $this->assertEquals(0, count($errors));
612  $errors = $a->getErrors('uniqueRequiredEmail');
613  $this->assertEquals(1, count($errors));
614  $this->assertEquals('Unique Required Email is gash.', $errors[0]);
615  $errors = $a->getErrors();
616  $this->assertEquals(1, count($errors));
617  $this->assertTrue(isset($errors['uniqueRequiredEmail']));
618  $this->assertEquals(1, count($errors['uniqueRequiredEmail']));
619  $this->assertEquals('Unique Required Email is gash.', $errors['uniqueRequiredEmail'][0]);
620 
621  $this->assertTrue($a->hasErrors());
622  $this->assertFalse($a->hasErrors('a'));
623  $this->assertTrue($a->hasErrors('uniqueRequiredEmail'));
624  $a->clearErrors();
625  $this->assertFalse($a->hasErrors());
626  $this->assertFalse($a->hasErrors('a'));
627  $this->assertFalse($a->hasErrors('uniqueRequiredEmail'));
628 
629  $a->uniqueRequiredEmail = 'jason@zurmoinc.com';
630  $this->assertTrue($a->validate());
631  $this->assertFalse($a->hasErrors());
632 
633  $this->assertTrue($a->save());
634  $id = $a->id;
635  unset($a);
636  $a = A::getById($id);
637  $this->assertEquals(1, $a->a);
638  $this->assertEquals('jason@zurmoinc.com', $a->uniqueRequiredEmail);
639  }
640 
644  public function testValidateAndGetErrors()
645  {
646  $a = new A();
647  $this->assertFalse($a->hasErrors());
648 
649  $a->a = null;
650  $this->assertFalse($a->validate());
651  $this->assertTrue($a->hasErrors());
652  $errors = $a->getErrors('a');
653  $this->assertEquals(1, count($errors));
654  $this->assertEquals('A cannot be blank.', $errors[0]);
655  $errors = $a->getErrors();
656  $this->assertEquals(1, count($errors));
657  $this->assertEquals(1, count($errors['a']));
658  $this->assertEquals('A cannot be blank.', $errors['a'][0]);
659 
660  $a->a = 'hello';
661  $this->assertFalse($a->validate());
662  $this->assertTrue($a->hasErrors());
663  $errors = $a->getErrors('a');
664  $this->assertEquals(1, count($errors));
665  $this->assertEquals('A must be either 1 or 0.', $errors[0]);
666  $errors = $a->getErrors();
667  $this->assertEquals(1, count($errors));
668  $this->assertEquals(1, count($errors['a']));
669  $this->assertEquals('A must be either 1 or 0.', $errors['a'][0]);
670 
671  $a->a = 1;
672  $this->assertTrue($a->validate());
673  $this->assertFalse($a->hasErrors());
674  $errors = $a->getErrors('a');
675  $this->assertEquals(0, count($errors));
676  $errors = $a->getErrors();
677  $this->assertEquals(0, count($errors));
678 
679  $a->uniqueRequiredEmail = 'something';
680  $this->assertFalse($a->validate());
681  $this->assertTrue($a->hasErrors());
682  $errors = $a->getErrors('a');
683  $this->assertEquals(0, count($errors));
684  $errors = $a->getErrors('uniqueRequiredEmail');
685  $this->assertEquals(1, count($errors));
686  $this->assertEquals('Unique Required Email is not a valid email address.', $errors[0]);
687  $errors = $a->getErrors();
688  $this->assertEquals(1, count($errors));
689  $this->assertFalse(isset($errors['a']));
690  $this->assertEquals(1, count($errors['uniqueRequiredEmail']));
691  $this->assertEquals('Unique Required Email is not a valid email address.', $errors['uniqueRequiredEmail'][0]);
692 
693  $a->uniqueRequiredEmail = 'nev@zurmoinc.com';
694  $this->assertTrue($a->validate());
695  $this->assertFalse($a->hasErrors());
696  $this->assertFalse($a->hasErrors('a'));
697  $this->assertFalse($a->hasErrors('uniqueRequiredEmail'));
698  $errors = $a->getErrors();
699  $this->assertEquals(0, count($errors));
700  $errors = $a->getErrors('a');
701  $this->assertEquals(0, count($errors));
702  $errors = $a->getErrors('uniqueRequiredEmail');
703  $this->assertEquals(0, count($errors));
704  }
705 
710  {
711  $c = new C();
712 
713  $c->a = null;
714  $this->assertFalse ($c->validate());
715  $this->assertTrue ($c->hasErrors());
716  $errors = $c->getErrors();
717  $this->assertEquals(2, count($errors));
718  $this->assertEquals(1, count($errors['a']));
719  $this->assertEquals('A cannot be blank.', $errors['a'][0]);
720  $this->assertEquals(1, count($errors['eRequired']));
721  $this->assertEquals('E Required cannot be blank.', $errors['eRequired'][0]);
722 
723  $c->a = 1;
724  $c->eRequired->e = 'hello';
725  $this->assertTrue ($c->validate());
726 
727  $c->a = 2;
728  $c->eRequired = null;
729  $this->assertFalse($c->validate());
730  $this->assertTrue ($c->hasErrors());
731  $errors = $c->getErrors();
732  $this->assertEquals(2, count($errors));
733  $this->assertEquals(1, count($errors['a']));
734  $this->assertEquals('A must be either 1 or 0.', $errors['a'][0]);
735  $this->assertEquals(1, count($errors['eRequired']));
736  $this->assertEquals('E Required cannot be blank.', $errors['eRequired'][0]);
737  $errors = $c->getErrors('a');
738  $this->assertEquals(1, count($errors));
739  $this->assertEquals(1, count($errors[0]));
740  $this->assertEquals('A must be either 1 or 0.', $errors[0]);
741 
742  $c->a = 1;
743  $c->eRequired = new E();
744  $c->eRequired->e = 'hello';
745  $this->assertTrue($c->validate());
746 
747  $c->e->e = 'thisistoolongbecauseehasalengthrule';
748  $this->assertFalse ($c->validate());
749  $this->assertTrue ($c->hasErrors());
750  $errors = $c->getErrors();
751  $this->assertEquals(1, count($errors));
752  $this->assertEquals(1, count($errors['e'])); // $c->e
753  $this->assertEquals(1, count($errors['e']['e'])); // $c->e->e
754  $this->assertEquals('E is too long (maximum is 16 characters).', $errors['e']['e'][0]);
755  $errors = $c->getErrors('e');
756  $this->assertEquals(1, count($errors));
757  $this->assertEquals(1, count($errors['e'])); // $c->e
758  $this->assertEquals('E is too long (maximum is 16 characters).', $errors['e'][0]);
759  $errors = $c->getErrors(array('e', 'e')); // $c->e->e
760  $this->assertEquals(1, count($errors));
761  $this->assertEquals('E is too long (maximum is 16 characters).', $errors[0]);
762  }
763 
768  {
769  $c = new C();
770  $c->a = 1;
771  $c->eRequired->e = 'hello';
772  $this->assertTrue($c->validate());
773 
774  $c->eMany->add(new E());
775  $c->eMany->add(new E());
776  $c->eMany->add(new E());
777  $e4 = new E();
778  $e5 = new E();
779  $c->eMany->add($e4);
780  $c->eMany->add($e5);
781  $this->assertEquals(5, $c->eMany->count());
782  $this->assertTrue($c->validate());
783 
784  $e4->e = 'this is too looooooong';
785  $e5->e = 'this is too looooooong as well';
786  $this->assertFalse($c->validate());
787  $errors = $c->getErrors();
788  $this->assertEquals(1, count($errors));
789  $this->assertEquals(2, count($errors['eMany']));
790  $this->assertEquals('E is too long (maximum is 16 characters).', $errors['eMany'][3]['e'][0]);
791  $this->assertEquals('E is too long (maximum is 16 characters).', $errors['eMany'][4]['e'][0]);
792  }
793 
794  public function testBulkSetAndGet()
795  {
796  $_FAKEPOST = array(
797  'a' => 1,
798  'junk' => 'something',
799  'uniqueRequiredEmail' => 'c1@zurmoinc.com',
800  'eRequired' => array('e' => 'hello'),
801  );
802 
803  $c1 = new C();
804  $c1->setAttributes($_FAKEPOST);
805  $this->assertEquals(1, $c1->a);
806  $this->assertEquals('something', $c1->junk);
807  $this->assertEquals('c1@zurmoinc.com', $c1->uniqueRequiredEmail);
808  $this->assertTrue($c1->validate());
809  $this->assertTrue($c1->save());
810  $idC1 = $c1->id;
811  unset($c1);
812 
813  $_FAKEPOST2 = array(
814  'a' => 1,
815  'junk' => 'something',
816  'uniqueRequiredEmail' => 'c2@zurmoinc.com',
817  'eRequired' => array('e' => 'hello'),
818  );
819 
820  $c2 = new C();
821  $c2->setAttributes($_FAKEPOST2, false);
822  $this->assertEquals(1, $c2->a);
823  $this->assertEquals('something', $c2->junk);
824  $this->assertEquals('c2@zurmoinc.com', $c2->uniqueRequiredEmail);
825  $this->assertTrue($c2->validate());
826  $this->assertTrue($c2->save());
827  $idC2 = $c2->id;
828  unset($c2);
829 
830  $_FAKEPOST = array(
831  'a' => 'garbage',
832  'junk' => 'something',
833  'uniqueRequiredEmail' => 'junk',
834  );
835 
836  $c3 = new C();
837  $c3->setAttributes($_FAKEPOST);
838  $this->assertEquals('garbage', $c3->a);
839  $this->assertEquals('something', $c3->junk);
840  $this->assertEquals('junk', $c3->uniqueRequiredEmail);
841  $this->assertFalse($c3->validate());
842  $this->assertFalse($c3->save());
843  unset($c3);
844 
845  $c1 = C::getById($idC1);
846  $this->assertEquals(1, $c1->a);
847  $this->assertEquals('something', $c1->junk);
848  $this->assertEquals('c1@zurmoinc.com', $c1->uniqueRequiredEmail);
849  $values = $c1->getAttributes();
850  $this->assertEquals(1, $values['a']);
851  $this->assertEquals('something', $values['junk']);
852  $this->assertEquals('c1@zurmoinc.com', $values['uniqueRequiredEmail']);
853 
854  $c2 = C::getById($idC2);
855  $this->assertEquals(1, $c2->a);
856  $this->assertEquals('something', $c2->junk);
857  $this->assertEquals('c2@zurmoinc.com', $c2->uniqueRequiredEmail);
858  $values = $c2->getAttributes();
859  $this->assertEquals(1, $values['a']);
860  $this->assertEquals('something', $values['junk']);
861  $this->assertEquals('c2@zurmoinc.com', $values['uniqueRequiredEmail']);
862  }
863 
868  {
869  $a1 = new A();
870  $a1->a = 1;
871  $a1->uniqueRequiredEmail = 'ross@zurmoinc.com';
872  $this->assertTrue($a1->save());
873  // Make sure it doesn't fail because it is
874  // the one that has the same uniqueRequiredEmail.
875  $a1->a = 0;
876  $this->assertTrue($a1->save());
877 
878  $a2 = new A();
879  $a2->a = 1;
880  $a2->uniqueRequiredEmail = 'webmaster@zurmoinc.com';
881  $this->assertTrue($a2->validate());
882 
883  $a2->uniqueRequiredEmail = 'ross@zurmoinc.com';
884  $this->assertFalse($a2->validate());
885  $this->assertTrue($a2->hasErrors());
886  $errors = $a2->getErrors('a');
887  $this->assertEquals(0, count($errors));
888  $errors = $a2->getErrors('uniqueRequiredEmail');
889  $this->assertEquals(1, count($errors));
890  $this->assertEquals('Unique Required Email "ross@zurmoinc.com" is already in use.', $errors[0]);
891  $errors = $a2->getErrors();
892  $this->assertEquals(1, count($errors));
893  $this->assertTrue(isset($errors['uniqueRequiredEmail']));
894  $this->assertEquals(1, count($errors['uniqueRequiredEmail']));
895  $this->assertEquals('Unique Required Email "ross@zurmoinc.com" is already in use.', $errors['uniqueRequiredEmail'][0]);
896  }
897 
902  {
903  // Note: RedBean finding that the attribute value is not unique will still
904  // have to be catered for because of concurrent uses of the system. Todo is
905  // to figure out what Yii does about it.
906 
907  $b1 = new B();
908  $b1->a = 1;
909  $b1->uniqueRequiredEmail = 'sales@zurmoinc.com';
910  $this->assertTrue($b1->save());
911 
912  $b2 = new B();
913  $b2->a = 1;
914  $b2->uniqueRequiredEmail = 'sales@zurmoinc.com';
915  $this->assertFalse($b2->validate()); // Fails validation...
916 
917  try
918  {
919  $b2->save(false); // ...saving it without validating.
920  $this->fail('Expected a RedBean_Exception_SQL.');
921  }
922  catch (RedBean_Exception_SQL $e)
923  {
924  if (RedBeanDatabase::getDatabaseType() == 'sqlite')
925  {
926  $this->assertEquals('SQLSTATE[23000]: Integrity constraint violation: 19 column uniquerequiredemail is not unique',
927  $e->getMessage());
928  }
929  elseif (RedBeanDatabase::getDatabaseType() == 'pgsql')
930  {
931  $this->assertEquals('SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "',
932  substr($e->getMessage(), 0, 93));
933  }
934  else
935  {
936  $this->assertEquals("SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'sales@zurmoinc.com' for key",
937  substr($e->getMessage(), 0, 98));
938  }
939  }
940  }
941 
946  {
947  $theDefaultE = E::getByE('theDefaultE');
948  $c1 = new C();
949  $c1->a = 1;
950  $this->assertEquals(69, $c1->defaultedInt);
951  $this->assertEquals($theDefaultE->id, $c1->eDefaulted1->id);
952  $this->assertEquals($theDefaultE->id, $c1->eDefaulted2->id);
953  $c1->validate();
954  }
955 
960  {
961  $model = new TestPrecisionModel();
962 
963  // Make sure it's inheriting the base validator.
964  $model->number = 2;
965  $this->assertFalse($model->validate());
966  $model->number = 6;
967  $this->assertFalse($model->validate());
968  $model->number = 4;
969  $this->assertTrue($model->validate());
970 
971  $model->numberPositive5Precision = 1.234567;
972  $model->validate();
973  $this->assertEquals(
974  array(
975  'numberPositive5Precision' => array(
976  'Number Positive 5 Precision is too precise (maximum decimal places is 5).'
977  ),
978  ),
979  $model->getErrors()
980  );
981 
982  $model->numberPositive5Precision = 1.23456;
983  $model->validate();
984  $this->assertEquals(array(), $model->getErrors());
985 
986  $model->numberNegative3Precision = 1234567;
987  $model->validate();
988  $this->assertEquals(
989  array(
990  'numberNegative3Precision' => array(
991  'Number Negative 3 Precision is too precise (maximum decimal places is -3).'
992  ),
993  ),
994  $model->getErrors()
995  );
996 
997  $model->numberNegative3Precision = 123000;
998  $model->validate();
999  $this->assertEquals(array(), $model->getErrors());
1000 
1001  $model->numberZeroPrecision = 1234567.123;
1002  $model->validate();
1003  $this->assertEquals(
1004  array(
1005  'numberZeroPrecision' => array(
1006  'Number Zero Precision is too precise (maximum decimal places is 0).'
1007  ),
1008  ),
1009  $model->getErrors()
1010  );
1011 
1012  $model->numberZeroPrecision = 1234567;
1013  $model->validate();
1014  $this->assertEquals(array(), $model->getErrors());
1015 
1016  $model->numberNullPrecision = 1.234567;
1017  $model->validate();
1018  $this->assertEquals(array(), $model->getErrors());
1019 
1020  $model->numberNullPrecision = 1.23456;
1021  $model->validate();
1022  $this->assertEquals(array(), $model->getErrors());
1023 
1024  $model->numberNullPrecision = 1234567;
1025  $model->validate();
1026  $this->assertEquals(array(), $model->getErrors());
1027 
1028  $model->numberNullPrecision = 123000;
1029  $model->validate();
1030  $this->assertEquals(array(), $model->getErrors());
1031 
1032  $model->numberNullPrecision = 1234567.123;
1033  $model->validate();
1034  $this->assertEquals(array(), $model->getErrors());
1035  }
1036 
1041  {
1042  $thing = new TestDefaultedAttributeModel();
1043  $this->assertEquals('no-reply@nowhere.com', $thing->email1);
1044  $this->assertEquals('no-reply@nowhere.com', $thing->email2);
1045  $thing->validate();
1046  $this->assertEquals('no-reply@nowhere.com', $thing->email2);
1047  $this->assertTrue($thing->save());
1048  $id = $thing->id;
1049  unset($thing);
1050 
1052  $this->assertEquals('no-reply@nowhere.com', $thing->email1);
1053  $this->assertEquals('no-reply@nowhere.com', $thing->email2);
1054 
1055  //Now test when default setting is false.
1056  $thing = new TestDefaultedAttributeModel(false);
1057  $this->assertEquals('no-reply@nowhere.com', $thing->email1);
1058  $this->assertEquals(null, $thing->email2);
1059  }
1060 
1065  {
1066  $thing = new TestDefaultedAttributeModel();
1067  $this->assertEquals('no-reply@nowhere.com', $thing->email1);
1068  $this->assertEquals('no-reply@nowhere.com', $thing->email2);
1069 
1070  $thing->email1 = 'a@zurmoinc.com';
1071  $thing->email2 = 'b@zurmoinc.com';
1072  $this->assertEquals('a@zurmoinc.com', $thing->email1);
1073  $this->assertEquals('b@zurmoinc.com', $thing->email2);
1074 
1075  $this->assertTrue($thing->save());
1076  $id = $thing->id;
1077  unset($thing);
1078 
1080  $this->assertEquals('a@zurmoinc.com', $thing->email1);
1081  $this->assertEquals('b@zurmoinc.com', $thing->email2);
1082  }
1083 
1088  {
1089  //Test that if you had a default value then removed it, that the
1090  //value remains empty.
1091  $thing = new TestDefaultedAttributeModel();
1092  $this->assertEquals('no-reply@nowhere.com', $thing->email2);
1093  $thing->email2 = null;
1094  $this->assertEquals(null, $thing->email2);
1095  $this->assertTrue($thing->save());
1096  //It should have saved.
1097  $this->assertEquals(null, $thing->email2);
1098  $id = $thing->id;
1099  unset($thing);
1101  $this->assertEquals(null, $thing->email2);
1102  }
1103 
1108  {
1110  $thing->column = 123;
1111  $this->assertTrue($thing->save());
1112  $id = $thing->id;
1113  unset($thing);
1115  $this->assertEquals(123, $thing->column);
1116  }
1117 
1121  public function testZeros()
1122  {
1123  $thing = new TestSimplestModel();
1124  $thing->member = 0;
1125  $this->assertTrue($thing->save());
1126  $id = $thing->id;
1127  unset($thing);
1128  $thing = TestSimplestModel::getById($id);
1129  $this->assertEquals(0, $thing->member);
1130  }
1131 
1135  public function testNull()
1136  {
1137  $thing = new TestSimplestModel();
1138  $thing->member = null;
1139  $this->assertTrue($thing->save());
1140  $id = $thing->id;
1141  unset($thing);
1142  $thing = TestSimplestModel::getById($id);
1143  $this->assertEquals(null, $thing->member);
1144  }
1145 
1149  public function testBools()
1150  {
1151  $thing = new TestBooleanAttributeModel();
1152  $thing->bool = 1;
1153  $this->assertTrue($thing->save());
1154  $id = $thing->id;
1155  unset($thing);
1156 
1157  $thing = TestBooleanAttributeModel::getById($id);
1158  $this->assertEquals (1, $thing->bool);
1159  $this->assertNotSame(true, $thing->bool);
1160 
1161  $thing->bool = 0;
1162  $this->assertTrue($thing->save());
1163  $id = $thing->id;
1164  unset($thing);
1165 
1166  $thing = TestBooleanAttributeModel::getById($id);
1167  $this->assertEquals (0, $thing->bool);
1168  $this->assertNotSame(false, $thing->bool);
1169 
1170  $thing->bool = 3;
1171  $this->assertFalse($thing->save());
1172  }
1173 
1177  public function testRequiredRelatedModel()
1178  {
1179  $c = new C();
1180  $c->a = 1;
1181  $c->eRequired->e = 'hello';
1182  $this->assertTrue($c->validate());
1183  $this->assertTrue($c->save());
1184  $c->eRequired = null;
1185  $this->assertFalse($c->validate());
1186  $this->assertFalse($c->save());
1187  // Make sure it doesn't fail because it is
1188  // the one that has the same related model.
1189  $c->a = 0;
1190  $this->assertFalse($c->save());
1191  }
1192 
1197  {
1198  $countBefore = intval(ZurmoRedBean::getCell("select count(*) from a;"));
1199  $account = new B();
1200  $countAfter = intval(ZurmoRedBean::getCell("select count(*) from a;"));
1201  $this->assertEquals($countBefore, $countAfter);
1202  }
1203 
1207  public function testIdIsReadOnly()
1208  {
1209  $a = new A();
1210  $a->id = 123;
1211  }
1212 
1216  public function testReadOnlyAttribute()
1217  {
1218  $thing = new TestReadOnlyAttributeModel();
1219  $this->assertFalse($thing->isAttributeReadOnly('notReadOnly'));
1220  $this->assertTrue ($thing->isAttributeReadOnly('readOnly'));
1221  $thing->notReadOnly = 1;
1222  $this->assertEquals(69, $thing->readOnly);
1223  $this->assertTrue ($thing->validate());
1224  $this->readOnly = 70;
1225  }
1226 
1227  public function testIsSame()
1228  {
1229  $model1 = new TestIdentityModel();
1230  $model1->name = '1';
1231  $this->assertTrue($model1->save());
1232  $this->assertTrue($model1->isSame($model1));
1233 
1234  $model2 = new TestIdentityModel();
1235  $model2->name = '2';
1236  $this->assertTrue($model2->save());
1237  $this->assertTrue($model2->isSame($model2));
1238 
1239  $this->assertFalse($model1->isSame($model2));
1240  $this->assertFalse($model2->isSame($model1));
1241 
1242  $anotherInstanceOfModel1 = TestIdentityModel::getByName('1');
1243  $this->assertTrue ($anotherInstanceOfModel1->isSame($model1));
1244  $this->assertFalse($anotherInstanceOfModel1->isSame($model2));
1245 
1246  $a = new A();
1247  $a->a = 1;
1248  $a->uniqueRequiredEmail = 'identity@zurmoinc.com';
1249  $this->assertTrue($a->save());
1250  $this->assertTrue($a->isSame($a));
1251 
1252  $this->assertFalse($a ->isSame($model1));
1253  $this->assertFalse($a ->isSame($model2));
1254  $this->assertFalse($model1->isSame($a));
1255  $this->assertFalse($model2->isSame($a));
1256  }
1257 
1258  public function testSelfRelatingThings()
1259  {
1260  $model1 = new TestSelfRelatingModel();
1261  $model1->name = 'Wilfred';
1262  $this->assertTrue($model1->save());
1263  $this->assertEquals(0, $model1->bunch->count());
1264 
1265  $model2 = new TestSelfRelatingModel();
1266  $model2->name = 'Jemima';
1267  $this->assertTrue($model2->save());
1268  $this->assertEquals(0, $model2->bunch->count());
1269 
1270  $model3 = new TestSelfRelatingModel();
1271  $model3->name = 'Elouise';
1272  $this->assertTrue($model3->save());
1273  $this->assertEquals(0, $model3->bunch->count());
1274 
1275  $model1->bunch->add($model2);
1276  $this->assertTrue($model1->save());
1277  $this->assertEquals(1, $model1->bunch->count());
1278  $this->assertEquals(0, $model2->bunch->count());
1279  $this->assertEquals(0, $model3->bunch->count());
1280 
1281  $model1->bunch->add($model3);
1282  $this->assertTrue($model1->save());
1283  $this->assertEquals(2, $model1->bunch->count());
1284  $this->assertEquals(0, $model2->bunch->count());
1285  $this->assertEquals(0, $model3->bunch->count());
1286 
1287  unset($model1);
1288  unset($model2);
1289  unset($model3);
1290 
1291  $model1 = TestSelfRelatingModel::getByName('Wilfred');
1292  $model2 = TestSelfRelatingModel::getByName('Jemima');
1293  $model3 = TestSelfRelatingModel::getByName('Elouise');
1294 
1295  $this->assertEquals(2, $model1->bunch->count());
1296  $this->assertEquals(0, $model2->bunch->count());
1297  $this->assertEquals(0, $model3->bunch->count());
1298  }
1299 
1303  public function testDownCast()
1304  {
1305  // Don't worry about this creation of the objects.
1306  // Skip...
1307 
1308  $b = new B();
1309  $b->name = 'the B model';
1310  $b->b = 'b';
1311  $this->assertTrue($b->validate());
1312  $this->assertTrue($b->save());
1313 
1314  $y = new Y();
1315  $y->name = 'the Y model';
1316  $y->y = 'y';
1317  $this->assertTrue($y->save());
1318 
1319  $c = new C();
1320  $c->name = 'the C model';
1321  $c->b = 'b';
1322  $c->c = 'c';
1323  $c->eUnique->e = strval(time());
1324  $c->e->e = 'e';
1325  $c->eRequired->e = 'hello';
1326  $c->e->g->g = 'g';
1327  $this->assertTrue($c->save());
1328 
1329  // ...down to here and just concern yourself
1330  // with the inheritance heirarchy.
1331 
1332  // As per the top of this file....
1333  // You will see that B and Y derive from A,
1334  // and C derives from B. E is required
1335  // because it is defaulted on C, which
1336  // is nothing to do with this test. Another
1337  // test uses it.
1338 
1339  $a1 = A::getByName('the B model');
1340  $a2 = A::getByName('the Y model');
1341  $a3 = A::getByName('the C model');
1342 
1343  // Here is where we see the problem with
1344  // the object relational mapping that
1345  // RedBeanModel supplies.
1346  // RedBeanModel knows that $a1 and
1347  // $a2 and $a3 are the same objects
1348  // as $b and $y and $c from its point
1349  // of view.
1350  $this->assertTrue($a1->isSame($b)); // :)
1351  $this->assertTrue($a2->isSame($y)); // :)
1352  $this->assertTrue($a3->isSame($c)); // :)
1353 
1354  // But they aren't from php's point
1355  // of view. We cannot treat $a1 as
1356  // a B, nor $a2 as a C, as we could
1357  // if they were purely OO objects in
1358  // memory that someone had given us
1359  // somehow.
1360  $this->assertTrue ($a1 instanceof A);
1361  $this->assertTrue ($a1 instanceof A);
1362  $this->assertFalse($a1 instanceof B); // :(
1363  $this->assertFalse($a1 instanceof Y); // :(
1364  $this->assertFalse($a3 instanceof C); // :(
1365 
1366  // So with the RedBeanModel object
1367  // relational model we have some
1368  // brokenness in our objects and their
1369  // polymorphism. To solve this in a
1370  // general and automatic way would
1371  // make something that is already too
1372  // slow more complicated, though
1373  // it would be nice (and OO correct)
1374  // from a usage point of view.
1375 
1376  // The solution/workaround is to give
1377  // us the ability to get $a1 and $a2
1378  // and $a3 as objects that are really
1379  // of the types of $b and $y and $c
1380  // when we know that that is possible.
1381 
1382  // Note that we look for C derived from B
1383  // first because if do B by itself it will
1384  // match and downcast to a B before it
1385  // finds C.
1386  $inheritancePossibilities = array(array('B', 'C'), 'B', 'Y');
1387 
1388  $this->assertTrue($a1->castDown($inheritancePossibilities) instanceof B); // :)
1389  $this->assertTrue($a2->castDown($inheritancePossibilities) instanceof Y); // :)
1390  $this->assertTrue($a3->castDown($inheritancePossibilities) instanceof C); // :)
1391 
1392  // Downcasting to what it already is doesn't
1393  // spaz out, and we could have just gone for
1394  // what we know they, if in fact we do know.
1395  // ie: we don't have to give it multiple
1396  // possibilities.
1397  $this->assertTrue($a1->castDown(array('A')) instanceof A); // :)
1398  $this->assertTrue($a2->castDown(array('A')) instanceof A); // :)
1399  $this->assertTrue($a3->castDown(array('A')) instanceof A); // :)
1400  $this->assertTrue($a1->castDown(array('B')) instanceof B); // :)
1401  $this->assertTrue($a2->castDown(array('Y')) instanceof Y); // :)
1402  $this->assertTrue($a3->castDown(array(array('B', 'C'))) instanceof C); // :)
1403 
1404  // It's not pretty, but it should be required
1405  // fairly rarely, and if we can make it prettier
1406  // later there are plenty of regression tests.
1407  }
1408 
1412  public function testDownCastToSameType()
1413  {
1414  $b = new B();
1415  $b->a = 0;
1416  $b->b = 'b';
1417  $this->assertTrue($b->validate());
1418  $this->assertTrue($b->save());
1419 
1420  // Will throw a NoFoundException, to be
1421  // taken as a bad cast exception.
1422  $b->castDown(array('B'));
1423  }
1424 
1429  public function testDownCastToWrongType()
1430  {
1431  $b = new B();
1432  $b->a = 0;
1433  $b->b = 'b';
1434  $this->assertTrue($b->validate());
1435  $this->assertTrue($b->save());
1436 
1437  // Will throw a NoFoundException, to be
1438  // taken as a bad cast exception.
1439  $b->castDown(array('C', 'D'));
1440  }
1441 
1442  public function testSetAttributeWithEmptyValue()
1443  {
1444  $model = new TestNameModel();
1445  $model->name = 'abc';
1446  $this->assertTrue($model->save());
1447  $model = TestNameModel::getById($model->id);
1448  $this->assertEquals('abc', $model->name);
1449  $fakePostData = array(
1450  'name' => '',
1451  );
1452  $model->setAttributes($fakePostData);
1453  $this->assertEquals('', $model->name);
1454  }
1455 
1456  public function testDateTime()
1457  {
1458  $now = time();
1459  $model = new TestDateTimeModel();
1460  $model->myDate = '2011-06-07';
1461  $model->myDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
1462  $this->assertTrue($model->save());
1463  $id = $model->id;
1464  unset($model);
1465  $model = TestDateTimeModel::getById($id);
1466  $this->assertEquals('2011-06-07', $model->myDate);
1467  $this->assertGreaterThanOrEqual($now - 2, DateTimeUtil::convertDbFormatDateTimeToTimestamp($model->myDateTime));
1468  $this->assertLessThanOrEqual($now + 2, DateTimeUtil::convertDbFormatDateTimeToTimestamp($model->myDateTime));
1469 
1470  $rows = ZurmoRedBean::getAll('desc testdatetimemodel');
1471  $this->assertEquals('mydate', $rows[1]['Field']);
1472  $this->assertEquals('date', $rows[1]['Type']);
1473  $this->assertEquals('mydatetime', $rows[2]['Field']);
1474  $this->assertEquals('datetime', $rows[2]['Type']);
1475  }
1476 
1477  public function testMakeSubsetOrCountSqlQueryWithExtraSelectParts()
1478  {
1480  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
1481  $quotedExtraSelectColumnNameAndAliases = array('a' => 'aAlias', 'b' => 'bAlias');
1482  $subsetSql = I::makeSubsetOrCountSqlQuery('i', $joinTablesAdapter, 1, 5, null, null, false, false,
1483  $quotedExtraSelectColumnNameAndAliases);
1484  $compareSubsetSql = "select {$quote}i{$quote}.{$quote}id{$quote} id, a aAlias, b bAlias ";
1485  $compareSubsetSql .= "from {$quote}i{$quote} ";
1486  $compareSubsetSql .= ' limit 5 offset 1';
1487  $this->assertEquals($compareSubsetSql, $subsetSql);
1488  }
1489 
1490  public function testBlobAttribute()
1491  {
1492  $binaryStuff = "test\x0\x1\xffstuff";
1493  $bigBinaryStuff = "test\x0\x1\xffbiggerstuff";
1494  $model = new TestBlobModel();
1495  $model->binaryStuff = $binaryStuff;
1496  $model->bigBinaryStuff = $bigBinaryStuff;
1497  $this->assertTrue($model->save());
1498  $id = $model->id;
1499  $model->forget();
1500  unset($model);
1501 
1502  $rows = ZurmoRedBean::getAll('desc testblobmodel');
1503  $this->assertEquals('binarystuff', $rows[1]['Field']);
1504  $this->assertEquals('blob', $rows[1]['Type']);
1505  $this->assertEquals('bigbinarystuff', $rows[2]['Field']);
1506  $this->assertEquals('longblob', $rows[2]['Type']);
1507 
1508  $model = TestBlobModel::getById($id);
1509  $this->assertEquals($binaryStuff, $model->binaryStuff);
1510  $this->assertEquals($bigBinaryStuff, $model->bigBinaryStuff);
1511  }
1512 
1519  {
1520  $m = new M();
1521  $m->m = 'aValue';
1522  $this->assertTrue($m->validate());
1523  $this->assertTrue($m->save());
1524  $mId = $m->id;
1525  $m->forget(); //need this here to demonstrate that the caching is working correctly
1526 
1527  $m = new M();
1528  $this->assertFalse($m->validate());
1529  $this->assertFalse($m->save());
1530  $m->forget(); //need this here to demonstrate that the caching is working correctly
1531 
1532  //Now edit the existing M and clear the required attribute m. It should fail validation.
1533  $m = M::getById($mId);
1534  unset($m);
1536  //Now reretrieve.
1537  $m = M::getById($mId);
1538  $m->m = null;
1539  $this->assertFalse($m->validate());
1540  $this->assertFalse($m->save());
1541  $m->m = 'aNewValue';
1542  $this->assertTrue($m->validate());
1543  $this->assertTrue($m->save());
1544  }
1545 
1546  public function testSwappingSameRelationModelsForSaving()
1547  {
1548  $j = new J();
1549  $j->jMember = 'b';
1550  $this->assertTrue($j->save());
1551  $jId = $j->id;
1552  $j->forget();
1553  unset($j);
1554 
1555  $i = new I();
1556  $i->iMember = 'a';
1557  $i->j = J::getById($jId);
1558  $this->assertTrue($i->save());
1559  $iId = $i->id;
1560  $i->forget();
1561  unset($i);
1562 
1563  $jSame = J::getById($jId);
1564  $i = I::getById($iId);
1565  $i->j = $jSame;
1566  $this->assertTrue($i->save());
1567  $i->forget();
1568  unset($i);
1569 
1570  $i = I::getById($iId);
1571  $this->assertEquals($i->j, $jSame);
1572  $this->assertEquals($i->j, J::getById($jId));
1573  }
1574 
1575  public function testRelatedValidation()
1576  {
1577  $x = new X();
1578  $p = new P();
1579  $b = new B();
1580  $x->prel = $p;
1581  $x->brel = $b;
1582  $this->assertFalse($x->validate());
1583  $this->assertEquals('Prel cannot be blank.', $x->getError('prel'));
1584  $this->assertEquals('Brel cannot be blank.', $x->getError('brel'));
1585  //After save errors
1586  $x = new X();
1587  $p = new P();
1588  $this->assertTrue($p->save(false));
1589  $id = $p->id;
1590  unset($p);
1591  $p = P::getById($id);
1592 
1593  $b = new B();
1594  $b->b = 'Hello';
1595  $this->assertTrue($b->save());
1596  $id = $b->id;
1597  unset($b);
1598  $b = B::getById($id);
1599 
1600  $x->prel = $p;
1601  $x->brel = $b;
1602  $this->assertFalse($x->save());
1603  $this->assertEquals('There was a problem validating Prel.', $x->getError('prel'));
1604  }
1605  }
1606 ?>
Definition: M.php:37
Definition: I.php:37
Definition: X.php:37
Definition: A.php:37
Definition: D.php:37
testThatUniqueValidatorCatchesTheNotUniqueBeforeRedBeanDoes()
static getSubset(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $offset=null, $count=null, $where=null, $orderBy=null, $modelClassName=null, $selectDistinct=false)
Definition: J.php:37
static makeSubsetOrCountSqlQuery($tableName, RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter, $offset=null, $count=null, $where=null, $orderBy=null, $selectCount=false, $selectDistinct=false, array $quotedExtraSelectColumnNameAndAliases=array())
testAddErrorAddErrorsHasErrorsAndGetErrors()
Definition: Y.php:37
testParentModelsOnNewModelsDoNotGetRowsUntilTheyAreSaved()
Definition: P.php:37
static getById($id, $modelClassName=null)
testAAAttributeDoesntValidateJunkAsBoolean1()
testModelCachesProperlyAndValidationWorksBasedOnAllNecessaryPropertiesCaching()
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
Definition: E.php:37
testGetErrorsDrillingDownRelatedOneToManyModels()
static forgetAllModelIdentifiersToModels()
Definition: C.php:37
testAAAttributeSavesJunkAsBooleanIfWeExplicitlyDontValidate()
Definition: B.php:36
Generated on Thu Jul 9 2020 07:10:29
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.