MyModelTest.php 27 KB

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