TableTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. namespace Tools\Model\Table;
  3. use Tools\TestSuite\TestCase;
  4. use Cake\ORM\Behavior;
  5. use Cake\ORM\Entity;
  6. use Cake\ORM\Query;
  7. use Cake\ORM\Table;
  8. use Cake\Core\Configure;
  9. use Cake\Auth\DefaultPasswordHasher;
  10. use Cake\ORM\TableRegistry;
  11. use Cake\Utility\Security;
  12. use Cake\Routing\Router;
  13. use Cake\Network\Request;
  14. use Cake\Auth\PasswordHasherFactory;
  15. use Cake\I18n\Time;
  16. use Cake\Datasource\ConnectionManager;
  17. class TableTest extends TestCase {
  18. public $fixtures = array(
  19. 'core.posts', 'core.authors',
  20. 'plugin.tools.tools_users', 'plugin.tools.roles',
  21. );
  22. public $Users;
  23. /**
  24. * SetUp method
  25. *
  26. * @return void
  27. */
  28. public function setUp() {
  29. parent::setUp();
  30. Configure::write('App.namespace', 'TestApp');
  31. $this->Users = TableRegistry::get('ToolsUsers');
  32. $this->Posts = TableRegistry::get('Posts');
  33. $this->Posts->belongsTo('Authors');
  34. }
  35. public function tearDown() {
  36. TableRegistry::clear();
  37. parent::tearDown();
  38. }
  39. /**
  40. * Test truncate
  41. *
  42. * @return void
  43. */
  44. public function testTruncate() {
  45. $is = $this->Users->find('count');
  46. $this->assertEquals(4, $is);
  47. $config = ConnectionManager::config('test');
  48. if ((strpos($config['driver'], 'Mysql') !== false)) {
  49. $is = $this->Users->getNextAutoIncrement();
  50. $this->assertEquals(5, $is);
  51. }
  52. $is = $this->Users->truncate();
  53. $is = $this->Users->find('count');
  54. $this->assertEquals(0, $is);
  55. if ((strpos($config['driver'], 'Mysql') !== false)) {
  56. $is = $this->Users->getNextAutoIncrement();
  57. $this->assertEquals(1, $is);
  58. }
  59. }
  60. /**
  61. * Check shims
  62. *
  63. * @return void
  64. */
  65. public function testFindFirst() {
  66. $result = $this->Users->find('first', ['conditions' => ['name LIKE' => 'User %']]);
  67. $this->assertEquals('User 1', $result['name']);
  68. $result = $this->Users->find('first', ['conditions' => ['name NOT LIKE' => 'User %']]);
  69. $this->assertNotEquals('User 1', $result['name']);
  70. }
  71. /**
  72. * Check shims
  73. *
  74. * @return void
  75. */
  76. public function testFindCount() {
  77. $result = $this->Users->find('count');
  78. $this->assertEquals(4, $result);
  79. $result = $this->Users->find('count', ['conditions' => ['name' => 'User 1']]);
  80. $this->assertEquals(1, $result);
  81. }
  82. public function testField() {
  83. $result = $this->Users->field('name', ['conditions' => ['name' => 'User 1']]);
  84. $this->assertEquals('User 1', $result);
  85. $result = $this->Users->field('name', ['conditions' => ['name' => 'User xxx']]);
  86. $this->assertNull($result);
  87. }
  88. /**
  89. * Test 2.x shimmed order property
  90. *
  91. * $this->order = array('field_name' => 'ASC') etc
  92. *
  93. * becomes
  94. *
  95. * $this->order = array('TableName.field_name' => 'ASC') and a beforeFind addition.
  96. *
  97. * @return void
  98. */
  99. public function testOrder() {
  100. $this->Users->truncate();
  101. $rows = array(
  102. array('role_id' => 1, 'name' => 'Gandalf'),
  103. array('role_id' => 2, 'name' => 'Asterix'),
  104. array('role_id' => 1, 'name' => 'Obelix'),
  105. array('role_id' => 3, 'name' => 'Harry Potter'));
  106. foreach ($rows as $row) {
  107. $entity = $this->Users->newEntity($row);
  108. $this->Users->save($entity);
  109. }
  110. $result = $this->Users->find('list')->toArray();
  111. $expected = array(
  112. 'Asterix',
  113. 'Gandalf',
  114. 'Harry Potter',
  115. 'Obelix'
  116. );
  117. $this->assertSame($expected, array_values($result));
  118. }
  119. /**
  120. * TableTest::testGetRelatedInUse()
  121. *
  122. * @return void
  123. */
  124. public function testGetRelatedInUse() {
  125. $this->skipIf(true, 'TODO');
  126. $results = $this->Posts->getRelatedInUse('Authors', 'author_id', 'list');
  127. //die(debug($results->toArray()));
  128. $expected = array(1 => 'mariano', 3 => 'larry');
  129. $this->assertEquals($expected, $results->toArray());
  130. }
  131. /**
  132. * TableTest::testGetFieldInUse()
  133. *
  134. * @return void
  135. */
  136. public function testGetFieldInUse() {
  137. $this->skipIf(true, 'TODO');
  138. $this->db = ConnectionManager::getDataSource('test');
  139. $this->skipIf(!($this->db instanceof Mysql), 'The test is only compatible with Mysql.');
  140. $results = $this->Posts->getFieldInUse('author_id', 'list');
  141. $expected = array(1 => 'First Post', 2 => 'Second Post');
  142. $this->assertEquals($expected, $results);
  143. }
  144. /**
  145. * TableTest::testValidateDate()
  146. *
  147. * @return void
  148. */
  149. public function testValidateDate() {
  150. $date = new Time('2010-01-22');
  151. $res = $this->Users->validateDate($date);
  152. //debug($res);
  153. $this->assertTrue($res);
  154. // Careful: now becomes 2010-03-01 in Cake3
  155. // FIXME
  156. $date = new Time('2010-02-29');
  157. //debug($date->format(FORMAT_DB_DATETIME));
  158. $res = $this->Users->validateDate($date);
  159. //$this->assertFalse($res);
  160. $this->assertTrue($res);
  161. $date = new Time('2010-02-23 11:11:11');
  162. $context = array('data' => array('after' => new Time('2010-02-22')));
  163. $res = $this->Users->validateDate($date, array('after' => 'after'), $context);
  164. //debug($res);
  165. $this->assertTrue($res);
  166. $date = new Time('2010-02-23');
  167. $context = array('data' => array('after' => new Time('2010-02-24 11:11:11')));
  168. $res = $this->Users->validateDate($date, array('after' => 'after'), $context);
  169. //debug($res);
  170. $this->assertFalse($res);
  171. $date = new Time('2010-02-25');
  172. $context = array('data' => array('after' => new Time('2010-02-25')));
  173. $res = $this->Users->validateDate($date, array('after' => 'after'), $context);
  174. //debug($res);
  175. $this->assertTrue($res);
  176. $date = new Time('2010-02-25');
  177. $context = array('data' => array('after' => new Time('2010-02-25')));
  178. $res = $this->Users->validateDate($date, array('after' => 'after', 'min' => 1), $context);
  179. //debug($res);
  180. $this->assertFalse($res);
  181. $date = new Time('2010-02-25');
  182. $context = array('data' => array('after' => new Time('2010-02-24')));
  183. $res = $this->Users->validateDate($date, array('after' => 'after', 'min' => 2), $context);
  184. //debug($res);
  185. $this->assertFalse($res);
  186. $date = new Time('2010-02-25');
  187. $context = array('data' => array('after' => new Time('2010-02-24')));
  188. $res = $this->Users->validateDate($date, array('after' => 'after', 'min' => 1), $context);
  189. //debug($res);
  190. $this->assertTrue($res);
  191. $date = new Time('2010-02-25');
  192. $context = array('data' => array('after' => new Time('2010-02-24')));
  193. $res = $this->Users->validateDate($date, array('after' => 'after', 'min' => 2), $context);
  194. //debug($res);
  195. $this->assertFalse($res);
  196. $date = new Time('2010-02-24');
  197. $context = array('data' => array('before' => new Time('2010-02-24')));
  198. $res = $this->Users->validateDate($date, array('before' => 'before', 'min' => 1), $context);
  199. //debug($res);
  200. $this->assertFalse($res);
  201. $date = new Time('2010-02-24');
  202. $context = array('data' => array('before' => new Time('2010-02-25')));
  203. $res = $this->Users->validateDate($date, array('before' => 'before', 'min' => 1), $context);
  204. //debug($res);
  205. $this->assertTrue($res);
  206. $date = new Time('2010-02-24');
  207. $context = array('data' => array('before' => new Time('2010-02-25')));
  208. $res = $this->Users->validateDate($date, array('before' => 'before', 'min' => 2), $context);
  209. //debug($res);
  210. $this->assertFalse($res);
  211. $date = new Time('2010-02-24');
  212. $context = array('data' => array('before' => new Time('2010-02-26')));
  213. $res = $this->Users->validateDate($date, array('before' => 'before', 'min' => 2), $context);
  214. //debug($res);
  215. $this->assertTrue($res);
  216. }
  217. /**
  218. * TableTest::testValidateDatetime()
  219. *
  220. * @return void
  221. */
  222. public function testValidateDatetime() {
  223. $date = new Time('2010-01-22 11:11:11');
  224. $res = $this->Users->validateDatetime($date);
  225. //debug($res);
  226. $this->assertTrue($res);
  227. /*
  228. $date = new Time('2010-01-22 11:61:11');
  229. $res = $this->Users->validateDatetime($date);
  230. //debug($res);
  231. $this->assertFalse($res);
  232. */
  233. //FIXME
  234. $date = new Time('2010-02-29 11:11:11');
  235. $res = $this->Users->validateDatetime($date);
  236. //debug($res);
  237. //$this->assertFalse($res);
  238. $this->assertTrue($res);
  239. $date = null;
  240. $res = $this->Users->validateDatetime($date, array('allowEmpty' => true));
  241. //debug($res);
  242. $this->assertTrue($res);
  243. /*
  244. $date = new Time => '0000-00-00 00:00:00');
  245. $res = $this->Users->validateDatetime($date, array('allowEmpty' => true));
  246. //debug($res);
  247. $this->assertTrue($res);
  248. */
  249. $date = new Time('2010-02-23 11:11:11');
  250. $context = array('data' => array('after' => new Time('2010-02-22 11:11:11')));
  251. $res = $this->Users->validateDatetime($date, array('after' => 'after'), $context);
  252. //debug($res);
  253. $this->assertTrue($res);
  254. $date = new Time('2010-02-23 11:11:11');
  255. $context = array('data' => array('after' => new Time('2010-02-24 11:11:11')));
  256. $res = $this->Users->validateDatetime($date, array('after' => 'after'), $context);
  257. //debug($res);
  258. $this->assertFalse($res);
  259. $date = new Time('2010-02-23 11:11:11');
  260. $context = array('data' => array('after' => new Time('2010-02-23 11:11:11')));
  261. $res = $this->Users->validateDatetime($date, array('after' => 'after'), $context);
  262. //debug($res);
  263. $this->assertFalse($res);
  264. $date = new Time('2010-02-23 11:11:11');
  265. $context = array('data' => array('after' => new Time('2010-02-23 11:11:11')));
  266. $res = $this->Users->validateDatetime($date, array('after' => 'after', 'min' => 1), $context);
  267. //debug($res);
  268. $this->assertFalse($res);
  269. $date = new Time('2010-02-23 11:11:11');
  270. $context = array('data' => array('after' => new Time('2010-02-23 11:11:11')));
  271. $res = $this->Users->validateDatetime($date, array('after' => 'after', 'min' => 0), $context);
  272. //debug($res);
  273. $this->assertTrue($res);
  274. $date = new Time('2010-02-23 11:11:11');
  275. $context = array('data' => array('after' => new Time('2010-02-23 11:11:10')));
  276. $res = $this->Users->validateDatetime($date, array('after' => 'after'), $context);
  277. //debug($res);
  278. $this->assertTrue($res);
  279. $date = new Time('2010-02-23 11:11:11');
  280. $context = array('data' => array('after' => new Time('2010-02-23 11:11:12')));
  281. $res = $this->Users->validateDatetime($date, array('after' => 'after'), $context);
  282. //debug($res);
  283. $this->assertFalse($res);
  284. }
  285. /**
  286. * TableTest::testValidateTime()
  287. *
  288. * @return void
  289. */
  290. public function testValidateTime() {
  291. $date = '11:21:11';
  292. $res = $this->Users->validateTime($date);
  293. //debug($res);
  294. $this->assertTrue($res);
  295. $date = '11:71:11';
  296. $res = $this->Users->validateTime($date);
  297. //debug($res);
  298. $this->assertFalse($res);
  299. $date = '2010-02-23 11:11:11';
  300. $context = array('data' => array('before' => new Time('2010-02-23 11:11:12')));
  301. $res = $this->Users->validateTime($date, array('before' => 'before'), $context);
  302. //debug($res);
  303. $this->assertTrue($res);
  304. $date = '2010-02-23 11:11:11';
  305. $context = array('data' => array('after' => new Time('2010-02-23 11:11:12')));
  306. $res = $this->Users->validateTime($date, array('after' => 'after'), $context);
  307. //debug($res);
  308. $this->assertFalse($res);
  309. }
  310. /**
  311. * TableTest::testValidateUrl()
  312. *
  313. * @return void
  314. */
  315. public function testValidateUrl() {
  316. $data = 'www.dereuromark.de';
  317. $res = $this->Users->validateUrl($data, array('allowEmpty' => true));
  318. $this->assertTrue($res);
  319. $data = 'www.xxxde';
  320. $res = $this->Users->validateUrl($data, array('allowEmpty' => true));
  321. $this->assertFalse($res);
  322. $data = 'www.dereuromark.de';
  323. $res = $this->Users->validateUrl($data, array('allowEmpty' => true, 'autoComplete' => false));
  324. $this->assertFalse($res);
  325. $data = 'http://www.dereuromark.de';
  326. $res = $this->Users->validateUrl($data, array('allowEmpty' => true, 'autoComplete' => false));
  327. $this->assertTrue($res);
  328. $data = 'www.dereuromark.de';
  329. $res = $this->Users->validateUrl($data, array('strict' => true));
  330. $this->assertTrue($res); # aha
  331. $data = 'http://www.dereuromark.de';
  332. $res = $this->Users->validateUrl($data, array('strict' => false));
  333. $this->assertTrue($res);
  334. $this->skipIf(empty($_SERVER['HTTP_HOST']), 'No HTTP_HOST');
  335. $data = 'http://xyz.de/some/link';
  336. $res = $this->Users->validateUrl($data, array('deep' => false, 'sameDomain' => true));
  337. $this->assertFalse($res);
  338. $data = '/some/link';
  339. $res = $this->Users->validateUrl($data, array('deep' => false, 'autoComplete' => true));
  340. $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
  341. $data = 'http://' . $_SERVER['HTTP_HOST'] . '/some/link';
  342. $res = $this->Users->validateUrl($data, array('deep' => false));
  343. $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
  344. $data = '/some/link';
  345. $res = $this->Users->validateUrl($data, array('deep' => false, 'autoComplete' => false));
  346. $this->assertTrue((env('REMOTE_ADDR') !== '127.0.0.1') ? !$res : $res);
  347. //$this->skipIf(strpos($_SERVER['HTTP_HOST'], '.') === false, 'No online HTTP_HOST');
  348. $data = '/some/link';
  349. $res = $this->Users->validateUrl($data, array('deep' => false, 'sameDomain' => true));
  350. $this->assertTrue($_SERVER['HTTP_HOST'] === 'localhost' ? !$res : $res);
  351. $data = 'https://github.com/';
  352. $res = $this->Users->validateUrl($data, array('deep' => false));
  353. $this->assertTrue($res);
  354. $data = 'https://github.com/';
  355. $res = $this->Users->validateUrl($data, array('deep' => true));
  356. $this->assertTrue($res);
  357. }
  358. /**
  359. * TableTest::testValidateUnique()
  360. *
  361. * @return void
  362. */
  363. public function _testValidateUnique() {
  364. $this->Post->validate['title'] = array(
  365. 'validateUnique' => array(
  366. 'rule' => 'validateUnique',
  367. 'message' => 'valErrRecordTitleExists'
  368. ),
  369. );
  370. $data = array(
  371. 'title' => 'abc',
  372. 'published' => 'N'
  373. );
  374. $this->Post->create($data);
  375. $res = $this->Post->validates();
  376. $this->assertTrue($res);
  377. $res = $this->Post->save($res, array('validate' => false));
  378. $this->assertTrue((bool)$res);
  379. $this->Post->create();
  380. $res = $this->Post->save($data);
  381. $this->assertFalse($res);
  382. $this->Post->validate['title'] = array(
  383. 'validateUnique' => array(
  384. 'rule' => array('validateUnique', array('published')),
  385. 'message' => 'valErrRecordTitleExists'
  386. ),
  387. );
  388. $data = array(
  389. 'title' => 'abc',
  390. 'published' => 'Y'
  391. );
  392. $this->Post->create($data);
  393. $res = $this->Post->validates();
  394. $this->assertTrue($res);
  395. $res = $this->Post->save($res, array('validate' => false));
  396. $this->assertTrue((bool)$res);
  397. $this->Post->create();
  398. $res = $this->Post->save($data);
  399. $this->assertFalse($res);
  400. }
  401. }