PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/zurmo/zurmo/
PHP | 219 lines | 174 code | 9 blank | 36 comment | 0 complexity | 56973c9d35a1de985d3278b31f3262c5 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 DataToReportUtilTest extends ZurmoBaseTest
  37. {
  38. public static function setUpBeforeClass()
  39. {
  40. parent::setUpBeforeClass();
  41. SecurityTestHelper::createSuperAdmin();
  42. UserTestHelper::createBasicUser('bobby');
  43. }
  44. public function setUp()
  45. {
  46. parent::setUp();
  47. Yii::app()->user->userModel = User::getByUsername('super');
  48. }
  49. public function testRowsAndColumnsResolveReportByWizardPostData()
  50. {
  51. $bobby = User::getByUserName('bobby');
  52. $wizardFormClassName = 'RowsAndColumnsReportWizardForm';
  53. $report = new Report();
  54. $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
  55. $data = array();
  56. $data['moduleClassName'] = 'ReportsTestModule';
  57. $data['description'] = 'a description';
  58. $data['name'] = 'name';
  59. $data['filtersStructure'] = '1 AND 2';
  60. $data['ownerId'] = $bobby->id;
  61. $data['currencyConversionType'] = Report::CURRENCY_CONVERSION_TYPE_SPOT;
  62. $data['spotConversionCurrencyCode'] = 'EUR';
  63. $data[ComponentForReportForm::TYPE_FILTERS][] = array('attributeIndexOrDerivedType' => 'date',
  64. 'valueType' => 'Between',
  65. 'value' => '2/24/2012',
  66. 'secondValue' => '2/28/2012');
  67. $data[ComponentForReportForm::TYPE_ORDER_BYS][] = array('attributeIndexOrDerivedType' => 'string',
  68. 'order' => 'desc');
  69. $data[ComponentForReportForm::TYPE_DISPLAY_ATTRIBUTES][] = array('attributeIndexOrDerivedType' => 'phone',
  70. 'label' => 'custom');
  71. DataToReportUtil::resolveReportByWizardPostData($report, array('RowsAndColumnsReportWizardForm' => $data),
  72. $wizardFormClassName);
  73. $this->assertEquals('ReportsTestModule', $report->getModuleClassName());
  74. $this->assertEquals('a description', $report->getDescription());
  75. $this->assertEquals('name', $report->getName());
  76. $this->assertEquals('1 AND 2', $report->getFiltersStructure());
  77. $this->assertEquals($bobby->id, $report->getOwner()->id);
  78. $this->assertEquals(Report::CURRENCY_CONVERSION_TYPE_SPOT, $report->getCurrencyConversionType());
  79. $this->assertEquals('EUR', $report->getSpotConversionCurrencyCode());
  80. $filters = $report->getFilters();
  81. $this->assertCount(1, $filters);
  82. $this->assertEquals('date', $filters[0]->getAttributeIndexOrDerivedType());
  83. $this->assertEquals('Between', $filters[0]->valueType);
  84. $this->assertEquals('2012-02-24', $filters[0]->value);
  85. $this->assertEquals('2012-02-28', $filters[0]->secondValue);
  86. $orderBys = $report->getOrderBys();
  87. $this->assertCount(1, $orderBys);
  88. $this->assertEquals('string', $orderBys[0]->getAttributeIndexOrDerivedType());
  89. $this->assertEquals('desc', $orderBys[0]->order);
  90. $displayAttributes = $report->getDisplayAttributes();
  91. $this->assertCount(1, $orderBys);
  92. $this->assertEquals('phone', $displayAttributes[0]->getAttributeIndexOrDerivedType());
  93. $this->assertEquals('custom', $displayAttributes[0]->label);
  94. }
  95. public function testSummationResolveReportByWizardPostData()
  96. {
  97. $bobby = User::getByUserName('bobby');
  98. $wizardFormClassName = 'SummationReportWizardForm';
  99. $report = new Report();
  100. $report->setType(Report::TYPE_SUMMATION);
  101. $data = array();
  102. $data['moduleClassName'] = 'ReportsTestModule';
  103. $data['description'] = 'a description';
  104. $data['name'] = 'name';
  105. $data['filtersStructure'] = '1 AND 2';
  106. $data['ownerId'] = $bobby->id;
  107. $data['currencyConversionType'] = Report::CURRENCY_CONVERSION_TYPE_SPOT;
  108. $data['spotConversionCurrencyCode'] = 'EUR';
  109. $data[ComponentForReportForm::TYPE_FILTERS][] = array('attributeIndexOrDerivedType' => 'date',
  110. 'valueType' => 'Between',
  111. 'value' => '2/24/2012',
  112. 'secondValue' => '2/28/2012');
  113. $data[ComponentForReportForm::TYPE_ORDER_BYS][] = array('attributeIndexOrDerivedType' => 'string',
  114. 'order' => 'desc');
  115. $data[ComponentForReportForm::TYPE_DISPLAY_ATTRIBUTES][] = array('attributeIndexOrDerivedType' => 'phone',
  116. 'label' => 'custom');
  117. $data[ComponentForReportForm::TYPE_DRILL_DOWN_DISPLAY_ATTRIBUTES][] =
  118. array('attributeIndexOrDerivedType' => 'string',
  119. 'label' => 'custom string');
  120. $data[ComponentForReportForm::TYPE_GROUP_BYS][] = array('attributeIndexOrDerivedType' => 'string',
  121. 'axis' => 'y');
  122. $data['ChartForReportForm'] = array('type' => ChartRules::TYPE_BAR_2D,
  123. 'firstSeries' => 'string',
  124. 'firstRange' => 'integer__Maximum');
  125. DataToReportUtil::resolveReportByWizardPostData($report, array('SummationReportWizardForm' => $data),
  126. $wizardFormClassName);
  127. $this->assertEquals('ReportsTestModule', $report->getModuleClassName());
  128. $this->assertEquals('a description', $report->getDescription());
  129. $this->assertEquals('name', $report->getName());
  130. $this->assertEquals('1 AND 2', $report->getFiltersStructure());
  131. $this->assertEquals($bobby->id, $report->getOwner()->id);
  132. $this->assertEquals(Report::CURRENCY_CONVERSION_TYPE_SPOT, $report->getCurrencyConversionType());
  133. $this->assertEquals('EUR', $report->getSpotConversionCurrencyCode());
  134. $filters = $report->getFilters();
  135. $this->assertCount(1, $filters);
  136. $this->assertEquals('date', $filters[0]->getAttributeIndexOrDerivedType());
  137. $this->assertEquals('Between', $filters[0]->valueType);
  138. $this->assertEquals('2012-02-24', $filters[0]->value);
  139. $this->assertEquals('2012-02-28', $filters[0]->secondValue);
  140. $orderBys = $report->getOrderBys();
  141. $this->assertCount(1, $orderBys);
  142. $this->assertEquals('string', $orderBys[0]->getAttributeIndexOrDerivedType());
  143. $this->assertEquals('desc', $orderBys[0]->order);
  144. $displayAttributes = $report->getDisplayAttributes();
  145. $this->assertCount(1, $displayAttributes);
  146. $this->assertEquals('phone', $displayAttributes[0]->getAttributeIndexOrDerivedType());
  147. $this->assertEquals('custom', $displayAttributes[0]->label);
  148. $groupBys = $report->getGroupBys();
  149. $this->assertCount(1, $groupBys);
  150. $this->assertEquals('string', $groupBys[0]->getAttributeIndexOrDerivedType());
  151. $this->assertEquals('y', $groupBys[0]->axis);
  152. $drillDownDisplayAttributes = $report->getDrillDownDisplayAttributes();
  153. $this->assertCount(1, $drillDownDisplayAttributes);
  154. $this->assertEquals('string', $drillDownDisplayAttributes[0]->getAttributeIndexOrDerivedType());
  155. $this->assertEquals('custom string', $drillDownDisplayAttributes[0]->label);
  156. $chart = $report->getChart();
  157. $this->assertEquals(ChartRules::TYPE_BAR_2D, $chart->type);
  158. $this->assertEquals('string', $chart->firstSeries);
  159. $this->assertEquals('integer__Maximum', $chart->firstRange);
  160. }
  161. public function testResolveFiltersAndDateConvertsProperlyToDbFormat()
  162. {
  163. $report = new Report();
  164. $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
  165. $report->setModuleClassName('ReportsTestModule');
  166. $data = array();
  167. $data[ComponentForReportForm::TYPE_FILTERS][] = array('attributeIndexOrDerivedType' => 'date',
  168. 'valueType' => 'Between',
  169. 'value' => '2/24/2012',
  170. 'secondValue' => '2/28/2012');
  171. $data[ComponentForReportForm::TYPE_FILTERS][] = array('attributeIndexOrDerivedType' => 'dateTime',
  172. 'value' => '2/25/2012',
  173. 'availableAtRunTime' => true);
  174. DataToReportUtil::resolveFilters($data, $report);
  175. $filters = $report->getFilters();
  176. $this->assertCount(2, $filters);
  177. $this->assertEquals('Between', $filters[0]->valueType);
  178. $this->assertEquals('2012-02-24', $filters[0]->value);
  179. $this->assertEquals('2012-02-28', $filters[0]->secondValue);
  180. $this->assertEquals('2012-02-25', $filters[1]->value);
  181. //Should remove only the runtime filter
  182. $data = array();
  183. $data[ComponentForReportForm::TYPE_FILTERS][] = array('attributeIndexOrDerivedType' => 'dateTime',
  184. 'value' => '2/25/2012',
  185. 'availableAtRunTime' => true);
  186. DataToReportUtil::resolveFilters($data, $report, true);
  187. $filters = $report->getFilters();
  188. $this->assertCount(2, $filters);
  189. $this->assertEquals('Between', $filters[0]->valueType);
  190. $this->assertEquals('2012-02-24', $filters[0]->value);
  191. $this->assertEquals('2012-02-28', $filters[0]->secondValue);
  192. $this->assertEquals('2012-02-25', $filters[1]->value);
  193. }
  194. public function testSanitizeFiltersData()
  195. {
  196. //test specifically for date/dateTime conversion from local to db format.
  197. $filtersData = array();
  198. $filtersData[0] = array('attributeIndexOrDerivedType' => 'date', 'value' => '2/24/2012');
  199. $filtersData[1] = array('attributeIndexOrDerivedType' => 'dateTime', 'value' => '2/25/2012');
  200. $filtersData[2] = array('attributeIndexOrDerivedType' => 'date', 'value' => '2/24/2012',
  201. 'secondValue' => '2/28/2012');
  202. $sanitizedFilterData = DataToReportUtil::sanitizeFiltersData('ReportsTestModule',
  203. Report::TYPE_ROWS_AND_COLUMNS, $filtersData);
  204. $this->assertEquals('2012-02-24', $sanitizedFilterData[0]['value']);
  205. $this->assertEquals('2012-02-25', $sanitizedFilterData[1]['value']);
  206. $this->assertEquals('2012-02-24', $sanitizedFilterData[2]['value']);
  207. $this->assertEquals('2012-02-28', $sanitizedFilterData[2]['secondValue']);
  208. }
  209. }
  210. ?>