Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ModelDataProviderUtilTest.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  ********************************************************************************/
44  {
45  public static function setUpBeforeClass()
46  {
47  parent::setUpBeforeClass();
48  SecurityTestHelper::createSuperAdmin();
49  }
50 
51  public function testUsingOnTableAliasCorrectlyResolves()
52  {
53  Yii::app()->user->userModel = User::getByUsername('super');
54 
55  $searchAttributeData = array();
56  $searchAttributeData['clauses'] = array(
57  1 => array(
58  'attributeName' => 'iMember',
59  'operatorType' => 'equals',
60  'value' => 'somevalue1',
61  ),
62  );
63  $searchAttributeData['structure'] = '1';
64  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
65 
67  $where = ModelDataProviderUtil::makeWhere('I', $searchAttributeData, $joinTablesAdapter, 'analias');
68  $compareWhere = "({$quote}analias{$quote}.{$quote}imember{$quote} = 'somevalue1')";
69  $this->assertEquals($compareWhere, $where);
70  }
71 
72  public function testResolveSortAttributeColumnName()
73  {
75 
76  //Test a standard non-relation attribute on I
77  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
78  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('I', 'iMember');
81  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
82  $this->assertEquals("{$quote}i{$quote}.{$quote}imember{$quote}", $sort);
83  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
84  $this->assertEquals(0, $joinTablesAdapter->getLeftTableJoinCount());
85 
86  //Test a standard casted up attribute on H from I
87  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
88  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('I', 'name');
91  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
92  $this->assertEquals("{$quote}h{$quote}.{$quote}name{$quote}", $sort);
93  $this->assertEquals(1, $joinTablesAdapter->getFromTableJoinCount());
94  $this->assertEquals(0, $joinTablesAdapter->getLeftTableJoinCount());
95 
96  //Test a relation attribute G->g from H (HAS_ONE)
97  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('H');
98  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('H', 'castUpHasOne', 'g');
101  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
102  $this->assertEquals("{$quote}g{$quote}.{$quote}g{$quote}", $sort);
103  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
104  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
105  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
106  $this->assertEquals('g', $leftTables[0]['tableName']);
107 
108  //Test a relation attribute G->g where casted up from I (HAS_ONE)
109  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
110  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('I', 'castUpHasOne', 'g');
113  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
114 
115  $this->assertEquals("{$quote}g{$quote}.{$quote}g{$quote}", $sort);
116  $this->assertEquals(1, $joinTablesAdapter->getFromTableJoinCount());
117  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
118  $fromTables = $joinTablesAdapter->getFromTablesAndAliases();
119  $this->assertEquals('h', $fromTables[0]['tableName']);
120  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
121  $this->assertEquals('g', $leftTables[0]['tableName']);
122 
123  //Test a customField like TestCustomFieldsModel->industry
124  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('TestCustomFieldsModel');
125  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('TestCustomFieldsModel', 'industry', 'value');
128  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
129  $this->assertEquals("{$quote}customfield{$quote}.{$quote}value{$quote}", $sort);
130  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
131  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
132  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
133  $this->assertEquals('customfield', $leftTables[0]['tableName']);
134 
135  //Test I HAS_MANY K -> kMember (Testing HAS_MANY)
136  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
137  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('I', 'ks', 'kMember');
140  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
141  $this->assertEquals("{$quote}k{$quote}.{$quote}kmember{$quote}", $sort);
142  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
143  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
144  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
145  $this->assertEquals('k', $leftTables[0]['tableName']);
146 
147  //Test I MANY_MANY Z -> z (Testing MANY_MANY)
148  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
149  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('I', 'manyManyRelation', 'z');
152  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
153  $this->assertEquals("{$quote}z{$quote}.{$quote}z{$quote}", $sort);
154  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
155  $this->assertEquals(2, $joinTablesAdapter->getLeftTableJoinCount());
156  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
157  $this->assertEquals('i_z', $leftTables[0]['tableName']);
158  $this->assertEquals('z', $leftTables[1]['tableName']);
159  }
160 
165  {
167  //Test a customField like TestCustomFieldsModel->industry
168  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('TestCustomFieldsModel');
169  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('TestCustomFieldsModel', 'industry', 'value');
172  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
173  $this->assertEquals("{$quote}customfield{$quote}.{$quote}value{$quote}", $sort);
174  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
175  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
176  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
177  $this->assertEquals('customfield', $leftTables[0]['tableName']);
178 
179  //Now add a second sort on a different CustomField
180  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('TestCustomFieldsModel', 'market', 'value');
183  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
184  $this->assertEquals("{$quote}customfield1{$quote}.{$quote}value{$quote}", $sort);
185  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
186  $this->assertEquals(2, $joinTablesAdapter->getLeftTableJoinCount());
187  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
188  $this->assertEquals('customfield', $leftTables[1]['tableName']);
189  $this->assertEquals('customfield1', $leftTables[1]['tableAliasName']);
190  }
191 
196  {
198 
199  //Test a standard non-relation attribute on I
200  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
201  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('I', 'iMember');
202  $groupBy = ModelDataProviderUtil::
204  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
205  $this->assertEquals("{$quote}i{$quote}.{$quote}imember{$quote}", $groupBy);
206  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
207  $this->assertEquals(0, $joinTablesAdapter->getLeftTableJoinCount());
208 
209  //Test a standard casted up attribute on H from I
210  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
211  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('I', 'name');
212  $groupBy = ModelDataProviderUtil::
214  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
215  $this->assertEquals("{$quote}h{$quote}.{$quote}name{$quote}", $groupBy);
216  $this->assertEquals(1, $joinTablesAdapter->getFromTableJoinCount());
217  $this->assertEquals(0, $joinTablesAdapter->getLeftTableJoinCount());
218 
219  //Test a relation attribute G->g from H (HAS_ONE)
220  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('H');
221  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('H', 'castUpHasOne', 'g');
222  $groupBy = ModelDataProviderUtil::
224  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
225  $this->assertEquals("{$quote}g{$quote}.{$quote}g{$quote}", $groupBy);
226  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
227  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
228  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
229  $this->assertEquals('g', $leftTables[0]['tableName']);
230 
231  //Test a relation attribute G->g where casted up from I (HAS_ONE)
232  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
233  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('I', 'castUpHasOne', 'g');
234  $groupBy = ModelDataProviderUtil::
236  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
237 
238  $this->assertEquals("{$quote}g{$quote}.{$quote}g{$quote}", $groupBy);
239  $this->assertEquals(1, $joinTablesAdapter->getFromTableJoinCount());
240  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
241  $fromTables = $joinTablesAdapter->getFromTablesAndAliases();
242  $this->assertEquals('h', $fromTables[0]['tableName']);
243  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
244  $this->assertEquals('g', $leftTables[0]['tableName']);
245 
246  //Test a customField like TestCustomFieldsModel->industry
247  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('TestCustomFieldsModel');
248  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('TestCustomFieldsModel', 'industry', 'value');
249  $groupBy = ModelDataProviderUtil::
251  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
252  $this->assertEquals("{$quote}customfield{$quote}.{$quote}value{$quote}", $groupBy);
253  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
254  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
255  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
256  $this->assertEquals('customfield', $leftTables[0]['tableName']);
257 
258  //Test I HAS_MANY K -> kMember (Testing HAS_MANY)
259  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
260  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('I', 'ks', 'kMember');
261  $groupBy = ModelDataProviderUtil::
263  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
264  $this->assertEquals("{$quote}k{$quote}.{$quote}kmember{$quote}", $groupBy);
265  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
266  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
267  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
268  $this->assertEquals('k', $leftTables[0]['tableName']);
269 
270  //Test I MANY_MANY Z -> z (Testing MANY_MANY)
271  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
272  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('I', 'manyManyRelation', 'z');
273  $groupBy = ModelDataProviderUtil::
275  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
276  $this->assertEquals("{$quote}z{$quote}.{$quote}z{$quote}", $groupBy);
277  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
278  $this->assertEquals(2, $joinTablesAdapter->getLeftTableJoinCount());
279  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
280  $this->assertEquals('i_z', $leftTables[0]['tableName']);
281  $this->assertEquals('z', $leftTables[1]['tableName']);
282  }
283 
288  {
290  //Test a customField like TestCustomFieldsModel->industry
291  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('TestCustomFieldsModel');
292  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('TestCustomFieldsModel', 'industry', 'value');
293  $groupBy = ModelDataProviderUtil::
295  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
296  $this->assertEquals("{$quote}customfield{$quote}.{$quote}value{$quote}", $groupBy);
297  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
298  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
299  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
300  $this->assertEquals('customfield', $leftTables[0]['tableName']);
301 
302  //Now add a second sort on a different CustomField
303  $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('TestCustomFieldsModel', 'market', 'value');
304  $groupBy = ModelDataProviderUtil::
306  $modelAttributeToDataProviderAdapter, $joinTablesAdapter);
307  $this->assertEquals("{$quote}customfield1{$quote}.{$quote}value{$quote}", $groupBy);
308  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
309  $this->assertEquals(2, $joinTablesAdapter->getLeftTableJoinCount());
310  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
311  $this->assertEquals('customfield', $leftTables[1]['tableName']);
312  $this->assertEquals('customfield1', $leftTables[1]['tableAliasName']);
313  }
314  }
315 ?>
static getByUsername($username)
Definition: User.php:49
static resolveSortAttributeColumnName(RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter, RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter, $onTableAliasName=null)
static makeWhere($modelClassName, array $metadata, $joinTablesAdapter, $onTableAliasName=null)
static resolveGroupByAttributeColumnName(RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter, RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter, $onTableAliasName=null)
Generated on Fri Jul 3 2020 07:10:28
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.