MyModelTest.php 26 KB

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