TableTest.php 14 KB

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