MyModelTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. public function testGet() {
  20. $record = $this->Post->get(2);
  21. $this->assertEquals(2, $record['Post']['id']);
  22. $record = $this->Post->get(2, array('fields' => 'id', 'created'));
  23. $this->assertEquals(2, count($record['Post']));
  24. $record = $this->Post->get(2, array('fields' => 'id', 'title', 'body'), array('Author'));
  25. $this->assertEquals(3, $record['Author']['id']);
  26. }
  27. public function testEnum() {
  28. $array = array(
  29. 1 => 'foo',
  30. 2 => 'bar',
  31. );
  32. $res = AppTestModel::enum(null, $array, false);
  33. $this->assertEquals($array, $res);
  34. $res = AppTestModel::enum(2, $array, false);
  35. $this->assertEquals('bar', $res);
  36. $res = AppTestModel::enum('2', $array, false);
  37. $this->assertEquals('bar', $res);
  38. $res = AppTestModel::enum(3, $array, false);
  39. $this->assertFalse($res);
  40. }
  41. /**
  42. * More tests in MyModel Test directly
  43. */
  44. public function testGetFalse() {
  45. $this->User->order = array();
  46. $is = $this->User->get('xyz');
  47. $this->assertSame(array(), $is);
  48. }
  49. /**
  50. * Test auto inc value of the current table
  51. */
  52. public function testGetNextAutoIncrement() {
  53. $this->out($this->_header(__FUNCTION__), true);
  54. $is = $this->User->getNextAutoIncrement();
  55. $this->out(returns($is));
  56. $schema = $this->User->schema('id');
  57. if ($schema['length'] == 36) {
  58. $this->assertFalse($is);
  59. } else {
  60. $this->assertTrue(is_int($is));
  61. }
  62. }
  63. public function testDeconstruct() {
  64. $data = array('year' => '2010', 'month' => '10', 'day' => 11);
  65. $res = $this->User->deconstruct('User.dob', $data);
  66. $this->assertEquals('2010-10-11', $res);
  67. $res = $this->User->deconstruct('User.dob', $data, 'datetime');
  68. $this->assertEquals('2010-10-11 00:00:00', $res);
  69. }
  70. /**
  71. * Test that strings are correctly escaped using '
  72. */
  73. public function testEscapeValue() {
  74. $res = $this->User->escapeValue(4);
  75. $this->assertSame(4, $res);
  76. $res = $this->User->escapeValue('4');
  77. $this->assertSame('4', $res);
  78. $res = $this->User->escapeValue('a');
  79. $this->assertSame('\'a\'', $res);
  80. $res = $this->User->escapeValue(true);
  81. $this->assertSame(1, $res);
  82. $res = $this->User->escapeValue(false);
  83. $this->assertSame(0, $res);
  84. $res = $this->User->escapeValue(null);
  85. $this->assertSame(null, $res);
  86. # comparison to cakes escapeField here (which use ` to escape)
  87. $res = $this->User->escapeField('dob');
  88. $this->assertSame('`User`.`dob`', $res);
  89. }
  90. public function testSaveAll() {
  91. $records = array(
  92. array('title' => 'x', 'body' => 'bx'),
  93. array('title' => 'y', 'body' => 'by'),
  94. );
  95. $result = $this->User->saveAll($records);
  96. $this->assertTrue($result);
  97. $result = $this->User->saveAll($records, array('atomic' => false));
  98. $this->assertTrue($result);
  99. $result = $this->User->saveAll($records, array('atomic' => false, 'returnArray' => true));
  100. $expected = array(true, true);
  101. $this->assertSame($expected, $result);
  102. }
  103. /**
  104. * Test deleteAllRaw()
  105. *
  106. * @return void
  107. */
  108. public function testDeleteAllRaw() {
  109. $result = $this->User->deleteAllRaw(array('user !=' => 'foo', 'created <' => date(FORMAT_DB_DATE), 'id >' => 1));
  110. $this->assertTrue($result);
  111. $result = $this->User->getAffectedRows();
  112. $this->assertIdentical(3, $result);
  113. $result = $this->User->deleteAllRaw();
  114. $this->assertTrue($result);
  115. $result = $this->User->getAffectedRows();
  116. $this->assertIdentical(1, $result);
  117. }
  118. /**
  119. * Test truncate
  120. */
  121. public function testTruncate() {
  122. $is = $this->User->find('count');
  123. $this->assertEquals(4, $is);
  124. $is = $this->User->getNextAutoIncrement();
  125. $this->assertEquals(5, $is);
  126. $is = $this->User->truncate();
  127. $is = $this->User->find('count');
  128. $this->assertEquals(0, $is);
  129. $is = $this->User->getNextAutoIncrement();
  130. $this->assertEquals(1, $is);
  131. }
  132. /**
  133. * Test that 2.x invalidates() can behave like 1.x invalidates()
  134. * and that you are able to abort on single errors (similar to using last=>true)
  135. *
  136. */
  137. public function testInvalidates() {
  138. $TestModel = new AppTestModel();
  139. $TestModel->validate = array(
  140. 'title' => array(
  141. 'tooShort' => array(
  142. 'rule' => array('minLength', 50),
  143. 'last' => false
  144. ),
  145. 'onlyLetters' => array('rule' => '/^[a-z]+$/i')
  146. ),
  147. );
  148. $data = array(
  149. 'title' => 'I am a short string'
  150. );
  151. $TestModel->create($data);
  152. $TestModel->invalidate('title', 'someCustomMessage');
  153. $result = $TestModel->validates();
  154. $this->assertFalse($result);
  155. $result = $TestModel->validationErrors;
  156. $expected = array(
  157. 'title' => array('someCustomMessage', 'tooShort', 'onlyLetters')
  158. );
  159. $this->assertEquals($expected, $result);
  160. $result = $TestModel->validationErrors;
  161. $this->assertEquals($expected, $result);
  162. // invalidate a field with 'last' => true and stop further validation for this field
  163. $TestModel->create($data);
  164. $TestModel->invalidate('title', 'someCustomMessage', true);
  165. $result = $TestModel->validates();
  166. $this->assertFalse($result);
  167. $result = $TestModel->validationErrors;
  168. $expected = array(
  169. 'title' => array('someCustomMessage')
  170. );
  171. $this->assertEquals($expected, $result);
  172. $result = $TestModel->validationErrors;
  173. $this->assertEquals($expected, $result);
  174. }
  175. public function testValidateIdentical() {
  176. $this->out($this->_header(__FUNCTION__), true);
  177. $this->User->data = array($this->User->alias => array('y' => 'efg'));
  178. $is = $this->User->validateIdentical(array('x' => 'efg'), 'y');
  179. $this->assertTrue($is);
  180. $this->User->data = array($this->User->alias => array('y' => '2'));
  181. $is = $this->User->validateIdentical(array('x' => 2), 'y');
  182. $this->assertFalse($is);
  183. $this->User->data = array($this->User->alias => array('y' => '3'));
  184. $is = $this->User->validateIdentical(array('x' => 3), 'y', array('cast' => 'int'));
  185. $this->assertTrue($is);
  186. $this->User->data = array($this->User->alias => array('y' => '3'));
  187. $is = $this->User->validateIdentical(array('x' => 3), 'y', array('cast' => 'string'));
  188. $this->assertTrue($is);
  189. }
  190. public function testValidateKey() {
  191. $this->out($this->_header(__FUNCTION__), true);
  192. //$this->User->data = array($this->User->alias=>array('y'=>'efg'));
  193. $testModel = new AppTestModel();
  194. $is = $testModel->validateKey(array('id' => '2'));
  195. $this->assertFalse($is);
  196. $is = $testModel->validateKey(array('id' => 2));
  197. $this->assertFalse($is);
  198. $is = $testModel->validateKey(array('id' => '4e6f-a2f2-19a4ab957338'));
  199. $this->assertFalse($is);
  200. $is = $testModel->validateKey(array('id' => '4dff6725-f0e8-4e6f-a2f2-19a4ab957338'));
  201. $this->assertTrue($is);
  202. $is = $testModel->validateKey(array('id' => ''));
  203. $this->assertFalse($is);
  204. $is = $testModel->validateKey(array('id' => ''), array('allowEmpty' => true));
  205. $this->assertTrue($is);
  206. $is = $testModel->validateKey(array('foreign_id' => '2'));
  207. $this->assertTrue($is);
  208. $is = $testModel->validateKey(array('foreign_id' => 2));
  209. $this->assertTrue($is);
  210. $is = $testModel->validateKey(array('foreign_id' => 2.3));
  211. $this->assertFalse($is);
  212. $is = $testModel->validateKey(array('foreign_id' => -2));
  213. $this->assertFalse($is);
  214. $is = $testModel->validateKey(array('foreign_id' => '4dff6725-f0e8-4e6f-a2f2-19a4ab957338'));
  215. $this->assertFalse($is);
  216. $is = $testModel->validateKey(array('foreign_id' => 0));
  217. $this->assertFalse($is);
  218. $is = $testModel->validateKey(array('foreign_id' => 0), array('allowEmpty' => true));
  219. $this->assertTrue($is);
  220. }
  221. public function testValidateEnum() {
  222. $this->out($this->_header(__FUNCTION__), true);
  223. //$this->User->data = array($this->User->alias=>array('y'=>'efg'));
  224. $testModel = new AppTestModel();
  225. $is = $testModel->validateEnum(array('x' => '1'), true);
  226. $this->assertTrue($is);
  227. $is = $testModel->validateEnum(array('x' => '4'), true);
  228. $this->assertFalse($is);
  229. $is = $testModel->validateEnum(array('x' => '5'), true, array('4', '5'));
  230. $this->assertTrue($is);
  231. $is = $testModel->validateEnum(array('some_key' => '3'), 'x', array('4', '5'));
  232. $this->assertTrue($is);
  233. }
  234. public function testGuaranteeFields() {
  235. $this->out($this->_header(__FUNCTION__), true);
  236. $res = $this->User->guaranteeFields(array());
  237. //debug($res);
  238. $this->assertTrue(empty($res));
  239. $res = $this->User->guaranteeFields(array('x', 'y'));
  240. //debug($res);
  241. $this->assertTrue(!empty($res));
  242. $this->assertEquals($res, array($this->modelName => array('x' => '', 'y' => '')));
  243. $res = $this->User->guaranteeFields(array('x', 'OtherModel.y'));
  244. //debug($res);
  245. $this->assertTrue(!empty($res));
  246. $this->assertEquals($res, array($this->modelName => array('x' => ''), 'OtherModel' => array('y' => '')));
  247. }
  248. public function testSet() {
  249. $this->out($this->_header(__FUNCTION__), true);
  250. $data = array($this->modelName => array('x' => 'hey'), 'OtherModel' => array('y' => ''));
  251. $this->User->data = array();
  252. $res = $this->User->set($data, null, array('x', 'z'));
  253. $this->out($res);
  254. $this->assertTrue(!empty($res));
  255. $this->assertEquals($res, array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')));
  256. $res = $this->User->data;
  257. $this->out($res);
  258. $this->assertTrue(!empty($res));
  259. $this->assertEquals($res, array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')));
  260. }
  261. public function testValidateWithGuaranteeFields() {
  262. $this->out($this->_header(__FUNCTION__), true);
  263. $data = array($this->modelName => array('x' => 'hey'), 'OtherModel' => array('y' => ''));
  264. $data = $this->User->guaranteeFields(array('x', 'z'), $data);
  265. $this->out($data);
  266. $this->assertTrue(!empty($data));
  267. $this->assertEquals(array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')), $data);
  268. $res = $this->User->set($data);
  269. $this->out($res);
  270. $this->assertTrue(!empty($res));
  271. $this->assertEquals($res, array($this->modelName => array('x' => 'hey', 'z' => ''), 'OtherModel' => array('y' => '')));
  272. }
  273. // not really working?
  274. public function testBlacklist() {
  275. $this->out($this->_header(__FUNCTION__), true);
  276. $data = array($this->modelName => array('name' => 'e', 'x' => 'hey'), 'OtherModel' => array('y' => ''));
  277. $schema = $this->User->schema();
  278. $data = $this->User->blacklist(array('x', 'z'));
  279. $this->out($data);
  280. if (!empty($schema)) {
  281. $this->assertTrue(!empty($data));
  282. } else {
  283. $this->assertTrue(empty($data));
  284. }
  285. //$this->assertEquals($data, array($this->modelName=>array('x'=>'hey', 'z'=>''), 'OtherModel'=>array('y'=>'')));
  286. }
  287. public function testInvalidate() {
  288. $this->out($this->_header(__FUNCTION__), true);
  289. $this->User->create();
  290. $this->User->invalidate('fieldx', __('e %s f', 33));
  291. $res = $this->User->validationErrors;
  292. $this->out($res);
  293. $this->assertTrue(!empty($res));
  294. $this->User->create();
  295. $this->User->invalidate('Model.fieldy', __('e %s f %s g', 33, 'xyz'));
  296. $res = $this->User->validationErrors;
  297. $this->out($res);
  298. $this->assertTrue(!empty($res) && $res['Model.fieldy'][0] === 'e 33 f xyz g');
  299. $this->User->create();
  300. $this->User->invalidate('fieldy', __('e %s f %s g %s', true, 'xyz', 55));
  301. $res = $this->User->validationErrors;
  302. $this->out($res);
  303. $this->assertTrue(!empty($res) && $res['fieldy'][0] === 'e 1 f xyz g 55');
  304. $this->User->create();
  305. $this->User->invalidate('fieldy', array('valErrMandatoryField'));
  306. $res = $this->User->validationErrors;
  307. $this->out($res);
  308. $this->assertTrue(!empty($res));
  309. $this->User->create();
  310. $this->User->invalidate('fieldy', 'valErrMandatoryField');
  311. $res = $this->User->validationErrors;
  312. $this->out($res);
  313. $this->assertTrue(!empty($res));
  314. $this->User->create();
  315. $this->User->invalidate('fieldy', __('a %s b %s c %s %s %s %s %s h %s', 1, 2, 3, 4, 5, 6, 7, 8));
  316. $res = $this->User->validationErrors;
  317. $this->out($res);
  318. $this->assertTrue(!empty($res) && $res['fieldy'][0] === 'a 1 b 2 c 3 4 5 6 7 h 8');
  319. }
  320. public function testValidateDate() {
  321. $this->out($this->_header(__FUNCTION__), true);
  322. $data = array('field' => '2010-01-22');
  323. $res = $this->User->validateDate($data);
  324. //debug($res);
  325. $this->assertTrue($res);
  326. $data = array('field' => '2010-02-29');
  327. $res = $this->User->validateDate($data);
  328. //debug($res);
  329. $this->assertFalse($res);
  330. $this->User->data = array($this->User->alias => array('after' => '2010-02-22'));
  331. $data = array('field' => '2010-02-23 11:11:11');
  332. $res = $this->User->validateDate($data, array('after' => 'after'));
  333. //debug($res);
  334. $this->assertTrue($res);
  335. $this->User->data = array($this->User->alias => array('after' => '2010-02-24 11:11:11'));
  336. $data = array('field' => '2010-02-23');
  337. $res = $this->User->validateDate($data, array('after' => 'after'));
  338. //debug($res);
  339. $this->assertFalse($res);
  340. $this->User->data = array($this->User->alias => array('after' => '2010-02-25'));
  341. $data = array('field' => '2010-02-25');
  342. $res = $this->User->validateDate($data, array('after' => 'after'));
  343. //debug($res);
  344. $this->assertTrue($res);
  345. $this->User->data = array($this->User->alias => array('after' => '2010-02-25'));
  346. $data = array('field' => '2010-02-25');
  347. $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 1));
  348. //debug($res);
  349. $this->assertFalse($res);
  350. $this->User->data = array($this->User->alias => array('after' => '2010-02-24'));
  351. $data = array('field' => '2010-02-25');
  352. $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 2));
  353. //debug($res);
  354. $this->assertFalse($res);
  355. $this->User->data = array($this->User->alias => array('after' => '2010-02-24'));
  356. $data = array('field' => '2010-02-25');
  357. $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 1));
  358. //debug($res);
  359. $this->assertTrue($res);
  360. $this->User->data = array($this->User->alias => array('after' => '2010-02-24'));
  361. $data = array('field' => '2010-02-25');
  362. $res = $this->User->validateDate($data, array('after' => 'after', 'min' => 2));
  363. //debug($res);
  364. $this->assertFalse($res);
  365. $this->User->data = array($this->User->alias => array('before' => '2010-02-24'));
  366. $data = array('field' => '2010-02-24');
  367. $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 1));
  368. //debug($res);
  369. $this->assertFalse($res);
  370. $this->User->data = array($this->User->alias => array('before' => '2010-02-25'));
  371. $data = array('field' => '2010-02-24');
  372. $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 1));
  373. //debug($res);
  374. $this->assertTrue($res);
  375. $this->User->data = array($this->User->alias => array('before' => '2010-02-25'));
  376. $data = array('field' => '2010-02-24');
  377. $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 2));
  378. //debug($res);
  379. $this->assertFalse($res);
  380. $this->User->data = array($this->User->alias => array('before' => '2010-02-26'));
  381. $data = array('field' => '2010-02-24');
  382. $res = $this->User->validateDate($data, array('before' => 'before', 'min' => 2));
  383. //debug($res);
  384. $this->assertTrue($res);
  385. }
  386. public function testValidateDatetime() {
  387. $this->out($this->_header(__FUNCTION__), true);
  388. $data = array('field' => '2010-01-22 11:11:11');
  389. $res = $this->User->validateDatetime($data);
  390. //debug($res);
  391. $this->assertTrue($res);
  392. $data = array('field' => '2010-01-22 11:61:11');
  393. $res = $this->User->validateDatetime($data);
  394. //debug($res);
  395. $this->assertFalse($res);
  396. $data = array('field' => '2010-02-29 11:11:11');
  397. $res = $this->User->validateDatetime($data);
  398. //debug($res);
  399. $this->assertFalse($res);
  400. $data = array('field' => '');
  401. $res = $this->User->validateDatetime($data, array('allowEmpty' => true));
  402. //debug($res);
  403. $this->assertTrue($res);
  404. $data = array('field' => '0000-00-00 00:00:00');
  405. $res = $this->User->validateDatetime($data, array('allowEmpty' => true));
  406. //debug($res);
  407. $this->assertTrue($res);
  408. $this->User->data = array($this->User->alias => array('after' => '2010-02-22 11:11:11'));
  409. $data = array('field' => '2010-02-23 11:11:11');
  410. $res = $this->User->validateDatetime($data, array('after' => 'after'));
  411. //debug($res);
  412. $this->assertTrue($res);
  413. $this->User->data = array($this->User->alias => array('after' => '2010-02-24 11:11:11'));
  414. $data = array('field' => '2010-02-23 11:11:11');
  415. $res = $this->User->validateDatetime($data, array('after' => 'after'));
  416. //debug($res);
  417. $this->assertFalse($res);
  418. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:11'));
  419. $data = array('field' => '2010-02-23 11:11:11');
  420. $res = $this->User->validateDatetime($data, array('after' => 'after'));
  421. //debug($res);
  422. $this->assertFalse($res);
  423. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:11'));
  424. $data = array('field' => '2010-02-23 11:11:11');
  425. $res = $this->User->validateDatetime($data, array('after' => 'after', 'min' => 1));
  426. //debug($res);
  427. $this->assertFalse($res);
  428. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:11'));
  429. $data = array('field' => '2010-02-23 11:11:11');
  430. $res = $this->User->validateDatetime($data, array('after' => 'after', 'min' => 0));
  431. //debug($res);
  432. $this->assertTrue($res);
  433. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:10'));
  434. $data = array('field' => '2010-02-23 11:11:11');
  435. $res = $this->User->validateDatetime($data, array('after' => 'after'));
  436. //debug($res);
  437. $this->assertTrue($res);
  438. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:12'));
  439. $data = array('field' => '2010-02-23 11:11:11');
  440. $res = $this->User->validateDatetime($data, array('after' => 'after'));
  441. //debug($res);
  442. $this->assertFalse($res);
  443. }
  444. public function testValidateTime() {
  445. $this->out($this->_header(__FUNCTION__), true);
  446. $data = array('field' => '11:21:11');
  447. $res = $this->User->validateTime($data);
  448. //debug($res);
  449. $this->assertTrue($res);
  450. $data = array('field' => '11:71:11');
  451. $res = $this->User->validateTime($data);
  452. //debug($res);
  453. $this->assertFalse($res);
  454. $this->User->data = array($this->User->alias => array('before' => '2010-02-23 11:11:12'));
  455. $data = array('field' => '2010-02-23 11:11:11');
  456. $res = $this->User->validateTime($data, array('before' => 'before'));
  457. //debug($res);
  458. $this->assertTrue($res);
  459. $this->User->data = array($this->User->alias => array('after' => '2010-02-23 11:11:12'));
  460. $data = array('field' => '2010-02-23 11:11:11');
  461. $res = $this->User->validateTime($data, array('after' => 'after'));
  462. //debug($res);
  463. $this->assertFalse($res);
  464. }
  465. public function testValidateUrl() {
  466. $this->out($this->_header(__FUNCTION__), true);
  467. $data = array('field' => 'www.dereuromark.de');
  468. $res = $this->User->validateUrl($data, array('allowEmpty' => true));
  469. $this->assertTrue($res);
  470. $data = array('field' => 'www.xxxde');
  471. $res = $this->User->validateUrl($data, array('allowEmpty' => true));
  472. $this->assertFalse($res);
  473. $data = array('field' => 'www.dereuromark.de');
  474. $res = $this->User->validateUrl($data, array('allowEmpty' => true, 'autoComplete' => false));
  475. $this->assertFalse($res);
  476. $data = array('field' => 'http://www.dereuromark.de');
  477. $res = $this->User->validateUrl($data, array('allowEmpty' => true, 'autoComplete' => false));
  478. $this->assertTrue($res);
  479. $data = array('field' => 'www.dereuromark.de');
  480. $res = $this->User->validateUrl($data, array('strict' => true));
  481. $this->assertTrue($res); # aha
  482. $data = array('field' => 'http://www.dereuromark.de');
  483. $res = $this->User->validateUrl($data, array('strict' => false));
  484. $this->assertTrue($res);
  485. $this->skipIf(empty($_SERVER['HTTP_HOST']), 'No HTTP_HOST');
  486. $data = array('field' => 'http://xyz.de/some/link');
  487. $res = $this->User->validateUrl($data, array('deep' => false, 'sameDomain' => true));
  488. $this->assertFalse($res);
  489. $data = array('field' => '/some/link');
  490. $res = $this->User->validateUrl($data, array('deep' => false, 'autoComplete' => true));
  491. $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
  492. $data = array('field' => 'http://' . $_SERVER['HTTP_HOST'] . '/some/link');
  493. $res = $this->User->validateUrl($data, array('deep' => false));
  494. $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
  495. $data = array('field' => '/some/link');
  496. $res = $this->User->validateUrl($data, array('deep' => false, 'autoComplete' => false));
  497. $this->assertTrue((env('REMOTE_ADDR') !== '127.0.0.1') ? !$res : $res);
  498. //$this->skipIf(strpos($_SERVER['HTTP_HOST'], '.') === false, 'No online HTTP_HOST');
  499. $data = array('field' => '/some/link');
  500. $res = $this->User->validateUrl($data, array('deep' => false, 'sameDomain' => true));
  501. $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
  502. $data = array('field' => 'https://github.com/');
  503. $res = $this->User->validateUrl($data, array('deep' => false));
  504. $this->assertTrue($res);
  505. $data = array('field' => 'https://github.com/');
  506. $res = $this->User->validateUrl($data, array('deep' => true));
  507. $this->assertTrue($res);
  508. }
  509. public function testValidateUnique() {
  510. $this->out($this->_header(__FUNCTION__), true);
  511. $this->Post->validate['title'] = array(
  512. 'validateUnique' => array(
  513. 'rule' => 'validateUnique',
  514. 'message' => 'valErrRecordTitleExists'
  515. ),
  516. );
  517. $data = array(
  518. 'title' => 'abc',
  519. 'published' => 'N'
  520. );
  521. $this->Post->create($data);
  522. $res = $this->Post->validates();
  523. $this->assertTrue($res);
  524. $res = $this->Post->save($res, false);
  525. $this->assertTrue((bool)$res);
  526. $this->Post->create();
  527. $res = $this->Post->save($data);
  528. $this->assertFalse($res);
  529. $this->Post->validate['title'] = array(
  530. 'validateUnique' => array(
  531. 'rule' => array('validateUnique', array('published')),
  532. 'message' => 'valErrRecordTitleExists'
  533. ),
  534. );
  535. $data = array(
  536. 'title' => 'abc',
  537. 'published' => 'Y'
  538. );
  539. $this->Post->create($data);
  540. $res = $this->Post->validates();
  541. $this->assertTrue($res);
  542. $res = $this->Post->save($res, false);
  543. $this->assertTrue((bool)$res);
  544. $this->Post->create();
  545. $res = $this->Post->save($data);
  546. $this->assertFalse($res);
  547. }
  548. }
  549. class MyAppModelPost extends MyModel {
  550. public $name = 'Post';
  551. public $alias = 'Post';
  552. public $belongsTo = 'Author';
  553. }
  554. class MyAppModelUser extends MyModel {
  555. public $name = 'User';
  556. public $alias = 'User';
  557. }
  558. class AppTestModel extends MyModel {
  559. public $useTable = false;
  560. protected $_schema = array(
  561. 'id' => array (
  562. 'type' => 'string',
  563. 'null' => false,
  564. 'default' => '',
  565. 'length' => 36,
  566. 'key' => 'primary',
  567. 'collate' => 'utf8_unicode_ci',
  568. 'charset' => 'utf8',
  569. ),
  570. 'foreign_id' => array (
  571. 'type' => 'integer',
  572. 'null' => false,
  573. 'default' => '0',
  574. 'length' => 10,
  575. ),
  576. );
  577. public static function x() {
  578. return array('1' => 'x', '2' => 'y', '3' => 'z');
  579. }
  580. }