MyModelTest.php 22 KB

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