PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Test/Case/Controller/Component/QueriesComponentTest.php

http://github.com/yandod/candycane
PHP | 483 lines | 417 code | 23 blank | 43 comment | 0 complexity | 9d8a948907126bfab0795c06ea034443 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. App::uses('QueriesComponent', 'Controller/Component');
  3. App::uses('Query', 'Model');
  4. App::uses('Project', 'Model');
  5. App::uses('AppController', 'Controller');
  6. App::uses('CakeRequest', 'Network');
  7. App::uses('CakeResponse', 'Network');
  8. App::uses('ComponentCollection', 'Controller');
  9. /**
  10. * testcase for QueriesComponent
  11. *
  12. * @author yando
  13. */
  14. class QueriesComponentTestController extends AppController
  15. {
  16. public $uses = array(
  17. 'Query',
  18. 'Project'
  19. );
  20. /**
  21. * @var Project
  22. */
  23. public $Project;
  24. /**
  25. * @var Query;
  26. */
  27. public $Query;
  28. }
  29. class QueriesComponentTest extends CakeTestCase
  30. {
  31. /**
  32. * @var QueriesComponent
  33. */
  34. public $Component;
  35. public $Controller;
  36. //public $autoFixtures = false;
  37. public $fixtures = array(
  38. 'app.issue_status',
  39. 'app.project',
  40. 'app.version',
  41. 'app.user',
  42. 'app.enumeration',
  43. 'app.query'
  44. );
  45. /**
  46. * startTest
  47. * initialize test case.
  48. */
  49. public function startTest()
  50. {
  51. $this->loadFixtures();
  52. $Collection = new ComponentCollection();
  53. $CakeRequest = new CakeRequest();
  54. $CakeResponse = new CakeResponse();
  55. $this->Controller = new QueriesComponentTestController($CakeRequest, $CakeResponse);
  56. $this->Controller->Query = ClassRegistry::init('Query');
  57. $this->Controller->Project = ClassRegistry::init('Project');
  58. $this->Component = new QueriesComponent($Collection);
  59. $this->Component->startup($this->Controller);
  60. }
  61. /**
  62. * testGetOptionValue
  63. * test case for get_option_value method
  64. */
  65. public function testGetOptionValue()
  66. {
  67. $this->assertEqual($this->Component->get_option_value(1),1);
  68. $this->assertEqual($this->Component->get_option_value("ABC"),"ABC");
  69. $this->assertEqual($this->Component->get_option_value(array(3)),3);
  70. $this->assertEqual($this->Component->get_option_value(array(3,2,1)),array(3,2,1));
  71. }
  72. /**
  73. * first access without any parameter
  74. */
  75. public function testRetrieveQueryDefault()
  76. {
  77. //data should be empty before process
  78. $this->assertEqual(
  79. $this->Controller->request->data,
  80. array()
  81. );
  82. //
  83. $this->assertEqual($this->Component->retrieve_query(),null);
  84. $this->assertEqual(
  85. $this->Controller->request->data,
  86. array(
  87. 'Filter' => array(
  88. 'fields_status_id' => 'status_id',
  89. 'operators_status_id' => 'o',
  90. 'values_status_id' => array(
  91. 0 => '',
  92. )
  93. )
  94. )
  95. );
  96. $this->assertEqual(
  97. $this->Controller->viewVars['show_filters'],
  98. array(
  99. 'status_id' => array(
  100. 'operator' => 'o',
  101. 'values' => array(
  102. 0 => ''
  103. )
  104. )
  105. )
  106. );
  107. }
  108. /**
  109. * access with query id in db.
  110. */
  111. public function testRetrieveQueryFromDb()
  112. {
  113. //data should be empty before process
  114. $this->assertEqual(
  115. $this->Controller->request->data,
  116. array()
  117. );
  118. //get parameter for query_id = 1
  119. $this->assertEqual($this->Component->retrieve_query(1),null);
  120. $this->assertEqual(
  121. $this->Controller->request->data,
  122. array(
  123. 'Filter' => array(
  124. 'fields_status_id' => 'status_id',
  125. 'operators_status_id' => 'o',
  126. 'values_status_id' => array(
  127. 0 => '1',
  128. ),
  129. 'fields_tracker_id' => 'tracker_id',
  130. 'operators_tracker_id' => '=',
  131. 'values_tracker_id' => array(
  132. 0 => '2'
  133. )
  134. )
  135. )
  136. );
  137. //check state of show_filters
  138. $this->assertEqual(
  139. $this->Controller->viewVars['show_filters'],
  140. array(
  141. 'status_id' => array(
  142. 'operator' => 'o',
  143. 'values' => array(
  144. 0 => 1
  145. )
  146. ),
  147. 'tracker_id' => array(
  148. 'operator' => '=',
  149. 'values' => array(
  150. 0 => 2
  151. )
  152. )
  153. )
  154. );
  155. }
  156. public function testRetrieveQueryFromQueryString()
  157. {
  158. //data should be empty before process
  159. $this->assertEqual(
  160. $this->Controller->request->data,
  161. array()
  162. );
  163. //get parameter for query string
  164. $this->Component->controller->request->query = array(
  165. 'set_filter' => 1,
  166. 'values' => array(
  167. 'status_id' => array(1,2,3),
  168. 'tracker_id' => '3',
  169. ),
  170. 'fields' => array(
  171. 'status_id',
  172. 'tracker_id'
  173. ),
  174. 'operators' => array(
  175. 'status_id' => '=',
  176. 'tracker_id' => '='
  177. )
  178. );
  179. $this->assertEqual($this->Component->retrieve_query(),null);
  180. $this->assertEqual(
  181. $this->Controller->request->data,
  182. array(
  183. 'Filter' => array(
  184. 'fields_tracker_id' => 'tracker_id',
  185. 'operators_tracker_id' => '=',
  186. 'values_tracker_id' => '3',
  187. 'fields_status_id' => 'status_id',
  188. 'operators_status_id' => '=',
  189. 'values_status_id' => array(1,2,3),
  190. )
  191. )
  192. );
  193. //check state of show_filters
  194. $this->assertEqual(
  195. $this->Controller->viewVars['show_filters'],
  196. array(
  197. 'status_id' => array(
  198. 'values' => array(
  199. 6 => 'Rejected',
  200. 1 => 'New',
  201. 2 => 'Assigned',
  202. 3 => 'Resolved',
  203. 4 => 'Feedback',
  204. 5 => 'Closed',
  205. ),
  206. 'type' => 'list_status',
  207. 'order' => 1,
  208. 'operators' => array(
  209. 'o' => 'open',
  210. '=' => 'is',
  211. '!' => 'is not',
  212. 'c' => 'closed',
  213. '*' => 'all',
  214. )
  215. ),
  216. 'tracker_id' => array(
  217. 'values' => array(),
  218. 'type' => 'list',
  219. 'order' => 2,
  220. 'operators' => array(
  221. '=' => 'is',
  222. '!' => 'is not',
  223. ),
  224. )
  225. )
  226. );
  227. }
  228. public function testRetrieveQueryFromQueryStringWithNotEqual()
  229. {
  230. //data should be empty before process
  231. $this->assertEqual(
  232. $this->Controller->request->data,
  233. array()
  234. );
  235. //get parameter for query string
  236. $this->Component->controller->request->query = array(
  237. 'set_filter' => 1,
  238. 'values' => array(
  239. 'status_id' => array(1),
  240. ),
  241. 'fields' => array(
  242. 'status_id',
  243. ),
  244. 'operators' => array(
  245. 'status_id' => '!',
  246. )
  247. );
  248. $this->assertEqual($this->Component->retrieve_query(),null);
  249. $this->assertEqual(
  250. $this->Controller->request->data,
  251. array(
  252. 'Filter' => array(
  253. 'fields_status_id' => 'status_id',
  254. 'operators_status_id' => '!',
  255. 'values_status_id' => 1,
  256. )
  257. )
  258. );
  259. //check state of show_filters
  260. $this->assertEqual(
  261. $this->Controller->viewVars['show_filters'],
  262. array(
  263. 'status_id' => array(
  264. 'values' => array(
  265. 6 => 'Rejected',
  266. 1 => 'New',
  267. 2 => 'Assigned',
  268. 3 => 'Resolved',
  269. 4 => 'Feedback',
  270. 5 => 'Closed',
  271. ),
  272. 'type' => 'list_status',
  273. 'order' => 1,
  274. 'operators' => array(
  275. 'o' => 'open',
  276. '=' => 'is',
  277. '!' => 'is not',
  278. 'c' => 'closed',
  279. '*' => 'all',
  280. )
  281. ),
  282. )
  283. );
  284. }
  285. public function testRetrieveQueryFromQueryStringWithoutIssueStatus()
  286. {
  287. //data should be empty before process
  288. $this->assertEqual(
  289. $this->Controller->request->data,
  290. array()
  291. );
  292. //get parameter for query string
  293. $this->Component->controller->request->query = array(
  294. 'set_filter' => 1,
  295. 'values' => array(
  296. 'subject' => 'private',
  297. ),
  298. 'fields' => array(
  299. 'subject'
  300. ),
  301. 'operators' => array(
  302. 'subject' => '~'
  303. )
  304. );
  305. $this->assertEqual($this->Component->retrieve_query(),null);
  306. $this->assertEqual(
  307. $this->Controller->request->data,
  308. array(
  309. 'Filter' => array(
  310. 'fields_subject' => 'subject',
  311. 'operators_subject' => '~',
  312. 'values_subject' => 'private',
  313. )
  314. )
  315. );
  316. //check state of show_filters
  317. $this->assertEqual(
  318. $this->Controller->viewVars['show_filters'],
  319. array(
  320. 'subject' => array(
  321. 'type' => 'text',
  322. 'order' => 8,
  323. 'operators' => array(
  324. '~' => 'contains',
  325. '!~' => "doesn't contain",
  326. ),
  327. )
  328. )
  329. );
  330. }
  331. public function testRetrieveQueryFromQueryStringWithMe()
  332. {
  333. //data should be empty before process
  334. $this->assertEqual(
  335. $this->Controller->request->data,
  336. array()
  337. );
  338. //get parameter for query string
  339. $this->Component->controller->request->query = array(
  340. 'set_filter' => 1,
  341. 'values' => array(
  342. 'assigned_to_id' => array('me'),
  343. ),
  344. 'fields' => array(
  345. 'assigned_to_id'
  346. ),
  347. 'operators' => array(
  348. 'assigned_to_id' => '='
  349. )
  350. );
  351. $this->Component->controller->current_user = array(
  352. 'id' => 4,
  353. 'admin' => false
  354. );
  355. $this->assertEqual($this->Component->retrieve_query(),null);
  356. $this->assertEqual(
  357. $this->Controller->request->data,
  358. array(
  359. 'Filter' => array(
  360. 'fields_assigned_to_id' => 'assigned_to_id',
  361. 'operators_assigned_to_id' => '=',
  362. 'values_assigned_to_id' => 'me',
  363. )
  364. )
  365. );
  366. //check state of show_filters
  367. $this->assertEqual(
  368. $this->Controller->viewVars['show_filters'],
  369. array(
  370. 'assigned_to_id' => array(
  371. 'type' => 'list_optional',
  372. 'order' => 4,
  373. 'operators' => array(
  374. '=' => 'is',
  375. '!' => 'is not',
  376. '!*' => 'none',
  377. '*' => 'all',
  378. ),
  379. 'values' => array(
  380. 'me' => 'me'
  381. )
  382. )
  383. )
  384. );
  385. }
  386. public function testRetrieveQueryFromQueryStringWithProject()
  387. {
  388. //data should be empty before process
  389. $this->assertEqual(
  390. $this->Controller->request->data,
  391. array()
  392. );
  393. //get parameter for query string
  394. $this->Component->controller->request->query = array(
  395. 'set_filter' => 1,
  396. 'values' => array(
  397. 'created_on' => '3',
  398. ),
  399. 'fields' => array(
  400. 'created_on' => 'created_on'
  401. ),
  402. 'operators' => array(
  403. 'created_on' => '>t-'
  404. )
  405. );
  406. $this->Component->controller->current_user = array(
  407. 'id' => 4
  408. );
  409. $this->Component->controller->_project = array(
  410. 'Project' => array('id' => 2)
  411. );
  412. $this->assertEqual($this->Component->retrieve_query(),null);
  413. $this->assertEqual(
  414. $this->Controller->request->data,
  415. array(
  416. 'Filter' => array(
  417. 'fields_created_on' => 'created_on',
  418. 'operators_created_on' => '>t-',
  419. 'values_created_on' => '3',
  420. )
  421. )
  422. );
  423. //check state of show_filters
  424. $this->assertEqual(
  425. $this->Controller->viewVars['show_filters'],
  426. array(
  427. 'created_on' => array(
  428. 'type' => 'date_past',
  429. 'order' => 9,
  430. 'operators' => array(
  431. '>t-' => 'less than days ago',
  432. '<t-' => 'more than days ago',
  433. 't-' => 'days ago',
  434. 't' => 'today',
  435. 'w' => 'this week',
  436. ),
  437. )
  438. )
  439. );
  440. $this->assertEqual(
  441. $this->Component->query_filter_cond,
  442. array(
  443. 'Issue.project_id' => 2,
  444. 0 => array(
  445. 'Issue.created_on >' => date('Y-m-d 23:59:59', strtotime('-3 days')),
  446. ),
  447. 1 => array(
  448. 'Issue.created_on <=' => date('Y-m-d 23:59:59'),
  449. ),
  450. )
  451. );
  452. }
  453. }