MyModelTest.php 21 KB

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