MyModelTest.php 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. <?php
  2. App::uses('MyModel', 'Tools.Model');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class MyModelTest extends MyCakeTestCase {
  5. public $Post;
  6. public $User;
  7. public $modelName = 'User';
  8. public $fixtures = array('core.user', 'core.post', 'core.author');
  9. public function setUp() {
  10. parent::setUp();
  11. $this->Post = ClassRegistry::init('MyAppModelPost');
  12. $this->User = ClassRegistry::init('MyAppModelUser');
  13. }
  14. public function testObject() {
  15. $this->Post = ClassRegistry::init('MyModel');
  16. $this->assertTrue(is_object($this->Post));
  17. $this->assertInstanceOf('MyModel', $this->Post);
  18. }
  19. /**
  20. * MyModelTest::testGet()
  21. *
  22. * @return void
  23. */
  24. public function testGet() {
  25. $record = $this->Post->get(2);
  26. $this->assertEquals(2, $record['Post']['id']);
  27. $record = $this->Post->get(2, array('fields' => array('id', 'created')));
  28. $this->assertEquals(2, count($record['Post']));
  29. $record = $this->Post->get(2, array('fields' => array('id', 'title', 'body'), 'contain' => array('Author')));
  30. $this->assertEquals(3, count($record['Post']));
  31. $this->assertEquals(3, $record['Author']['id']);
  32. // BC
  33. $record = $this->Post->get(2, array('id', 'title', 'body'), array('Author'));
  34. $this->assertEquals(3, count($record['Post']));
  35. $this->assertEquals(3, $record['Author']['id']);
  36. }
  37. /**
  38. * MyModelTest::testGetRelatedInUse()
  39. *
  40. * @return void
  41. */
  42. public function testGetRelatedInUse() {
  43. $this->Post->Author->displayField = 'user';
  44. $results = $this->Post->getRelatedInUse('Author', 'author_id', 'list');
  45. $expected = array(1 => 'mariano', 3 => 'larry');
  46. $this->assertEquals($expected, $results);
  47. }
  48. /**
  49. * MyModelTest::testGetFieldInUse()
  50. *
  51. * @return void
  52. */
  53. public function testGetFieldInUse() {
  54. $this->db = ConnectionManager::getDataSource('test');
  55. $this->skipIf(!($this->db instanceof Mysql), 'The test is only compatible with Mysql.');
  56. $results = $this->Post->getFieldInUse('author_id', 'list');
  57. $expected = array(1 => 'First Post', 2 => 'Second Post');
  58. $this->assertEquals($expected, $results);
  59. }
  60. /**
  61. * MyModelTest::testEnum()
  62. *
  63. * @return void
  64. */
  65. public function testEnum() {
  66. $array = array(
  67. 1 => 'foo',
  68. 2 => 'bar',
  69. );
  70. $res = AppTestModel::enum(null, $array, false);
  71. $this->assertEquals($array, $res);
  72. $res = AppTestModel::enum(2, $array, false);
  73. $this->assertEquals('bar', $res);
  74. $res = AppTestModel::enum('2', $array, false);
  75. $this->assertEquals('bar', $res);
  76. $res = AppTestModel::enum(3, $array, false);
  77. $this->assertFalse($res);
  78. }
  79. /**
  80. * More tests in MyModel Test directly
  81. *
  82. * @return void
  83. */
  84. public function testGetFalse() {
  85. $this->User->order = array();
  86. $is = $this->User->get('xyz');
  87. $this->assertSame(array(), $is);
  88. }
  89. /**
  90. * Test auto inc value of the current table
  91. *
  92. * @return void
  93. */
  94. public function testGetNextAutoIncrement() {
  95. $this->db = ConnectionManager::getDataSource('test');
  96. $this->skipIf(!($this->db instanceof Mysql), 'The test is only compatible with Mysql.');
  97. $is = $this->User->getNextAutoIncrement();
  98. $this->out(returns($is));
  99. $schema = $this->User->schema('id');
  100. if ($schema['length'] == 36) {
  101. $this->assertFalse($is);
  102. } else {
  103. $this->assertTrue(is_int($is));
  104. }
  105. }
  106. /**
  107. * MyModelTest::testDeconstruct()
  108. *
  109. * @return void
  110. */
  111. public function testDeconstruct() {
  112. $data = array('year' => '2010', 'month' => '10', 'day' => 11);
  113. $res = $this->User->deconstruct('User.dob', $data);
  114. $this->assertEquals('2010-10-11', $res);
  115. $res = $this->User->deconstruct('User.dob', $data, 'datetime');
  116. $this->assertEquals('2010-10-11 00:00:00', $res);
  117. }
  118. /**
  119. * Test that strings are correctly escaped using '
  120. *
  121. * @return void
  122. */
  123. public function testEscapeValue() {
  124. $this->db = ConnectionManager::getDataSource('test');
  125. $this->skipIf(!($this->db instanceof Mysql), 'The test is only compatible with Mysql.');
  126. $res = $this->User->escapeValue(4);
  127. $this->assertSame(4, $res);
  128. $res = $this->User->escapeValue('4');
  129. $this->assertSame('4', $res);
  130. $res = $this->User->escapeValue('a');
  131. $this->assertSame('\'a\'', $res);
  132. $res = $this->User->escapeValue(true);
  133. $this->assertSame(1, $res);
  134. $res = $this->User->escapeValue(false);
  135. $this->assertSame(0, $res);
  136. $res = $this->User->escapeValue(null);
  137. $this->assertSame(null, $res);
  138. // comparison to cakes escapeField here (which use ` to escape)
  139. $res = $this->User->escapeField('dob');
  140. $this->assertSame('`User`.`dob`', $res);
  141. }
  142. /**
  143. * MyModelTest::testSaveAll()
  144. *
  145. * @return void
  146. */
  147. public function testSaveAll() {
  148. $records = array(
  149. array('title' => 'x', 'body' => 'bx'),
  150. array('title' => 'y', 'body' => 'by'),
  151. );
  152. $result = $this->User->saveAll($records);
  153. $this->assertTrue($result);
  154. $result = $this->User->saveAll($records, array('atomic' => false));
  155. $this->assertTrue($result);
  156. $result = $this->User->saveAll($records, array('atomic' => false, 'returnArray' => true));
  157. $expected = array(true, true);
  158. $this->assertSame($expected, $result);
  159. }
  160. /**
  161. * MyModelTest::testUpdateAllJoinless()
  162. *
  163. * @return void
  164. */
  165. public function testUpdateAllJoinless() {
  166. $db = ConnectionManager::getDataSource($this->Post->useDbConfig);
  167. $db->getLog();
  168. $postTable = $db->fullTableName($this->Post->table);
  169. $authorTable = $db->fullTableName($this->Post->Author->table);
  170. $result = $this->Post->updateAll(array('title' => '"Foo"'), array('title !=' => 'Foo'));
  171. $this->assertTrue($result);
  172. $queries = $db->getLog();
  173. $expected = 'UPDATE ' . $postTable . ' AS `Post` LEFT JOIN ' . $authorTable . ' AS `Author` ON (`Post`.`author_id` = `Author`.`id`) SET `Post`.`title` = "Foo" WHERE `title` != \'Foo\'';
  174. $this->assertSame($expected, $queries['log'][0]['query']);
  175. // Now joinless
  176. $result = $this->Post->updateAllJoinless(array('title' => '"Foo"'), array('title !=' => 'Foo'));
  177. $this->assertTrue($result);
  178. $queries = $db->getLog();
  179. $expected = 'UPDATE ' . $postTable . ' AS `Post` SET `Post`.`title` = "Foo" WHERE `title` != \'Foo\'';
  180. $this->assertSame($expected, $queries['log'][0]['query']);
  181. }
  182. /**
  183. * MyModelTest::testDeleteAll()
  184. *
  185. * @return void
  186. */
  187. public function testDeleteAll() {
  188. $db = ConnectionManager::getDataSource($this->Post->useDbConfig);
  189. $db->getLog();
  190. $postTable = $db->fullTableName($this->Post->table);
  191. $authorTable = $db->fullTableName($this->Post->Author->table);
  192. $result = $this->Post->deleteAll(array('title !=' => 'Foo'));
  193. $this->assertTrue($result);
  194. $queries = $db->getLog();
  195. $expected = 'SELECT `Post`.`id` FROM ' . $postTable . ' AS `Post` LEFT JOIN ' . $authorTable . ' AS `Author` ON (`Post`.`author_id` = `Author`.`id`) WHERE `title` != \'Foo\' GROUP BY `Post`.`id`';
  196. $this->assertSame($expected, $queries['log'][0]['query']);
  197. $expected = 'DELETE `Post` FROM ' . $postTable . ' AS `Post` WHERE `Post`.`id` IN';
  198. $this->assertContains($expected, $queries['log'][1]['query']);
  199. }
  200. /**
  201. * MyModelTest::testDeleteAllJoinless()
  202. *
  203. * @return void
  204. */
  205. public function testDeleteAllJoinless() {
  206. // Now joinless
  207. $db = ConnectionManager::getDataSource($this->Post->useDbConfig);
  208. $db->getLog();
  209. $postTable = $db->fullTableName($this->Post->table);
  210. $authorTable = $db->fullTableName($this->Post->Author->table);
  211. $result = $this->Post->deleteAllJoinless(array('title !=' => 'Foo'));
  212. $this->assertTrue($result);
  213. $queries = $db->getLog();
  214. $expected = 'SELECT `Post`.`id` FROM ' . $postTable . ' AS `Post` WHERE `title` != \'Foo\' GROUP BY `Post`.`id`';
  215. $this->assertSame($expected, $queries['log'][0]['query']);
  216. $expected = 'DELETE `Post` FROM ' . $postTable . ' AS `Post` WHERE `Post`.`id` IN';
  217. $this->assertContains($expected, $queries['log'][1]['query']);
  218. }
  219. /**
  220. * Test deleteAllRaw()
  221. *
  222. * @return void
  223. */
  224. public function testDeleteAllRaw() {
  225. $result = $this->User->deleteAllRaw(array('user !=' => 'foo', 'created <' => date(FORMAT_DB_DATE), 'id >' => 1));
  226. $this->assertTrue($result);
  227. $result = $this->User->getAffectedRows();
  228. $this->assertIdentical(3, $result);
  229. $result = $this->User->deleteAllRaw();
  230. $this->assertTrue($result);
  231. $result = $this->User->getAffectedRows();
  232. $this->assertIdentical(1, $result);
  233. }
  234. /**
  235. * Test truncate
  236. *
  237. * @return void
  238. */
  239. public function testTruncate() {
  240. $is = $this->User->find('count');
  241. $this->assertEquals(4, $is);
  242. $is = $this->User->getNextAutoIncrement();
  243. $this->assertEquals(5, $is);
  244. $is = $this->User->truncate();
  245. $is = $this->User->find('count');
  246. $this->assertEquals(0, $is);
  247. $is = $this->User->getNextAutoIncrement();
  248. $this->assertEquals(1, $is);
  249. }
  250. /**
  251. * Test that 2.x invalidates() can behave like 1.x invalidates()
  252. * and that you are able to abort on single errors (similar to using last=>true)
  253. *
  254. * @return void
  255. */
  256. public function testInvalidates() {
  257. $TestModel = new AppTestModel();
  258. $TestModel->validate = array(
  259. 'title' => array(
  260. 'tooShort' => array(
  261. 'rule' => array('minLength', 50),
  262. 'last' => false
  263. ),
  264. 'onlyLetters' => array('rule' => '/^[a-z]+$/i')
  265. ),
  266. );
  267. $data = array(
  268. 'title' => 'I am a short string'
  269. );
  270. $TestModel->create($data);
  271. $TestModel->invalidate('title', 'someCustomMessage');
  272. $result = $TestModel->validates();
  273. $this->assertFalse($result);
  274. $result = $TestModel->validationErrors;
  275. $expected = array(
  276. 'title' => array('someCustomMessage', 'tooShort', 'onlyLetters')
  277. );
  278. $this->assertEquals($expected, $result);
  279. $result = $TestModel->validationErrors;
  280. $this->assertEquals($expected, $result);
  281. // invalidate a field with 'last' => true and stop further validation for this field
  282. $TestModel->create($data);
  283. $TestModel->invalidate('title', 'someCustomMessage', true);
  284. $result = $TestModel->validates();
  285. $this->assertFalse($result);
  286. $result = $TestModel->validationErrors;
  287. $expected = array(
  288. 'title' => array('someCustomMessage')
  289. );
  290. $this->assertEquals($expected, $result);
  291. $result = $TestModel->validationErrors;
  292. $this->assertEquals($expected, $result);
  293. }
  294. /**
  295. * MyModelTest::testValidateRange()
  296. *
  297. * @return void
  298. */
  299. public function testValidateRange() {
  300. $is = $this->User->validateRange(array('range' => 2), 1, 3);
  301. $this->assertTrue($is);
  302. $is = $this->User->validateRange(array('range' => 2.4), 1.5, 2.3);
  303. $this->assertFalse($is);
  304. $is = $this->User->validateRange(array('range' => -5), -10, 1);
  305. $this->assertTrue($is);
  306. $is = $this->User->validateRange(array('range' => 'word'), 1.5, 2.3);
  307. $this->assertFalse($is);
  308. $is = $this->User->validateRange(array('range' => 5.1));
  309. $this->assertTrue($is);
  310. $is = $this->User->validateRange(array('range' => 2.1), 2.1, 3.2);
  311. $this->assertTrue($is);
  312. $is = $this->User->validateRange(array('range' => 3.2), 2.1, 3.2);
  313. $this->assertTrue($is);
  314. }
  315. /**
  316. * MyModelTest::testValidateIdentical()
  317. *
  318. * @return void
  319. */
  320. public function testValidateIdentical() {
  321. $this->User->data = array($this->User->alias => array('y' => 'efg'));
  322. $is = $this->User->validateIdentical(array('x' => 'efg'), 'y');
  323. $this->assertTrue($is);
  324. $this->User->data = array($this->User->alias => array('y' => '2'));
  325. $is = $this->User->validateIdentical(array('x' => 2), 'y');
  326. $this->assertFalse($is);
  327. $this->User->data = array($this->User->alias => array('y' => '3'));
  328. $is = $this->User->validateIdentical(array('x' => 3), 'y', array('cast' => 'int'));
  329. $this->assertTrue($is);
  330. $this->User->data = array($this->User->alias => array('y' => '3'));
  331. $is = $this->User->validateIdentical(array('x' => 3), 'y', array('cast' => 'string'));
  332. $this->assertTrue($is);
  333. }
  334. /**
  335. * MyModelTest::testValidateKey()
  336. *
  337. * @return void
  338. */
  339. public function testValidateKey() {
  340. //$this->User->data = array($this->User->alias=>array('y'=>'efg'));
  341. $testModel = new AppTestModel();
  342. $is = $testModel->validateKey(array('id' => '2'));
  343. $this->assertFalse($is);
  344. $is = $testModel->validateKey(array('id' => 2));
  345. $this->assertFalse($is);
  346. $is = $testModel->validateKey(array('id' => '4e6f-a2f2-19a4ab957338'));
  347. $this->assertFalse($is);
  348. $is = $testModel->validateKey(array('id' => '4dff6725-f0e8-4e6f-a2f2-19a4ab957338'));
  349. $this->assertTrue($is);
  350. $is = $testModel->validateKey(array('id' => ''));
  351. $this->assertFalse($is);
  352. $is = $testModel->validateKey(array('id' => ''), array('allowEmpty' => true));
  353. $this->assertTrue($is);
  354. $is = $testModel->validateKey(array('foreign_id' => '2'));
  355. $this->assertTrue($is);
  356. $is = $testModel->validateKey(array('foreign_id' => 2));
  357. $this->assertTrue($is);
  358. $is = $testModel->validateKey(array('foreign_id' => 2.3));
  359. $this->assertFalse($is);
  360. $is = $testModel->validateKey(array('foreign_id' => -2));
  361. $this->assertFalse($is);
  362. $is = $testModel->validateKey(array('foreign_id' => '4dff6725-f0e8-4e6f-a2f2-19a4ab957338'));
  363. $this->assertFalse($is);
  364. $is = $testModel->validateKey(array('foreign_id' => 0));
  365. $this->assertFalse($is);
  366. $is = $testModel->validateKey(array('foreign_id' => 0), array('allowEmpty' => true));
  367. $this->assertTrue($is);
  368. }
  369. /**
  370. * MyModelTest::testValidateEnum()
  371. *
  372. * @return void
  373. */
  374. public function testValidateEnum() {
  375. //$this->User->data = array($this->User->alias=>array('y'=>'efg'));
  376. $testModel = new AppTestModel();
  377. $is = $testModel->validateEnum(array('x' => '1'), true);
  378. $this->assertTrue($is);
  379. $is = $testModel->validateEnum(array('x' => '4'), true);
  380. $this->assertFalse($is);
  381. $is = $testModel->validateEnum(array('x' => '5'), true, array('4', '5'));
  382. $this->assertTrue($is);
  383. $is = $testModel->validateEnum(array('some_key' => '3'), 'x', array('4', '5'));
  384. $this->assertTrue($is);
  385. }
  386. /**
  387. * MyModelTest::testGuaranteeFields()
  388. *
  389. * @return void
  390. */
  391. public function testGuaranteeFields() {
  392. $res = $this->User->guaranteeFields(array());
  393. //debug($res);
  394. $this->assertTrue(empty($res));
  395. $res = $this->User->guaranteeFields(array('x', 'y'));
  396. //debug($res);
  397. $this->assertTrue(!empty($res));
  398. $this->assertEquals($res, array($this->modelName => array('x' => '', 'y' => '')));
  399. $res = $this->User->guaranteeFields(array('x', 'OtherModel.y'));
  400. //debug($res);
  401. $this->assertTrue(!empty($res));
  402. $this->assertEquals($res, array($this->modelName => array('x' => ''), 'OtherModel' => array('y' => '')));
  403. }
  404. /**
  405. * MyModelTest::testRequireFields()
  406. *
  407. * @return void
  408. */
  409. public function testRequireFields() {
  410. $this->User->requireFields(array('foo', 'bar'));
  411. $data = array(
  412. 'foo' => 'foo',
  413. );
  414. $this->User->set($data);
  415. $result = $this->User->validates();
  416. $this->assertFalse($result);
  417. $data = array(
  418. 'foo' => 'foo',
  419. 'bar' => '',
  420. );
  421. $this->User->set($data);
  422. $result = $this->User->validates();
  423. $this->assertTrue($result);
  424. // Allow field to be empty as long as it is present
  425. $this->User->requireFields(array('foo', 'test'), true);
  426. $data = array(
  427. 'foo' => 'foo',
  428. 'test' => ''
  429. );
  430. $this->User->set($data);
  431. $result = $this->User->validates();
  432. $this->assertTrue($result);
  433. }
  434. /**
  435. * MyModelTest::testSet()
  436. *
  437. * @return void
  438. */
  439. public function testSet() {
  440. $data = array($this->modelName => array('x' => 'hey'), 'OtherModel' => array('y' => ''));
  441. $this->User->data = array();
  442. $res = $this->User->set($data, null, array('x', 'z'));
  443. $this->out($res);
  444. $this->assertTrue(!empty($res));
  445. $this->assertEquals($res, array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')));
  446. $res = $this->User->data;
  447. $this->out($res);
  448. $this->assertTrue(!empty($res));
  449. $this->assertEquals($res, array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')));
  450. }
  451. /**
  452. * MyModelTest::testValidateWithGuaranteeFields()
  453. *
  454. * @return void
  455. */
  456. public function testValidateWithGuaranteeFields() {
  457. $data = array($this->modelName => array('x' => 'hey'), 'OtherModel' => array('y' => ''));
  458. $data = $this->User->guaranteeFields(array('x', 'z'), $data);
  459. $this->out($data);
  460. $this->assertTrue(!empty($data));
  461. $this->assertEquals(array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')), $data);
  462. $res = $this->User->set($data);
  463. $this->out($res);
  464. $this->assertTrue(!empty($res));
  465. $this->assertEquals($res, array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')));
  466. }
  467. public function testWhitelist() {
  468. $data = array(
  469. 'name' => 'foo',
  470. 'x' => 'y',
  471. 'z' => 'yes'
  472. );
  473. $this->User->set($data);
  474. $result = $this->User->whitelist(array('name', 'x'));
  475. $this->assertEquals(array('name', 'x'), array_keys($this->User->data['User']));
  476. }
  477. /**
  478. * MyModelTest::testBlacklist()
  479. * Note that one should always prefer a whitelist over a blacklist.
  480. *
  481. * @return void
  482. */
  483. public function testBlacklist() {
  484. $data = array(
  485. 'name' => 'foo',
  486. 'x' => 'y',
  487. 'z' => 'yes'
  488. );
  489. $this->User->set($data);
  490. $this->User->blacklist(array('x'));
  491. $this->assertEquals(array('name', 'z'), array_keys($this->User->data['User']));
  492. }
  493. /**
  494. * MyModelTest::testGenerateWhitelistFromBlacklist()
  495. *
  496. * @return void
  497. */
  498. public function testGenerateWhitelistFromBlacklist() {
  499. $result = $this->User->generateWhitelistFromBlacklist(array('password'));
  500. $expected = array('id', 'user', 'created', 'updated');
  501. $this->assertEquals($expected, array_values($expected));
  502. }
  503. /**
  504. * MyModelTest::testInvalidate()
  505. *
  506. * @return void
  507. */
  508. public function testInvalidate() {
  509. $this->User->create();
  510. $this->User->invalidate('fieldx', __d('tools', 'e %s f', 33));
  511. $res = $this->User->validationErrors;
  512. $this->out($res);
  513. $this->assertTrue(!empty($res));
  514. $this->User->create();
  515. $this->User->invalidate('Model.fieldy', __d('tools', 'e %s f %s g', 33, 'xyz'));
  516. $res = $this->User->validationErrors;
  517. $this->out($res);
  518. $this->assertTrue(!empty($res) && $res['Model.fieldy'][0] === 'e 33 f xyz g');
  519. $this->User->create();
  520. $this->User->invalidate('fieldy', __d('tools', 'e %s f %s g %s', true, 'xyz', 55));
  521. $res = $this->User->validationErrors;
  522. $this->out($res);
  523. $this->assertTrue(!empty($res) && $res['fieldy'][0] === 'e 1 f xyz g 55');
  524. $this->User->create();
  525. $this->User->invalidate('fieldy', array('valErrMandatoryField'));
  526. $res = $this->User->validationErrors;
  527. $this->out($res);
  528. $this->assertTrue(!empty($res));
  529. $this->User->create();
  530. $this->User->invalidate('fieldy', 'valErrMandatoryField');
  531. $res = $this->User->validationErrors;
  532. $this->out($res);
  533. $this->assertTrue(!empty($res));
  534. $this->User->create();
  535. $this->User->invalidate('fieldy', __d('tools', 'a %s b %s c %s %s %s %s %s h %s', 1, 2, 3, 4, 5, 6, 7, 8));
  536. $res = $this->User->validationErrors;
  537. $this->out($res);
  538. $this->assertTrue(!empty($res) && $res['fieldy'][0] === 'a 1 b 2 c 3 4 5 6 7 h 8');
  539. }
  540. /**
  541. * MyModelTest::testValidateDate()
  542. *
  543. * @return void
  544. */
  545. public function testValidateDate() {
  546. $data = array('field' => '2010-01-22');
  547. $res = $this->User->validateDate($data);
  548. //debug($res);
  549. $this->assertTrue($res);
  550. $data = array('field' => '2010-02-29');
  551. $res = $this->User->validateDate($data);
  552. //debug($res);
  553. $this->assertFalse($res);
  554. $this->User->data = array($this->User->alias => array('after' => '2010-02-22'));
  555. $data = array('field' => '2010-02-23 11:11:11');
  556. $res = $this->User->validateDate($data, array('after' => 'after'));
  557. //debug($res);
  558. $this->assertTrue($res);
  559. $this->User->data = array($this->User->alias => array('after' => '2010-02-24 11:11:11'));
  560. $data = array('field' => '2010-02-23');
  561. $res = $this->User->validateDate($data, array('after' => 'after'));
  562. //debug($res);
  563. $this->assertFalse($res);
  564. $this->User->data = array($this->User->alias => array('after' => '2010-02-25'));
  565. $data = array('field' => '2010-02-25');
  566. $res = $this->User->validateDate($data, array('after' => 'after'));
  567. //debug($res);
  568. $this->assertTrue($res);
  569. $this->User->data = array($this->User->alias => array('after' => '2010-02-25'));
  570. $data = array('field' => '2010-02-25');
  571. $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 1));
  572. //debug($res);
  573. $this->assertFalse($res);
  574. $this->User->data = array($this->User->alias => array('after' => '2010-02-24'));
  575. $data = array('field' => '2010-02-25');
  576. $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 2));
  577. //debug($res);
  578. $this->assertFalse($res);
  579. $this->User->data = array($this->User->alias => array('after' => '2010-02-24'));
  580. $data = array('field' => '2010-02-25');
  581. $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 1));
  582. //debug($res);
  583. $this->assertTrue($res);
  584. $this->User->data = array($this->User->alias => array('after' => '2010-02-24'));
  585. $data = array('field' => '2010-02-25');
  586. $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 2));
  587. //debug($res);
  588. $this->assertFalse($res);
  589. $this->User->data = array($this->User->alias => array('before' => '2010-02-24'));
  590. $data = array('field' => '2010-02-24');
  591. $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 1));
  592. //debug($res);
  593. $this->assertFalse($res);
  594. $this->User->data = array($this->User->alias => array('before' => '2010-02-25'));
  595. $data = array('field' => '2010-02-24');
  596. $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 1));
  597. //debug($res);
  598. $this->assertTrue($res);
  599. $this->User->data = array($this->User->alias => array('before' => '2010-02-25'));
  600. $data = array('field' => '2010-02-24');
  601. $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 2));
  602. //debug($res);
  603. $this->assertFalse($res);
  604. $this->User->data = array($this->User->alias => array('before' => '2010-02-26'));
  605. $data = array('field' => '2010-02-24');
  606. $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 2));
  607. //debug($res);
  608. $this->assertTrue($res);
  609. }
  610. /**
  611. * MyModelTest::testValidateDatetime()
  612. *
  613. * @return void
  614. */
  615. public function testValidateDatetime() {
  616. $data = array('field' => '2010-01-22 11:11:11');
  617. $res = $this->User->validateDatetime($data);
  618. //debug($res);
  619. $this->assertTrue($res);
  620. $data = array('field' => '2010-01-22 11:61:11');
  621. $res = $this->User->validateDatetime($data);
  622. //debug($res);
  623. $this->assertFalse($res);
  624. $data = array('field' => '2010-02-29 11:11:11');
  625. $res = $this->User->validateDatetime($data);
  626. //debug($res);
  627. $this->assertFalse($res);
  628. $data = array('field' => '');
  629. $res = $this->User->validateDatetime($data, array('allowEmpty' => true));
  630. //debug($res);
  631. $this->assertTrue($res);
  632. $data = array('field' => '0000-00-00 00:00:00');
  633. $res = $this->User->validateDatetime($data, array('allowEmpty' => true));
  634. //debug($res);
  635. $this->assertTrue($res);
  636. $this->User->data = array($this->User->alias => array('after' => '2010-02-22 11:11:11'));
  637. $data = array('field' => '2010-02-23 11:11:11');
  638. $res = $this->User->validateDatetime($data, array('after' => 'after'));
  639. //debug($res);
  640. $this->assertTrue($res);
  641. $this->User->data = array($this->User->alias => array('after' => '2010-02-24 11:11:11'));
  642. $data = array('field' => '2010-02-23 11:11:11');
  643. $res = $this->User->validateDatetime($data, array('after' => 'after'));
  644. //debug($res);
  645. $this->assertFalse($res);
  646. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:11'));
  647. $data = array('field' => '2010-02-23 11:11:11');
  648. $res = $this->User->validateDatetime($data, array('after' => 'after'));
  649. //debug($res);
  650. $this->assertFalse($res);
  651. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:11'));
  652. $data = array('field' => '2010-02-23 11:11:11');
  653. $res = $this->User->validateDatetime($data, array('after' => 'after', 'min' => 1));
  654. //debug($res);
  655. $this->assertFalse($res);
  656. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:11'));
  657. $data = array('field' => '2010-02-23 11:11:11');
  658. $res = $this->User->validateDatetime($data, array('after' => 'after', 'min' => 0));
  659. //debug($res);
  660. $this->assertTrue($res);
  661. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:10'));
  662. $data = array('field' => '2010-02-23 11:11:11');
  663. $res = $this->User->validateDatetime($data, array('after' => 'after'));
  664. //debug($res);
  665. $this->assertTrue($res);
  666. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:12'));
  667. $data = array('field' => '2010-02-23 11:11:11');
  668. $res = $this->User->validateDatetime($data, array('after' => 'after'));
  669. //debug($res);
  670. $this->assertFalse($res);
  671. }
  672. /**
  673. * MyModelTest::testValidateTime()
  674. *
  675. * @return void
  676. */
  677. public function testValidateTime() {
  678. $data = array('field' => '11:21:11');
  679. $res = $this->User->validateTime($data);
  680. //debug($res);
  681. $this->assertTrue($res);
  682. $data = array('field' => '11:71:11');
  683. $res = $this->User->validateTime($data);
  684. //debug($res);
  685. $this->assertFalse($res);
  686. $this->User->data = array($this->User->alias => array('before' => '2010-02-23 11:11:12'));
  687. $data = array('field' => '2010-02-23 11:11:11');
  688. $res = $this->User->validateTime($data, array('before' => 'before'));
  689. //debug($res);
  690. $this->assertTrue($res);
  691. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:12'));
  692. $data = array('field' => '2010-02-23 11:11:11');
  693. $res = $this->User->validateTime($data, array('after' => 'after'));
  694. //debug($res);
  695. $this->assertFalse($res);
  696. }
  697. /**
  698. * MyModelTest::testValidateUrl()
  699. *
  700. * @return void
  701. */
  702. public function testValidateUrl() {
  703. $data = array('field' => 'www.dereuromark.de');
  704. $res = $this->User->validateUrl($data, array('allowEmpty' => true));
  705. $this->assertTrue($res);
  706. $data = array('field' => 'www.xxxde');
  707. $res = $this->User->validateUrl($data, array('allowEmpty' => true));
  708. $this->assertFalse($res);
  709. $data = array('field' => 'www.dereuromark.de');
  710. $res = $this->User->validateUrl($data, array('allowEmpty' => true, 'autoComplete' => false));
  711. $this->assertFalse($res);
  712. $data = array('field' => 'http://www.dereuromark.de');
  713. $res = $this->User->validateUrl($data, array('allowEmpty' => true, 'autoComplete' => false));
  714. $this->assertTrue($res);
  715. $data = array('field' => 'www.dereuromark.de');
  716. $res = $this->User->validateUrl($data, array('strict' => true));
  717. $this->assertTrue($res); # aha
  718. $data = array('field' => 'http://www.dereuromark.de');
  719. $res = $this->User->validateUrl($data, array('strict' => false));
  720. $this->assertTrue($res);
  721. $this->skipIf(empty($_SERVER['HTTP_HOST']), 'No HTTP_HOST');
  722. $data = array('field' => 'http://xyz.de/some/link');
  723. $res = $this->User->validateUrl($data, array('deep' => false, 'sameDomain' => true));
  724. $this->assertFalse($res);
  725. $data = array('field' => '/some/link');
  726. $res = $this->User->validateUrl($data, array('deep' => false, 'autoComplete' => true));
  727. $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
  728. $data = array('field' => 'http://' . $_SERVER['HTTP_HOST'] . '/some/link');
  729. $res = $this->User->validateUrl($data, array('deep' => false));
  730. $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
  731. $data = array('field' => '/some/link');
  732. $res = $this->User->validateUrl($data, array('deep' => false, 'autoComplete' => false));
  733. $this->assertTrue((env('REMOTE_ADDR') !== '127.0.0.1') ? !$res : $res);
  734. //$this->skipIf(strpos($_SERVER['HTTP_HOST'], '.') === false, 'No online HTTP_HOST');
  735. $data = array('field' => '/some/link');
  736. $res = $this->User->validateUrl($data, array('deep' => false, 'sameDomain' => true));
  737. $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
  738. $data = array('field' => 'https://github.com/');
  739. $res = $this->User->validateUrl($data, array('deep' => false));
  740. $this->assertTrue($res);
  741. $data = array('field' => 'https://github.com/');
  742. $res = $this->User->validateUrl($data, array('deep' => true));
  743. $this->assertTrue($res);
  744. }
  745. /**
  746. * MyModelTest::testValidateUnique()
  747. *
  748. * @return void
  749. */
  750. public function testValidateUnique() {
  751. $this->Post->validate['title'] = array(
  752. 'validateUnique' => array(
  753. 'rule' => 'validateUnique',
  754. 'message' => 'valErrRecordTitleExists'
  755. ),
  756. );
  757. $data = array(
  758. 'title' => 'abc',
  759. 'published' => 'N'
  760. );
  761. $this->Post->create($data);
  762. $res = $this->Post->validates();
  763. $this->assertTrue($res);
  764. $res = $this->Post->save($res, array('validate' => false));
  765. $this->assertTrue((bool)$res);
  766. $this->Post->create();
  767. $res = $this->Post->save($data);
  768. $this->assertFalse($res);
  769. $this->Post->validate['title'] = array(
  770. 'validateUnique' => array(
  771. 'rule' => array('validateUnique', array('published')),
  772. 'message' => 'valErrRecordTitleExists'
  773. ),
  774. );
  775. $data = array(
  776. 'title' => 'abc',
  777. 'published' => 'Y'
  778. );
  779. $this->Post->create($data);
  780. $res = $this->Post->validates();
  781. $this->assertTrue($res);
  782. $res = $this->Post->save($res, array('validate' => false));
  783. $this->assertTrue((bool)$res);
  784. $this->Post->create();
  785. $res = $this->Post->save($data);
  786. $this->assertFalse($res);
  787. }
  788. }
  789. class MyAppModelPost extends MyModel {
  790. public $name = 'Post';
  791. public $alias = 'Post';
  792. public $belongsTo = 'Author';
  793. }
  794. class MyAppModelUser extends MyModel {
  795. public $name = 'User';
  796. public $alias = 'User';
  797. }
  798. class AppTestModel extends MyModel {
  799. public $useTable = false;
  800. protected $_schema = array(
  801. 'id' => array (
  802. 'type' => 'string',
  803. 'null' => false,
  804. 'default' => '',
  805. 'length' => 36,
  806. 'key' => 'primary',
  807. 'collate' => 'utf8_unicode_ci',
  808. 'charset' => 'utf8',
  809. ),
  810. 'foreign_id' => array (
  811. 'type' => 'integer',
  812. 'null' => false,
  813. 'default' => '0',
  814. 'length' => 10,
  815. ),
  816. );
  817. public static function x() {
  818. return array('1' => 'x', '2' => 'y', '3' => 'z');
  819. }
  820. }