PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/app/protected/modules/reports/tests/unit/ReportTest.php

https://bitbucket.org/zurmo/zurmo/
PHP | 347 lines | 262 code | 31 blank | 54 comment | 0 complexity | 4e78fc6cd283421e92daae3ae0be343f MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2015 Zurmo Inc.
  5. *
  6. * Zurmo is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * Zurmo is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
  24. * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
  25. *
  26. * The interactive user interfaces in original and modified versions
  27. * of this program must display Appropriate Legal Notices, as required under
  28. * Section 5 of the GNU Affero General Public License version 3.
  29. *
  30. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  31. * these Appropriate Legal Notices must retain the display of the Zurmo
  32. * logo and Zurmo copyright notice. If the display of the logo is not reasonably
  33. * feasible for technical reasons, the Appropriate Legal Notices must display the words
  34. * "Copyright Zurmo Inc. 2015. All rights reserved".
  35. ********************************************************************************/
  36. class ReportTest extends ZurmoBaseTest
  37. {
  38. public static function setUpBeforeClass()
  39. {
  40. parent::setUpBeforeClass();
  41. SecurityTestHelper::createSuperAdmin();
  42. UserTestHelper::createBasicUser('billy');
  43. }
  44. public function setup()
  45. {
  46. parent::setUp();
  47. Yii::app()->user->userModel = User::getByUsername('super');
  48. DisplayAttributeForReportForm::resetCount();
  49. DrillDownDisplayAttributeForReportForm::resetCount();
  50. }
  51. public function testHasRuntimeFilters()
  52. {
  53. $report = new Report();
  54. $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
  55. $filter = new FilterForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  56. $filter->attributeIndexOrDerivedType = 'string';
  57. $filter->value = 'aValue';
  58. $filter->operator = 'equals';
  59. $report->addFilter($filter);
  60. $this->assertFalse($report->hasRuntimeFilters());
  61. $filter2 = new FilterForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  62. $filter2->attributeIndexOrDerivedType = 'string';
  63. $filter2->value = 'aValue';
  64. $filter2->operator = 'equals';
  65. $filter2->availableAtRunTime = true;
  66. $report->addFilter($filter2);
  67. $this->assertTrue($report->hasRuntimeFilters());
  68. }
  69. public function testGetDisplayAttributeIndex()
  70. {
  71. $report = new Report();
  72. $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
  73. $displayAttribute = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem',
  74. $report->getType());
  75. $displayAttribute->attributeIndexOrDerivedType = 'phone';
  76. $displayAttribute->label = 'someNewLabel';
  77. $report->addDisplayAttribute($displayAttribute);
  78. $displayAttribute2 = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem',
  79. $report->getType());
  80. $displayAttribute2->attributeIndexOrDerivedType = 'string';
  81. $displayAttribute2->label = 'someNewLabel 2';
  82. $report->addDisplayAttribute($displayAttribute2);
  83. $this->assertEquals(1, $report->getDisplayAttributeIndex('string'));
  84. $this->assertNull($report->getDisplayAttributeIndex('notHere'));
  85. }
  86. public function testGetDisplayAttributeByAttribute()
  87. {
  88. $report = new Report();
  89. $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
  90. $displayAttribute = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem',
  91. $report->getType());
  92. $displayAttribute->attributeIndexOrDerivedType = 'phone';
  93. $displayAttribute->label = 'someNewLabel';
  94. $report->addDisplayAttribute($displayAttribute);
  95. $displayAttribute2 = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem',
  96. $report->getType());
  97. $displayAttribute2->attributeIndexOrDerivedType = 'string';
  98. $displayAttribute2->label = 'someNewLabel 2';
  99. $report->addDisplayAttribute($displayAttribute2);
  100. $this->assertEquals(1, $report->getDisplayAttributeIndex('string'));
  101. $displayAttributeResult = $report->getDisplayAttributeByAttribute('phone');
  102. $this->assertEquals($displayAttribute->getAttributeIndexOrDerivedType(),
  103. $displayAttributeResult->getAttributeIndexOrDerivedType());
  104. }
  105. public function testResolveGroupBysAsFilters()
  106. {
  107. $report = new Report();
  108. $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
  109. $groupBy = new GroupByForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  110. $groupBy->attributeIndexOrDerivedType = 'lastName';
  111. $groupBy->axis = 'x';
  112. $report->addGroupBy($groupBy);
  113. $groupBy2 = new GroupByForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  114. $groupBy2->attributeIndexOrDerivedType = 'firstName';
  115. $groupBy2->axis = 'y';
  116. $report->addGroupBy($groupBy2);
  117. $this->assertNull($report->getFiltersStructure());
  118. $getData = array('groupByRowValuelastName' => '50', 'groupByRowValuefirstName' => '');
  119. $this->assertCount(0, $report->getFilters());
  120. $report->resolveGroupBysAsFilters($getData);
  121. $this->assertEquals('(1 AND 2)', $report->getFiltersStructure());
  122. $filters = $report->getFilters();
  123. $this->assertCount(2, $filters);
  124. $this->assertEquals('lastName', $filters[0]->getAttributeIndexOrDerivedType());
  125. $this->assertEquals('50', $filters[0]->value);
  126. $this->assertEquals(OperatorRules::TYPE_EQUALS, $filters[0]->operator);
  127. $this->assertEquals('firstName', $filters[1]->getAttributeIndexOrDerivedType());
  128. $this->assertEmpty ($filters[1]->value);
  129. $this->assertEquals(OperatorRules::TYPE_EQUALS, $filters[1]->operator);
  130. $report = new Report();
  131. $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
  132. $groupBy = new GroupByForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  133. $groupBy->attributeIndexOrDerivedType = 'lastName';
  134. $groupBy->axis = 'x';
  135. $report->addGroupBy($groupBy);
  136. $groupBy2 = new GroupByForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  137. $groupBy2->attributeIndexOrDerivedType = 'firstName';
  138. $groupBy2->axis = 'y';
  139. $report->addGroupBy($groupBy2);
  140. $this->assertNull($report->getFiltersStructure());
  141. $getData = array('groupByRowValuelastName' => '50');
  142. $this->assertCount(0, $report->getFilters());
  143. $report->resolveGroupBysAsFilters($getData);
  144. $this->assertEquals('(1 AND 2)', $report->getFiltersStructure());
  145. $filters = $report->getFilters();
  146. $this->assertCount(2, $filters);
  147. $this->assertEquals('lastName', $filters[0]->getAttributeIndexOrDerivedType());
  148. $this->assertEquals('50', $filters[0]->value);
  149. $this->assertEquals(OperatorRules::TYPE_EQUALS, $filters[0]->operator);
  150. $this->assertEquals('firstName', $filters[1]->getAttributeIndexOrDerivedType());
  151. $this->assertNull ($filters[1]->value);
  152. $this->assertEquals(OperatorRules::TYPE_IS_NULL, $filters[1]->operator);
  153. }
  154. public function testGetReportableModulesAndLabelsForCurrentUser()
  155. {
  156. $modulesAndLabels = Report::getReportableModulesAndLabelsForCurrentUser();
  157. $this->assertCount(11, $modulesAndLabels);
  158. Yii::app()->user->userModel = User::getByUsername('billy');
  159. $modulesAndLabels = Report::getReportableModulesAndLabelsForCurrentUser();
  160. $this->assertCount(0, $modulesAndLabels);
  161. }
  162. public function testGetReportableModulesClassNamesCurrentUserHasAccessTo()
  163. {
  164. $modulesAndLabels = Report::getReportableModulesClassNamesCurrentUserHasAccessTo();
  165. $this->assertCount(11, $modulesAndLabels);
  166. Yii::app()->user->userModel = User::getByUsername('billy');
  167. $modulesAndLabels = Report::getReportableModulesClassNamesCurrentUserHasAccessTo();
  168. $this->assertCount(0, $modulesAndLabels);
  169. }
  170. public function testCanCurrentUserProperlyRenderResults()
  171. {
  172. $billy = User::getByUsername('billy');
  173. $billy->setRight('AccountsModule', AccountsModule::RIGHT_ACCESS_ACCOUNTS);
  174. $billy->setRight('OpportunitiesModule', OpportunitiesModule::RIGHT_ACCESS_OPPORTUNITIES);
  175. $saved = $billy->save();
  176. $this->assertTrue($saved);
  177. $report = new Report();
  178. $report->setModuleClassName('ContactsModule');
  179. $report->setType (Report::TYPE_ROWS_AND_COLUMNS);
  180. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  181. //Now set to Billy
  182. Yii::app()->user->userModel = $billy;
  183. //Billy can't see the contacts module.
  184. $this->assertFalse($report->canCurrentUserProperlyRenderResults());
  185. //Billy can see accounts
  186. $report->setModuleClassName('AccountsModule');
  187. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  188. //A filter on accounts is ok for Billy to see
  189. $filter = new FilterForReportForm('AccountsModule', 'Account', $report->getType());
  190. $filter->attributeIndexOrDerivedType = 'officePhone';
  191. $filter->value = 'aValue';
  192. $filter->operator = 'equals';
  193. $report->addFilter($filter);
  194. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  195. //A filter on contacts is not ok for Billy to see
  196. $filter2 = new FilterForReportForm('AccountsModule', 'Account', $report->getType());
  197. $filter2->attributeIndexOrDerivedType = 'contacts___lastName';
  198. $filter2->value = 'aValue';
  199. $filter2->operator = 'equals';
  200. $report->addFilter($filter2);
  201. $this->assertFalse($report->canCurrentUserProperlyRenderResults());
  202. //A related filter on opportunities would be ok for Billy to see
  203. $report->removeAllFilters();
  204. $filter = new FilterForReportForm('AccountsModule', 'Account', $report->getType());
  205. $filter->attributeIndexOrDerivedType = 'opportunities___name';
  206. $filter->value = 'aValue';
  207. $filter->operator = 'equals';
  208. $report->addFilter($filter);
  209. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  210. $report->removeAllFilters();
  211. //Billy can see a groupBy on Accounts
  212. $groupBy = new GroupByForReportForm('AccountsModule', 'Account', $report->getType());
  213. $groupBy->attributeIndexOrDerivedType = 'name';
  214. $groupBy->axis = 'y';
  215. $report->addGroupBy($groupBy);
  216. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  217. //Billy cannot see a related groupBy on Contacts
  218. $groupBy = new GroupByForReportForm('AccountsModule', 'Account', $report->getType());
  219. $groupBy->attributeIndexOrDerivedType = 'contacts___lastName';
  220. $groupBy->axis = 'y';
  221. $report->addGroupBy($groupBy);
  222. $this->assertFalse($report->canCurrentUserProperlyRenderResults());
  223. //Billy can see a related groupBy on Opportunities
  224. $report->removeAllGroupBys();
  225. $groupBy = new GroupByForReportForm('AccountsModule', 'Account', $report->getType());
  226. $groupBy->attributeIndexOrDerivedType = 'opportunities___name';
  227. $groupBy->axis = 'y';
  228. $report->addGroupBy($groupBy);
  229. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  230. $report->removeAllGroupBys();
  231. //Billy can see an orderBy on Accounts
  232. $orderBy = new OrderByForReportForm('AccountsModule', 'Account', $report->getType());
  233. $orderBy->attributeIndexOrDerivedType = 'name';
  234. $orderBy->order = 'desc';
  235. $report->addOrderBy($orderBy);
  236. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  237. //Billy cannot see a related orderBy on Contacts
  238. $orderBy = new OrderByForReportForm('AccountsModule', 'Account', $report->getType());
  239. $orderBy->attributeIndexOrDerivedType = 'contacts___lastName';
  240. $orderBy->order = 'desc';
  241. $report->addOrderBy($orderBy);
  242. $this->assertFalse($report->canCurrentUserProperlyRenderResults());
  243. //Billy can see a related orderBy on Opportunities
  244. $report->removeAllOrderBys();
  245. $orderBy = new OrderByForReportForm('AccountsModule', 'Account', $report->getType());
  246. $orderBy->attributeIndexOrDerivedType = 'opportunities___name';
  247. $orderBy->order = 'desc';
  248. $report->addOrderBy($orderBy);
  249. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  250. $report->removeAllOrderBys();
  251. //Billy can see a displayAttribute on Accounts
  252. $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
  253. $displayAttribute->attributeIndexOrDerivedType = 'name';
  254. $displayAttribute->label = 'someNewLabel';
  255. $report->addDisplayAttribute($displayAttribute);
  256. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  257. //Billy cannot see a related displayAttribute on Contacts
  258. $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
  259. $displayAttribute->attributeIndexOrDerivedType = 'contacts___firstName';
  260. $displayAttribute->label = 'someNewLabel';
  261. $report->addDisplayAttribute($displayAttribute);
  262. $this->assertFalse($report->canCurrentUserProperlyRenderResults());
  263. //Billy can see a related displayAttribute on Opportunities
  264. $report->removeAllDisplayAttributes();
  265. $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
  266. $displayAttribute->attributeIndexOrDerivedType = 'opportunities___name';
  267. $displayAttribute->label = 'someNewLabel';
  268. $report->addDisplayAttribute($displayAttribute);
  269. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  270. $report->removeAllDisplayAttributes();
  271. //Billy can see a drillDownDisplayAttribute on Accounts
  272. $drillDownDisplayAttribute = new DrillDownDisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
  273. $drillDownDisplayAttribute->attributeIndexOrDerivedType = 'name';
  274. $drillDownDisplayAttribute->label = 'someNewLabel';
  275. $report->addDrillDownDisplayAttribute($drillDownDisplayAttribute);
  276. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  277. //Billy cannot see a related drillDownDisplayAttribute on Contacts
  278. $drillDownDisplayAttribute = new DrillDownDisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
  279. $drillDownDisplayAttribute->attributeIndexOrDerivedType = 'contacts___firstName';
  280. $drillDownDisplayAttribute->label = 'someNewLabel';
  281. $report->addDrillDownDisplayAttribute($drillDownDisplayAttribute);
  282. $this->assertFalse($report->canCurrentUserProperlyRenderResults());
  283. //Billy can see a related drillDownDisplayAttribute on Opportunities
  284. $report->removeAllDrillDownDisplayAttributes();
  285. $drillDownDisplayAttribute = new DrillDownDisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
  286. $drillDownDisplayAttribute->attributeIndexOrDerivedType = 'opportunities___name';
  287. $drillDownDisplayAttribute->label = 'someNewLabel';
  288. $report->addDrillDownDisplayAttribute($drillDownDisplayAttribute);
  289. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  290. }
  291. public function testRemoveRuntimeFilters()
  292. {
  293. $report = new Report();
  294. $report->setModuleClassName('ContactsModule');
  295. $report->setType (Report::TYPE_ROWS_AND_COLUMNS);
  296. //A filter available at runtime
  297. $filter = new FilterForReportForm('AccountsModule', 'Account', $report->getType());
  298. $filter->availableAtRunTime = true;
  299. $filter->attributeIndexOrDerivedType = 'officePhone';
  300. $filter->value = 'aValue';
  301. $filter->operator = 'equals';
  302. $report->addFilter($filter);
  303. //A filter not available at runtime
  304. $filter2 = new FilterForReportForm('AccountsModule', 'Account', $report->getType());
  305. $filter2->availableAtRunTime = false;
  306. $filter2->attributeIndexOrDerivedType = 'contacts___lastName';
  307. $filter2->value = 'aValue';
  308. $filter2->operator = 'equals';
  309. $report->addFilter($filter2);
  310. $report->removeRuntimeFilters();
  311. $filters = $report->getFilters();
  312. $this->assertCount(1, $filters);
  313. $this->assertEquals($filter2, $filters[0]);
  314. }
  315. }
  316. ?>