Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
MultipleValuesCustomFieldTest.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 function testSaveAndLoadMultipleValuesCustomFieldData()
40  {
41  $values = array(
42  'Item 1',
43  'Item 2',
44  'Item 3',
45  );
46  $labels = array(
47  'fr' => 'Item 1 fr',
48  'fr' => 'Item 2 fr',
49  'fr' => 'Item 3 fr',
50  );
51  $customFieldData = CustomFieldData::getByName('Items');
52  $customFieldData->serializedData = serialize($values);
53  $customFieldData->serializedLabels = serialize($labels);
54  $this->assertTrue($customFieldData->save());
55  $id = $customFieldData->id;
56  unset($customFieldData);
57 
58  $customFieldData = CustomFieldData::getById($id);
59  $loadedValues = unserialize($customFieldData->serializedData);
60  $loadedLabels = unserialize($customFieldData->serializedLabels);
61  $this->assertEquals('Items', $customFieldData->name);
62  $this->assertNull ( $customFieldData->defaultValue);
63  $this->assertEquals($values, $loadedValues);
64  $this->assertEquals($labels, $loadedLabels);
65 
66  $customFieldData->defaultValue = $values[2];
67  $this->assertTrue($customFieldData->save());
68  unset($customFieldData);
69  $customFieldData = CustomFieldData::getById($id);
70  $this->assertEquals('Items', $customFieldData->name);
71  $this->assertEquals('Item 3', $customFieldData->defaultValue);
72  $this->assertEquals($values, $loadedValues);
73  }
74 
79  {
80  $customFieldData = CustomFieldData::getByName('Items');
81  $customField = new MultipleValuesCustomField();
82  $customFieldValue1 = new CustomFieldValue();
83  $customFieldValue1->value = 'Item 2';
84  $customField->values->add($customFieldValue1);
85  $customFieldValue2 = new CustomFieldValue();
86  $customFieldValue2->value = 'Item 3';
87  $customField->values->add($customFieldValue2);
88  $customField->data = $customFieldData;
89  $this->assertTrue($customField->save());
90 
91  $customFieldId = $customField->id;
92  $customField->forget();
93  unset($customField);
94  $customField = MultipleValuesCustomField::getById($customFieldId);
95  $this->assertEquals(2, $customField->values->count());
96 
97  $values = unserialize($customField->data->serializedData);
98  $customField->values->removeAll();
99  $customFieldValue = new CustomFieldValue();
100  $customFieldValue->value = 'Item 1';
101  $customField->values->add($customFieldValue);
102  $this->assertTrue($customField->save());
103  $customField->forget();
104  unset($customField);
105 
106  $customField = MultipleValuesCustomField::getById($customFieldId);
107  $this->assertEquals(1, $customField->values->count());
108  $this->assertTrue($customField->values[0]->isSame($customFieldValue));
109  }
110 
115  {
116  $values = array(
117  'Automotive',
118  'Adult Entertainment',
119  'Financial Services',
120  'Mercenaries & Armaments',
121  );
122  $industryFieldData = CustomFieldData::getByName('MultipleIndustries');
123  $industryFieldData->defaultValue = $values[1];
124  $industryFieldData->serializedData = serialize($values);
125  $this->assertTrue($industryFieldData->save());
126 
127  $model = new TestCustomFieldsModel();
128  $this->assertEquals(1, $model->multipleIndustries->values->count());
129  $this->assertEquals($values[1], $model->multipleIndustries->values[0]->value);
130  $this->assertTrue($model->validate());
131 
132  //Test populating with a single value
133  $_FAKEPOST = array(
134  'multipleIndustries' => array(
135  'values' => array($values[2]),
136  ),
137  );
138  $model->setAttributes($_FAKEPOST);
139  $this->assertEquals(1, $model->multipleIndustries->values->count());
140  $this->assertEquals($values[2], $model->multipleIndustries->values[0]->value);
141  $this->assertEquals('Financial Services', strval($model->multipleIndustries));
142 
143  //Now test populating more than one value
144  $_FAKEPOST = array(
145  'multipleIndustries' => array(
146  'values' => array($values[1], $values[3]),
147  ),
148  );
149  $model->setAttributes($_FAKEPOST);
150  $this->assertEquals(2, $model->multipleIndustries->values->count());
151  $this->assertEquals($values[1], $model->multipleIndustries->values[0]->value);
152  $this->assertEquals($values[3], $model->multipleIndustries->values[1]->value);
153  $this->assertEquals('Adult Entertainment, Mercenaries & Armaments', strval($model->multipleIndustries));
154 
155  //Test clearing out the values
156  $_FAKEPOST = array(
157  'multipleIndustries' => array(
158  'values' => array(),
159  ),
160  );
161  $model->setAttributes($_FAKEPOST);
162  $this->assertEquals(0, $model->multipleIndustries->values->count());
163  $this->assertEquals('(None)', strval($model->multipleIndustries));
164  }
165 
170  {
171  $values = array(
172  'A',
173  'B',
174  'C',
175  );
176  $customFieldData = CustomFieldData::getByName('updateItems');
177  $customFieldData->serializedData = serialize($values);
178  $this->assertTrue($customFieldData->save());
179  $id = $customFieldData->id;
180 
181  $customField = new MultipleValuesCustomField();
182  $customFieldValue = new CustomFieldValue();
183  $customFieldValue->value = 'A';
184  $customField->values->add($customFieldValue);
185  $customField->data = $customFieldData;
186  $this->assertTrue($customField->save());
187 
188  $customField = new MultipleValuesCustomField();
189  $customFieldValue = new CustomFieldValue();
190  $customFieldValue->value = 'B';
191  $customField->values->add($customFieldValue);
192  $customFieldValue = new CustomFieldValue();
193  $customFieldValue->value = 'C';
194  $customField->values->add($customFieldValue);
195  $customField->data = $customFieldData;
196  $this->assertTrue($customField->save());
197 
198  $customField = new MultipleValuesCustomField();
199  $customFieldValue = new CustomFieldValue();
200  $customFieldValue->value = 'C';
201  $customField->values->add($customFieldValue);
202  $customField->data = $customFieldData;
203  $this->assertTrue($customField->save());
204 
205  $customField = new MultipleValuesCustomField();
206  $customFieldValue = new CustomFieldValue();
207  $customFieldValue->value = 'D';
208  $customField->values->add($customFieldValue);
209  $customField->data = $customFieldData;
210  $this->assertTrue($customField->save());
211 
213  $customFieldTableName = MultipleValuesCustomField::getTableName();
214  $baseCustomFieldTableName = BaseCustomField::getTableName();
215  $customFieldValueTableName = CustomFieldValue::getTableName();
216  $valueAttributeColumnName = 'value';
217  $dataAttributeColumnName = RedBeanModel::getForeignKeyName('MultipleValuesCustomField', 'data');
218  $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
219  $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
220  $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
221  $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
222  $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id}";
223  $ids = ZurmoRedBean::getCol($sql);
224  $beans = ZurmoRedBean::batch($customFieldTableName, $ids);
225  $customFields = RedBeanModel::makeModels($beans, 'CustomField');
226  $this->assertEquals(4, count($customFields));
227 
228  $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
229  $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
230  $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
231  $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
232  $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} ";
233  $sql .= "and (select count(*) from {$quote}{$customFieldValueTableName}{$quote} ";
234  $sql .= "where {$quote}{$valueAttributeColumnName}{$quote} IN('B','C') "; // Not Coding Standard
235  $sql .= "and {$quote}{$customFieldTableName}{$quote}.id = {$customFieldValueTableName}.{$customFieldTableName}_id)";
236  $sql .= " = 2";
237  $this->assertEquals(1, count(ZurmoRedBean::getCol($sql)));
238  $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
239  $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
240  $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
241  $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
242  $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} ";
243  $sql .= "and (select count(*) from {$quote}{$customFieldValueTableName}{$quote} ";
244  $sql .= "where {$quote}{$valueAttributeColumnName}{$quote} IN('C') ";
245  $sql .= "and {$quote}{$customFieldTableName}{$quote}.id = {$customFieldValueTableName}.{$customFieldTableName}_id)";
246  $sql .= " = 1";
247  $this->assertEquals(2, count(ZurmoRedBean::getCol($sql)));
248  $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
249  $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
250  $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
251  $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
252  $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} ";
253  $sql .= "and (select count(*) from {$quote}{$customFieldValueTableName}{$quote} ";
254  $sql .= "where {$quote}{$valueAttributeColumnName}{$quote} IN('E') ";
255  $sql .= "and {$quote}{$customFieldTableName}{$quote}.id = {$customFieldValueTableName}.{$customFieldTableName}_id)";
256  $sql .= " = 1";
257  $this->assertEquals(0, count(ZurmoRedBean::getCol($sql)));
258 
259  MultipleValuesCustomField::updateValueByDataIdAndOldValueAndNewValue($id, 'C', 'E');
260  $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
261  $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
262  $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
263  $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
264  $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} ";
265  $sql .= "and (select count(*) from {$quote}{$customFieldValueTableName}{$quote} ";
266  $sql .= "where {$quote}{$valueAttributeColumnName}{$quote} IN('B') ";
267  $sql .= "and {$quote}{$customFieldTableName}{$quote}.id = {$customFieldValueTableName}.{$customFieldTableName}_id)";
268  $sql .= " = 1";
269  $this->assertEquals(1, count(ZurmoRedBean::getCol($sql)));
270  $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
271  $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
272  $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
273  $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
274  $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} ";
275  $sql .= "and (select count(*) from {$quote}{$customFieldValueTableName}{$quote} ";
276  $sql .= "where {$quote}{$valueAttributeColumnName}{$quote} IN('C') ";
277  $sql .= "and {$quote}{$customFieldTableName}{$quote}.id = {$customFieldValueTableName}.{$customFieldTableName}_id)";
278  $sql .= " = 1";
279  $this->assertEquals(0, count(ZurmoRedBean::getCol($sql)));
280  $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
281  $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
282  $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
283  $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
284  $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} ";
285  $sql .= "and (select count(*) from {$quote}{$customFieldValueTableName}{$quote} ";
286  $sql .= "where {$quote}{$valueAttributeColumnName}{$quote} IN('E') ";
287  $sql .= "and {$quote}{$customFieldTableName}{$quote}.id = {$customFieldValueTableName}.{$customFieldTableName}_id)";
288  $sql .= " = 1";
289  $this->assertEquals(2, count(ZurmoRedBean::getCol($sql)));
290  $sql = "select {$quote}{$customFieldTableName}{$quote}.id from {$quote}{$customFieldTableName}{$quote} ";
291  $sql .= "left join {$quote}{$baseCustomFieldTableName}{$quote} on ";
292  $sql .= "{$quote}{$baseCustomFieldTableName}{$quote}.id = ";
293  $sql .= "{$quote}{$customFieldTableName}{$quote}.basecustomfield_id ";
294  $sql .= "where {$quote}{$dataAttributeColumnName}{$quote} = {$id} ";
295  $sql .= "and (select count(*) from {$quote}{$customFieldValueTableName}{$quote} ";
296  $sql .= "where {$quote}{$valueAttributeColumnName}{$quote} IN('B', 'E') ";
297  $sql .= "and {$quote}{$customFieldTableName}{$quote}.id = {$customFieldValueTableName}.{$customFieldTableName}_id)";
298  $sql .= " = 2";
299  $this->assertEquals(1, count(ZurmoRedBean::getCol($sql)));
300  }
301  }
static getForeignKeyName($modelClassName, $relationName)
static makeModels(array $beans, $modelClassName=null)
static getByName($name, $shouldCache=true)
static getById($id, $modelClassName=null)
static getTableName()
Generated on Thu Jul 9 2020 07:10:29
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.