Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
RedBeanModelDataProviderTest.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 
41  {
42  public static function setUpBeforeClass()
43  {
44  parent::setUpBeforeClass();
45  SecurityTestHelper::createSuperAdmin();
46 
47  $values = array(
48  'A',
49  'B',
50  'C',
51  'CC',
52  'CCC',
53  );
54  $customFieldData = CustomFieldData::getByName('MultipleIndustries');
55  $customFieldData->serializedData = serialize($values);
56  $saved = $customFieldData->save();
57  assert($saved); // Not Coding Standard
58 
59  $values = array(
60  'D',
61  'E',
62  'F',
63  'FF',
64  'FFF',
65  );
66  $customFieldData = CustomFieldData::getByName('MultipleSomethings');
67  $customFieldData->serializedData = serialize($values);
68  $saved = $customFieldData->save();
69  assert($saved); // Not Coding Standard
70 
71  $values = array(
72  'A1',
73  'B2',
74  'C3',
75  'D4',
76  );
77  $customFieldData = CustomFieldData::getByName('Industries');
78  $customFieldData->serializedData = serialize($values);
79  $saved = $customFieldData->save();
80  assert($saved); // Not Coding Standard
81  }
82 
83  public static function getDependentTestModelClassNames()
84  {
85  return array(
86  'G',
87  'GG',
88  'H',
89  'I',
90  'II',
91  'K',
92  'L',
93  'TestCustomFieldsModel');
94  }
95 
96  public function testSearchByCustomFieldWithMultipleValues()
97  {
98  //Save a sample model.
99  $model = new TestCustomFieldsModel();
100  $model->industry->value = 'A1';
101  $this->assertTrue($model->save());
102 
103  //Save a second and third sample model
104  $model = new TestCustomFieldsModel();
105  $model->industry->value = 'B2';
106  $this->assertTrue($model->save());
107 
108  $model = new TestCustomFieldsModel();
109  $model->industry->value = 'D4';
110  $this->assertTrue($model->save());
111 
112  //Save a second model with nothing.
113  $model = new TestCustomFieldsModel();
114  $this->assertTrue($model->save());
115 
117  //Test where relatioon id is in a joining table. Many to Many relationship
118  $_FAKEPOST['TestCustomFieldsModel'] = array();
119  $_FAKEPOST['TestCustomFieldsModel']['industry']['value'] = array('A1', 'B2', 'C3');
120  $metadataAdapter = new SearchDataProviderMetadataAdapter(
121  new TestCustomFieldsModel(false), 1, $_FAKEPOST['TestCustomFieldsModel']);
122  $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
123  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('TestCustomFieldsModel');
124  $where = RedBeanModelDataProvider::makeWhere('TestCustomFieldsModel', $searchAttributeData, $joinTablesAdapter);
125  $compareWhere = "({$quote}customfield{$quote}.{$quote}value{$quote} IN('A1','B2','C3'))"; // Not Coding Standard
126  $this->assertEquals($compareWhere, $where);
127  //Now test that the joinTablesAdapter has correct information.
128  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
129  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
130  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
131  $this->assertEquals('customfield', $leftTables[0]['tableName']);
132 
133  //Now test that the subsetSQL query produced is correct.
134  $subsetSql = TestCustomFieldsModel::
135  makeSubsetOrCountSqlQuery('testcustomfieldsmodel', $joinTablesAdapter, 1, 5, $where, null);
136  $compareSubsetSql = "select {$quote}testcustomfieldsmodel{$quote}.{$quote}id{$quote} id ";
137  $compareSubsetSql .= "from {$quote}testcustomfieldsmodel{$quote} ";
138  $compareSubsetSql .= "left join {$quote}customfield{$quote} on ";
139  $compareSubsetSql .= "{$quote}customfield{$quote}.{$quote}id{$quote} = ";
140  $compareSubsetSql .= "{$quote}testcustomfieldsmodel{$quote}.{$quote}industry_customfield_id{$quote} ";
141  $compareSubsetSql .= "where " . $compareWhere . ' ';
142  $compareSubsetSql .= 'limit 5 offset 1';
143  $this->assertEquals($compareSubsetSql, $subsetSql);
144 
145  //Make sure the sql runs properly.
146  $dataProvider = new RedBeanModelDataProvider('TestCustomFieldsModel', null, false, $searchAttributeData);
147  $data = $dataProvider->getData();
148  $this->assertEquals(2, count($data));
149  }
150 
155  {
156  //Save a sample model.
157  $model = new TestCustomFieldsModel();
158  $customFieldValue = new CustomFieldValue();
159  $customFieldValue->value = 'A';
160  $model->multipleIndustries->values->add($customFieldValue);
161  $customFieldValue = new CustomFieldValue();
162  $customFieldValue->value = 'D';
163  $model->multipleSomethings->values->add($customFieldValue);
164  $this->assertTrue($model->save());
165 
166  //Save a second model with nothing.
167  $model = new TestCustomFieldsModel();
168  $this->assertTrue($model->save());
169 
171  //Test where relatioon id is in a joining table. Many to Many relationship
172  $_FAKEPOST['TestCustomFieldsModel'] = array();
173  $_FAKEPOST['TestCustomFieldsModel']['multipleIndustries']['values'] = array('A', 'B', 'C');
174  $metadataAdapter = new SearchDataProviderMetadataAdapter(
175  new TestCustomFieldsModel(false), 1, $_FAKEPOST['TestCustomFieldsModel']);
176  $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
177  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('TestCustomFieldsModel');
178  $where = RedBeanModelDataProvider::makeWhere('TestCustomFieldsModel', $searchAttributeData, $joinTablesAdapter);
179  $compareWhere = "(1 = (select 1 from {$quote}customfieldvalue{$quote} customfieldvalue " .
180  "where {$quote}customfieldvalue{$quote}.{$quote}multiplevaluescustomfield_id{$quote} = " .
181  "{$quote}multiplevaluescustomfield{$quote}.id " .
182  "and {$quote}customfieldvalue{$quote}.{$quote}value{$quote} IN('A','B','C') limit 1))"; // Not Coding Standard
183  $this->assertEquals($compareWhere, $where);
184  //Now test that the joinTablesAdapter has correct information.
185  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
186  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
187  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
188  $this->assertEquals('multiplevaluescustomfield', $leftTables[0]['tableName']);
189 
190  //Now test that the subsetSQL query produced is correct.
191  $subsetSql = TestCustomFieldsModel::
192  makeSubsetOrCountSqlQuery('testcustomfieldsmodel', $joinTablesAdapter, 1, 5, $where, null);
193  $compareSubsetSql = "select {$quote}testcustomfieldsmodel{$quote}.{$quote}id{$quote} id ";
194  $compareSubsetSql .= "from {$quote}testcustomfieldsmodel{$quote} ";
195  $compareSubsetSql .= "left join {$quote}multiplevaluescustomfield{$quote} on ";
196  $compareSubsetSql .= "{$quote}multiplevaluescustomfield{$quote}.{$quote}id{$quote} = ";
197  $compareSubsetSql .= "{$quote}testcustomfieldsmodel{$quote}.{$quote}multipleindustries_multiplevaluescustomfield_id{$quote} ";
198  $compareSubsetSql .= "where " . $compareWhere . ' ';
199  $compareSubsetSql .= 'limit 5 offset 1';
200  $this->assertEquals($compareSubsetSql, $subsetSql);
201 
202  //Make sure the sql runs properly.
203  $dataProvider = new RedBeanModelDataProvider('TestCustomFieldsModel', null, false, $searchAttributeData);
204  $data = $dataProvider->getData();
205  $this->assertEquals(1, count($data));
206  }
207 
212  {
214  //Test where relatioon id is in a joining table. Many to Many relationship
215  $_FAKEPOST['TestCustomFieldsModel'] = array();
216  $_FAKEPOST['TestCustomFieldsModel']['multipleIndustries']['values'] = array('A', 'B', 'C');
217  $_FAKEPOST['TestCustomFieldsModel']['multipleSomethings']['values'] = array('D', 'E', 'F');
218  $metadataAdapter = new SearchDataProviderMetadataAdapter(
219  new TestCustomFieldsModel(false), 1, $_FAKEPOST['TestCustomFieldsModel']);
220  $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
221  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('TestCustomFieldsModel');
222  $where = RedBeanModelDataProvider::makeWhere('TestCustomFieldsModel', $searchAttributeData, $joinTablesAdapter);
223  $compareWhere = "(1 = (select 1 from {$quote}customfieldvalue{$quote} customfieldvalue " .
224  "where {$quote}customfieldvalue{$quote}.{$quote}multiplevaluescustomfield_id{$quote} = " .
225  "{$quote}multiplevaluescustomfield{$quote}.id " .
226  "and {$quote}customfieldvalue{$quote}.{$quote}value{$quote} IN('A','B','C') limit 1))"; // Not Coding Standard
227  $compareWhere .= " and (1 = (select 1 from {$quote}customfieldvalue{$quote} customfieldvalue " .
228  "where {$quote}customfieldvalue{$quote}.{$quote}multiplevaluescustomfield_id{$quote} = " .
229  "{$quote}multiplevaluescustomfield1{$quote}.id " .
230  "and {$quote}customfieldvalue{$quote}.{$quote}value{$quote} IN('D','E','F') limit 1))"; // Not Coding Standard
231  $this->assertEquals($compareWhere, $where);
232  //Now test that the joinTablesAdapter has correct information.
233  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
234  $this->assertEquals(2, $joinTablesAdapter->getLeftTableJoinCount());
235  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
236  $this->assertEquals('multiplevaluescustomfield', $leftTables[0]['tableName']);
237  $this->assertEquals('multiplevaluescustomfield', $leftTables[1]['tableName']);
238 
239  //Now test that the subsetSQL query produced is correct.
240  $subsetSql = TestCustomFieldsModel::
241  makeSubsetOrCountSqlQuery('testcustomfieldsmodel', $joinTablesAdapter, 1, 5, $where, null);
242  $compareSubsetSql = "select {$quote}testcustomfieldsmodel{$quote}.{$quote}id{$quote} id ";
243  $compareSubsetSql .= "from {$quote}testcustomfieldsmodel{$quote} ";
244  $compareSubsetSql .= "left join {$quote}multiplevaluescustomfield{$quote} on ";
245  $compareSubsetSql .= "{$quote}multiplevaluescustomfield{$quote}.{$quote}id{$quote} = ";
246  $compareSubsetSql .= "{$quote}testcustomfieldsmodel{$quote}.{$quote}multipleindustries_multiplevaluescustomfield_id{$quote} ";
247  $compareSubsetSql .= "left join {$quote}multiplevaluescustomfield{$quote} multiplevaluescustomfield1 on ";
248  $compareSubsetSql .= "{$quote}multiplevaluescustomfield1{$quote}.{$quote}id{$quote} = ";
249  $compareSubsetSql .= "{$quote}testcustomfieldsmodel{$quote}.{$quote}multiplesomethings_multiplevaluescustomfield_id{$quote} ";
250  $compareSubsetSql .= "where " . $compareWhere . ' ';
251  $compareSubsetSql .= 'limit 5 offset 1';
252  $this->assertEquals($compareSubsetSql, $subsetSql);
253 
254  //Make sure the sql runs properly.
255  $dataProvider = new RedBeanModelDataProvider('TestCustomFieldsModel', null, false, $searchAttributeData);
256  $data = $dataProvider->getData();
257  $this->assertEquals(1, count($data));
258  }
259 
264  {
265  $super = User::getByUsername('super');
266  Yii::app()->user->userModel = $super;
267  //I has many ls.
268  $i = new I();
269  $i->iMember = 'abc';
270  $this->assertTrue($i->save());
271 
272  $searchAttributeData = array();
273  $searchAttributeData['clauses'] = array(
274  1 => array(
275  'attributeName' => 'iMember',
276  'operatorType' => 'isNull',
277  'value' => null,
278  )
279  );
280  $searchAttributeData['structure'] = '1';
281  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
282 
284  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
285  $compareWhere = "({$quote}i{$quote}.{$quote}imember{$quote} IS NULL)"; // Not Coding Standard
286  $this->assertEquals($compareWhere, $where);
287  //Make sure the sql runs properly.
288  $dataProvider = new RedBeanModelDataProvider('I', null, false, $searchAttributeData);
289  $data = $dataProvider->getData();
290 
291  //Test is not null
292  $searchAttributeData = array();
293  $searchAttributeData['clauses'] = array(
294  1 => array(
295  'attributeName' => 'iMember',
296  'operatorType' => 'isNotNull',
297  'value' => null,
298  )
299  );
300  $searchAttributeData['structure'] = '1';
301  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
302 
304  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
305  $compareWhere = "({$quote}i{$quote}.{$quote}imember{$quote} IS NOT NULL)"; // Not Coding Standard
306  $this->assertEquals($compareWhere, $where);
307  //Make sure the sql runs properly.
308  $dataProvider = new RedBeanModelDataProvider('I', null, false, $searchAttributeData);
309  $data = $dataProvider->getData();
310 
311  //Test is empty
312  $searchAttributeData = array();
313  $searchAttributeData['clauses'] = array(
314  1 => array(
315  'attributeName' => 'iMember',
316  'operatorType' => 'isEmpty',
317  'value' => null,
318  )
319  );
320  $searchAttributeData['structure'] = '1';
321  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
322 
324  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
325  $compareWhere = "({$quote}i{$quote}.{$quote}imember{$quote} = '')";
326  $this->assertEquals($compareWhere, $where);
327  //Make sure the sql runs properly.
328  $dataProvider = new RedBeanModelDataProvider('I', null, false, $searchAttributeData);
329  $data = $dataProvider->getData();
330 
331  //Test is not empty
332  $searchAttributeData = array();
333  $searchAttributeData['clauses'] = array(
334  1 => array(
335  'attributeName' => 'iMember',
336  'operatorType' => 'isNotEmpty',
337  'value' => null,
338  )
339  );
340  $searchAttributeData['structure'] = '1';
341  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
342 
344  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
345  $compareWhere = "({$quote}i{$quote}.{$quote}imember{$quote} != '')";
346  $this->assertEquals($compareWhere, $where);
347  //Make sure the sql runs properly.
348  $dataProvider = new RedBeanModelDataProvider('I', null, false, $searchAttributeData);
349  $data = $dataProvider->getData();
350  }
351 
352  public function testEscapedSingleQuoteInWhereClause()
353  {
354  $super = User::getByUsername('super');
355  Yii::app()->user->userModel = $super;
356  //I has many ls.
357  $i = new I();
358  $l = new L();
359  $l->lMember = 'def';
360  $this->assertTrue($l->save());
361  $i->iMember = 'abc';
362  $i->ls->add($l);
363  $this->assertTrue($i->save());
364 
365  $searchAttributeData = array();
366  $searchAttributeData['clauses'] = array(
367  1 => array(
368  'attributeName' => 'ls',
369  'relatedAttributeName' => 'lMember',
370  'operatorType' => 'equals',
371  'value' => "some'value",
372  )
373  );
374  $searchAttributeData['structure'] = '1';
375  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
376 
378  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
379  $compareWhere = "({$quote}l{$quote}.{$quote}lmember{$quote} = 'some\'value')";
380  $this->assertEquals($compareWhere, $where);
381 
382  //Make sure the sql runs properly.
383  $dataProvider = new RedBeanModelDataProvider('I', null, false, $searchAttributeData);
384  $data = $dataProvider->getData();
385  }
386 
390  public function testSearchByRelationId()
391  {
393  //Test where relation id is a column on the model.
394  $_FAKEPOST['I'] = array();
395  $_FAKEPOST['I']['hasOneRelation']['id'] = '3';
396  $metadataAdapter = new SearchDataProviderMetadataAdapter(new I(false), 1, $_FAKEPOST['I']);
397  $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
398  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
399  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
400  $compareWhere = "({$quote}i{$quote}.{$quote}hasonerelation_customfield_id{$quote} = 3)";
401  $this->assertEquals($compareWhere, $where);
402  //Now test that the joinTablesAdapter has correct information.
403 
404  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
405  $this->assertEquals(0, $joinTablesAdapter->getLeftTableJoinCount());
406 
407  //Test where relation id is no a column on the model.
408  $_FAKEPOST['I'] = array();
409  $_FAKEPOST['I']['hasManyRelation']['id'] = '5';
410  $metadataAdapter = new SearchDataProviderMetadataAdapter(new I(false), 1, $_FAKEPOST['I']);
411  $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
412  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
413  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
414  $compareWhere = "({$quote}a{$quote}.{$quote}id{$quote} = 5)";
415  $this->assertEquals($compareWhere, $where);
416  //Now test that the joinTablesAdapter has correct information.
417  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
418  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
419  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
420  $this->assertEquals('a', $leftTables[0]['tableName']);
421 
422  //Test where relation id is a column on the castUp model.
423  $_FAKEPOST['I'] = array();
424  $_FAKEPOST['I']['castUpHasOne']['id'] = '4';
425  $metadataAdapter = new SearchDataProviderMetadataAdapter(new I(false), 1, $_FAKEPOST['I']);
426  $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
427  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
428  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
429  $compareWhere = "({$quote}h{$quote}.{$quote}castuphasone_g_id{$quote} = 4)";
430  $this->assertEquals($compareWhere, $where);
431  //Now test that the joinTablesAdapter has correct information.
432  $this->assertEquals(1, $joinTablesAdapter->getFromTableJoinCount());
433  $this->assertEquals(0, $joinTablesAdapter->getLeftTableJoinCount());
434 
435  //Test where relatioon id is in a joining table. Many to Many relationship
436  $_FAKEPOST['I'] = array();
437  $_FAKEPOST['I']['manyManyRelation']['id'] = '55';
438  $metadataAdapter = new SearchDataProviderMetadataAdapter(new I(false), 1, $_FAKEPOST['I']);
439  $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
440  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
441  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
442  $compareWhere = "({$quote}i_z{$quote}.{$quote}z_id{$quote} = 55)";
443  $this->assertEquals($compareWhere, $where);
444  //Now test that the joinTablesAdapter has correct information.
445  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
446  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
447  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
448  $this->assertEquals('i_z', $leftTables[0]['tableName']);
449 
450  //Now test that the subsetSQL query produced is correct.
451  $subsetSql = I::makeSubsetOrCountSqlQuery('i', $joinTablesAdapter, 1, 5, $where, null);
452  $compareSubsetSql = "select {$quote}i{$quote}.{$quote}id{$quote} id ";
453  $compareSubsetSql .= "from {$quote}i{$quote} ";
454  $compareSubsetSql .= "left join {$quote}i_z{$quote} on ";
455  $compareSubsetSql .= "{$quote}i_z{$quote}.{$quote}i_id{$quote} = {$quote}i{$quote}.{$quote}id{$quote} ";
456  $compareSubsetSql .= "where " . $compareWhere . ' ';
457  $compareSubsetSql .= 'limit 5 offset 1';
458  $this->assertEquals($compareSubsetSql, $subsetSql);
459 
460  //Make sure the sql runs properly.
461  $dataProvider = new RedBeanModelDataProvider('II', null, false, $searchAttributeData);
462  $data = $dataProvider->getData();
463  }
464 
469  {
470  //Test member of search.
471  $_FAKEPOST['I'] = array();
472  $_FAKEPOST['I']['i']['id'] = '4';
473  $metadataAdapter = new SearchDataProviderMetadataAdapter(
474  new I(false),
475  1,
476  $_FAKEPOST['I']
477  );
478  $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
479  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
481  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
482  $compareWhere = "({$quote}i{$quote}.{$quote}i_id{$quote} = 4)";
483  $this->assertEquals($compareWhere, $where);
484 
485  //Now test that the joinTablesAdapter has correct information.
486  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
487  $this->assertEquals(0, $joinTablesAdapter->getLeftTableJoinCount());
488 
489  //Make sure the sql runs properly.
490  $dataProvider = new RedBeanModelDataProvider('I', null, false, $searchAttributeData);
491  $data = $dataProvider->getData();
492  }
493 
498  {
499  $_FAKEPOST['I'] = array();
500  $_FAKEPOST['I']['is']['id'] = '5';
501  $metadataAdapter = new SearchDataProviderMetadataAdapter(new I(false), 1, $_FAKEPOST['I']);
502  $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
503  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
505  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
506  $compareWhere = "({$quote}i1{$quote}.{$quote}id{$quote} = 5)";
507  $this->assertEquals($compareWhere, $where);
508  //Now test that the joinTablesAdapter has correct information.
509  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
510  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
511  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
512  $this->assertEquals('i', $leftTables[0]['tableName']);
513 
514  //Make sure the sql runs properly.
515  $dataProvider = new RedBeanModelDataProvider('I', null, false, $searchAttributeData);
516  $data = $dataProvider->getData();
517  }
518 
523  {
524  $_FAKEPOST['I'] = array();
525  $_FAKEPOST['I']['castUpHasOne']['g'] = 'somevalue';
526  $metadataAdapter = new SearchDataProviderMetadataAdapter(new I(false), 1, $_FAKEPOST['I']);
527  $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
528  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
530  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
531  $compareWhere = "({$quote}g{$quote}.{$quote}g{$quote} like 'somevalue%')";
532  $this->assertEquals($compareWhere, $where);
533  //Now test that the joinTablesAdapter has correct information.
534  $this->assertEquals(1, $joinTablesAdapter->getFromTableJoinCount());
535  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
536  $fromTables = $joinTablesAdapter->getFromTablesAndAliases();
537  $this->assertEquals('h', $fromTables[0]['tableName']);
538  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
539  $this->assertEquals('g', $leftTables[0]['tableName']);
540  }
541 
546  {
547  $super = User::getByUsername('super');
548  Yii::app()->user->userModel = $super;
549  //II extends I which extends H, show search on II where you are querying an attribute on H.
550  $_FAKEPOST['II'] = array();
551  $_FAKEPOST['II']['castUpHasOne']['g'] = 'somevalue';
552  $metadataAdapter = new SearchDataProviderMetadataAdapter(new II(false), 1, $_FAKEPOST['II']);
553  $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
554  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('II');
556  $where = RedBeanModelDataProvider::makeWhere('II', $searchAttributeData, $joinTablesAdapter);
557  $compareWhere = "({$quote}g{$quote}.{$quote}g{$quote} like 'somevalue%')";
558  $this->assertEquals($compareWhere, $where);
559  //Now test that the joinTablesAdapter has correct information.
560  $this->assertEquals(2, $joinTablesAdapter->getFromTableJoinCount());
561  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
562  $fromTables = $joinTablesAdapter->getFromTablesAndAliases();
563  $this->assertEquals('i', $fromTables[0]['tableName']);
564  $this->assertEquals('h', $fromTables[1]['tableName']);
565  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
566  $this->assertEquals('g', $leftTables[0]['tableName']);
567 
568  //Now test that the subsetSQL query produced is correct.
569  $subsetSql = II::makeSubsetOrCountSqlQuery('ii', $joinTablesAdapter, 1, 5, $where, null);
570  $compareSubsetSql = "select {$quote}ii{$quote}.{$quote}id{$quote} id ";
571  $compareSubsetSql .= "from ({$quote}ii{$quote}, {$quote}i{$quote}, {$quote}h{$quote}) ";
572  $compareSubsetSql .= "left join {$quote}g{$quote} on ";
573  $compareSubsetSql .= "{$quote}g{$quote}.{$quote}id{$quote} = {$quote}h{$quote}.{$quote}castuphasone_g_id{$quote} ";
574  $compareSubsetSql .= "where " . $compareWhere . ' ';
575  $compareSubsetSql .= "and {$quote}i{$quote}.{$quote}id{$quote} = {$quote}ii{$quote}.{$quote}i_id{$quote} ";
576  $compareSubsetSql .= "and {$quote}h{$quote}.{$quote}id{$quote} = {$quote}i{$quote}.{$quote}h_id{$quote} ";
577  $compareSubsetSql .= 'limit 5 offset 1';
578  $this->assertEquals($compareSubsetSql, $subsetSql);
579 
580  //Make sure the sql runs properly.
581  $dataProvider = new RedBeanModelDataProvider('II', null, false, $searchAttributeData);
582  $data = $dataProvider->getData();
583  }
584 
588  public function testHasManyRelationSqlQuery()
589  {
590  $super = User::getByUsername('super');
591  Yii::app()->user->userModel = $super;
592  //I has many Ks.
593  $searchAttributeData = array();
594  $searchAttributeData['clauses'] = array(
595  1 => array(
596  'attributeName' => 'ks',
597  'relatedAttributeName' => 'kMember',
598  'operatorType' => 'equals',
599  'value' => 'somevalue',
600  )
601  );
602  $searchAttributeData['structure'] = '1';
603  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
604 
606  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
607  $compareWhere = "({$quote}k{$quote}.{$quote}kmember{$quote} = 'somevalue')";
608  $this->assertEquals($compareWhere, $where);
609  //Now test that the joinTablesAdapter has correct information.
610  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
611  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
612  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
613  $this->assertEquals('k', $leftTables[0]['tableName']);
614 
615  //Now test that the subsetSQL query produced is correct.
616  $subsetSql = I::makeSubsetOrCountSqlQuery('i', $joinTablesAdapter, 1, 5,
617  $where, null, false, $joinTablesAdapter->getSelectDistinct());
618  $compareSubsetSql = "select distinct {$quote}i{$quote}.{$quote}id{$quote} id ";
619  $compareSubsetSql .= "from {$quote}i{$quote} ";
620  $compareSubsetSql .= "left join {$quote}k{$quote} on ";
621  $compareSubsetSql .= "{$quote}k{$quote}.{$quote}i_id{$quote} = {$quote}i{$quote}.{$quote}id{$quote} ";
622  $compareSubsetSql .= "where " . $compareWhere . ' ';
623  $compareSubsetSql .= 'limit 5 offset 1';
624  $this->assertEquals($compareSubsetSql, $subsetSql);
625 
626  //Make sure the sql runs properly.
627  $dataProvider = new RedBeanModelDataProvider('I', null, false, $searchAttributeData);
628  $data = $dataProvider->getData();
629  }
630 
635  {
636  $super = User::getByUsername('super');
637  Yii::app()->user->userModel = $super;
638  //I has many ls.
639  $i = new I();
640  $l = new L();
641  $l->lMember = 'def';
642  $this->assertTrue($l->save());
643  $i->iMember = 'abc';
644  $i->ls->add($l);
645  $this->assertTrue($i->save());
646 
647  $searchAttributeData = array();
648  $searchAttributeData['clauses'] = array(
649  1 => array(
650  'attributeName' => 'ls',
651  'relatedAttributeName' => 'lMember',
652  'operatorType' => 'equals',
653  'value' => 'somevalue',
654  )
655  );
656  $searchAttributeData['structure'] = '1';
657  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
658 
660  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
661  $compareWhere = "({$quote}l{$quote}.{$quote}lmember{$quote} = 'somevalue')";
662  $this->assertEquals($compareWhere, $where);
663  //Now test that the joinTablesAdapter has correct information.
664  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
665  $this->assertEquals(2, $joinTablesAdapter->getLeftTableJoinCount());
666  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
667  $this->assertEquals('i_l', $leftTables[0]['tableName']);
668  $this->assertEquals('l', $leftTables[1]['tableName']);
669 
670  //Now test that the subsetSQL query produced is correct.
671  $subsetSql = I::makeSubsetOrCountSqlQuery('i', $joinTablesAdapter, 1, 5,
672  $where, null, false, $joinTablesAdapter->getSelectDistinct());
673  $compareSubsetSql = "select distinct {$quote}i{$quote}.{$quote}id{$quote} id ";
674  $compareSubsetSql .= "from {$quote}i{$quote} ";
675  $compareSubsetSql .= "left join {$quote}i_l{$quote} on ";
676  $compareSubsetSql .= "{$quote}i_l{$quote}.{$quote}i_id{$quote} = {$quote}i{$quote}.{$quote}id{$quote} ";
677  $compareSubsetSql .= "left join {$quote}l{$quote} on ";
678  $compareSubsetSql .= "{$quote}l{$quote}.{$quote}id{$quote} = {$quote}i_l{$quote}.{$quote}l_id{$quote} ";
679  $compareSubsetSql .= "where " . $compareWhere . ' ';
680  $compareSubsetSql .= 'limit 5 offset 1';
681  $this->assertEquals($compareSubsetSql, $subsetSql);
682 
683  //Make sure the sql runs properly.
684  $dataProvider = new RedBeanModelDataProvider('I', null, false, $searchAttributeData);
685  $data = $dataProvider->getData();
686  }
687 
692  public function testOrderByCombinations()
693  {
694  $gg = new GG();
695  $gg->gg = 'a';
696  $gg->g = 'v';
697  $this->assertTrue($gg->save());
698  $gg = new GG();
699  $gg->gg = 'b';
700  $gg->g = 't';
701  $this->assertTrue($gg->save());
702  $gg = new GG();
703  $gg->gg = 'c';
704  $gg->g = 'u';
705  $this->assertTrue($gg->save());
706 
708  $_FAKEPOST['GG'] = array();
709  $metadataAdapter = new SearchDataProviderMetadataAdapter(new GG(false), 1, $_FAKEPOST['GG']);
710  $searchAttributeData = $metadataAdapter->getAdaptedMetadata();
711  $dataProvider = new RedBeanModelDataProvider('GG', 'gg', false, $searchAttributeData);
712  $data = $dataProvider->getData();
713  $this->assertEquals(3, count($data));
714  $this->assertEquals('a', $data[0]->gg);
715  $this->assertEquals('b', $data[1]->gg);
716  $this->assertEquals('c', $data[2]->gg);
717  $dataProvider = new RedBeanModelDataProvider('GG', 'gg', true, $searchAttributeData);
718  $data = $dataProvider->getData();
719  $this->assertEquals(3, count($data));
720  $this->assertEquals('c', $data[0]->gg);
721  $this->assertEquals('b', $data[1]->gg);
722  $this->assertEquals('a', $data[2]->gg);
723  $compareString = "{$quote}gg{$quote}.{$quote}gg{$quote}";
724  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('GG');
725  $this->assertEquals($compareString, RedBeanModelDataProvider::resolveSortAttributeColumnName('GG', $joinTablesAdapter, 'gg'));
726  $compareString = "{$quote}g{$quote}.{$quote}g{$quote}";
727  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('GG');
728  $this->assertEquals($compareString, RedBeanModelDataProvider::resolveSortAttributeColumnName('GG', $joinTablesAdapter, 'g'));
729 
730  //test ordering by castedUp model.
731  $dataProvider = new RedBeanModelDataProvider('GG', 'g', false, $searchAttributeData);
732  $data = $dataProvider->getData();
733  $this->assertEquals(3, count($data));
734  $this->assertEquals('b', $data[0]->gg);
735  $this->assertEquals('c', $data[1]->gg);
736  $this->assertEquals('a', $data[2]->gg);
737  $dataProvider = new RedBeanModelDataProvider('GG', 'g', true, $searchAttributeData);
738  $data = $dataProvider->getData();
739  $this->assertEquals(3, count($data));
740  $this->assertEquals('a', $data[0]->gg);
741  $this->assertEquals('c', $data[1]->gg);
742  $this->assertEquals('b', $data[2]->gg);
743 
744  //test ordering by custom attribute value.
745  }
746 
751  {
753 
754  //Test a standard non-relation attribute on I
755  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
756  $sort = RedBeanModelDataProvider::resolveSortAttributeColumnName('I', $joinTablesAdapter, 'iMember');
757  $this->assertEquals("{$quote}i{$quote}.{$quote}imember{$quote}", $sort);
758  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
759  $this->assertEquals(0, $joinTablesAdapter->getLeftTableJoinCount());
760 
761  //Test a standard casted up attribute on H from I
762  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
763  $sort = RedBeanModelDataProvider::resolveSortAttributeColumnName('I', $joinTablesAdapter, 'name');
764  $this->assertEquals("{$quote}h{$quote}.{$quote}name{$quote}", $sort);
765  $this->assertEquals(1, $joinTablesAdapter->getFromTableJoinCount());
766  $this->assertEquals(0, $joinTablesAdapter->getLeftTableJoinCount());
767 
768  //Test a relation attribute G->g from H
769  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('H');
770  $sort = RedBeanModelDataProvider::resolveSortAttributeColumnName('H', $joinTablesAdapter, 'castUpHasOne');
771  $this->assertEquals("{$quote}g{$quote}.{$quote}g{$quote}", $sort);
772  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
773  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
774  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
775  $this->assertEquals('g', $leftTables[0]['tableName']);
776 
777  //Test a relation attribute G->g where casted up from I
778  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
779  $sort = RedBeanModelDataProvider::resolveSortAttributeColumnName('I', $joinTablesAdapter, 'castUpHasOne');
780  $this->assertEquals("{$quote}g{$quote}.{$quote}g{$quote}", $sort);
781  $this->assertEquals(1, $joinTablesAdapter->getFromTableJoinCount());
782  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
783  $fromTables = $joinTablesAdapter->getFromTablesAndAliases();
784  $this->assertEquals('h', $fromTables[0]['tableName']);
785  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
786  $this->assertEquals('g', $leftTables[0]['tableName']);
787 
788  //Test a customField like TestCustomFieldsModel->industry
789  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('TestCustomFieldsModel');
790  $sort = RedBeanModelDataProvider::resolveSortAttributeColumnName(
791  'TestCustomFieldsModel', $joinTablesAdapter, 'industry');
792  $this->assertEquals("{$quote}customfield{$quote}.{$quote}value{$quote}", $sort);
793  $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
794  $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
795  $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
796  $this->assertEquals('customfield', $leftTables[0]['tableName']);
797  }
798 
805  {
806  Yii::app()->user->userModel = User::getByUsername('super');
807 
808  $searchAttributeData = array();
809  $searchAttributeData['clauses'] = array(
810  12 => array(
811  'attributeName' => 'iMember',
812  'operatorType' => 'equals',
813  'value' => 'somevalue12',
814  ),
815  1 => array(
816  'attributeName' => 'iMember',
817  'operatorType' => 'equals',
818  'value' => 'somevalue1',
819  ),
820  2 => array(
821  'attributeName' => 'iMember',
822  'operatorType' => 'equals',
823  'value' => 'somevalue2',
824  ),
825  11 => array(
826  'attributeName' => 'iMember',
827  'operatorType' => 'equals',
828  'value' => 'somevalue11',
829  ),
830  22 => array(
831  'attributeName' => 'iMember',
832  'operatorType' => 'equals',
833  'value' => 'somevalue22',
834  ),
835  );
836  $searchAttributeData['structure'] = '1 and 11 and 2 and 22 and 12';
837  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
838 
840  $where = RedBeanModelDataProvider::makeWhere('I', $searchAttributeData, $joinTablesAdapter);
841  $compareWhere = "({$quote}i{$quote}.{$quote}imember{$quote} = 'somevalue1') and ";
842  $compareWhere .= "({$quote}i{$quote}.{$quote}imember{$quote} = 'somevalue11') and ";
843  $compareWhere .= "({$quote}i{$quote}.{$quote}imember{$quote} = 'somevalue2') and ";
844  $compareWhere .= "({$quote}i{$quote}.{$quote}imember{$quote} = 'somevalue22') and ";
845  $compareWhere .= "({$quote}i{$quote}.{$quote}imember{$quote} = 'somevalue12')";
846  $this->assertEquals($compareWhere, $where);
847  }
848 
849  public function testGetDataReturningAllResults()
850  {
851  GG::deleteAll();
852  $numberOfRecords = rand (12, 100);
853  for ($i = 1; $i <= $numberOfRecords; $i++)
854  {
855  $gg = new GG();
856  $gg->g = 'a';
857  $this->assertTrue($gg->save());
858  }
859  $dataProvider = new RedBeanModelDataProvider('GG', 'g', false);
860  $totalItems = (int)$dataProvider->calculateTotalItemCount();
861  $dataProvider->getPagination()->setPageSize($totalItems);
862  $data = $dataProvider->getData();
863  $this->assertEquals($numberOfRecords, count($data));
864  }
865  }
866 ?>
Definition: I.php:37
static getByUsername($username)
Definition: User.php:49
Definition: GG.php:37
Definition: II.php:37
static makeWhere($modelClassName, array $metadata, &$joinTablesAdapter)
static getByName($name, $shouldCache=true)
static makeSubsetOrCountSqlQuery($tableName, RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter, $offset=null, $count=null, $where=null, $orderBy=null, $selectCount=false, $selectDistinct=false, array $quotedExtraSelectColumnNameAndAliases=array())
Definition: L.php:37
static deleteAll()
Generated on Sun Jul 12 2020 07:10:28
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.