MyModelTest.php 30 KB

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