MyModelTest.php 27 KB

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