Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ReportDataProviderTest.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 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. 2017. All rights reserved".
35  ********************************************************************************/
36 
38  {
39  public static function setUpBeforeClass()
40  {
41  parent::setUpBeforeClass();
43  SecurityTestHelper::createSuperAdmin();
44  $sally = UserTestHelper::createBasicUser('sally');
45  $sally->setRight('AccountsModule', AccountsModule::RIGHT_ACCESS_ACCOUNTS);
46  $sally->setRight('ContactsModule', ContactsModule::RIGHT_ACCESS_CONTACTS);
47  $sally->setRight('MeetingsModule', MeetingsModule::RIGHT_ACCESS_MEETINGS);
48  $sally->setRight('OpportunitiesModule', OpportunitiesModule::RIGHT_ACCESS_OPPORTUNITIES);
49  $sally->setRight('ReportsTestModule', ReportsTestModule::RIGHT_ACCESS_REPORTS_TESTS);
50  if (!$sally->save())
51  {
52  throw new FailedToSaveModelException();
53  }
54  $sarah = UserTestHelper::createBasicUser('sarah');
55  $sarah->setRight('AccountsModule', AccountsModule::RIGHT_ACCESS_ACCOUNTS);
56  $sarah->setRight('LeadsModule', LeadsModule::RIGHT_ACCESS_LEADS);
57  $sarah->setRight('MeetingsModule', MeetingsModule::RIGHT_ACCESS_MEETINGS);
58  $sarah->setRight('OpportunitiesModule', OpportunitiesModule::RIGHT_ACCESS_OPPORTUNITIES);
59  $sarah->setRight('ReportsTestModule', ReportsTestModule::RIGHT_ACCESS_REPORTS_TESTS);
60  if (!$sarah->save())
61  {
62  throw new FailedToSaveModelException();
63  }
64  $nobody = UserTestHelper::createBasicUser('nobody');
65  if (!$nobody->save())
66  {
67  throw new FailedToSaveModelException();
68  }
69  }
70 
71  public static function getDependentTestModelClassNames()
72  {
73  return array('ReportModelTestItem', 'ReportModelTestItem2');
74  }
75 
76  public function testResolveFiltersForReadPermissionsWithoutAnyExistingFiltersForASuperUser()
77  {
78  Yii::app()->user->userModel = User::getByUsername('super');
79  $report = new Report();
80  $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
81  $report->setModuleClassName('ReportsTestModule');
82  $reportDataProvider = new RowsAndColumnsReportDataProvider($report);
83  $filtersStructure = '';
84  $filters = array();
85  $filters = $reportDataProvider->resolveFiltersForReadPermissions($filters, $filtersStructure);
86  $this->assertEquals(0, count($filters));
87  $this->assertEquals('', $filtersStructure);
88  }
89 
90  public function testResolveFiltersForReadPermissionsWithoutAnyExistingFiltersForANonSuperUser()
91  {
92  Yii::app()->user->userModel = User::getByUsername('sally');
93  $report = new Report();
94  $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
95  $report->setModuleClassName('ReportsTestModule');
96  $reportDataProvider = new RowsAndColumnsReportDataProvider($report);
97  $filtersStructure = '';
98  $filters = array();
99  $filters = $reportDataProvider->resolveFiltersForReadPermissions($filters, $filtersStructure);
100  $this->assertEquals(2, count($filters));
101  $this->assertEquals('owner__User', $filters[0]->attributeIndexOrDerivedType);
102  $this->assertEquals('ReadOptimization', $filters[1]->attributeIndexOrDerivedType);
103  $this->assertEquals('(1 or 2)', $filtersStructure);
104  }
105 
106  public function testResolveFiltersForReadPermissionsWithOneDisplayAttributeAndOneFilterForANonSuperUser()
107  {
108  Yii::app()->user->userModel = User::getByUsername('sally');
109  $report = new Report();
110  $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
111  $report->setModuleClassName('ReportsTestModule');
112 
113  $displayAttribute = new DisplayAttributeForReportForm('ReportsTestModule', 'ReportModelTestItem',
115  $displayAttribute->attributeIndexOrDerivedType = 'hasOne___name';
116  $report->addDisplayAttribute($displayAttribute);
117 
118  $reportDataProvider = new RowsAndColumnsReportDataProvider($report);
119  $filtersStructure = '1';
120  $filter = new FilterForReportForm('ReportsTestModule', 'ReportModelTestItem',
122  $filter->attributeIndexOrDerivedType = 'string';
123  $filter->operator = OperatorRules::TYPE_EQUALS;
124  $filter->value = 'Zurmo';
125  $filters = array($filter);
126  $filters = $reportDataProvider->resolveFiltersForReadPermissions($filters, $filtersStructure);
127  $this->assertEquals(5, count($filters));
128  $this->assertEquals('owner__User', $filters[1]->attributeIndexOrDerivedType);
129  $this->assertEquals('ReadOptimization', $filters[2]->attributeIndexOrDerivedType);
130  $this->assertEquals('hasOne___owner__User', $filters[3]->attributeIndexOrDerivedType);
131  $this->assertEquals('hasOne___ReadOptimization', $filters[4]->attributeIndexOrDerivedType);
132  $this->assertEquals('1 and ((2 or 3) and (4 or 5))', $filtersStructure);
133  }
134 
135  public function testResolveFiltersForVariableStatesWithoutAnyExistingFiltersForASuperUser()
136  {
137  Yii::app()->user->userModel = User::getByUsername('super');
138  $report = new Report();
139  $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
140  $report->setModuleClassName('ContactsModule');
141  $reportDataProvider = new RowsAndColumnsReportDataProvider($report);
142  $filtersStructure = '';
143  $filters = array();
144  $filters = $reportDataProvider->resolveFiltersForVariableStates($filters, $filtersStructure);
145  $this->assertEquals(0, count($filters));
146  $this->assertEquals('', $filtersStructure);
147  }
148 
149  public function testResolveFiltersForVariableStatesWithoutAnyExistingFiltersForANonSuperUserWhoCanSeeOneState()
150  {
151  Yii::app()->user->userModel = User::getByUsername('sally');
152  $report = new Report();
153  $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
154  $report->setModuleClassName('ContactsModule');
155  $reportDataProvider = new RowsAndColumnsReportDataProvider($report);
156  $filtersStructure = '';
157  $filters = array();
158  $filters = $reportDataProvider->resolveFiltersForVariableStates($filters, $filtersStructure);
159  $stateAdapter = new ContactsStateMetadataAdapter(array('clauses' => array(), 'structure' => ''));
160  $this->assertTrue(count($stateAdapter->getStateIds()) > 0);
161  $this->assertEquals(1, count($filters));
162  $this->assertEquals('state', $filters[0]->attributeIndexOrDerivedType);
163  $this->assertEquals(OperatorRules::TYPE_ONE_OF, $filters[0]->operator);
164  $this->assertEquals($stateAdapter->getStateIds(), $filters[0]->value);
165  $this->assertEquals('1', $filtersStructure);
166  }
167 
168  public function testResolveFiltersForVariableStatesWithoutAnyExistingFiltersForANonSuperUserWhoCanSeeAnotherState()
169  {
170  Yii::app()->user->userModel = User::getByUsername('sarah');
171  $report = new Report();
172  $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
173  $report->setModuleClassName('ContactsModule');
174  $reportDataProvider = new RowsAndColumnsReportDataProvider($report);
175  $filtersStructure = '';
176  $filters = array();
177  $filters = $reportDataProvider->resolveFiltersForVariableStates($filters, $filtersStructure);
178  $stateAdapter = new LeadsStateMetadataAdapter(array('clauses' => array(), 'structure' => ''));
179  $this->assertTrue(count($stateAdapter->getStateIds()) > 0);
180  $this->assertEquals(1, count($filters));
181  $this->assertEquals('state', $filters[0]->attributeIndexOrDerivedType);
182  $this->assertEquals(OperatorRules::TYPE_ONE_OF, $filters[0]->operator);
183  $this->assertEquals($stateAdapter->getStateIds(), $filters[0]->value);
184  $this->assertEquals('1', $filtersStructure);
185  }
186 
191  {
192  Yii::app()->user->userModel = User::getByUsername('nobody');
193  $report = new Report();
194  $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
195  $report->setModuleClassName('ContactsModule');
196  $reportDataProvider = new RowsAndColumnsReportDataProvider($report);
197  $filtersStructure = '';
198  $filters = array();
199  $reportDataProvider->resolveFiltersForVariableStates($filters, $filtersStructure);
200  }
201 
202  public function testResolveFiltersForVariableStatesWithOneDisplayAttributeAndOneFilterForANonSuperUserWhoCanSeeOneState()
203  {
204  Yii::app()->user->userModel = User::getByUsername('sally');
205  $report = new Report();
206  $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
207  $report->setModuleClassName('AccountsModule');
208 
209  $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account',
211  $displayAttribute->attributeIndexOrDerivedType = 'contacts___officePhone';
212  $report->addDisplayAttribute($displayAttribute);
213 
214  $reportDataProvider = new RowsAndColumnsReportDataProvider($report);
215  $filtersStructure = '1';
216  $filter = new FilterForReportForm('AccountsModule', 'Account',
218  $filter->attributeIndexOrDerivedType = 'opportunities___contacts___website';
219  $filter->operator = OperatorRules::TYPE_EQUALS;
220  $filter->value = 'Zurmo';
221  $filters = array($filter);
222  $filters = $reportDataProvider->resolveFiltersForVariableStates($filters, $filtersStructure);
223  $stateAdapter = new ContactsStateMetadataAdapter(array('clauses' => array(), 'structure' => ''));
224  $this->assertTrue(count($stateAdapter->getStateIds()) > 0);
225  $this->assertEquals(3, count($filters));
226  $this->assertEquals('contacts___state', $filters[1]->attributeIndexOrDerivedType);
227  $this->assertEquals(OperatorRules::TYPE_ONE_OF, $filters[1]->operator);
228  $this->assertEquals($stateAdapter->getStateIds(), $filters[1]->value);
229  $this->assertEquals('opportunities___contacts___state', $filters[2]->attributeIndexOrDerivedType);
230  $this->assertEquals(OperatorRules::TYPE_ONE_OF, $filters[2]->operator);
231  $this->assertEquals($stateAdapter->getStateIds(), $filters[2]->value);
232  $this->assertEquals('1 and (2 and 3)', $filtersStructure);
233  }
234 
235  public function testResolveFiltersForVariableStatesWithOneDisplayAttributeAndOneFilterForANonSuperUserWhoCanSeeAnotherState()
236  {
237  Yii::app()->user->userModel = User::getByUsername('sarah');
238  $report = new Report();
239  $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
240  $report->setModuleClassName('AccountsModule');
241 
242  $displayAttribute = new DisplayAttributeForReportForm('AccountsModule', 'Account',
244  $displayAttribute->attributeIndexOrDerivedType = 'contacts___officePhone';
245  $report->addDisplayAttribute($displayAttribute);
246 
247  $reportDataProvider = new RowsAndColumnsReportDataProvider($report);
248  $filtersStructure = '1';
249  $filter = new FilterForReportForm('AccountsModule', 'Account',
251  $filter->attributeIndexOrDerivedType = 'opportunities___contacts___website';
252  $filter->operator = OperatorRules::TYPE_EQUALS;
253  $filter->value = 'Zurmo';
254  $filters = array($filter);
255  $filters = $reportDataProvider->resolveFiltersForVariableStates($filters, $filtersStructure);
256  $stateAdapter = new LeadsStateMetadataAdapter(array('clauses' => array(), 'structure' => ''));
257  $this->assertTrue(count($stateAdapter->getStateIds()) > 0);
258  $this->assertEquals(3, count($filters));
259  $this->assertEquals('contacts___state', $filters[1]->attributeIndexOrDerivedType);
260  $this->assertEquals(OperatorRules::TYPE_ONE_OF, $filters[1]->operator);
261  $this->assertEquals($stateAdapter->getStateIds(), $filters[1]->value);
262  $this->assertEquals('opportunities___contacts___state', $filters[2]->attributeIndexOrDerivedType);
263  $this->assertEquals(OperatorRules::TYPE_ONE_OF, $filters[2]->operator);
264  $this->assertEquals($stateAdapter->getStateIds(), $filters[2]->value);
265  $this->assertEquals('1 and (2 and 3)', $filtersStructure);
266  }
267 
268  public function testSqlQueryWithLinkTypeSpecificOnRelatedModels()
269  {
271  Yii::app()->user->userModel = User::getByUsername('super');
272 
273  $reportModelTestItem2 = new ReportModelTestItem2();
274  $reportModelTestItem2->name = 'name';
275  $this->assertTrue($reportModelTestItem2->save());
276  $reportModelTestItem = new ReportModelTestItem();
277  $reportModelTestItem->lastName = 'lastName';
278  $reportModelTestItem->string = 'string';
279  $reportModelTestItem->hasOne = $reportModelTestItem2;
280  $this->assertTrue($reportModelTestItem->save());
281 
282  $report = new Report();
283  $report->setType(Report::TYPE_ROWS_AND_COLUMNS);
284  $report->setModuleClassName('ReportsTest2Module');
285  $report->setFiltersStructure('');
286  $displayAttribute1 = new DisplayAttributeForReportForm('ReportsTest2Module', 'ReportModelTestItem2',
288  $displayAttribute1->setModelAliasUsingTableAliasName('relatedModel');
289  $displayAttribute1->attributeIndexOrDerivedType = 'hasMany2___FullName';
290  $report->addDisplayAttribute($displayAttribute1);
291 
292  $dataProvider = new RowsAndColumnsReportDataProvider($report);
293  $content = $dataProvider->makeSqlQueryForDisplay();
294  $compareContent = "select {$quote}reportmodeltestitem{$quote}.{$quote}id{$quote} reportmodeltestitemid " .
295  "from {$quote}reportmodeltestitem2{$quote} " .
296  "left join {$quote}reportmodeltestitem{$quote} on " .
297  "{$quote}reportmodeltestitem{$quote}.{$quote}hasone_reportmodeltestitem2_id{$quote} " .
298  "= {$quote}reportmodeltestitem2{$quote}.{$quote}id{$quote} limit 10 offset 0";
299  $this->assertEquals($compareContent, $content);
300  }
301  }
302 ?>
static getByUsername($username)
Definition: User.php:49
static loadStartingData()
const TYPE_ROWS_AND_COLUMNS
Definition: Report.php:51
Generated on Thu Jul 9 2020 07:10:35
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.