<?php /** * Lithium: the most rad php framework * * @copyright Copyright 2015, Union of RAD (http://union-of-rad.org) * @license http://opensource.org/licenses/bsd-license.php The BSD License */ namespace lithium\tests\mocks\data; use lithium\util\Validator; class MockPostForValidates extends \lithium\data\Model { protected $_meta = array('source' => 'mock_posts', 'connection' => false); public $validates = array('title' => 'please enter a title', 'email' => array(array('notEmpty', 'message' => 'email is empty'), array('email', 'message' => 'email is not valid'), array('modelIsSet', 'required' => false, 'message' => 'model is not set'), array('inList', 'list' => array('*****@*****.**', '*****@*****.**'), 'on' => 'customEvent', 'message' => 'email is not in 1st list'), array('inList', 'list' => array('*****@*****.**'), 'on' => 'anotherCustomEvent', 'message' => 'email is not in 2nd list'))); public static function init() { $class = __CLASS__; Validator::add('modelIsSet', function ($value, $format, $options) use($class) { if (isset($options['model']) && ($options['model'] = $class)) { return true; } return false; }); } } MockPostForValidates::init();
public function testSaveFailedWithValidationByModelDefinitionAndTriggeredCustomEvents() { $post = MockPostForValidates::create(); $events = array('customEvent', 'anotherCustomEvent'); $result = $post->save(null, compact('events')); $this->assertFalse($result); $result = $post->errors(); $this->assertNotEmpty($result); $expected = array('title' => array('please enter a title'), 'email' => array('email is empty', 'email is not valid', 'email is not in 1st list', 'email is not in 2nd list')); $result = $post->errors(); $this->assertEqual($expected, $result); }
public function testCustomValidationCriteria() { $validates = array('title' => 'A custom message here for empty titles.', 'email' => array(array('notEmpty', 'message' => 'email is empty.'))); $post = MockPostForValidates::create(array('title' => 'custom validation', 'email' => 'asdf')); $result = $post->validates(array('rules' => $validates)); $this->assertTrue($result === true); $this->assertIdentical(array(), $post->errors()); }
public function testValidatesEmailIsValid() { $post = MockPostForValidates::create(array('title' => 'new post', 'email' => '*****@*****.**')); $result = $post->validates(); $this->assertTrue($result === true); $result = $post->errors(); $this->assertTrue(empty($result)); }
public function testSaveWithValidationAndWhitelist() { $post = MockPostForValidates::create(); $whitelist = array('title'); $post->title = 'title'; $result = $post->save(null, compact('whitelist')); $this->assertTrue($result); $post->title = ''; $result = $post->save(null, compact('whitelist')); $this->assertFalse($result); $result = $post->errors(); $this->assertNotEmpty($result); $expected = array('title' => array('please enter a title')); $result = $post->errors(); $this->assertEqual($expected, $result); }