MyModelTest.php 26 KB

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