PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/ddonthula/zurmofeb
PHP | 288 lines | 217 code | 29 blank | 42 comment | 0 complexity | d10056c53a80e4634f4e34b8fa2186ce MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-2-Clause, GPL-3.0, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
  5. *
  6. * Zurmo is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU 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 General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU 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 113 McHenry Road Suite 207,
  24. * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
  25. ********************************************************************************/
  26. class ReportTest extends ZurmoBaseTest
  27. {
  28. public static function setUpBeforeClass()
  29. {
  30. parent::setUpBeforeClass();
  31. SecurityTestHelper::createSuperAdmin();
  32. UserTestHelper::createBasicUser('billy');
  33. }
  34. public function setup()
  35. {
  36. parent::setUp();
  37. Yii::app()->user->userModel = User::getByUsername('super');
  38. DisplayAttributeForReportForm::resetCount();
  39. DrillDownDisplayAttributeForReportForm::resetCount();
  40. }
  41. public function testHasRuntimeFilters()
  42. {
  43. $report = new Report();
  44. $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
  45. $filter = new FilterForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  46. $filter->attributeIndexOrDerivedType = 'string';
  47. $filter->value = 'aValue';
  48. $filter->operator = 'equals';
  49. $report->addFilter($filter);
  50. $this->assertFalse($report->hasRuntimeFilters());
  51. $filter2 = new FilterForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  52. $filter2->attributeIndexOrDerivedType = 'string';
  53. $filter2->value = 'aValue';
  54. $filter2->operator = 'equals';
  55. $filter2->availableAtRunTime = true;
  56. $report->addFilter($filter2);
  57. $this->assertTrue($report->hasRuntimeFilters());
  58. }
  59. public function testGetDisplayAttributeIndex()
  60. {
  61. $report = new Report();
  62. $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
  63. $displayAttribute = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem',
  64. $report->getType());
  65. $displayAttribute->attributeIndexOrDerivedType = 'phone';
  66. $displayAttribute->label = 'someNewLabel';
  67. $report->addDisplayAttribute($displayAttribute);
  68. $displayAttribute2 = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem',
  69. $report->getType());
  70. $displayAttribute2->attributeIndexOrDerivedType = 'string';
  71. $displayAttribute2->label = 'someNewLabel 2';
  72. $report->addDisplayAttribute($displayAttribute2);
  73. $this->assertEquals(1, $report->getDisplayAttributeIndex('string'));
  74. $this->assertNull($report->getDisplayAttributeIndex('notHere'));
  75. }
  76. public function testGetDisplayAttributeByAttribute()
  77. {
  78. $report = new Report();
  79. $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
  80. $displayAttribute = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem',
  81. $report->getType());
  82. $displayAttribute->attributeIndexOrDerivedType = 'phone';
  83. $displayAttribute->label = 'someNewLabel';
  84. $report->addDisplayAttribute($displayAttribute);
  85. $displayAttribute2 = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem',
  86. $report->getType());
  87. $displayAttribute2->attributeIndexOrDerivedType = 'string';
  88. $displayAttribute2->label = 'someNewLabel 2';
  89. $report->addDisplayAttribute($displayAttribute2);
  90. $this->assertEquals(1, $report->getDisplayAttributeIndex('string'));
  91. $displayAttributeResult = $report->getDisplayAttributeByAttribute('phone');
  92. $this->assertEquals($displayAttribute->getAttributeIndexOrDerivedType(),
  93. $displayAttributeResult->getAttributeIndexOrDerivedType());
  94. }
  95. public function testResolveGroupBysAsFilters()
  96. {
  97. $report = new Report();
  98. $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
  99. $groupBy = new GroupByForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  100. $groupBy->attributeIndexOrDerivedType = 'lastName';
  101. $groupBy->axis = 'x';
  102. $report->addGroupBy($groupBy);
  103. $groupBy2 = new GroupByForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  104. $groupBy2->attributeIndexOrDerivedType = 'firstName';
  105. $groupBy2->axis = 'y';
  106. $report->addGroupBy($groupBy2);
  107. $this->assertNull($report->getFiltersStructure());
  108. $getData = array('groupByRowValuelastName' => '50', 'groupByRowValuefirstName' => '');
  109. $this->assertCount(0, $report->getFilters());
  110. $report->resolveGroupBysAsFilters($getData);
  111. $this->assertEquals('(1 AND 2)', $report->getFiltersStructure());
  112. $filters = $report->getFilters();
  113. $this->assertCount(2, $filters);
  114. $this->assertEquals('lastName', $filters[0]->getAttributeIndexOrDerivedType());
  115. $this->assertEquals('50', $filters[0]->value);
  116. $this->assertEquals(OperatorRules::TYPE_EQUALS, $filters[0]->operator);
  117. $this->assertEquals('firstName', $filters[1]->getAttributeIndexOrDerivedType());
  118. $this->assertNull ($filters[1]->value);
  119. $this->assertEquals(OperatorRules::TYPE_IS_NULL, $filters[1]->operator);
  120. }
  121. public function testGetReportableModulesAndLabelsForCurrentUser()
  122. {
  123. $modulesAndLabels = Report::getReportableModulesAndLabelsForCurrentUser();
  124. $this->assertCount(6, $modulesAndLabels);
  125. Yii::app()->user->userModel = User::getByUsername('billy');
  126. $modulesAndLabels = Report::getReportableModulesAndLabelsForCurrentUser();
  127. $this->assertCount(0, $modulesAndLabels);
  128. }
  129. public function testGetReportableModulesClassNamesCurrentUserHasAccessTo()
  130. {
  131. $modulesAndLabels = Report::getReportableModulesClassNamesCurrentUserHasAccessTo();
  132. $this->assertCount(6, $modulesAndLabels);
  133. Yii::app()->user->userModel = User::getByUsername('billy');
  134. $modulesAndLabels = Report::getReportableModulesClassNamesCurrentUserHasAccessTo();
  135. $this->assertCount(0, $modulesAndLabels);
  136. }
  137. public function testCanCurrentUserProperlyRenderResults()
  138. {
  139. $billy = User::getByUsername('billy');
  140. $billy->setRight('AccountsModule', AccountsModule::RIGHT_ACCESS_ACCOUNTS);
  141. $billy->setRight('OpportunitiesModule', OpportunitiesModule::RIGHT_ACCESS_OPPORTUNITIES);
  142. $saved = $billy->save();
  143. $this->assertTrue($saved);
  144. $report = new Report();
  145. $report->setModuleClassName('ContactsModule');
  146. $report->setType (Report::TYPE_ROWS_AND_COLUMNS);
  147. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  148. //Now set to Billy
  149. Yii::app()->user->userModel = $billy;
  150. //Billy can't see the contacts module.
  151. $this->assertFalse($report->canCurrentUserProperlyRenderResults());
  152. //Billy can see accounts
  153. $report->setModuleClassName('AccountsModule');
  154. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  155. //A filter on accounts is ok for Billy to see
  156. $filter = new FilterForReportForm('AccountsModule', 'Account', $report->getType());
  157. $filter->attributeIndexOrDerivedType = 'officePhone';
  158. $filter->value = 'aValue';
  159. $filter->operator = 'equals';
  160. $report->addFilter($filter);
  161. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  162. //A filter on contacts is not ok for Billy to see
  163. $filter2 = new FilterForReportForm('AccountsModule', 'Account', $report->getType());
  164. $filter2->attributeIndexOrDerivedType = 'contacts___lastName';
  165. $filter2->value = 'aValue';
  166. $filter2->operator = 'equals';
  167. $report->addFilter($filter2);
  168. $this->assertFalse($report->canCurrentUserProperlyRenderResults());
  169. //A related filter on opportunities would be ok for Billy to see
  170. $report->removeAllFilters();
  171. $filter = new FilterForReportForm('AccountsModule', 'Account', $report->getType());
  172. $filter->attributeIndexOrDerivedType = 'opportunities___name';
  173. $filter->value = 'aValue';
  174. $filter->operator = 'equals';
  175. $report->addFilter($filter);
  176. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  177. $report->removeAllFilters();
  178. //Billy can see a groupBy on Accounts
  179. $groupBy = new GroupByForReportForm('AccountsModule', 'Account', $report->getType());
  180. $groupBy->attributeIndexOrDerivedType = 'name';
  181. $groupBy->axis = 'y';
  182. $report->addGroupBy($groupBy);
  183. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  184. //Billy cannot see a related groupBy on Contacts
  185. $groupBy = new GroupByForReportForm('AccountsModule', 'Account', $report->getType());
  186. $groupBy->attributeIndexOrDerivedType = 'contacts___lastName';
  187. $groupBy->axis = 'y';
  188. $report->addGroupBy($groupBy);
  189. $this->assertFalse($report->canCurrentUserProperlyRenderResults());
  190. //Billy can see a related groupBy on Opportunities
  191. $report->removeAllGroupBys();
  192. $groupBy = new GroupByForReportForm('AccountsModule', 'Account', $report->getType());
  193. $groupBy->attributeIndexOrDerivedType = 'opportunities___name';
  194. $groupBy->axis = 'y';
  195. $report->addGroupBy($groupBy);
  196. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  197. $report->removeAllGroupBys();
  198. //Billy can see an orderBy on Accounts
  199. $orderBy = new OrderByForReportForm('AccountsModule', 'Account', $report->getType());
  200. $orderBy->attributeIndexOrDerivedType = 'name';
  201. $orderBy->order = 'desc';
  202. $report->addOrderBy($orderBy);
  203. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  204. //Billy cannot see a related orderBy on Contacts
  205. $orderBy = new OrderByForReportForm('AccountsModule', 'Account', $report->getType());
  206. $orderBy->attributeIndexOrDerivedType = 'contacts___lastName';
  207. $orderBy->order = 'desc';
  208. $report->addOrderBy($orderBy);
  209. $this->assertFalse($report->canCurrentUserProperlyRenderResults());
  210. //Billy can see a related orderBy on Opportunities
  211. $report->removeAllOrderBys();
  212. $orderBy = new OrderByForReportForm('AccountsModule', 'Account', $report->getType());
  213. $orderBy->attributeIndexOrDerivedType = 'opportunities___name';
  214. $orderBy->order = 'desc';
  215. $report->addOrderBy($orderBy);
  216. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  217. $report->removeAllOrderBys();
  218. //Billy can see a displayAttribute on Accounts
  219. $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
  220. $displayAttribute->attributeIndexOrDerivedType = 'name';
  221. $displayAttribute->label = 'someNewLabel';
  222. $report->addDisplayAttribute($displayAttribute);
  223. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  224. //Billy cannot see a related displayAttribute on Contacts
  225. $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
  226. $displayAttribute->attributeIndexOrDerivedType = 'contacts___firstName';
  227. $displayAttribute->label = 'someNewLabel';
  228. $report->addDisplayAttribute($displayAttribute);
  229. $this->assertFalse($report->canCurrentUserProperlyRenderResults());
  230. //Billy can see a related displayAttribute on Opportunities
  231. $report->removeAllDisplayAttributes();
  232. $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
  233. $displayAttribute->attributeIndexOrDerivedType = 'opportunities___name';
  234. $displayAttribute->label = 'someNewLabel';
  235. $report->addDisplayAttribute($displayAttribute);
  236. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  237. $report->removeAllDisplayAttributes();
  238. //Billy can see a drillDownDisplayAttribute on Accounts
  239. $drillDownDisplayAttribute = new DrillDownDisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
  240. $drillDownDisplayAttribute->attributeIndexOrDerivedType = 'name';
  241. $drillDownDisplayAttribute->label = 'someNewLabel';
  242. $report->addDrillDownDisplayAttribute($drillDownDisplayAttribute);
  243. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  244. //Billy cannot see a related drillDownDisplayAttribute on Contacts
  245. $drillDownDisplayAttribute = new DrillDownDisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
  246. $drillDownDisplayAttribute->attributeIndexOrDerivedType = 'contacts___firstName';
  247. $drillDownDisplayAttribute->label = 'someNewLabel';
  248. $report->addDrillDownDisplayAttribute($drillDownDisplayAttribute);
  249. $this->assertFalse($report->canCurrentUserProperlyRenderResults());
  250. //Billy can see a related drillDownDisplayAttribute on Opportunities
  251. $report->removeAllDrillDownDisplayAttributes();
  252. $drillDownDisplayAttribute = new DrillDownDisplayAttributeForReportForm('AccountsModule', 'Account', $report->getType());
  253. $drillDownDisplayAttribute->attributeIndexOrDerivedType = 'opportunities___name';
  254. $drillDownDisplayAttribute->label = 'someNewLabel';
  255. $report->addDrillDownDisplayAttribute($drillDownDisplayAttribute);
  256. $this->assertTrue($report->canCurrentUserProperlyRenderResults());
  257. }
  258. }
  259. ?>