PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ddonthula/zurmofeb
PHP | 270 lines | 193 code | 20 blank | 57 comment | 0 complexity | db2297c2a6a41663321e4c8f274a8f2c 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 ModelRelationsAndAttributesToSummationReportAdapterTest extends ZurmoBaseTest
  27. {
  28. public static function setUpBeforeClass()
  29. {
  30. parent::setUpBeforeClass();
  31. SecurityTestHelper::createSuperAdmin();
  32. $attributeName = 'calculated';
  33. $attributeForm = new CalculatedNumberAttributeForm();
  34. $attributeForm->attributeName = $attributeName;
  35. $attributeForm->attributeLabels = array('en' => 'Test Calculated');
  36. $attributeForm->formula = 'integer + float';
  37. $modelAttributesAdapterClassName = $attributeForm::getModelAttributeAdapterNameForSavingAttributeFormData();
  38. $adapter = new $modelAttributesAdapterClassName(new ReportModelTestItem());
  39. $adapter->setAttributeMetadataFromForm($attributeForm);
  40. }
  41. public function setup()
  42. {
  43. parent::setUp();
  44. Yii::app()->user->userModel = User::getByUsername('super');
  45. }
  46. public function testGetAttributesForChartSeries()
  47. {
  48. $model = new ReportModelTestItem();
  49. $rules = new ReportsTestReportRules();
  50. $report = new Report();
  51. $report->setType(Report::TYPE_SUMMATION);
  52. $report->setModuleClassName('ReportsTestModule');
  53. $adapter = new ModelRelationsAndAttributesToSummationReportAdapter($model, $rules, $report->getType());
  54. $attributes = $adapter->getAttributesForChartSeries($report->getGroupBys());
  55. $this->assertEquals(0, count($attributes));
  56. //Add a group by, but not as a display attribute
  57. $groupBy = new GroupByForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  58. $groupBy->attributeIndexOrDerivedType = 'dropDown';
  59. $report->setModuleClassName('ReportsTestModule');
  60. $report->addGroupBy($groupBy);
  61. $attributes = $adapter->getAttributesForChartSeries($report->getGroupBys());
  62. $this->assertEquals(0, count($attributes));
  63. //Add a group by as a display attribute
  64. $groupBy = new GroupByForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  65. $groupBy->attributeIndexOrDerivedType = 'dropDown';
  66. $report->setModuleClassName('ReportsTestModule');
  67. $report->addGroupBy($groupBy);
  68. $displayAttribute = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  69. $displayAttribute->attributeIndexOrDerivedType = 'dropDown';
  70. $report->addDisplayAttribute($displayAttribute);
  71. $attributes = $adapter->getAttributesForChartSeries($report->getGroupBys(), $report->getDisplayAttributes());
  72. $this->assertEquals(1, count($attributes));
  73. $compareData = array('label' => 'Drop Down');
  74. $this->assertEquals($compareData, $attributes['dropDown']);
  75. //Add a second group by as a display attribute
  76. $groupBy = new GroupByForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  77. $groupBy->attributeIndexOrDerivedType = 'radioDropDown';
  78. $report->setModuleClassName('ReportsTestModule');
  79. $report->addGroupBy($groupBy);
  80. $displayAttribute = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  81. $displayAttribute->attributeIndexOrDerivedType = 'radioDropDown';
  82. $report->addDisplayAttribute($displayAttribute);
  83. $attributes = $adapter->getAttributesForChartSeries($report->getGroupBys(), $report->getDisplayAttributes());
  84. $this->assertEquals(2, count($attributes));
  85. $compareData = array('label' => 'Drop Down');
  86. $this->assertEquals($compareData, $attributes['dropDown']);
  87. $compareData = array('label' => 'Radio Drop Down');
  88. $this->assertEquals($compareData, $attributes['radioDropDown']);
  89. //Add a third group by that is likeContactState as a display attribute
  90. $groupBy = new GroupByForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  91. $groupBy->attributeIndexOrDerivedType = 'likeContactState';
  92. $report->setModuleClassName('ReportsTestModule');
  93. $report->addGroupBy($groupBy);
  94. $displayAttribute = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  95. $displayAttribute->attributeIndexOrDerivedType = 'likeContactState';
  96. $report->addDisplayAttribute($displayAttribute);
  97. $attributes = $adapter->getAttributesForChartSeries($report->getGroupBys(), $report->getDisplayAttributes());
  98. $this->assertEquals(3, count($attributes));
  99. $compareData = array('label' => 'Drop Down');
  100. $this->assertEquals($compareData, $attributes['dropDown']);
  101. $compareData = array('label' => 'Radio Drop Down');
  102. $this->assertEquals($compareData, $attributes['radioDropDown']);
  103. $compareData = array('label' => 'A name for a state');
  104. $this->assertEquals($compareData, $attributes['likeContactState']);
  105. }
  106. /**
  107. * @depends testGetAttributesForChartSeries
  108. */
  109. public function testGetAttributesForChartRange()
  110. {
  111. $model = new ReportModelTestItem();
  112. $rules = new ReportsTestReportRules();
  113. $report = new Report();
  114. $report->setType(Report::TYPE_SUMMATION);
  115. $report->setModuleClassName('ReportsTestModule');
  116. $adapter = new ModelRelationsAndAttributesToSummationReportAdapter($model, $rules, $report->getType());
  117. $attributes = $adapter->getAttributesForChartRange($report->getDisplayAttributes());
  118. $this->assertEquals(0, count($attributes));
  119. //Add a display attribute that cannot be a range
  120. $displayAttribute = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  121. $displayAttribute->attributeIndexOrDerivedType = 'dropDown';
  122. $report->setModuleClassName('ReportsTestModule');
  123. $report->addDisplayAttribute($displayAttribute);
  124. $attributes = $adapter->getAttributesForChartRange($report->getDisplayAttributes());
  125. $this->assertEquals(0, count($attributes));
  126. //Add a display attribute that can be a range
  127. $displayAttribute = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  128. $displayAttribute->attributeIndexOrDerivedType = 'float__Summation';
  129. $report->setModuleClassName('ReportsTestModule');
  130. $report->addDisplayAttribute($displayAttribute);
  131. $attributes = $adapter->getAttributesForChartRange($report->getDisplayAttributes());
  132. $this->assertEquals(1, count($attributes));
  133. $compareData = array('label' => 'Float -(Sum)');
  134. $this->assertEquals($compareData, $attributes['float__Summation']);
  135. //Add a second display attribute that can be a range
  136. $displayAttribute = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem', $report->getType());
  137. $displayAttribute->attributeIndexOrDerivedType = 'float__Average';
  138. $report->setModuleClassName('ReportsTestModule');
  139. $report->addDisplayAttribute($displayAttribute);
  140. $attributes = $adapter->getAttributesForChartRange($report->getDisplayAttributes());
  141. $this->assertEquals(2, count($attributes));
  142. $compareData = array('label' => 'Float -(Sum)');
  143. $this->assertEquals($compareData, $attributes['float__Summation']);
  144. $compareData = array('label' => 'Float -(Avg)');
  145. $this->assertEquals($compareData, $attributes['float__Average']);
  146. }
  147. /**
  148. * @depends testGetAttributesForChartRange
  149. */
  150. public function testIsAttributeIndexOrDerivedTypeADisplayCalculation()
  151. {
  152. $model = new ReportModelTestItem();
  153. $rules = new ReportsTestReportRules();
  154. $report = new Report();
  155. $report->setType(Report::TYPE_SUMMATION);
  156. $report->setModuleClassName('ReportsTestModule');
  157. $adapter = new ModelRelationsAndAttributesToSummationReportAdapter($model, $rules, $report->getType());
  158. $this->assertFalse ($adapter->isAttributeIndexOrDerivedTypeADisplayCalculation('string'));
  159. $this->assertTrue ($adapter->isAttributeIndexOrDerivedTypeADisplayCalculation('float__Summation'));
  160. $this->assertTrue ($adapter->isAttributeIndexOrDerivedTypeADisplayCalculation(
  161. ModelRelationsAndAttributesToSummableReportAdapter::DISPLAY_CALCULATION_COUNT));
  162. }
  163. /**
  164. * @depends testIsAttributeIndexOrDerivedTypeADisplayCalculation
  165. */
  166. public function testIsDisplayAttributeMadeViaSelect()
  167. {
  168. $model = new ReportModelTestItem();
  169. $rules = new ReportsTestReportRules(); //ReportsTestModule rules
  170. $report = new Report();
  171. $report->setType(Report::TYPE_SUMMATION);
  172. $report->setModuleClassName('ReportsTestModule');
  173. $adapter = new ModelRelationsAndAttributesToSummationReportAdapter($model, $rules, $report->getType());
  174. $this->assertFalse($adapter->isDisplayAttributeMadeViaSelect('date'));
  175. $this->assertFalse($adapter->isDisplayAttributeMadeViaSelect('phone'));
  176. $this->assertTrue($adapter->isDisplayAttributeMadeViaSelect('date__Day'));
  177. $this->assertTrue($adapter->isDisplayAttributeMadeViaSelect('integer__Maximum'));
  178. }
  179. /**
  180. * @depends testIsDisplayAttributeMadeViaSelect
  181. */
  182. public function testIsAttributeACalculationOrModifier()
  183. {
  184. $model = new ReportModelTestItem();
  185. $rules = new ReportsTestReportRules(); //ReportsTestModule rules
  186. $report = new Report();
  187. $report->setType(Report::TYPE_SUMMATION);
  188. $report->setModuleClassName('ReportsTestModule');
  189. $adapter = new ModelRelationsAndAttributesToSummationReportAdapter($model, $rules, $report->getType());
  190. $this->assertTrue ($adapter->isAttributeACalculationOrModifier('Count'));
  191. $this->assertFalse($adapter->isAttributeACalculationOrModifier('phone'));
  192. $this->assertTrue ($adapter->isAttributeACalculationOrModifier('date__Day'));
  193. $this->assertTrue ($adapter->isAttributeACalculationOrModifier('integer__Maximum'));
  194. }
  195. /**
  196. * @depends testIsAttributeACalculationOrModifier
  197. */
  198. public function testIsAttributeACalculatedGroupByModifier()
  199. {
  200. $model = new ReportModelTestItem();
  201. $rules = new ReportsTestReportRules(); //ReportsTestModule rules
  202. $report = new Report();
  203. $report->setType(Report::TYPE_SUMMATION);
  204. $report->setModuleClassName('ReportsTestModule');
  205. $adapter = new ModelRelationsAndAttributesToSummationReportAdapter($model, $rules, $report->getType());
  206. $this->assertFalse ($adapter->isAttributeACalculatedGroupByModifier('Count'));
  207. $this->assertFalse ($adapter->isAttributeACalculatedGroupByModifier('phone'));
  208. $this->assertTrue ($adapter->isAttributeACalculatedGroupByModifier('date__Day'));
  209. $this->assertFalse ($adapter->isAttributeACalculatedGroupByModifier('integer__Maximum'));
  210. }
  211. /**
  212. * @depends testIsAttributeACalculatedGroupByModifier
  213. */
  214. public function testResolveRealAttributeName()
  215. {
  216. $model = new ReportModelTestItem();
  217. $rules = new ReportsTestReportRules(); //ReportsTestModule rules
  218. $report = new Report();
  219. $report->setType(Report::TYPE_SUMMATION);
  220. $report->setModuleClassName('ReportsTestModule');
  221. $adapter = new ModelRelationsAndAttributesToSummationReportAdapter($model, $rules, $report->getType());
  222. $this->assertEquals ('id', $adapter->resolveRealAttributeName('Count'));
  223. $this->assertEquals ('string', $adapter->resolveRealAttributeName('string'));
  224. $this->assertEquals ('owner', $adapter->resolveRealAttributeName('owner__User'));
  225. $this->assertEquals ('owner', $adapter->resolveRealAttributeName('ReportsTestModel__owner__Inferred'));
  226. }
  227. /**
  228. * @depends testResolveRealAttributeName
  229. */
  230. public function testGetCalculationOrModifierType()
  231. {
  232. $model = new ReportModelTestItem();
  233. $rules = new ReportsTestReportRules(); //ReportsTestModule rules
  234. $report = new Report();
  235. $report->setType(Report::TYPE_SUMMATION);
  236. $report->setModuleClassName('ReportsTestModule');
  237. $adapter = new ModelRelationsAndAttributesToSummationReportAdapter($model, $rules, $report->getType());
  238. $this->assertEquals('Maximum', $adapter->getCalculationOrModifierType('integer__Maximum'));
  239. $this->assertEquals('something', $adapter->getCalculationOrModifierType('something'));
  240. }
  241. /**
  242. * @depends testGetCalculationOrModifierType
  243. */
  244. public function testResolveDisplayAttributeTypeAndAddSelectClause()
  245. {
  246. //todo:
  247. //$this->fail();
  248. }
  249. }
  250. ?>