PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/yandod/candycane
PHP | 276 lines | 240 code | 23 blank | 13 comment | 2 complexity | 6ecb35964ed1fbad9c1fc689f79e8b1b MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. App::uses('FetcherComponent', 'Controller/Component');
  3. App::uses('Controller', 'Controller');
  4. App::uses('CakeRequest', 'Network');
  5. App::uses('CakeResponse', 'Network');
  6. App::uses('ComponentCollection', 'Controller');
  7. class FetcherComponentTestController extends Controller
  8. {
  9. }
  10. class FetcherComponentTest extends CakeTestCase
  11. {
  12. public $autoFixtures = false;
  13. public $fixtures = array(
  14. 'app.issue',
  15. 'app.project',
  16. 'app.tracker',
  17. 'app.issue_status',
  18. 'app.user',
  19. 'app.version',
  20. 'app.enumeration',
  21. 'app.issue_category',
  22. 'app.token',
  23. 'app.member',
  24. 'app.role',
  25. 'app.user_preference',
  26. 'app.custom_fields_project',
  27. 'app.enabled_module',
  28. 'app.time_entry',
  29. 'app.changeset',
  30. 'app.changesets_issue',
  31. 'app.attachment',
  32. 'app.projects_tracker',
  33. 'app.custom_value',
  34. 'app.custom_field',
  35. 'app.watcher',
  36. 'app.journal',
  37. 'app.journal_detail',
  38. 'app.news',
  39. 'app.comment',
  40. 'app.document',
  41. 'app.wiki',
  42. 'app.wiki_page',
  43. 'app.wiki_content',
  44. 'app.wiki_content_version',
  45. 'app.wiki_redirect',
  46. 'app.workflow'
  47. );
  48. public $Controller = null;
  49. /**
  50. * @var FetcherComponent
  51. */
  52. public $Component = null;
  53. public $Project = null;
  54. public function startTest()
  55. {
  56. $this->loadFixtures(
  57. 'Project',
  58. 'Version',
  59. 'User',
  60. 'Role',
  61. 'Member',
  62. 'Issue',
  63. 'Journal',
  64. 'JournalDetail',
  65. 'Tracker',
  66. 'ProjectsTracker',
  67. 'IssueStatus',
  68. 'EnabledModule',
  69. 'IssueCategory',
  70. 'TimeEntry',
  71. 'Enumeration',
  72. 'CustomValue',
  73. 'CustomField',
  74. 'News',
  75. 'Comment',
  76. 'Document',
  77. 'CustomFieldsProject',
  78. 'Changeset',
  79. 'ChangesetsIssue',
  80. 'Token',
  81. 'UserPreference',
  82. 'Watcher',
  83. 'Attachment',
  84. 'Wiki'
  85. );
  86. $this->Project = ClassRegistry::init('Project');
  87. $this->Project->read(null, 1);
  88. $Collection = new ComponentCollection();
  89. $CakeRequest = new CakeRequest();
  90. $CakeResponse = new CakeResponse();
  91. $this->Controller = new FetcherComponentTestController($CakeRequest, $CakeResponse);
  92. $this->Component = new FetcherComponent($Collection);
  93. $this->Component->initialize($this->Controller);
  94. }
  95. public function test_activity_without_subprojects()
  96. {
  97. $User = ClassRegistry::init('User');
  98. $user = $User->read(null, 6); // User.anonymous
  99. $this->Component->fetch($user['User'], array('project' => $this->Project->data));
  100. $events = $this->Component->events(date('Y-m-d', strtotime('-30 day')), date('Y-m-d', strtotime('+1 day')));
  101. $this->assertNotNull($events);
  102. $this->assertEqual(4, count($events));
  103. $this->assertEqual('issue-note', $events[0]['type']);
  104. $this->assertEqual(2, $events[0]['id']);
  105. $this->assertEqual('issue-note', $events[1]['type']);
  106. $this->assertEqual(1, $events[1]['id']);
  107. $this->assertEqual('issue', $events[2]['type']);
  108. $this->assertEqual(1, $events[2]['id']);
  109. $this->assertEqual('issue', $events[3]['type']);
  110. $this->assertEqual(7, $events[3]['id']);
  111. foreach (range(0, 2) as $i) {
  112. $this->assertTrue(
  113. strtotime($events[$i]['datetime']) >= strtotime($events[$i + 1]['datetime']),
  114. "Compare dates {$events[$i]['datetime']} > {$events[$i + 1]['datetime']}"
  115. );
  116. $this->assertFalse(
  117. (strtotime($events[$i]['datetime']) == strtotime($events[$i + 1]['datetime']) &&
  118. $events[$i]['type'] == $events[$i + 1]['type'] &&
  119. $events[$i]['id'] < $events[$i + 1]['id']
  120. ),
  121. "Compare id {$events[$i]['id']} > {$events[$i + 1]['id']}"
  122. );
  123. }
  124. }
  125. public function test_activity_with_subprojects()
  126. {
  127. $User = ClassRegistry::init('User');
  128. $user = $User->read(null, 6); // User.anonymous
  129. $this->Component->fetch($user['User'], array('project' => $this->Project->data, 'with_subprojects' => 1));
  130. $events = $this->Component->events(date('Y-m-d', strtotime('-30 day')), date('Y-m-d', strtotime('+1 day')));
  131. $this->assertNotNull($events);
  132. $this->assertEqual(5, count($events));
  133. $this->assertEqual('issue-note', $events[0]['type']);
  134. $this->assertEqual(2, $events[0]['id']);
  135. $this->assertEqual('issue', $events[2]['type']);
  136. $this->assertEqual(1, $events[2]['id']);
  137. # subproject issue
  138. $this->assertEqual('issue-note', $events[1]['type']);
  139. $this->assertEqual(1, $events[1]['id']);
  140. $this->assertEqual('issue', $events[3]['type']);
  141. $this->assertEqual(5, $events[3]['id']);
  142. $this->assertEqual('issue', $events[4]['type']);
  143. $this->assertEqual(7, $events[4]['id']);
  144. }
  145. public function test_global_activity_anonymous()
  146. {
  147. $User = ClassRegistry::init('User');
  148. $user = $User->read(null, 6); // User.anonymous
  149. $this->Component->fetch($user['User']);
  150. $events = $this->Component->events(date('Y-m-d', strtotime('-30 day')), date('Y-m-d', strtotime('+1 day')));
  151. $this->assertNotNull($events);
  152. $this->assertEqual(5, count($events));
  153. $this->assertEqual('issue-note', $events[0]['type']);
  154. $this->assertEqual(2, $events[0]['id']);
  155. $this->assertEqual('issue', $events[2]['type']);
  156. $this->assertEqual(1, $events[2]['id']);
  157. # subproject issue
  158. $this->assertEqual('issue-note', $events[1]['type']);
  159. $this->assertEqual(1, $events[1]['id']);
  160. $this->assertEqual('Bug #1: Can\'t print recipes', $events[1]['title']);
  161. $this->assertEqual('issue', $events[3]['type']);
  162. $this->assertEqual(5, $events[3]['id']);
  163. $this->assertEqual('Bug #5: Subproject issue', $events[3]['title']);
  164. $this->assertEqual('issue', $events[4]['type']);
  165. $this->assertEqual(7, $events[4]['id']);
  166. $this->assertEqual('Bug #7: Issue due today', $events[4]['title']);
  167. // TODO Message feature
  168. // assert events.include?(Message.find(5))
  169. # Issue of a private project
  170. }
  171. public function test_global_activity_logged_user()
  172. {
  173. $User = ClassRegistry::init('User');
  174. $user = $User->find_by_id_logged(2); // manager
  175. $this->Component->fetch($user);
  176. $events = $this->Component->events(date('Y-m-d', strtotime('-30 day')), date('Y-m-d', strtotime('+1 day')));
  177. $this->assertNotNull($events);
  178. $this->assertEqual(7, count($events));
  179. $this->assertEqual('issue', $events[0]['type']);
  180. $this->assertEqual(6, $events[0]['id']);
  181. $this->assertEqual('issue-note', $events[1]['type']);
  182. $this->assertEqual('Bug #1: Can\'t print recipes', $events[1]['title']);
  183. # Issue of a private project the user belongs to
  184. $this->assertEqual(2, $events[1]['id']);
  185. $this->assertEqual('issue-note', $events[1]['type']);
  186. $this->assertEqual('Bug #1: Can\'t print recipes', $events[1]['title']);
  187. $this->assertEqual(1, $events[2]['id']);
  188. $this->assertEqual('issue', $events[3]['type']);
  189. $this->assertEqual('Bug #1: Can\'t print recipes', $events[3]['title']);
  190. # Issue of a private project the user belongs to
  191. $this->assertEqual(1, $events[3]['id']);
  192. $this->assertEqual('issue', $events[4]['type']);
  193. $this->assertEqual(4, $events[4]['id']);
  194. $this->assertEqual('issue', $events[5]['type']);
  195. $this->assertEqual(5, $events[5]['id']);
  196. $this->assertEqual('issue', $events[6]['type']);
  197. $this->assertEqual(7, $events[6]['id']);
  198. }
  199. public function test_user_activity()
  200. {
  201. $User = ClassRegistry::init('User');
  202. $user = $User->find_by_id_logged(2); // manager
  203. $anonymous = $User->read(null, 6); // User.anonymous
  204. $this->Component->fetch($anonymous['User'], array('author' => $user));
  205. $events = $this->Component->events(null, null, array('limit' => 10));
  206. $this->assertTrue(count($events) > 0);
  207. $this->assertTrue(count($events) <= 10);
  208. $this->assertEqual(array('2' => count($events)), array_count_values(Set::extract('{n}.author.id', $events)));
  209. }
  210. public function test_news_and_files_activity()
  211. {
  212. $this->loadFixtures('Attachment');
  213. $User = ClassRegistry::init('User');
  214. $user = $User->find_by_id_logged(2); // manager
  215. $this->Component->fetch($user);
  216. $events = $this->Component->events(
  217. date('Y-m-d', strtotime('2006-07-19 0:0:0')),
  218. date('Y-m-d', strtotime('2006-07-20 0:0:0'))
  219. );
  220. $this->assertNotNull($events);
  221. $this->assertEqual('news', $events[0]['type']);
  222. $this->assertEqual(2, $events[0]['id']);
  223. $this->assertEqual(
  224. array('controller' => 'news', 'action' => 'show', 'id' => 2, 'project_id' => 1),
  225. $events[0]['url']
  226. );
  227. $this->assertEqual('news', $events[1]['type']);
  228. $this->assertEqual(1, $events[1]['id']);
  229. $this->assertEqual(
  230. array('controller' => 'news', 'action' => 'show', 'id' => 1, 'project_id' => 1),
  231. $events[1]['url']
  232. );
  233. //$this->assertEqual('issue', $events[2]['type']);
  234. //$this->assertEqual(3, $events[2]['id']);
  235. //$this->assertEqual(array('controller'=>'issues', 'action'=>'show',3), $events[2]['url']);
  236. }
  237. public function test_documents_activity()
  238. {
  239. $this->loadFixtures('Attachment');
  240. $User = ClassRegistry::init('User');
  241. $user = $User->find_by_id_logged(2); // manager
  242. $this->Component->fetch($user);
  243. $events = $this->Component->events(
  244. date('Y-m-d', strtotime('2007-01-27 0:0:0')),
  245. date('Y-m-d', strtotime('2007-01-28 0:0:0'))
  246. );
  247. $this->assertNotNull($events);
  248. $this->assertEqual(1, count($events));
  249. $this->assertEqual('document', $events[0]['type']);
  250. $this->assertEqual(1, $events[0]['id']);
  251. $this->assertEqual(array('controller' => 'documents', 'action' => 'show', 'id' => 1), $events[0]['url']);
  252. }
  253. }