MyModelTest.php 28 KB

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