MyModelTest.php 29 KB

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