ValidatesNumericalityOf now handles integer_strings correctly.
[akelos.git] / test / unit / lib / AkActiveRecord / AkValidation.php
blob34a796838a6eb59273442241f8d270c6c7865754
1 <?php
3 defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true);
4 require_once(dirname(__FILE__).'/../../../fixtures/config/config.php');
6 require_once(AK_LIB_DIR.DS.'AkActiveRecord.php');
7 require_once(AK_LIB_DIR.DS.'AkActiveRecord'.DS.'AkObserver.php');
10 class test_AkActiveRecord_validators extends AkUnitTest
13 function test_start()
15 $this->installAndIncludeModels(
16 array('TestPerson'=>
17 'id,
18 user_name string(32),
19 first_name string(200),
20 last_name string(200),
21 city string(40),
22 state string(40),
23 email string(150),
24 country string(2),
25 age int,
26 password string(32),
27 tos boolean,
28 score int'
29 ));
31 $this->installAndIncludeModels(array('Picture', 'Landlord'));
34 function Test_of_isBlank()
36 $Person = new TestPerson();
37 $this->assertTrue($Person->isBlank());
38 $this->assertTrue($Person->isBlank(''));
39 $this->assertTrue($Person->isBlank(' '));
40 $this->assertTrue($Person->isBlank("\n\r"));
41 $this->assertTrue($Person->isBlank("\t"));
42 $this->assertFalse($Person->isBlank(0));
45 function Test_of_addError()
47 $Person = new TestPerson();
48 $Person->addError('user_name');
49 $this->assertTrue(count($Person->_errors['user_name']) == 1);
50 $Person->addError('user_name','has an error');
51 $this->assertTrue(count($Person->_errors['user_name']) == 2);
52 $Person->addError('password');
53 $this->assertTrue(count($Person->_errors['password']) == 1);
54 $this->assertTrue(count($Person->_errors) == 2);
55 $this->assertTrue(count($Person->_errors['user_name']) == 2);
58 function Test_of_clearErrors()
60 $Person = new TestPerson();
61 $Person->addError('user_name');
62 $Person->addError('user_name','has an error');
63 $Person->addError('password');
64 $Person->clearErrors();
65 $this->assertTrue(count($Person->_errors) == 0);
68 function Test_of_hasErrors()
70 $Person = new TestPerson();
71 $this->assertFalse($Person->hasErrors());
72 $Person->addError('user_name');
73 $this->assertTrue($Person->hasErrors());
76 function Test_of_getErrorsOn()
78 $Person = new TestPerson();
79 $this->assertFalse($Person->getErrorsOn('user_name'));
80 $Person->addError('user_name');
81 $this->assertEqual($Person->getErrorsOn('user_name'),$Person->_defaultErrorMessages['invalid']);
82 $Person->addError('user_name','not nice');
83 $this->assertEqual($Person->getErrorsOn('user_name'),array($Person->_defaultErrorMessages['invalid'],'not nice'));
86 function Test_of_countErrors()
88 $Person = new TestPerson();
89 $this->assertEqual($Person->countErrors(), 0);
90 $Person->addError('user_name');
91 $this->assertEqual($Person->countErrors(), 1);
92 $Person->addError('user_name','has an error');
93 $this->assertEqual($Person->countErrors(), 2);
94 $Person->addError('password');
95 $this->assertEqual($Person->countErrors(), 3);
98 function Test_of_isInvalid()
100 $Person = new TestPerson();
101 $this->assertFalse($Person->isInvalid('user_name'));
102 $Person->addError('user_name');
103 $this->assertTrue($Person->isInvalid('user_name'));
106 function Test_of_getErrors()
108 $Person = new TestPerson();
109 $this->assertTrue(is_array($Person->getErrors()));
110 $this->assertEqual(count($Person->getErrors()), 0);
112 $Person->addError('user_name');
113 $expected = array('user_name'=>array($Person->_defaultErrorMessages['invalid']));
114 $this->assertEqual($Person->getErrors(), $expected);
116 $Person->addError('password','is not a valid password');
117 $expected = array_merge($expected,array('password'=>array('is not a valid password')));
118 $this->assertEqual($Person->getErrors(), $expected);
120 $expected = array_merge($expected, array('password'=>array_merge($expected['password'],array('too short'))));
121 $Person->addError('password','too short');
122 $this->assertEqual($Person->getErrors(), $expected);
125 function Test_of_getFullErrorMessages()
127 $Person = new TestPerson();
128 $this->assertTrue(is_array($Person->getFullErrorMessages()));
129 $this->assertEqual(count($Person->getFullErrorMessages()), 0);
131 $Person->addError('user_name');
132 $expected = array('user_name'=>array('User name '.$Person->_defaultErrorMessages['invalid']));
133 $this->assertEqual($Person->getFullErrorMessages(), $expected);
135 $Person->addError('password','is not a valid password');
136 $expected = array_merge($expected,array('password'=>array('Password is not a valid password')));
137 $this->assertEqual($Person->getFullErrorMessages(), $expected);
139 $expected = array_merge($expected, array('password'=>array_merge($expected['password'],array('Password too short'))));
140 $Person->addError('password','too short');
141 $this->assertEqual($Person->getFullErrorMessages(), $expected);
145 function Test_of_addErrorOnEmpty()
147 $Person = new TestPerson();
148 $Person->addErrorOnEmpty('user_name');
149 $expected = array('user_name'=>array($Person->_defaultErrorMessages['empty']));
150 $this->assertEqual($Person->getErrors(), $expected);
152 $Person->set('first_name','Bermi');
153 $Person->addErrorOnEmpty('first_name');
154 $this->assertEqual($Person->getErrors(), $expected);
157 function Test_of_addErrorOnBlank()
159 $Person = new TestPerson();
160 $Person->addErrorOnBlank('user_name');
161 $expected = array('user_name'=>array($Person->_defaultErrorMessages['blank']));
162 $this->assertEqual($Person->getErrors(), $expected);
164 $Person->set('first_name','Bermi');
165 $Person->addErrorOnBlank('first_name');
166 $this->assertEqual($Person->getErrors(), $expected);
169 function Test_of_addErrorOnBoundaryBreaking()
171 $Person = new TestPerson();
172 $Person->set('age',17);
173 $Person->addErrorOnBoundaryBreaking('age',18,65,'too old','too young');
174 $expected = array('age'=>array('too young'));
175 $this->assertEqual($Person->getErrors(), $expected);
177 $Person = new TestPerson();
178 $Person->set('age',66);
179 $Person->addErrorOnBoundaryBreaking('age',18,65,'too old','too young');
180 $expected = array('age'=>array('too old'));
181 $this->assertEqual($Person->getErrors(), $expected);
183 $Person = new TestPerson();
184 $Person->set('age',65);
185 $Person->addErrorOnBoundaryBreaking('age',18,65,'too old','too young');
186 $Person->set('age',18);
187 $Person->addErrorOnBoundaryBreaking('age',18,65,'too old','too young');
189 $this->assertEqual($Person->getErrors(), array());
193 function Test_of_addErrorOnBoundryBreaking()
195 //this is an alias of addErrorOnBoundaryBreaking
198 function Test_of_addErrorToBase()
200 $Person = new TestPerson();
201 $Person->addErrorToBase('Nothing has changed');
202 $expected = array('TestPerson'=>array('Nothing has changed'));
203 $this->assertEqual($Person->getErrors(), $expected);
204 $expected = array('TestPerson'=>array('Nothing has changed','Nothing has changed at all'));
205 $Person->addErrorToBase('Nothing has changed at all');
206 $this->assertEqual($Person->getErrors(), $expected);
209 function Test_of_getBaseErrors()
211 $Person = new TestPerson();
212 $this->assertEqual($Person->getBaseErrors(), array());
213 $Person->addErrorToBase('Nothing has changed');
214 $expected = array('Nothing has changed');
215 $this->assertEqual($Person->getBaseErrors(), $expected);
216 $expected = array('Nothing has changed','Nothing has changed at all');
217 $Person->addErrorToBase('Nothing has changed at all');
218 $this->assertEqual($Person->getBaseErrors(), $expected);
223 function Test_of_errorsToString()
225 $Person = new TestPerson();
226 $Person->addErrorOnBlank('user_name');
227 $Person->addErrorOnBlank('first_name');
228 $this->assertTrue(strstr($Person->errorsToString(), "User name can't be blank"));
229 $this->assertTrue(strstr($Person->errorsToString(), "First name can't be blank"));
233 function Test_of_validatesConfirmationOf()
235 $Person = new TestPerson();
236 $Person->validatesConfirmationOf('user_name');
237 $this->assertFalse($Person->hasErrors());
239 $Person->set('user_name', 'bermi');
241 $this->assertEqual($Person->getErrorsOn('user_name'),'');
243 $Person->user_name_confirmation = '';
244 $Person->validatesConfirmationOf('user_name');
245 $this->assertEqual($Person->getErrorsOn('user_name'),$Person->_defaultErrorMessages['confirmation']);
247 $Person = new TestPerson();
248 $Person->set('user_name', 'Bermi');
249 $Person->user_name_confirmation = 'bermi';
250 $Person->validatesConfirmationOf('user_name');
251 $this->assertEqual($Person->getErrorsOn('user_name'),$Person->_defaultErrorMessages['confirmation']);
253 $Person = new TestPerson();
254 $Person->setAttributes(array('password'=>'abc','password_confirmation'=>'ake'));
255 $Person->validatesConfirmationOf('password');
256 $this->assertEqual($Person->getErrorsOn('password'), $Person->_defaultErrorMessages['confirmation']);
260 function Test_of_validatesAcceptanceOf()
262 $Person = new TestPerson();
263 $Person->validatesAcceptanceOf('tos');
264 $this->assertEqual($Person->getErrorsOn('tos'),$Person->_defaultErrorMessages['accepted']);
266 $Person = new TestPerson();
267 $Person->validatesAcceptanceOf('tos','You need to type down "I accept this terms and conditions"',"I accept this terms and conditions");
268 $this->assertEqual($Person->getErrorsOn('tos'),'You need to type down "I accept this terms and conditions"');
269 $Person->clearErrors();
270 $Person->set('tos',"I accept this terms and conditions");
271 $this->assertFalse($Person->hasErrors());
275 function Test_of_validatesPresenceOf()
277 $Person = new TestPerson();
278 $Person->validatesPresenceOf('user_name');
279 $this->assertEqual($Person->getErrorsOn('user_name'),$Person->_defaultErrorMessages['blank']);
281 $Person = new TestPerson();
282 $Person->validatesPresenceOf('user_name','is a compulsory field');
283 $this->assertEqual($Person->getErrorsOn('user_name'),'is a compulsory field');
284 $Person->clearErrors();
285 $Person->set('user_name','bermi');
286 $this->assertFalse($Person->hasErrors());
291 function Test_of_validatesLengthOf()
293 $Person = new TestPerson();
295 $Person->city = 'Vilanova i la Geltrí';
296 $Person->validatesLengthOf("city", array("maximum"=>5,'message'=>"less than %d if you don't mind"));
297 $this->assertEqual($Person->getErrorsOn('city'),"less than 5 if you don't mind");
299 $Person->clearErrors();
300 $Person->city = 'Carlet';
301 $Person->validatesLengthOf("city", array("maximum"=>10));
302 $this->assertFalse($Person->getErrorsOn('city'));
304 $Person->clearErrors();
305 $Person->city = '';
306 $Person->validatesLengthOf("city", array("maximum"=>10, 'allow_null'=>true, 'message'=> "less than %d if you don't mind"));
307 $this->assertFalse($Person->getErrorsOn('city'));
309 $Person->clearErrors();
310 $Person->score = 101;
311 $Person->validatesLengthOf("score", array("within"=>array(1, 100)));
312 $this->assertEqual($Person->getErrorsOn('score'),sprintf($Person->_defaultErrorMessages['too_long'],100));
314 $Person->clearErrors();
315 $Person->score = -5;
316 $Person->validatesLengthOf("score", array("within"=>array(1, 100)));
317 $this->assertEqual($Person->getErrorsOn('score'),sprintf($Person->_defaultErrorMessages['too_short'],1));
319 $Person->clearErrors();
320 $Person->score = 25;
321 $Person->validatesLengthOf("score", array("within"=>array(1, 100)));
322 $this->assertFalse($Person->getErrorsOn('score'));
324 $Person->clearErrors();
325 $Person->state = 'CA';
326 $Person->validatesLengthOf("state", array("in"=>array(5, 20), "too_long" => "pick a shorter name", "too_short" => "pick a longer name"));
327 $this->assertEqual($Person->getErrorsOn('state'),"pick a longer name");
329 $Person->clearErrors();
330 $Person->state = 'Barcelona';
331 $Person->validatesLengthOf("state", array("in"=>array(2, 5), "too_long" => "pick a shorter name", "too_short" => "pick a longer name"));
332 $this->assertEqual($Person->getErrorsOn('state'),"pick a shorter name");
334 $Person->clearErrors();
335 $Person->state = 'Valencia';
336 $Person->validatesLengthOf("state", array("in"=>array(5, 20), "too_long" => "pick a shorter name", "too_short" => "pick a longer name"));
337 $this->assertFalse($Person->getErrorsOn('state'));
340 $Person->clearErrors();
341 $Person->subscriptions = array();
342 $Person->validatesLengthOf("subscriptions", array("minimum"=>4, "too_short"=>"you need to select at least 4 subscriptions"));
343 $this->assertEqual($Person->getErrorsOn('subscriptions'),"you need to select at least 4 subscriptions");
345 $Person->clearErrors();
346 $Person->subscriptions = array('php architect');
347 $Person->validatesLengthOf("subscriptions", array("minimum"=>4, "too_short"=>"you need to select at least 4 subscriptions"));
348 $this->assertEqual($Person->getErrorsOn('subscriptions'),"you need to select at least 4 subscriptions");
350 $Person->clearErrors();
351 $Person->subscriptions = array('php architect','computer world', 'wired','slashdot');
352 $Person->validatesLengthOf("subscriptions", array("minimum"=>4, "too_short"=>"you need to select at least 4 subscriptions"));
353 $this->assertFalse($Person->getErrorsOn('subscriptions'));
355 $Person->clearErrors();
356 $Person->validatesLengthOf("country", array("is"=>2, "message"=>"must be %d characters long as specified on ISO 3166"));
357 $this->assertEqual($Person->getErrorsOn('country'),"must be 2 characters long as specified on ISO 3166");
359 $Person->clearErrors();
360 $Person->country = '';
361 $Person->validatesLengthOf("country", array("is"=>2, "message"=>"must be %d characters long as specified on ISO 3166"));
362 $this->assertEqual($Person->getErrorsOn('country'),"must be 2 characters long as specified on ISO 3166");
364 $Person->clearErrors();
365 $Person->country = 2;
366 $Person->validatesLengthOf("country", array("is"=>2, "message"=>"must be %d characters long as specified on ISO 3166"));
367 $this->assertFalse($Person->getErrorsOn('country'));
369 $Person->clearErrors();
370 $Person->country = 'ES';
371 $Person->validatesLengthOf("country", array("is"=>2, "message"=>"must be %d characters long as specified on ISO 3166"));
372 $this->assertFalse($Person->getErrorsOn('country'));
376 function Test_of_validatesSizeOf()
378 //alias for validatesLengthOf
381 function Test_of_validatesUniquenessOf()
383 $Person = new TestPerson('user_name->','bermi','first_name->','Bermi','last_name->','Ferrer','country->','ES','tos->',1);
384 $this->assertTrue($Person->save());
386 $Person = new TestPerson('user_name->','bermi','first_name->','Bermi','last_name->','Ferrer');
387 $Person->validatesUniquenessOf("user_name");
388 $this->assertTrue($Person->hasErrors());
390 $Person = $Person->findFirst(array('user_name' => 'bermi'));
391 $this->assertEqual($Person->user_name,'bermi');
392 $Person->validatesUniquenessOf("user_name");
393 $this->assertFalse($Person->hasErrors());
396 $Person = $Person->findFirst(array('user_name = ?','bermi'));
397 $Person->validatesUniquenessOf("user_name",array('scope'=>'country'));
398 $this->assertFalse($Person->hasErrors());
400 $Person = new TestPerson('user_name->','bermi','first_name->','Bermi','last_name->','Ferrer','country->','US');
401 $Person->validatesUniquenessOf("user_name",array('scope'=>'country'));
402 $this->assertFalse($Person->hasErrors());
404 $Person = new TestPerson('user_name->','bermi','first_name->','Bermi','last_name->','Ferrer','country->','ES');
405 $Person->validatesUniquenessOf("user_name",array('scope'=>'country'));
406 $this->assertTrue($Person->hasErrors());
410 function Test_of_validatesUniquenessOfUsingMultipleScopes()
412 $Person = new TestPerson('user_name->','admin','first_name->','Sam','last_name->','','country->','ES','tos->',1);
413 $this->assertTrue($Person->save());
415 $Person = new TestPerson('user_name->','admin','first_name->','Sam','last_name->','','country->','FR','tos->',1);
416 $Person->validatesUniquenessOf("user_name",array('scope'=>'first_name'));
417 $this->assertTrue($Person->hasErrors());
419 $Person = new TestPerson('user_name->','admin','first_name->','Sam','last_name->','','country->','FR','tos->',1);
420 $Person->validatesUniquenessOf("user_name",array('scope'=>array('first_name','country')));
421 $this->assertFalse($Person->hasErrors());
425 function Test_of_validatesUniquenessOfConditionally()
427 $Person = new TestPerson('user_name->','james','first_name->','James','last_name->','','country->','ES','tos->',1);
428 $this->assertTrue($Person->save());
430 $Person = new TestPerson('user_name->','james','first_name->','James','last_name->','','country->','ES','tos->',1);
431 $Person->validatesUniquenessOf("user_name");
432 $this->assertTrue($Person->hasErrors());
434 $Person = new TestPerson('user_name->','james','first_name->','James','last_name->','','country->','ES','tos->',1);
435 $Person->force_validation = false;
436 $Person->validatesUniquenessOf("user_name", array('if'=>'$this->force_validation'));
437 $this->assertFalse($Person->hasErrors());
443 function Test_of_validatesFormatOf()
445 $Person = new TestPerson();
446 $Person->validatesFormatOf('email', AK_EMAIL_REGULAR_EXPRESSION);
447 $this->assertEqual($Person->getErrorsOn('email'),$Person->_defaultErrorMessages['invalid']);
449 $Person->clearErrors();
450 $Person->email = 'bermi [at] example.com';
451 $Person->validatesFormatOf('email', AK_EMAIL_REGULAR_EXPRESSION);
452 $this->assertEqual($Person->getErrorsOn('email'),$Person->_defaultErrorMessages['invalid']);
454 $Person->clearErrors();
455 $Person->email = 'bermi@example.com';
456 $Person->validatesFormatOf('email', AK_EMAIL_REGULAR_EXPRESSION);
457 $Person->email = 'bermi@example.co.uk';
458 $Person->validatesFormatOf('email', AK_EMAIL_REGULAR_EXPRESSION);
459 $Person->email = 'bermi_ferrer@exam-ple.co.uk';
460 $Person->validatesFormatOf('email', AK_EMAIL_REGULAR_EXPRESSION);
461 $this->assertFalse($Person->hasErrors());
464 $Person->clearErrors();
465 $Person->email = 'bermi [at] example.com';
466 $Person->validatesFormatOf('email', AK_EMAIL_REGULAR_EXPRESSION);
467 $this->assertEqual($Person->getErrorsOn('email'),$Person->_defaultErrorMessages['invalid']);
469 $Person->clearErrors();
470 $Person->email = 'bermi@example.com';
471 $Person->validatesFormatOf('email', AK_EMAIL_REGULAR_EXPRESSION);
472 $Person->email = 'bermi@example.co.uk';
473 $Person->validatesFormatOf('email', AK_EMAIL_REGULAR_EXPRESSION);
474 $Person->email = 'bermi_ferrer@exam-ple.co.uk';
475 $Person->validatesFormatOf('email', AK_EMAIL_REGULAR_EXPRESSION);
476 $this->assertFalse($Person->hasErrors());
479 $Person->clearErrors();
480 $Person->first_name = '';
481 $Person->validatesFormatOf('first_name', AK_NOT_EMPTY_REGULAR_EXPRESSION,"can not be empty");
482 $this->assertEqual($Person->getErrorsOn('first_name'),"can not be empty");
484 $Person->clearErrors();
485 $Person->first_name = 'Bermi';
486 $Person->validatesFormatOf('first_name', AK_NOT_EMPTY_REGULAR_EXPRESSION,"can not be empty");
487 $this->assertFalse($Person->hasErrors());
490 $Person->clearErrors();
491 $Person->number = 12.56;
492 $Person->validatesFormatOf('number', AK_NUMBER_REGULAR_EXPRESSION);
493 $this->assertEqual($Person->getErrorsOn('number'),$Person->_defaultErrorMessages['invalid']);
495 $Person->clearErrors();
496 $Person->number = 1256;
497 $Person->validatesFormatOf('number', AK_NUMBER_REGULAR_EXPRESSION);
498 $this->assertFalse($Person->hasErrors());
500 $Person->clearErrors();
501 $Person->phone = 'blah';
502 $Person->validatesFormatOf('phone', AK_PHONE_REGULAR_EXPRESSION);
503 $this->assertEqual($Person->getErrorsOn('phone'),$Person->_defaultErrorMessages['invalid']);
505 $Person->clearErrors();
506 $Person->phone = '+34 96 299 3000';
507 $Person->validatesFormatOf('phone', AK_PHONE_REGULAR_EXPRESSION);
508 $this->assertFalse($Person->hasErrors());
511 $Person->clearErrors();
512 $Person->date = 'Monday';
513 $Person->validatesFormatOf('date', AK_DATE_REGULAR_EXPRESSION);
514 $this->assertEqual($Person->getErrorsOn('date'),$Person->_defaultErrorMessages['invalid']);
516 $Person->clearErrors();
517 $Person->date = '1978/06/16';
518 $Person->validatesFormatOf('date', AK_DATE_REGULAR_EXPRESSION);
519 $this->assertFalse($Person->hasErrors());
522 $Person->clearErrors();
523 $Person->ip = '257.0.0.1';
524 $Person->validatesFormatOf('ip', AK_IP4_REGULAR_EXPRESSION);
525 $this->assertEqual($Person->getErrorsOn('ip'),$Person->_defaultErrorMessages['invalid']);
527 $Person->clearErrors();
528 $Person->ip = '255.0.0.1';
529 $Person->validatesFormatOf('ip', AK_IP4_REGULAR_EXPRESSION);
530 $this->assertFalse($Person->hasErrors());
533 $Person->clearErrors();
534 $Person->post_code = 'a';
535 $Person->validatesFormatOf('post_code', AK_POST_CODE_REGULAR_EXPRESSION);
536 $this->assertEqual($Person->getErrorsOn('post_code'),$Person->_defaultErrorMessages['invalid']);
538 $Person->clearErrors();
539 $Person->post_code = 'san francisco';
540 $Person->validatesFormatOf('post_code', AK_POST_CODE_REGULAR_EXPRESSION);
541 $this->assertEqual($Person->getErrorsOn('post_code'),$Person->_defaultErrorMessages['invalid']);
543 $Person->clearErrors();
544 $Person->post_code = 'NSW 8376';
545 $Person->validatesFormatOf('post_code', AK_POST_CODE_REGULAR_EXPRESSION);
546 $this->assertFalse($Person->hasErrors());
548 $Person->clearErrors();
549 $Person->post_code = 'NSW 1008';
550 $Person->validatesFormatOf('post_code', AK_POST_CODE_REGULAR_EXPRESSION);
551 $Person->post_code = 46240;
552 $Person->validatesFormatOf('post_code', AK_POST_CODE_REGULAR_EXPRESSION);
553 $this->assertFalse($Person->hasErrors());
557 function Test_of_validatesInclusionOf()
559 $Person = new TestPerson();
560 $Person->validatesInclusionOf('gender', array('male', 'female'), "woah! what are you then!??!!");
561 $this->assertEqual($Person->getErrorsOn('gender'),"woah! what are you then!??!!");
563 $Person->clearErrors();
564 $Person->gender = 'm';
565 $Person->validatesInclusionOf('gender', array('male', 'female'), "woah! what are you then!??!!");
566 $this->assertEqual($Person->getErrorsOn('gender'),"woah! what are you then!??!!");
568 $Person->clearErrors();
569 $Person->gender = 'male';
570 $Person->validatesInclusionOf('gender', array('male', 'female'));
571 $this->assertFalse($Person->hasErrors());
573 $Person->clearErrors();
574 unset($Person->gender);
575 $Person->validatesInclusionOf('gender', array('male', 'female'),'I need to know your gender', true);
576 $this->assertFalse($Person->hasErrors());
578 $Person = new TestPerson();
579 $Person->gender = '0';
580 $Person->validatesInclusionOf('gender', array('male', 'female'),'I need to know your gender', true);
581 $this->assertTrue($Person->hasErrors());
583 $Person = new TestPerson();
584 $Person->gender = 0;
585 $Person->validatesInclusionOf('gender', array('male', 'female'),'I need to know your gender', true);
586 $this->assertTrue($Person->hasErrors());
588 $Person = new TestPerson();
589 $Person->gender = null;
590 $Person->validatesInclusionOf('gender', array('male', 'female'),'I need to know your gender', true);
591 $this->assertFalse($Person->hasErrors());
593 $Person->clearErrors();
594 $Person->age = 17;
595 $Person->validatesInclusionOf('age', range(18, 120));
596 $this->assertEqual($Person->getErrorsOn('age'),$Person->_defaultErrorMessages['inclusion']);
598 $Person->clearErrors();
599 $Person->age = 121;
600 $Person->validatesInclusionOf('age', range(18, 120));
601 $this->assertEqual($Person->getErrorsOn('age'),$Person->_defaultErrorMessages['inclusion']);
603 $Person->clearErrors();
604 $Person->age = 18;
605 $Person->validatesInclusionOf('age', range(18, 120));
606 $this->assertFalse($Person->hasErrors());
610 function Test_of_validatesExclusionOf()
612 $Person = new TestPerson();
613 $Person->validatesExclusionOf('gender', array('too much'), "don't lie");
614 $this->assertEqual($Person->getErrorsOn('gender'),"don't lie");
616 $Person->clearErrors();
617 $Person->gender = 'too much';
618 $Person->validatesExclusionOf('gender', array('too much'), "don't lie");
619 $this->assertEqual($Person->getErrorsOn('gender'),"don't lie");
621 $Person->clearErrors();
622 $Person->gender = 'male';
623 $Person->validatesExclusionOf('gender', array('too much'), "don't lie");
624 $this->assertFalse($Person->hasErrors());
626 $Person->clearErrors();
627 unset($Person->gender);
628 $Person->validatesExclusionOf('gender', array('too much'), "don't lie", true);
629 $this->assertFalse($Person->hasErrors());
631 $Person->clearErrors();
632 $Person->age = 17;
633 $Person->validatesExclusionOf('age', range(18, 120));
634 $this->assertFalse($Person->hasErrors());
636 $Person->clearErrors();
637 $Person->age = 121;
638 $Person->validatesExclusionOf('age', range(18, 120));
639 $this->assertFalse($Person->hasErrors());
641 $Person->clearErrors();
642 $Person->age = 18;
643 $Person->validatesExclusionOf('age', range(18, 120));
644 $this->assertEqual($Person->getErrorsOn('age'),$Person->_defaultErrorMessages['exclusion']);
647 function Test_of_validatesNumericalityOf()
649 $Person = new TestPerson();
651 $Person->validatesNumericalityOf('age');
652 $this->assertEqual($Person->getErrorsOn('age'),$Person->_defaultErrorMessages['not_a_number']);
654 $Person->clearErrors();
655 $Person->age = 'text';
656 $Person->validatesNumericalityOf('age');
657 $this->assertEqual($Person->getErrorsOn('age'),$Person->_defaultErrorMessages['not_a_number']);
659 $Person->clearErrors();
660 $Person->age = 15.98;
661 $Person->validatesNumericalityOf('age');
662 $this->assertFalse($Person->hasErrors());
664 $Person->clearErrors();
665 $Person->age = 15.98;
666 $Person->validatesNumericalityOf('age','not_valid', true);
667 $this->assertEqual($Person->getErrorsOn('age'),'not_valid');
669 $Person->clearErrors();
670 $Person->age = 18;
671 $Person->validatesNumericalityOf('age');
672 $this->assertFalse($Person->hasErrors());
674 $Person->clearErrors();
675 $Person->age = '18';
676 $Person->validatesNumericalityOf('age','not_valid',true);
677 $this->assertFalse($Person->hasErrors());
679 $Person->clearErrors();
680 unset($Person->age);
681 $Person->validatesNumericalityOf('age', 'not_valid',false, true);
682 $this->assertFalse($Person->hasErrors());
684 $Person->clearErrors();
685 $Person->age = null;
686 $Person->validatesNumericalityOf('age', 'not_valid',false, false);
687 $this->assertTrue($Person->hasErrors());
690 function Test_of_validateOnCreate()
692 $Person = new TestPerson('user_name->','hilario','first_name->','Hilario','last_name->','Hervás','country->','ES','tos->',1);
693 $Person->validateOnCreate();
694 $this->assertFalse($Person->hasErrors());
696 $Person = new TestPerson('user_name->','hilario','first_name->','Hilario','last_name->','Hervás','country->','ES');
697 $Person->validateOnCreate();
698 $this->assertEqual($Person->getErrorsOn('tos'),$Person->_defaultErrorMessages['accepted']);
699 $this->assertFalse($Person->save());
702 function Test_of_validateOnUpdate()
704 $Person = new TestPerson('email->','email@example.com');
705 $Person->validateOnUpdate();
706 $this->assertFalse($Person->hasErrors());
708 $Person = new TestPerson('user_name->','hilario','first_name->','Hilario','last_name->','Hervás','country->','ES');
709 $Person->validateOnUpdate();
710 $this->assertEqual($Person->getErrorsOn('email'),$Person->_defaultErrorMessages['blank']);
714 function Test_of_validate()
716 $Person = new TestPerson('first_name->','Alicia');
717 $Person->validate();
718 $this->assertFalse($Person->hasErrors());
720 $Person = new TestPerson('last_name->','Sadurní','country->','ES');
721 $Person->validate();
722 $this->assertEqual($Person->getErrorsOn('first_name'),$Person->_defaultErrorMessages['blank']);
725 function Test_of_isValid()
727 $Person = new TestPerson('country->','ES');
728 $this->assertFalse($Person->isValid());
729 $this->assertEqual($Person->getErrors(), array('first_name' => array("can't be blank"),'tos' =>array("must be accepted")));
731 $Person->clearErrors();
732 $Person = $Person->findFirst(array('user_name = ?','bermi'));
733 $Person->set('tos',0);
734 $this->assertFalse($Person->isValid());
735 $this->assertEqual($Person->getErrors(), array('email' => array("can't be blank")));
739 function Test_of_validatesAssociated()
741 $Picture =& new Picture('title->','Carlet');
743 $Landlord =& new Landlord();
744 $Landlord->test_validators = array('validatesPresenceOf'=>array('name'));
745 $Picture->landlord->assign($Landlord);
747 $Picture->validatesAssociated('landlord');
749 $this->assertEqual($Picture->getErrorsOn('landlord'),$Picture->_defaultErrorMessages['invalid']);
756 ak_test('test_AkActiveRecord_validators',true);