MyModelTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. }
  252. /**
  253. * MyModelTest::testValidateIdentical()
  254. *
  255. * @return void
  256. */
  257. public function testValidateIdentical() {
  258. $this->out($this->_header(__FUNCTION__), true);
  259. $this->User->data = array($this->User->alias => array('y' => 'efg'));
  260. $is = $this->User->validateIdentical(array('x' => 'efg'), 'y');
  261. $this->assertTrue($is);
  262. $this->User->data = array($this->User->alias => array('y' => '2'));
  263. $is = $this->User->validateIdentical(array('x' => 2), 'y');
  264. $this->assertFalse($is);
  265. $this->User->data = array($this->User->alias => array('y' => '3'));
  266. $is = $this->User->validateIdentical(array('x' => 3), 'y', array('cast' => 'int'));
  267. $this->assertTrue($is);
  268. $this->User->data = array($this->User->alias => array('y' => '3'));
  269. $is = $this->User->validateIdentical(array('x' => 3), 'y', array('cast' => 'string'));
  270. $this->assertTrue($is);
  271. }
  272. /**
  273. * MyModelTest::testValidateKey()
  274. *
  275. * @return void
  276. */
  277. public function testValidateKey() {
  278. $this->out($this->_header(__FUNCTION__), true);
  279. //$this->User->data = array($this->User->alias=>array('y'=>'efg'));
  280. $testModel = new AppTestModel();
  281. $is = $testModel->validateKey(array('id' => '2'));
  282. $this->assertFalse($is);
  283. $is = $testModel->validateKey(array('id' => 2));
  284. $this->assertFalse($is);
  285. $is = $testModel->validateKey(array('id' => '4e6f-a2f2-19a4ab957338'));
  286. $this->assertFalse($is);
  287. $is = $testModel->validateKey(array('id' => '4dff6725-f0e8-4e6f-a2f2-19a4ab957338'));
  288. $this->assertTrue($is);
  289. $is = $testModel->validateKey(array('id' => ''));
  290. $this->assertFalse($is);
  291. $is = $testModel->validateKey(array('id' => ''), array('allowEmpty' => true));
  292. $this->assertTrue($is);
  293. $is = $testModel->validateKey(array('foreign_id' => '2'));
  294. $this->assertTrue($is);
  295. $is = $testModel->validateKey(array('foreign_id' => 2));
  296. $this->assertTrue($is);
  297. $is = $testModel->validateKey(array('foreign_id' => 2.3));
  298. $this->assertFalse($is);
  299. $is = $testModel->validateKey(array('foreign_id' => -2));
  300. $this->assertFalse($is);
  301. $is = $testModel->validateKey(array('foreign_id' => '4dff6725-f0e8-4e6f-a2f2-19a4ab957338'));
  302. $this->assertFalse($is);
  303. $is = $testModel->validateKey(array('foreign_id' => 0));
  304. $this->assertFalse($is);
  305. $is = $testModel->validateKey(array('foreign_id' => 0), array('allowEmpty' => true));
  306. $this->assertTrue($is);
  307. }
  308. /**
  309. * MyModelTest::testValidateEnum()
  310. *
  311. * @return void
  312. */
  313. public function testValidateEnum() {
  314. $this->out($this->_header(__FUNCTION__), true);
  315. //$this->User->data = array($this->User->alias=>array('y'=>'efg'));
  316. $testModel = new AppTestModel();
  317. $is = $testModel->validateEnum(array('x' => '1'), true);
  318. $this->assertTrue($is);
  319. $is = $testModel->validateEnum(array('x' => '4'), true);
  320. $this->assertFalse($is);
  321. $is = $testModel->validateEnum(array('x' => '5'), true, array('4', '5'));
  322. $this->assertTrue($is);
  323. $is = $testModel->validateEnum(array('some_key' => '3'), 'x', array('4', '5'));
  324. $this->assertTrue($is);
  325. }
  326. /**
  327. * MyModelTest::testGuaranteeFields()
  328. *
  329. * @return void
  330. */
  331. public function testGuaranteeFields() {
  332. $this->out($this->_header(__FUNCTION__), true);
  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. $this->out($this->_header(__FUNCTION__), true);
  382. $data = array($this->modelName => array('x' => 'hey'), 'OtherModel' => array('y' => ''));
  383. $this->User->data = array();
  384. $res = $this->User->set($data, null, array('x', 'z'));
  385. $this->out($res);
  386. $this->assertTrue(!empty($res));
  387. $this->assertEquals($res, array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')));
  388. $res = $this->User->data;
  389. $this->out($res);
  390. $this->assertTrue(!empty($res));
  391. $this->assertEquals($res, array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')));
  392. }
  393. /**
  394. * MyModelTest::testValidateWithGuaranteeFields()
  395. *
  396. * @return void
  397. */
  398. public function testValidateWithGuaranteeFields() {
  399. $this->out($this->_header(__FUNCTION__), true);
  400. $data = array($this->modelName => array('x' => 'hey'), 'OtherModel' => array('y' => ''));
  401. $data = $this->User->guaranteeFields(array('x', 'z'), $data);
  402. $this->out($data);
  403. $this->assertTrue(!empty($data));
  404. $this->assertEquals(array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')), $data);
  405. $res = $this->User->set($data);
  406. $this->out($res);
  407. $this->assertTrue(!empty($res));
  408. $this->assertEquals($res, array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')));
  409. }
  410. public function testWhitelist() {
  411. $data = array(
  412. 'name' => 'foo',
  413. 'x' => 'y',
  414. 'z' => 'yes'
  415. );
  416. $this->User->set($data);
  417. $result = $this->User->whitelist(array('name', 'x'));
  418. $this->assertEquals(array('name', 'x'), array_keys($this->User->data['User']));
  419. }
  420. /**
  421. * MyModelTest::testBlacklist()
  422. * Note that one should always prefer a whitelist over a blacklist.
  423. *
  424. * @return void
  425. */
  426. public function testBlacklist() {
  427. $data = array(
  428. 'name' => 'foo',
  429. 'x' => 'y',
  430. 'z' => 'yes'
  431. );
  432. $this->User->set($data);
  433. $this->User->blacklist(array('x'));
  434. $this->assertEquals(array('name', 'z'), array_keys($this->User->data['User']));
  435. }
  436. /**
  437. * MyModelTest::testGenerateWhitelistFromBlacklist()
  438. *
  439. * @return void
  440. */
  441. public function testGenerateWhitelistFromBlacklist() {
  442. $result = $this->User->generateWhitelistFromBlacklist(array('password'));
  443. $expected = array('id', 'user', 'created', 'updated');
  444. $this->assertEquals($expected, array_values($expected));
  445. }
  446. /**
  447. * MyModelTest::testInvalidate()
  448. *
  449. * @return void
  450. */
  451. public function testInvalidate() {
  452. $this->out($this->_header(__FUNCTION__), true);
  453. $this->User->create();
  454. $this->User->invalidate('fieldx', __('e %s f', 33));
  455. $res = $this->User->validationErrors;
  456. $this->out($res);
  457. $this->assertTrue(!empty($res));
  458. $this->User->create();
  459. $this->User->invalidate('Model.fieldy', __('e %s f %s g', 33, 'xyz'));
  460. $res = $this->User->validationErrors;
  461. $this->out($res);
  462. $this->assertTrue(!empty($res) && $res['Model.fieldy'][0] === 'e 33 f xyz g');
  463. $this->User->create();
  464. $this->User->invalidate('fieldy', __('e %s f %s g %s', true, 'xyz', 55));
  465. $res = $this->User->validationErrors;
  466. $this->out($res);
  467. $this->assertTrue(!empty($res) && $res['fieldy'][0] === 'e 1 f xyz g 55');
  468. $this->User->create();
  469. $this->User->invalidate('fieldy', array('valErrMandatoryField'));
  470. $res = $this->User->validationErrors;
  471. $this->out($res);
  472. $this->assertTrue(!empty($res));
  473. $this->User->create();
  474. $this->User->invalidate('fieldy', 'valErrMandatoryField');
  475. $res = $this->User->validationErrors;
  476. $this->out($res);
  477. $this->assertTrue(!empty($res));
  478. $this->User->create();
  479. $this->User->invalidate('fieldy', __('a %s b %s c %s %s %s %s %s h %s', 1, 2, 3, 4, 5, 6, 7, 8));
  480. $res = $this->User->validationErrors;
  481. $this->out($res);
  482. $this->assertTrue(!empty($res) && $res['fieldy'][0] === 'a 1 b 2 c 3 4 5 6 7 h 8');
  483. }
  484. /**
  485. * MyModelTest::testValidateDate()
  486. *
  487. * @return void
  488. */
  489. public function testValidateDate() {
  490. $this->out($this->_header(__FUNCTION__), true);
  491. $data = array('field' => '2010-01-22');
  492. $res = $this->User->validateDate($data);
  493. //debug($res);
  494. $this->assertTrue($res);
  495. $data = array('field' => '2010-02-29');
  496. $res = $this->User->validateDate($data);
  497. //debug($res);
  498. $this->assertFalse($res);
  499. $this->User->data = array($this->User->alias => array('after' => '2010-02-22'));
  500. $data = array('field' => '2010-02-23 11:11:11');
  501. $res = $this->User->validateDate($data, array('after' => 'after'));
  502. //debug($res);
  503. $this->assertTrue($res);
  504. $this->User->data = array($this->User->alias => array('after' => '2010-02-24 11:11:11'));
  505. $data = array('field' => '2010-02-23');
  506. $res = $this->User->validateDate($data, array('after' => 'after'));
  507. //debug($res);
  508. $this->assertFalse($res);
  509. $this->User->data = array($this->User->alias => array('after' => '2010-02-25'));
  510. $data = array('field' => '2010-02-25');
  511. $res = $this->User->validateDate($data, array('after' => 'after'));
  512. //debug($res);
  513. $this->assertTrue($res);
  514. $this->User->data = array($this->User->alias => array('after' => '2010-02-25'));
  515. $data = array('field' => '2010-02-25');
  516. $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 1));
  517. //debug($res);
  518. $this->assertFalse($res);
  519. $this->User->data = array($this->User->alias => array('after' => '2010-02-24'));
  520. $data = array('field' => '2010-02-25');
  521. $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 2));
  522. //debug($res);
  523. $this->assertFalse($res);
  524. $this->User->data = array($this->User->alias => array('after' => '2010-02-24'));
  525. $data = array('field' => '2010-02-25');
  526. $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 1));
  527. //debug($res);
  528. $this->assertTrue($res);
  529. $this->User->data = array($this->User->alias => array('after' => '2010-02-24'));
  530. $data = array('field' => '2010-02-25');
  531. $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 2));
  532. //debug($res);
  533. $this->assertFalse($res);
  534. $this->User->data = array($this->User->alias => array('before' => '2010-02-24'));
  535. $data = array('field' => '2010-02-24');
  536. $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 1));
  537. //debug($res);
  538. $this->assertFalse($res);
  539. $this->User->data = array($this->User->alias => array('before' => '2010-02-25'));
  540. $data = array('field' => '2010-02-24');
  541. $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 1));
  542. //debug($res);
  543. $this->assertTrue($res);
  544. $this->User->data = array($this->User->alias => array('before' => '2010-02-25'));
  545. $data = array('field' => '2010-02-24');
  546. $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 2));
  547. //debug($res);
  548. $this->assertFalse($res);
  549. $this->User->data = array($this->User->alias => array('before' => '2010-02-26'));
  550. $data = array('field' => '2010-02-24');
  551. $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 2));
  552. //debug($res);
  553. $this->assertTrue($res);
  554. }
  555. /**
  556. * MyModelTest::testValidateDatetime()
  557. *
  558. * @return void
  559. */
  560. public function testValidateDatetime() {
  561. $this->out($this->_header(__FUNCTION__), true);
  562. $data = array('field' => '2010-01-22 11:11:11');
  563. $res = $this->User->validateDatetime($data);
  564. //debug($res);
  565. $this->assertTrue($res);
  566. $data = array('field' => '2010-01-22 11:61:11');
  567. $res = $this->User->validateDatetime($data);
  568. //debug($res);
  569. $this->assertFalse($res);
  570. $data = array('field' => '2010-02-29 11:11:11');
  571. $res = $this->User->validateDatetime($data);
  572. //debug($res);
  573. $this->assertFalse($res);
  574. $data = array('field' => '');
  575. $res = $this->User->validateDatetime($data, array('allowEmpty' => true));
  576. //debug($res);
  577. $this->assertTrue($res);
  578. $data = array('field' => '0000-00-00 00:00:00');
  579. $res = $this->User->validateDatetime($data, array('allowEmpty' => true));
  580. //debug($res);
  581. $this->assertTrue($res);
  582. $this->User->data = array($this->User->alias => array('after' => '2010-02-22 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->assertTrue($res);
  587. $this->User->data = array($this->User->alias => array('after' => '2010-02-24 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'));
  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' => 1));
  600. //debug($res);
  601. $this->assertFalse($res);
  602. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:11'));
  603. $data = array('field' => '2010-02-23 11:11:11');
  604. $res = $this->User->validateDatetime($data, array('after' => 'after', 'min' => 0));
  605. //debug($res);
  606. $this->assertTrue($res);
  607. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:10'));
  608. $data = array('field' => '2010-02-23 11:11:11');
  609. $res = $this->User->validateDatetime($data, array('after' => 'after'));
  610. //debug($res);
  611. $this->assertTrue($res);
  612. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:12'));
  613. $data = array('field' => '2010-02-23 11:11:11');
  614. $res = $this->User->validateDatetime($data, array('after' => 'after'));
  615. //debug($res);
  616. $this->assertFalse($res);
  617. }
  618. /**
  619. * MyModelTest::testValidateTime()
  620. *
  621. * @return void
  622. */
  623. public function testValidateTime() {
  624. $this->out($this->_header(__FUNCTION__), true);
  625. $data = array('field' => '11:21:11');
  626. $res = $this->User->validateTime($data);
  627. //debug($res);
  628. $this->assertTrue($res);
  629. $data = array('field' => '11:71:11');
  630. $res = $this->User->validateTime($data);
  631. //debug($res);
  632. $this->assertFalse($res);
  633. $this->User->data = array($this->User->alias => array('before' => '2010-02-23 11:11:12'));
  634. $data = array('field' => '2010-02-23 11:11:11');
  635. $res = $this->User->validateTime($data, array('before' => 'before'));
  636. //debug($res);
  637. $this->assertTrue($res);
  638. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:12'));
  639. $data = array('field' => '2010-02-23 11:11:11');
  640. $res = $this->User->validateTime($data, array('after' => 'after'));
  641. //debug($res);
  642. $this->assertFalse($res);
  643. }
  644. /**
  645. * MyModelTest::testValidateUrl()
  646. *
  647. * @return void
  648. */
  649. public function testValidateUrl() {
  650. $this->out($this->_header(__FUNCTION__), true);
  651. $data = array('field' => 'www.dereuromark.de');
  652. $res = $this->User->validateUrl($data, array('allowEmpty' => true));
  653. $this->assertTrue($res);
  654. $data = array('field' => 'www.xxxde');
  655. $res = $this->User->validateUrl($data, array('allowEmpty' => true));
  656. $this->assertFalse($res);
  657. $data = array('field' => 'www.dereuromark.de');
  658. $res = $this->User->validateUrl($data, array('allowEmpty' => true, 'autoComplete' => false));
  659. $this->assertFalse($res);
  660. $data = array('field' => 'http://www.dereuromark.de');
  661. $res = $this->User->validateUrl($data, array('allowEmpty' => true, 'autoComplete' => false));
  662. $this->assertTrue($res);
  663. $data = array('field' => 'www.dereuromark.de');
  664. $res = $this->User->validateUrl($data, array('strict' => true));
  665. $this->assertTrue($res); # aha
  666. $data = array('field' => 'http://www.dereuromark.de');
  667. $res = $this->User->validateUrl($data, array('strict' => false));
  668. $this->assertTrue($res);
  669. $this->skipIf(empty($_SERVER['HTTP_HOST']), 'No HTTP_HOST');
  670. $data = array('field' => 'http://xyz.de/some/link');
  671. $res = $this->User->validateUrl($data, array('deep' => false, 'sameDomain' => true));
  672. $this->assertFalse($res);
  673. $data = array('field' => '/some/link');
  674. $res = $this->User->validateUrl($data, array('deep' => false, 'autoComplete' => true));
  675. $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
  676. $data = array('field' => 'http://' . $_SERVER['HTTP_HOST'] . '/some/link');
  677. $res = $this->User->validateUrl($data, array('deep' => false));
  678. $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
  679. $data = array('field' => '/some/link');
  680. $res = $this->User->validateUrl($data, array('deep' => false, 'autoComplete' => false));
  681. $this->assertTrue((env('REMOTE_ADDR') !== '127.0.0.1') ? !$res : $res);
  682. //$this->skipIf(strpos($_SERVER['HTTP_HOST'], '.') === false, 'No online HTTP_HOST');
  683. $data = array('field' => '/some/link');
  684. $res = $this->User->validateUrl($data, array('deep' => false, 'sameDomain' => true));
  685. $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
  686. $data = array('field' => 'https://github.com/');
  687. $res = $this->User->validateUrl($data, array('deep' => false));
  688. $this->assertTrue($res);
  689. $data = array('field' => 'https://github.com/');
  690. $res = $this->User->validateUrl($data, array('deep' => true));
  691. $this->assertTrue($res);
  692. }
  693. /**
  694. * MyModelTest::testValidateUnique()
  695. *
  696. * @return void
  697. */
  698. public function testValidateUnique() {
  699. $this->out($this->_header(__FUNCTION__), true);
  700. $this->Post->validate['title'] = array(
  701. 'validateUnique' => array(
  702. 'rule' => 'validateUnique',
  703. 'message' => 'valErrRecordTitleExists'
  704. ),
  705. );
  706. $data = array(
  707. 'title' => 'abc',
  708. 'published' => 'N'
  709. );
  710. $this->Post->create($data);
  711. $res = $this->Post->validates();
  712. $this->assertTrue($res);
  713. $res = $this->Post->save($res, array('validate' => false));
  714. $this->assertTrue((bool)$res);
  715. $this->Post->create();
  716. $res = $this->Post->save($data);
  717. $this->assertFalse($res);
  718. $this->Post->validate['title'] = array(
  719. 'validateUnique' => array(
  720. 'rule' => array('validateUnique', array('published')),
  721. 'message' => 'valErrRecordTitleExists'
  722. ),
  723. );
  724. $data = array(
  725. 'title' => 'abc',
  726. 'published' => 'Y'
  727. );
  728. $this->Post->create($data);
  729. $res = $this->Post->validates();
  730. $this->assertTrue($res);
  731. $res = $this->Post->save($res, array('validate' => false));
  732. $this->assertTrue((bool)$res);
  733. $this->Post->create();
  734. $res = $this->Post->save($data);
  735. $this->assertFalse($res);
  736. }
  737. }
  738. class MyAppModelPost extends MyModel {
  739. public $name = 'Post';
  740. public $alias = 'Post';
  741. public $belongsTo = 'Author';
  742. }
  743. class MyAppModelUser extends MyModel {
  744. public $name = 'User';
  745. public $alias = 'User';
  746. }
  747. class AppTestModel extends MyModel {
  748. public $useTable = false;
  749. protected $_schema = array(
  750. 'id' => array (
  751. 'type' => 'string',
  752. 'null' => false,
  753. 'default' => '',
  754. 'length' => 36,
  755. 'key' => 'primary',
  756. 'collate' => 'utf8_unicode_ci',
  757. 'charset' => 'utf8',
  758. ),
  759. 'foreign_id' => array (
  760. 'type' => 'integer',
  761. 'null' => false,
  762. 'default' => '0',
  763. 'length' => 10,
  764. ),
  765. );
  766. public static function x() {
  767. return array('1' => 'x', '2' => 'y', '3' => 'z');
  768. }
  769. }