| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731 |
- <?php
- App::uses('MyModel', 'Tools.Model');
- App::uses('MyCakeTestCase', 'Tools.TestSuite');
- class MyModelTest extends MyCakeTestCase {
- public $Post;
- public $User;
- public $modelName = 'User';
- public $fixtures = array('core.user', 'core.post', 'core.author');
- public function setUp() {
- parent::setUp();
- $this->Post = ClassRegistry::init('MyAppModelPost');
- $this->User = ClassRegistry::init('MyAppModelUser');
- }
- public function testObject() {
- $this->Post = ClassRegistry::init('MyModel');
- $this->assertTrue(is_object($this->Post));
- $this->assertInstanceOf('MyModel', $this->Post);
- }
- public function testGet() {
- $record = $this->Post->get(2);
- $this->assertEquals(2, $record['Post']['id']);
- $record = $this->Post->get(2, array('fields' => 'id', 'created'));
- $this->assertEquals(2, count($record['Post']));
- $record = $this->Post->get(2, array('fields' => 'id', 'title', 'body'), array('Author'));
- $this->assertEquals(3, $record['Author']['id']);
- }
- public function testEnum() {
- $array = array(
- 1 => 'foo',
- 2 => 'bar',
- );
- $res = AppTestModel::enum(null, $array, false);
- $this->assertEquals($array, $res);
- $res = AppTestModel::enum(2, $array, false);
- $this->assertEquals('bar', $res);
- $res = AppTestModel::enum('2', $array, false);
- $this->assertEquals('bar', $res);
- $res = AppTestModel::enum(3, $array, false);
- $this->assertFalse($res);
- }
- /**
- * More tests in MyModel Test directly
- */
- public function testGetFalse() {
- $this->User->order = array();
- $is = $this->User->get('xyz');
- $this->assertSame(array(), $is);
- }
- /**
- * Test auto inc value of the current table
- */
- public function testGetNextAutoIncrement() {
- $this->out($this->_header(__FUNCTION__), true);
- $is = $this->User->getNextAutoIncrement();
- $this->out(returns($is));
- $schema = $this->User->schema('id');
- if ($schema['length'] == 36) {
- $this->assertFalse($is);
- } else {
- $this->assertTrue(is_int($is));
- }
- }
- public function testDeconstruct() {
- $data = array('year' => '2010', 'month' => '10', 'day' => 11);
- $res = $this->User->deconstruct('User.dob', $data);
- $this->assertEquals('2010-10-11', $res);
- $res = $this->User->deconstruct('User.dob', $data, 'datetime');
- $this->assertEquals('2010-10-11 00:00:00', $res);
- }
- /**
- * Test that strings are correctly escaped using '
- */
- public function testEscapeValue() {
- $res = $this->User->escapeValue(4);
- $this->assertSame(4, $res);
- $res = $this->User->escapeValue('4');
- $this->assertSame('4', $res);
- $res = $this->User->escapeValue('a');
- $this->assertSame('\'a\'', $res);
- $res = $this->User->escapeValue(true);
- $this->assertSame(1, $res);
- $res = $this->User->escapeValue(false);
- $this->assertSame(0, $res);
- $res = $this->User->escapeValue(null);
- $this->assertSame(null, $res);
- # comparison to cakes escapeField here (which use ` to escape)
- $res = $this->User->escapeField('dob');
- $this->assertSame('`User`.`dob`', $res);
- }
- public function testSaveAll() {
- $records = array(
- array('title' => 'x', 'body' => 'bx'),
- array('title' => 'y', 'body' => 'by'),
- );
- $result = $this->User->saveAll($records);
- $this->assertTrue($result);
- $result = $this->User->saveAll($records, array('atomic' => false));
- $this->assertTrue($result);
- $result = $this->User->saveAll($records, array('atomic' => false, 'returnArray' => true));
- $expected = array(true, true);
- $this->assertSame($expected, $result);
- }
- /**
- * Test deleteAllRaw()
- *
- * @return void
- */
- public function testDeleteAllRaw() {
- $result = $this->User->deleteAllRaw(array('user !=' => 'foo', 'created <' => date(FORMAT_DB_DATE), 'id >' => 1));
- $this->assertTrue($result);
- $result = $this->User->getAffectedRows();
- $this->assertIdentical(3, $result);
- $result = $this->User->deleteAllRaw();
- $this->assertTrue($result);
- $result = $this->User->getAffectedRows();
- $this->assertIdentical(1, $result);
- }
- /**
- * Test truncate
- */
- public function testTruncate() {
- $is = $this->User->find('count');
- $this->assertEquals(4, $is);
- $is = $this->User->getNextAutoIncrement();
- $this->assertEquals(5, $is);
- $is = $this->User->truncate();
- $is = $this->User->find('count');
- $this->assertEquals(0, $is);
- $is = $this->User->getNextAutoIncrement();
- $this->assertEquals(1, $is);
- }
- /**
- * Test that 2.x invalidates() can behave like 1.x invalidates()
- * and that you are able to abort on single errors (similar to using last=>true)
- *
- */
- public function testInvalidates() {
- $TestModel = new AppTestModel();
- $TestModel->validate = array(
- 'title' => array(
- 'tooShort' => array(
- 'rule' => array('minLength', 50),
- 'last' => false
- ),
- 'onlyLetters' => array('rule' => '/^[a-z]+$/i')
- ),
- );
- $data = array(
- 'title' => 'I am a short string'
- );
- $TestModel->create($data);
- $TestModel->invalidate('title', 'someCustomMessage');
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $result = $TestModel->validationErrors;
- $expected = array(
- 'title' => array('someCustomMessage', 'tooShort', 'onlyLetters')
- );
- $this->assertEquals($expected, $result);
- $result = $TestModel->validationErrors;
- $this->assertEquals($expected, $result);
- // invalidate a field with 'last' => true and stop further validation for this field
- $TestModel->create($data);
- $TestModel->invalidate('title', 'someCustomMessage', true);
- $result = $TestModel->validates();
- $this->assertFalse($result);
- $result = $TestModel->validationErrors;
- $expected = array(
- 'title' => array('someCustomMessage')
- );
- $this->assertEquals($expected, $result);
- $result = $TestModel->validationErrors;
- $this->assertEquals($expected, $result);
- }
- public function testValidateIdentical() {
- $this->out($this->_header(__FUNCTION__), true);
- $this->User->data = array($this->User->alias => array('y' => 'efg'));
- $is = $this->User->validateIdentical(array('x' => 'efg'), 'y');
- $this->assertTrue($is);
- $this->User->data = array($this->User->alias => array('y' => '2'));
- $is = $this->User->validateIdentical(array('x' => 2), 'y');
- $this->assertFalse($is);
- $this->User->data = array($this->User->alias => array('y' => '3'));
- $is = $this->User->validateIdentical(array('x' => 3), 'y', array('cast' => 'int'));
- $this->assertTrue($is);
- $this->User->data = array($this->User->alias => array('y' => '3'));
- $is = $this->User->validateIdentical(array('x' => 3), 'y', array('cast' => 'string'));
- $this->assertTrue($is);
- }
- public function testValidateKey() {
- $this->out($this->_header(__FUNCTION__), true);
- //$this->User->data = array($this->User->alias=>array('y'=>'efg'));
- $testModel = new AppTestModel();
- $is = $testModel->validateKey(array('id' => '2'));
- $this->assertFalse($is);
- $is = $testModel->validateKey(array('id' => 2));
- $this->assertFalse($is);
- $is = $testModel->validateKey(array('id' => '4e6f-a2f2-19a4ab957338'));
- $this->assertFalse($is);
- $is = $testModel->validateKey(array('id' => '4dff6725-f0e8-4e6f-a2f2-19a4ab957338'));
- $this->assertTrue($is);
- $is = $testModel->validateKey(array('id' => ''));
- $this->assertFalse($is);
- $is = $testModel->validateKey(array('id' => ''), array('allowEmpty' => true));
- $this->assertTrue($is);
- $is = $testModel->validateKey(array('foreign_id' => '2'));
- $this->assertTrue($is);
- $is = $testModel->validateKey(array('foreign_id' => 2));
- $this->assertTrue($is);
- $is = $testModel->validateKey(array('foreign_id' => 2.3));
- $this->assertFalse($is);
- $is = $testModel->validateKey(array('foreign_id' => -2));
- $this->assertFalse($is);
- $is = $testModel->validateKey(array('foreign_id' => '4dff6725-f0e8-4e6f-a2f2-19a4ab957338'));
- $this->assertFalse($is);
- $is = $testModel->validateKey(array('foreign_id' => 0));
- $this->assertFalse($is);
- $is = $testModel->validateKey(array('foreign_id' => 0), array('allowEmpty' => true));
- $this->assertTrue($is);
- }
- public function testValidateEnum() {
- $this->out($this->_header(__FUNCTION__), true);
- //$this->User->data = array($this->User->alias=>array('y'=>'efg'));
- $testModel = new AppTestModel();
- $is = $testModel->validateEnum(array('x' => '1'), true);
- $this->assertTrue($is);
- $is = $testModel->validateEnum(array('x' => '4'), true);
- $this->assertFalse($is);
- $is = $testModel->validateEnum(array('x' => '5'), true, array('4', '5'));
- $this->assertTrue($is);
- $is = $testModel->validateEnum(array('some_key' => '3'), 'x', array('4', '5'));
- $this->assertTrue($is);
- }
- public function testGuaranteeFields() {
- $this->out($this->_header(__FUNCTION__), true);
- $res = $this->User->guaranteeFields(array());
- //debug($res);
- $this->assertTrue(empty($res));
- $res = $this->User->guaranteeFields(array('x', 'y'));
- //debug($res);
- $this->assertTrue(!empty($res));
- $this->assertEquals($res, array($this->modelName => array('x' => '', 'y' => '')));
- $res = $this->User->guaranteeFields(array('x', 'OtherModel.y'));
- //debug($res);
- $this->assertTrue(!empty($res));
- $this->assertEquals($res, array($this->modelName => array('x' => ''), 'OtherModel' => array('y' => '')));
- }
- public function testSet() {
- $this->out($this->_header(__FUNCTION__), true);
- $data = array($this->modelName => array('x' => 'hey'), 'OtherModel' => array('y' => ''));
- $this->User->data = array();
- $res = $this->User->set($data, null, array('x', 'z'));
- $this->out($res);
- $this->assertTrue(!empty($res));
- $this->assertEquals($res, array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')));
- $res = $this->User->data;
- $this->out($res);
- $this->assertTrue(!empty($res));
- $this->assertEquals($res, array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')));
- }
- public function testValidateWithGuaranteeFields() {
- $this->out($this->_header(__FUNCTION__), true);
- $data = array($this->modelName => array('x' => 'hey'), 'OtherModel' => array('y' => ''));
- $data = $this->User->guaranteeFields(array('x', 'z'), $data);
- $this->out($data);
- $this->assertTrue(!empty($data));
- $this->assertEquals(array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')), $data);
- $res = $this->User->set($data);
- $this->out($res);
- $this->assertTrue(!empty($res));
- $this->assertEquals($res, array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')));
- }
- // not really working?
- public function testBlacklist() {
- $this->out($this->_header(__FUNCTION__), true);
- $data = array($this->modelName => array('name' => 'e', 'x' => 'hey'), 'OtherModel' => array('y' => ''));
- $schema = $this->User->schema();
- $data = $this->User->blacklist(array('x', 'z'));
- $this->out($data);
- if (!empty($schema)) {
- $this->assertTrue(!empty($data));
- } else {
- $this->assertTrue(empty($data));
- }
- //$this->assertEquals($data, array($this->modelName=>array('x'=>'hey', 'z'=>''), 'OtherModel'=>array('y'=>'')));
- }
- public function testInvalidate() {
- $this->out($this->_header(__FUNCTION__), true);
- $this->User->create();
- $this->User->invalidate('fieldx', __('e %s f', 33));
- $res = $this->User->validationErrors;
- $this->out($res);
- $this->assertTrue(!empty($res));
- $this->User->create();
- $this->User->invalidate('Model.fieldy', __('e %s f %s g', 33, 'xyz'));
- $res = $this->User->validationErrors;
- $this->out($res);
- $this->assertTrue(!empty($res) && $res['Model.fieldy'][0] === 'e 33 f xyz g');
- $this->User->create();
- $this->User->invalidate('fieldy', __('e %s f %s g %s', true, 'xyz', 55));
- $res = $this->User->validationErrors;
- $this->out($res);
- $this->assertTrue(!empty($res) && $res['fieldy'][0] === 'e 1 f xyz g 55');
- $this->User->create();
- $this->User->invalidate('fieldy', array('valErrMandatoryField'));
- $res = $this->User->validationErrors;
- $this->out($res);
- $this->assertTrue(!empty($res));
- $this->User->create();
- $this->User->invalidate('fieldy', 'valErrMandatoryField');
- $res = $this->User->validationErrors;
- $this->out($res);
- $this->assertTrue(!empty($res));
- $this->User->create();
- $this->User->invalidate('fieldy', __('a %s b %s c %s %s %s %s %s h %s', 1, 2, 3, 4, 5, 6, 7, 8));
- $res = $this->User->validationErrors;
- $this->out($res);
- $this->assertTrue(!empty($res) && $res['fieldy'][0] === 'a 1 b 2 c 3 4 5 6 7 h 8');
- }
- public function testValidateDate() {
- $this->out($this->_header(__FUNCTION__), true);
- $data = array('field' => '2010-01-22');
- $res = $this->User->validateDate($data);
- //debug($res);
- $this->assertTrue($res);
- $data = array('field' => '2010-02-29');
- $res = $this->User->validateDate($data);
- //debug($res);
- $this->assertFalse($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-22'));
- $data = array('field' => '2010-02-23 11:11:11');
- $res = $this->User->validateDate($data, array('after' => 'after'));
- //debug($res);
- $this->assertTrue($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-24 11:11:11'));
- $data = array('field' => '2010-02-23');
- $res = $this->User->validateDate($data, array('after' => 'after'));
- //debug($res);
- $this->assertFalse($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-25'));
- $data = array('field' => '2010-02-25');
- $res = $this->User->validateDate($data, array('after' => 'after'));
- //debug($res);
- $this->assertTrue($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-25'));
- $data = array('field' => '2010-02-25');
- $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 1));
- //debug($res);
- $this->assertFalse($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-24'));
- $data = array('field' => '2010-02-25');
- $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 2));
- //debug($res);
- $this->assertFalse($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-24'));
- $data = array('field' => '2010-02-25');
- $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 1));
- //debug($res);
- $this->assertTrue($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-24'));
- $data = array('field' => '2010-02-25');
- $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 2));
- //debug($res);
- $this->assertFalse($res);
- $this->User->data = array($this->User->alias => array('before' => '2010-02-24'));
- $data = array('field' => '2010-02-24');
- $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 1));
- //debug($res);
- $this->assertFalse($res);
- $this->User->data = array($this->User->alias => array('before' => '2010-02-25'));
- $data = array('field' => '2010-02-24');
- $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 1));
- //debug($res);
- $this->assertTrue($res);
- $this->User->data = array($this->User->alias => array('before' => '2010-02-25'));
- $data = array('field' => '2010-02-24');
- $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 2));
- //debug($res);
- $this->assertFalse($res);
- $this->User->data = array($this->User->alias => array('before' => '2010-02-26'));
- $data = array('field' => '2010-02-24');
- $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 2));
- //debug($res);
- $this->assertTrue($res);
- }
- public function testValidateDatetime() {
- $this->out($this->_header(__FUNCTION__), true);
- $data = array('field' => '2010-01-22 11:11:11');
- $res = $this->User->validateDatetime($data);
- //debug($res);
- $this->assertTrue($res);
- $data = array('field' => '2010-01-22 11:61:11');
- $res = $this->User->validateDatetime($data);
- //debug($res);
- $this->assertFalse($res);
- $data = array('field' => '2010-02-29 11:11:11');
- $res = $this->User->validateDatetime($data);
- //debug($res);
- $this->assertFalse($res);
- $data = array('field' => '');
- $res = $this->User->validateDatetime($data, array('allowEmpty' => true));
- //debug($res);
- $this->assertTrue($res);
- $data = array('field' => '0000-00-00 00:00:00');
- $res = $this->User->validateDatetime($data, array('allowEmpty' => true));
- //debug($res);
- $this->assertTrue($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-22 11:11:11'));
- $data = array('field' => '2010-02-23 11:11:11');
- $res = $this->User->validateDatetime($data, array('after' => 'after'));
- //debug($res);
- $this->assertTrue($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-24 11:11:11'));
- $data = array('field' => '2010-02-23 11:11:11');
- $res = $this->User->validateDatetime($data, array('after' => 'after'));
- //debug($res);
- $this->assertFalse($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:11'));
- $data = array('field' => '2010-02-23 11:11:11');
- $res = $this->User->validateDatetime($data, array('after' => 'after'));
- //debug($res);
- $this->assertFalse($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:11'));
- $data = array('field' => '2010-02-23 11:11:11');
- $res = $this->User->validateDatetime($data, array('after' => 'after', 'min' => 1));
- //debug($res);
- $this->assertFalse($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:11'));
- $data = array('field' => '2010-02-23 11:11:11');
- $res = $this->User->validateDatetime($data, array('after' => 'after', 'min' => 0));
- //debug($res);
- $this->assertTrue($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:10'));
- $data = array('field' => '2010-02-23 11:11:11');
- $res = $this->User->validateDatetime($data, array('after' => 'after'));
- //debug($res);
- $this->assertTrue($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:12'));
- $data = array('field' => '2010-02-23 11:11:11');
- $res = $this->User->validateDatetime($data, array('after' => 'after'));
- //debug($res);
- $this->assertFalse($res);
- }
- public function testValidateTime() {
- $this->out($this->_header(__FUNCTION__), true);
- $data = array('field' => '11:21:11');
- $res = $this->User->validateTime($data);
- //debug($res);
- $this->assertTrue($res);
- $data = array('field' => '11:71:11');
- $res = $this->User->validateTime($data);
- //debug($res);
- $this->assertFalse($res);
- $this->User->data = array($this->User->alias => array('before' => '2010-02-23 11:11:12'));
- $data = array('field' => '2010-02-23 11:11:11');
- $res = $this->User->validateTime($data, array('before' => 'before'));
- //debug($res);
- $this->assertTrue($res);
- $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:12'));
- $data = array('field' => '2010-02-23 11:11:11');
- $res = $this->User->validateTime($data, array('after' => 'after'));
- //debug($res);
- $this->assertFalse($res);
- }
- public function testValidateUrl() {
- $this->out($this->_header(__FUNCTION__), true);
- $data = array('field' => 'www.dereuromark.de');
- $res = $this->User->validateUrl($data, array('allowEmpty' => true));
- $this->assertTrue($res);
- $data = array('field' => 'www.xxxde');
- $res = $this->User->validateUrl($data, array('allowEmpty' => true));
- $this->assertFalse($res);
- $data = array('field' => 'www.dereuromark.de');
- $res = $this->User->validateUrl($data, array('allowEmpty' => true, 'autoComplete' => false));
- $this->assertFalse($res);
- $data = array('field' => 'http://www.dereuromark.de');
- $res = $this->User->validateUrl($data, array('allowEmpty' => true, 'autoComplete' => false));
- $this->assertTrue($res);
- $data = array('field' => 'www.dereuromark.de');
- $res = $this->User->validateUrl($data, array('strict' => true));
- $this->assertTrue($res); # aha
- $data = array('field' => 'http://www.dereuromark.de');
- $res = $this->User->validateUrl($data, array('strict' => false));
- $this->assertTrue($res);
- $this->skipIf(empty($_SERVER['HTTP_HOST']), 'No HTTP_HOST');
- $data = array('field' => 'http://xyz.de/some/link');
- $res = $this->User->validateUrl($data, array('deep' => false, 'sameDomain' => true));
- $this->assertFalse($res);
- $data = array('field' => '/some/link');
- $res = $this->User->validateUrl($data, array('deep' => false, 'autoComplete' => true));
- $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
- $data = array('field' => 'http://' . $_SERVER['HTTP_HOST'] . '/some/link');
- $res = $this->User->validateUrl($data, array('deep' => false));
- $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
- $data = array('field' => '/some/link');
- $res = $this->User->validateUrl($data, array('deep' => false, 'autoComplete' => false));
- $this->assertTrue((env('REMOTE_ADDR') !== '127.0.0.1') ? !$res : $res);
- //$this->skipIf(strpos($_SERVER['HTTP_HOST'], '.') === false, 'No online HTTP_HOST');
- $data = array('field' => '/some/link');
- $res = $this->User->validateUrl($data, array('deep' => false, 'sameDomain' => true));
- $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
- $data = array('field' => 'https://github.com/');
- $res = $this->User->validateUrl($data, array('deep' => false));
- $this->assertTrue($res);
- $data = array('field' => 'https://github.com/');
- $res = $this->User->validateUrl($data, array('deep' => true));
- $this->assertTrue($res);
- }
- public function testValidateUnique() {
- $this->out($this->_header(__FUNCTION__), true);
- $this->Post->validate['title'] = array(
- 'validateUnique' => array(
- 'rule' => 'validateUnique',
- 'message' => 'valErrRecordTitleExists'
- ),
- );
- $data = array(
- 'title' => 'abc',
- 'published' => 'N'
- );
- $this->Post->create($data);
- $res = $this->Post->validates();
- $this->assertTrue($res);
- $res = $this->Post->save($res, false);
- $this->assertTrue((bool)$res);
- $this->Post->create();
- $res = $this->Post->save($data);
- $this->assertFalse($res);
- $this->Post->validate['title'] = array(
- 'validateUnique' => array(
- 'rule' => array('validateUnique', array('published')),
- 'message' => 'valErrRecordTitleExists'
- ),
- );
- $data = array(
- 'title' => 'abc',
- 'published' => 'Y'
- );
- $this->Post->create($data);
- $res = $this->Post->validates();
- $this->assertTrue($res);
- $res = $this->Post->save($res, false);
- $this->assertTrue((bool)$res);
- $this->Post->create();
- $res = $this->Post->save($data);
- $this->assertFalse($res);
- }
- }
- class MyAppModelPost extends MyModel {
- public $name = 'Post';
- public $alias = 'Post';
- public $belongsTo = 'Author';
- }
- class MyAppModelUser extends MyModel {
- public $name = 'User';
- public $alias = 'User';
- }
- class AppTestModel extends MyModel {
- public $useTable = false;
- protected $_schema = array(
- 'id' => array (
- 'type' => 'string',
- 'null' => false,
- 'default' => '',
- 'length' => 36,
- 'key' => 'primary',
- 'collate' => 'utf8_unicode_ci',
- 'charset' => 'utf8',
- ),
- 'foreign_id' => array (
- 'type' => 'integer',
- 'null' => false,
- 'default' => '0',
- 'length' => 10,
- ),
- );
- public static function x() {
- return array('1' => 'x', '2' => 'y', '3' => 'z');
- }
- }
|