PageRenderTime 83ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/zurmo/zurmo/
PHP | 173 lines | 111 code | 8 blank | 54 comment | 0 complexity | eb5473dc718d0f3f42345b0b99111a20 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 QueryBuilderDocumentationTest extends ZurmoBaseTest
  37. {
  38. public static function setUpBeforeClass()
  39. {
  40. parent::setUpBeforeClass();
  41. SecurityTestHelper::createSuperAdmin();
  42. }
  43. public function setup()
  44. {
  45. parent::setUp();
  46. Yii::app()->user->userModel = User::getByUsername('super');
  47. }
  48. public function testSomething()
  49. {
  50. //This class should serve as documentation for all various scenarios that occur in the various builders
  51. //Additionally deeper nesting should be tested for all scenarios. In OrderBysReportQueryBuilderTest we are not
  52. //testing beyond one layer deep and we should in this class.
  53. //Also test when existing filters have nestings and you are getting orderBys or groupBys generated.
  54. }
  55. /**
  56. * Test to confirm joins are working correctly when ordering by an attribute on item.
  57. * Testing when the join adapter has already added a display attribute from a related account
  58. */
  59. public function testOrderByNoteCreatedDateTimeAndConfirmJoinsAreCorrect()
  60. {
  61. $q = DatabaseCompatibilityUtil::getQuote();
  62. $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('Note');
  63. $selectQueryAdapter = new RedBeanModelSelectQueryAdapter();
  64. $builder = new DisplayAttributesReportQueryBuilder($joinTablesAdapter, $selectQueryAdapter);
  65. $displayAttribute = new DisplayAttributeForReportForm('NotesModule', 'Note',
  66. Report::TYPE_ROWS_AND_COLUMNS);
  67. $displayAttribute->attributeIndexOrDerivedType = 'Account__activityItems__Inferred___name';
  68. //First build the display attributes from clauses
  69. $builder->makeQueryContent(array($displayAttribute));
  70. $builder = new OrderBysReportQueryBuilder($joinTablesAdapter);
  71. $orderBy = new OrderByForReportForm('NotesModule', 'Note',
  72. Report::TYPE_ROWS_AND_COLUMNS);
  73. $orderBy->attributeIndexOrDerivedType = 'createdDateTime';
  74. //Second build the order by from clauses
  75. $builder->makeQueryContent(array($orderBy));
  76. $fromTablesAndAliases = $joinTablesAdapter->getFromTablesAndAliases();
  77. $this->assertEquals('activity', $fromTablesAndAliases[0]['tableAliasName']);
  78. $this->assertEquals('note', $fromTablesAndAliases[0]['onTableAliasName']);
  79. $this->assertEquals('ownedsecurableitem1', $fromTablesAndAliases[1]['tableAliasName']);
  80. $this->assertEquals('activity', $fromTablesAndAliases[1]['onTableAliasName']);
  81. $this->assertEquals('securableitem1', $fromTablesAndAliases[2]['tableAliasName']);
  82. $this->assertEquals('ownedsecurableitem1', $fromTablesAndAliases[2]['onTableAliasName']);
  83. $this->assertEquals('item1', $fromTablesAndAliases[3]['tableAliasName']);
  84. $this->assertEquals('securableitem1', $fromTablesAndAliases[3]['onTableAliasName']);
  85. }
  86. /**
  87. * Running a report centered on notes, with a display attribute from notes and accounts. Ordered by
  88. * created date time in notes. Should produce proper query to order by notes. This test just makes sure
  89. * the sql is structured properly
  90. */
  91. public function testOrderByWorksOnAccountsAndNoteReport()
  92. {
  93. $report = new Report();
  94. $report->setFiltersStructure('');
  95. $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
  96. $report->setModuleClassName('NotesModule');
  97. $displayAttribute = new DisplayAttributeForReportForm('NotesModule', 'Note', $report->getType());
  98. $displayAttribute->attributeIndexOrDerivedType = 'description';
  99. $report->addDisplayAttribute($displayAttribute);
  100. $displayAttribute2 = new DisplayAttributeForReportForm('NotesModule', 'Note', $report->getType());
  101. $displayAttribute2->attributeIndexOrDerivedType = 'Account__activityItems__Inferred___name';
  102. $report->addDisplayAttribute($displayAttribute2);
  103. $orderBy = new OrderByForReportForm('NotesModule', 'Note', Report::TYPE_SUMMATION);
  104. $orderBy->attributeIndexOrDerivedType = 'createdDateTime';
  105. $report->addOrderBy($orderBy);
  106. $reportDataProvider = new RowsAndColumnsReportDataProvider($report);
  107. $sql = $reportDataProvider->makeSqlQueryForDisplay();
  108. $q = DatabaseCompatibilityUtil::getQuote();
  109. $compareSql = "select {$q}note{$q}.{$q}id{$q} noteid, {$q}account{$q}.{$q}id{$q} accountid " .
  110. "from ({$q}note{$q}, {$q}activity{$q}, {$q}ownedsecurableitem{$q} ownedsecurableitem1, {$q}securableitem{$q} securableitem1, {$q}item{$q} item1) " .
  111. "left join {$q}activity_item{$q} on {$q}activity_item{$q}.{$q}activity_id{$q} = {$q}activity{$q}.{$q}id{$q} " .
  112. "left join {$q}item{$q} on {$q}item{$q}.{$q}id{$q} = {$q}activity_item{$q}.{$q}item_id{$q} " .
  113. "left join {$q}securableitem{$q} on {$q}securableitem{$q}.{$q}item_id{$q} = {$q}item{$q}.{$q}id{$q} " .
  114. "left join {$q}ownedsecurableitem{$q} on {$q}ownedsecurableitem{$q}.{$q}securableitem_id{$q} = {$q}securableitem{$q}.{$q}id{$q} " .
  115. "left join {$q}account{$q} on {$q}account{$q}.{$q}ownedsecurableitem_id{$q} = {$q}ownedsecurableitem{$q}.{$q}id{$q} " .
  116. "where {$q}activity{$q}.{$q}id{$q} = {$q}note{$q}.{$q}activity_id{$q} and " .
  117. "{$q}ownedsecurableitem1{$q}.{$q}id{$q} = {$q}activity{$q}.{$q}ownedsecurableitem_id{$q} " .
  118. "and {$q}securableitem1{$q}.{$q}id{$q} = {$q}ownedsecurableitem1{$q}.{$q}securableitem_id{$q} and " .
  119. "{$q}item1{$q}.{$q}id{$q} = {$q}securableitem1{$q}.{$q}item_id{$q} order by {$q}item1{$q}.{$q}createddatetime{$q} asc limit 10 offset 0";
  120. $this->assertEquals($compareSql, $sql);
  121. }
  122. /**
  123. * Running a report centered on notes, with a display attribute from notes and accounts. With a created
  124. * by dateTime filter should produce proper query filter notes.
  125. * This test just makes sure the sql is structured properly
  126. */
  127. public function testFiltersWorksOnAccountsAndNoteReport()
  128. {
  129. $report = new Report();
  130. $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
  131. $filter = new FilterForReportForm('NotesModule', 'Note', $report->getType());
  132. $filter->attributeIndexOrDerivedType = 'createdDateTime';
  133. $filter->valueType = MixedDateTypesSearchFormAttributeMappingRules::TYPE_BETWEEN;
  134. $filter->value = '1991-05-05';
  135. $filter->secondValue = '1991-06-05';
  136. $filter->availableAtRunTime = true;
  137. $report->addFilter($filter);
  138. $report->setFiltersStructure('1');
  139. $report->setModuleClassName('NotesModule');
  140. $displayAttribute = new DisplayAttributeForReportForm('NotesModule', 'Note', $report->getType());
  141. $displayAttribute->attributeIndexOrDerivedType = 'description';
  142. $report->addDisplayAttribute($displayAttribute);
  143. $displayAttribute2 = new DisplayAttributeForReportForm('NotesModule', 'Note', $report->getType());
  144. $displayAttribute2->attributeIndexOrDerivedType = 'Account__activityItems__Inferred___name';
  145. $report->addDisplayAttribute($displayAttribute2);
  146. $reportDataProvider = new RowsAndColumnsReportDataProvider($report);
  147. $sql = $reportDataProvider->makeSqlQueryForDisplay();
  148. $q = DatabaseCompatibilityUtil::getQuote();
  149. $compareSql = "select {$q}note{$q}.{$q}id{$q} noteid, {$q}account{$q}.{$q}id{$q} accountid " .
  150. "from ({$q}note{$q}, {$q}activity{$q}, {$q}ownedsecurableitem{$q} ownedsecurableitem1, {$q}securableitem{$q} securableitem1, {$q}item{$q} item1) " .
  151. "left join {$q}activity_item{$q} on {$q}activity_item{$q}.{$q}activity_id{$q} = {$q}activity{$q}.{$q}id{$q} " .
  152. "left join {$q}item{$q} on {$q}item{$q}.{$q}id{$q} = {$q}activity_item{$q}.{$q}item_id{$q} " .
  153. "left join {$q}securableitem{$q} on {$q}securableitem{$q}.{$q}item_id{$q} = {$q}item{$q}.{$q}id{$q} " .
  154. "left join {$q}ownedsecurableitem{$q} on {$q}ownedsecurableitem{$q}.{$q}securableitem_id{$q} = {$q}securableitem{$q}.{$q}id{$q} " .
  155. "left join {$q}account{$q} on {$q}account{$q}.{$q}ownedsecurableitem_id{$q} = {$q}ownedsecurableitem{$q}.{$q}id{$q} " .
  156. "where ((({$q}item1{$q}.{$q}createddatetime{$q} >= '1991-05-05 00:00:00') " .
  157. "and ({$q}item1{$q}.{$q}createddatetime{$q} <= '1991-06-05 23:59:59'))) " .
  158. "and {$q}activity{$q}.{$q}id{$q} = {$q}note{$q}.{$q}activity_id{$q} " .
  159. "and {$q}ownedsecurableitem1{$q}.{$q}id{$q} = {$q}activity{$q}.{$q}ownedsecurableitem_id{$q} " .
  160. "and {$q}securableitem1{$q}.{$q}id{$q} = {$q}ownedsecurableitem1{$q}.{$q}securableitem_id{$q} " .
  161. "and {$q}item1{$q}.{$q}id{$q} = {$q}securableitem1{$q}.{$q}item_id{$q} limit 10 offset 0";
  162. $this->assertEquals($compareSql, $sql);
  163. }
  164. }
  165. ?>