MyModelTest.php 30 KB

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