MyModelTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. $this->Post->validate['title'] = [
  791. 'validateUnique' => [
  792. 'rule' => ['validateUnique', ['published']],
  793. 'message' => 'valErrRecordTitleExists'
  794. ],
  795. ];
  796. $data = [
  797. 'title' => 'abc',
  798. 'published' => 'Y'
  799. ];
  800. $this->Post->create($data);
  801. $res = $this->Post->validates();
  802. $this->assertTrue($res);
  803. $res = $this->Post->save($res, ['validate' => false]);
  804. $this->assertTrue((bool)$res);
  805. $this->Post->create();
  806. $res = $this->Post->save($data);
  807. $this->assertFalse($res);
  808. }
  809. }
  810. class MyAppModelPost extends MyModel {
  811. public $name = 'Post';
  812. public $alias = 'Post';
  813. public $belongsTo = 'Author';
  814. }
  815. class MyAppModelUser extends MyModel {
  816. public $name = 'User';
  817. public $alias = 'User';
  818. }
  819. class AppTestModel extends MyModel {
  820. public $useTable = false;
  821. protected $_schema = [
  822. 'id' => [
  823. 'type' => 'string',
  824. 'null' => false,
  825. 'default' => '',
  826. 'length' => 36,
  827. 'key' => 'primary',
  828. 'collate' => 'utf8_unicode_ci',
  829. 'charset' => 'utf8',
  830. ],
  831. 'foreign_id' => [
  832. 'type' => 'integer',
  833. 'null' => false,
  834. 'default' => '0',
  835. 'length' => 10,
  836. ],
  837. ];
  838. public static function x() {
  839. return ['1' => 'x', '2' => 'y', '3' => 'z'];
  840. }
  841. }