MyModelTest.php 22 KB

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