MyModelTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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->assertIsA($this->Model, 'MyModel');
  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 = User::enum(null, $array, false);
  33. $this->assertEquals($array, $res);
  34. $res = User::enum(2, $array, false);
  35. $this->assertEquals('bar', $res);
  36. $res = User::enum('2', $array, false);
  37. $this->assertEquals('bar', $res);
  38. $res = User::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 truncate
  72. */
  73. public function testTruncate() {
  74. $is = $this->App->find('count');
  75. $this->assertEquals(4, $is);
  76. $is = $this->App->getNextAutoIncrement();
  77. $this->assertEquals(5, $is);
  78. $is = $this->App->truncate();
  79. $is = $this->App->find('count');
  80. $this->assertEquals(0, $is);
  81. $is = $this->App->getNextAutoIncrement();
  82. $this->assertEquals(1, $is);
  83. }
  84. public function testValidateIdentical() {
  85. $this->out($this->_header(__FUNCTION__));
  86. $this->App->data = array($this->App->alias=>array('y'=>'efg'));
  87. $is = $this->App->validateIdentical(array('x'=>'efg'), 'y');
  88. $this->assertTrue($is);
  89. $this->App->data = array($this->App->alias=>array('y'=>'2'));
  90. $is = $this->App->validateIdentical(array('x'=>2), 'y');
  91. $this->assertFalse($is);
  92. $this->App->data = array($this->App->alias=>array('y'=>'3'));
  93. $is = $this->App->validateIdentical(array('x'=>3), 'y', array('cast'=>'int'));
  94. $this->assertTrue($is);
  95. $this->App->data = array($this->App->alias=>array('y'=>'3'));
  96. $is = $this->App->validateIdentical(array('x'=>3), 'y', array('cast'=>'string'));
  97. $this->assertTrue($is);
  98. }
  99. public function testValidateKey() {
  100. $this->out($this->_header(__FUNCTION__));
  101. //$this->App->data = array($this->App->alias=>array('y'=>'efg'));
  102. $testModel = new AppTestModel();
  103. $is = $testModel->validateKey(array('id'=>'2'));
  104. $this->assertFalse($is);
  105. $is = $testModel->validateKey(array('id'=>2));
  106. $this->assertFalse($is);
  107. $is = $testModel->validateKey(array('id'=>'4e6f-a2f2-19a4ab957338'));
  108. $this->assertFalse($is);
  109. $is = $testModel->validateKey(array('id'=>'4dff6725-f0e8-4e6f-a2f2-19a4ab957338'));
  110. $this->assertTrue($is);
  111. $is = $testModel->validateKey(array('id'=>''));
  112. $this->assertFalse($is);
  113. $is = $testModel->validateKey(array('id'=>''), array('allowEmpty'=>true));
  114. $this->assertTrue($is);
  115. $is = $testModel->validateKey(array('foreign_id'=>'2'));
  116. $this->assertTrue($is);
  117. $is = $testModel->validateKey(array('foreign_id'=>2));
  118. $this->assertTrue($is);
  119. $is = $testModel->validateKey(array('foreign_id'=>2.3));
  120. $this->assertFalse($is);
  121. $is = $testModel->validateKey(array('foreign_id'=>-2));
  122. $this->assertFalse($is);
  123. $is = $testModel->validateKey(array('foreign_id'=>'4dff6725-f0e8-4e6f-a2f2-19a4ab957338'));
  124. $this->assertFalse($is);
  125. $is = $testModel->validateKey(array('foreign_id'=>0));
  126. $this->assertFalse($is);
  127. $is = $testModel->validateKey(array('foreign_id'=>0), array('allowEmpty'=>true));
  128. $this->assertTrue($is);
  129. }
  130. public function testValidateEnum() {
  131. $this->out($this->_header(__FUNCTION__));
  132. //$this->App->data = array($this->App->alias=>array('y'=>'efg'));
  133. $testModel = new AppTestModel();
  134. $is = $testModel->validateEnum(array('x'=>'1'), true);
  135. $this->assertTrue($is);
  136. $is = $testModel->validateEnum(array('x'=>'4'), true);
  137. $this->assertFalse($is);
  138. $is = $testModel->validateEnum(array('x'=>'5'), true, array('4', '5'));
  139. $this->assertTrue($is);
  140. $is = $testModel->validateEnum(array('some_key'=>'3'), 'x', array('4', '5'));
  141. $this->assertTrue($is);
  142. }
  143. public function testGuaranteeFields() {
  144. $this->out($this->_header(__FUNCTION__));
  145. $res = $this->App->guaranteeFields(array());
  146. $this->out(returns($res));
  147. $this->assertTrue(empty($res));
  148. $res = $this->App->guaranteeFields(array('x', 'y'));
  149. $this->out(returns($res));
  150. $this->assertTrue(!empty($res));
  151. $this->assertEquals($res, array($this->modelName=>array('x'=>'', 'y'=>'')));
  152. $res = $this->App->guaranteeFields(array('x', 'OtherModel.y'));
  153. $this->out(returns($res));
  154. $this->assertTrue(!empty($res));
  155. $this->assertEquals($res, array($this->modelName=>array('x'=>''), 'OtherModel'=>array('y'=>'')));
  156. }
  157. public function testSet() {
  158. $this->out($this->_header(__FUNCTION__));
  159. $data = array($this->modelName=>array('x'=>'hey'), 'OtherModel'=>array('y'=>''));
  160. $this->App->data = array();
  161. $res = $this->App->set($data, null, array('x', 'z'));
  162. $this->out($res);
  163. $this->assertTrue(!empty($res));
  164. $this->assertEquals($res, array($this->modelName=>array('x'=>'hey', 'z'=>''), 'OtherModel'=>array('y'=>'')));
  165. $res = $this->App->data;
  166. $this->out($res);
  167. $this->assertTrue(!empty($res));
  168. $this->assertEquals($res, array($this->modelName=>array('x'=>'hey', 'z'=>''), 'OtherModel'=>array('y'=>'')));
  169. }
  170. public function testValidateWithGuaranteeFields() {
  171. $this->out($this->_header(__FUNCTION__));
  172. $data = array($this->modelName=>array('x'=>'hey'), 'OtherModel'=>array('y'=>''));
  173. $data = $this->App->guaranteeFields(array('x', 'z'), $data);
  174. $this->out($data);
  175. $this->assertTrue(!empty($data));
  176. $this->assertEquals(array($this->modelName=>array('x'=>'hey', 'z'=>''), 'OtherModel'=>array('y'=>'')), $data);
  177. $res = $this->App->set($data);
  178. $this->out($res);
  179. $this->assertTrue(!empty($res));
  180. $this->assertEquals($res, array($this->modelName=>array('x'=>'hey', 'z'=>''), 'OtherModel'=>array('y'=>'')));
  181. }
  182. // not really working?
  183. public function testBlacklist() {
  184. $this->out($this->_header(__FUNCTION__));
  185. $data = array($this->modelName=>array('name'=>'e', 'x'=>'hey'), 'OtherModel'=>array('y'=>''));
  186. $schema = $this->App->schema();
  187. $data = $this->App->blacklist(array('x', 'z'));
  188. $this->out($data);
  189. if (!empty($schema)) {
  190. $this->assertTrue(!empty($data));
  191. } else {
  192. $this->assertTrue(empty($data));
  193. }
  194. //$this->assertEquals($data, array($this->modelName=>array('x'=>'hey', 'z'=>''), 'OtherModel'=>array('y'=>'')));
  195. }
  196. public function testAppInvalidate() {
  197. $this->out($this->_header(__FUNCTION__));
  198. $this->App->create();
  199. $this->App->invalidate('fieldx', __('e %s f', 33));
  200. $res = $this->App->validationErrors;
  201. $this->out($res);
  202. $this->assertTrue(!empty($res));
  203. $this->App->create();
  204. $this->App->invalidate('Model.fieldy', __('e %s f %s g', 33, 'xyz'));
  205. $res = $this->App->validationErrors;
  206. $this->out($res);
  207. $this->assertTrue(!empty($res) && $res['Model.fieldy'][0] == 'e 33 f xyz g');
  208. $this->App->create();
  209. $this->App->invalidate('fieldy', __('e %s f %s g %s', true, 'xyz', 55));
  210. $res = $this->App->validationErrors;
  211. $this->out($res);
  212. $this->assertTrue(!empty($res) && $res['fieldy'][0] == 'e 1 f xyz g 55');
  213. $this->App->create();
  214. $this->App->invalidate('fieldy', array('valErrMandatoryField'));
  215. $res = $this->App->validationErrors;
  216. $this->out($res);
  217. $this->assertTrue(!empty($res));
  218. $this->App->create();
  219. $this->App->invalidate('fieldy', 'valErrMandatoryField');
  220. $res = $this->App->validationErrors;
  221. $this->out($res);
  222. $this->assertTrue(!empty($res));
  223. $this->App->create();
  224. $this->App->invalidate('fieldy', __('a %s b %s c %s %s %s %s %s h %s', 1, 2, 3, 4, 5, 6, 7, 8));
  225. $res = $this->App->validationErrors;
  226. $this->out($res);
  227. $this->assertTrue(!empty($res) && $res['fieldy'][0] == 'a 1 b 2 c 3 4 5 6 7 h 8');
  228. }
  229. public function testAppValidateDate() {
  230. $this->out($this->_header(__FUNCTION__));
  231. $data = array('field' => '2010-01-22');
  232. $res = $this->App->validateDate($data);
  233. $this->out(returns($res));
  234. $this->assertTrue($res);
  235. $data = array('field' => '2010-02-29');
  236. $res = $this->App->validateDate($data);
  237. $this->out(returns($res));
  238. $this->assertFalse($res);
  239. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-22'));
  240. $data = array('field' => '2010-02-23 11:11:11');
  241. $res = $this->App->validateDate($data, array('after'=>'after'));
  242. $this->out(returns($res));
  243. $this->assertTrue($res);
  244. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-24 11:11:11'));
  245. $data = array('field' => '2010-02-23');
  246. $res = $this->App->validateDate($data, array('after'=>'after'));
  247. $this->out(returns($res));
  248. $this->assertFalse($res);
  249. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-25'));
  250. $data = array('field' => '2010-02-25');
  251. $res = $this->App->validateDate($data, array('after'=>'after'));
  252. $this->out(returns($res));
  253. $this->assertTrue($res);
  254. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-25'));
  255. $data = array('field' => '2010-02-25');
  256. $res = $this->App->validateDate($data, array('after'=>'after', 'min'=>1));
  257. $this->out(returns($res));
  258. $this->assertFalse($res);
  259. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-24'));
  260. $data = array('field' => '2010-02-25');
  261. $res = $this->App->validateDate($data, array('after'=>'after', 'min'=>2));
  262. $this->out(returns($res));
  263. $this->assertFalse($res);
  264. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-24'));
  265. $data = array('field' => '2010-02-25');
  266. $res = $this->App->validateDate($data, array('after'=>'after', 'min'=>1));
  267. $this->out(returns($res));
  268. $this->assertTrue($res);
  269. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-24'));
  270. $data = array('field' => '2010-02-25');
  271. $res = $this->App->validateDate($data, array('after'=>'after', 'min'=>2));
  272. $this->out(returns($res));
  273. $this->assertFalse($res);
  274. $this->App->data = array($this->App->alias=>array('before'=>'2010-02-24'));
  275. $data = array('field' => '2010-02-24');
  276. $res = $this->App->validateDate($data, array('before'=>'before', 'min'=>1));
  277. $this->out(returns($res));
  278. $this->assertFalse($res);
  279. $this->App->data = array($this->App->alias=>array('before'=>'2010-02-25'));
  280. $data = array('field' => '2010-02-24');
  281. $res = $this->App->validateDate($data, array('before'=>'before', 'min'=>1));
  282. $this->out(returns($res));
  283. $this->assertTrue($res);
  284. $this->App->data = array($this->App->alias=>array('before'=>'2010-02-25'));
  285. $data = array('field' => '2010-02-24');
  286. $res = $this->App->validateDate($data, array('before'=>'before', 'min'=>2));
  287. $this->out(returns($res));
  288. $this->assertFalse($res);
  289. $this->App->data = array($this->App->alias=>array('before'=>'2010-02-26'));
  290. $data = array('field' => '2010-02-24');
  291. $res = $this->App->validateDate($data, array('before'=>'before', 'min'=>2));
  292. $this->out(returns($res));
  293. $this->assertTrue($res);
  294. }
  295. public function testAppValidateDatetime() {
  296. $this->out($this->_header(__FUNCTION__));
  297. $data = array('field' => '2010-01-22 11:11:11');
  298. $res = $this->App->validateDatetime($data);
  299. $this->out(returns($res));
  300. $this->assertTrue($res);
  301. $data = array('field' => '2010-01-22 11:61:11');
  302. $res = $this->App->validateDatetime($data);
  303. $this->out(returns($res));
  304. $this->assertFalse($res);
  305. $data = array('field' => '2010-02-29 11:11:11');
  306. $res = $this->App->validateDatetime($data);
  307. $this->out(returns($res));
  308. $this->assertFalse($res);
  309. $data = array('field' => '');
  310. $res = $this->App->validateDatetime($data, array('allowEmpty'=>true));
  311. $this->out(returns($res));
  312. $this->assertTrue($res);
  313. $data = array('field' => '0000-00-00 00:00:00');
  314. $res = $this->App->validateDatetime($data, array('allowEmpty'=>true));
  315. $this->out(returns($res));
  316. $this->assertTrue($res);
  317. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-22 11:11:11'));
  318. $data = array('field' => '2010-02-23 11:11:11');
  319. $res = $this->App->validateDatetime($data, array('after'=>'after'));
  320. $this->out(returns($res));
  321. $this->assertTrue($res);
  322. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-24 11:11:11'));
  323. $data = array('field' => '2010-02-23 11:11:11');
  324. $res = $this->App->validateDatetime($data, array('after'=>'after'));
  325. $this->out(returns($res));
  326. $this->assertFalse($res);
  327. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-23 11:11:11'));
  328. $data = array('field' => '2010-02-23 11:11:11');
  329. $res = $this->App->validateDatetime($data, array('after'=>'after'));
  330. $this->out(returns($res));
  331. $this->assertFalse($res);
  332. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-23 11:11:11'));
  333. $data = array('field' => '2010-02-23 11:11:11');
  334. $res = $this->App->validateDatetime($data, array('after'=>'after', 'min'=>1));
  335. $this->out(returns($res));
  336. $this->assertFalse($res);
  337. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-23 11:11:11'));
  338. $data = array('field' => '2010-02-23 11:11:11');
  339. $res = $this->App->validateDatetime($data, array('after'=>'after', 'min'=>0));
  340. $this->out(returns($res));
  341. $this->assertTrue($res);
  342. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-23 11:11:10'));
  343. $data = array('field' => '2010-02-23 11:11:11');
  344. $res = $this->App->validateDatetime($data, array('after'=>'after'));
  345. $this->out(returns($res));
  346. $this->assertTrue($res);
  347. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-23 11:11:12'));
  348. $data = array('field' => '2010-02-23 11:11:11');
  349. $res = $this->App->validateDatetime($data, array('after'=>'after'));
  350. $this->out(returns($res));
  351. $this->assertFalse($res);
  352. }
  353. public function testAppValidateTime() {
  354. $this->out($this->_header(__FUNCTION__));
  355. $data = array('field' => '11:21:11');
  356. $res = $this->App->validateTime($data);
  357. $this->out(returns($res));
  358. $this->assertTrue($res);
  359. $data = array('field' => '11:71:11');
  360. $res = $this->App->validateTime($data);
  361. $this->out(returns($res));
  362. $this->assertFalse($res);
  363. $this->App->data = array($this->App->alias=>array('before'=>'2010-02-23 11:11:12'));
  364. $data = array('field' => '2010-02-23 11:11:11');
  365. $res = $this->App->validateTime($data, array('before'=>'before'));
  366. $this->out(returns($res));
  367. $this->assertTrue($res);
  368. $this->App->data = array($this->App->alias=>array('after'=>'2010-02-23 11:11:12'));
  369. $data = array('field' => '2010-02-23 11:11:11');
  370. $res = $this->App->validateTime($data, array('after'=>'after'));
  371. $this->out(returns($res));
  372. $this->assertFalse($res);
  373. }
  374. public function testAppValidateUrl() {
  375. $this->out($this->_header(__FUNCTION__));
  376. $data = array('field' => 'www.dereuromark.de');
  377. $res = $this->App->validateUrl($data, array('allowEmpty'=>true));
  378. $this->assertTrue($res);
  379. $data = array('field' => 'www.xxxde');
  380. $res = $this->App->validateUrl($data, array('allowEmpty'=>true));
  381. $this->assertFalse($res);
  382. $data = array('field' => 'www.dereuromark.de');
  383. $res = $this->App->validateUrl($data, array('allowEmpty'=>true, 'autoComplete'=>false));
  384. $this->assertFalse($res);
  385. $data = array('field' => 'http://www.dereuromark.de');
  386. $res = $this->App->validateUrl($data, array('allowEmpty'=>true, 'autoComplete'=>false));
  387. $this->assertTrue($res);
  388. $data = array('field' => 'www.dereuromark.de');
  389. $res = $this->App->validateUrl($data, array('strict'=>true));
  390. $this->assertTrue($res); # aha
  391. $data = array('field' => 'http://www.dereuromark.de');
  392. $res = $this->App->validateUrl($data, array('strict'=>false));
  393. $this->assertTrue($res);
  394. $this->skipIf(empty($_SERVER['HTTP_HOST']), 'No HTTP_HOST');
  395. $data = array('field' => 'http://xyz.de/some/link');
  396. $res = $this->App->validateUrl($data, array('deep'=>false, 'sameDomain'=>true));
  397. $this->assertFalse($res);
  398. $data = array('field' => '/some/link');
  399. $res = $this->App->validateUrl($data, array('deep'=>false, 'autoComplete'=>true));
  400. $this->assertTrue($res);
  401. $data = array('field' => 'http://'.$_SERVER['HTTP_HOST'].'/some/link');
  402. $res = $this->App->validateUrl($data, array('deep'=>false));
  403. $this->assertTrue($res);
  404. $data = array('field' => '/some/link');
  405. $res = $this->App->validateUrl($data, array('deep'=>false, 'autoComplete'=>false));
  406. $this->assertTrue((env('REMOTE_ADDR') !== '127.0.0.1') ? !$res : $res);
  407. //$this->skipIf(strpos($_SERVER['HTTP_HOST'], '.') === false, 'No online HTTP_HOST');
  408. $data = array('field' => '/some/link');
  409. $res = $this->App->validateUrl($data, array('deep'=>false, 'sameDomain'=>true));
  410. $this->assertTrue($res);
  411. $data = array('field' => 'https://github.com/');
  412. $res = $this->App->validateUrl($data, array('deep'=>false));
  413. $this->assertTrue($res);
  414. $data = array('field' => 'https://github.com/');
  415. $res = $this->App->validateUrl($data, array('deep'=>true));
  416. $this->assertTrue($res);
  417. }
  418. }
  419. class Post extends MyModel {
  420. public $belongsTo = 'Author';
  421. }
  422. class AppTestModel extends MyModel {
  423. public $useTable = false;
  424. protected $_schema = array(
  425. 'id' => array (
  426. 'type' => 'string',
  427. 'null' => false,
  428. 'default' => '',
  429. 'length' => 36,
  430. 'key' => 'primary',
  431. 'collate' => 'utf8_unicode_ci',
  432. 'charset' => 'utf8',
  433. ),
  434. 'foreign_id' => array (
  435. 'type' => 'integer',
  436. 'null' => false,
  437. 'default' => '0',
  438. 'length' => 10,
  439. ),
  440. );
  441. public static function x() {
  442. return array('1'=>'x', '2'=>'y', '3'=>'z');
  443. }
  444. }