Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
DropDownModelAttributesAdapterTest.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();
42  SecurityTestHelper::createSuperAdmin();
43  self::makeDropDownAttributeUsingAdapter('aaa', 'a', new Account());
44  self::makeDropDownAttributeUsingAdapter('bbb', 'b', new Account());
45  self::makeDropDownAttributeUsingAdapter('ccc', 'c', new Account());
46  }
47 
48  public static function makeDropDownAttributeUsingAdapter($namePrefix, $valuesPrefix, RedBeanModel $modelTouse)
49  {
50  $values = array(
51  $valuesPrefix . '1',
52  $valuesPrefix . '2',
53  $valuesPrefix . '3',
54  $valuesPrefix . '4',
55  );
56  $labels = array('fr' => array($valuesPrefix . '1 fr', $valuesPrefix . '1 fr', $valuesPrefix . '1 fr', $valuesPrefix . '1 fr'),
57  'de' => array($valuesPrefix . '1 de', $valuesPrefix . '1 de', $valuesPrefix . '1 de', $valuesPrefix . '1 de'),
58  );
59  $airplanesFieldData = CustomFieldData::getByName($namePrefix . 'TheData');
60  $airplanesFieldData->serializedData = serialize($values);
61  $saved = $airplanesFieldData->save();
62  if (!$saved)
63  {
64  throw new NotSupportedException();
65  }
66 
67  $attributeForm = new DropDownAttributeForm();
68  $attributeForm->attributeName = $namePrefix;
69  $attributeForm->attributeLabels = array(
70  'de' => $namePrefix . ' de',
71  'en' => $namePrefix . ' en',
72  'es' => $namePrefix . ' es',
73  'fr' => $namePrefix . ' fr',
74  'it' => $namePrefix . ' it',
75  );
76  $attributeForm->isAudited = false;
77  $attributeForm->isRequired = false;
78  $attributeForm->defaultValueOrder = null;
79  $attributeForm->customFieldDataData = $values;
80  $attributeForm->customFieldDataName = $namePrefix . 'TheData';
81  $attributeForm->customFieldDataLabels = $labels;
82 
83  $modelAttributesAdapterClassName = $attributeForm::getModelAttributeAdapterNameForSavingAttributeFormData();
84  $adapter = new $modelAttributesAdapterClassName($modelTouse);
85  try
86  {
87  $adapter->setAttributeMetadataFromForm($attributeForm);
88  }
90  {
91  echo $e->getMessage();
92  throw new NotSupportedException();
93  }
94  }
95 
96  public function testUsingTheAdapterAsAWrapperToUpdateValueInMappingByOldAndNewValue()
97  {
98  $account = new Account();
99 
100  //First create a dependency
101  $mappingData = array(array('attributeName' => 'aaaCstm'),
102  array('attributeName' => 'bbbCstm',
103  'valuesToParentValues' =>
104  array('b1' => 'a1',
105  'b2' => 'a2',
106  'b3' => 'a3',
107  'b4' => 'a4'
108  )),
109  array('attributeName' => 'cccCstm',
110  'valuesToParentValues' =>
111  array('c1' => 'b1',
112  'c2' => 'b2',
113  'c3' => 'b3',
114  'c4' => 'b4'
115  )));
117  $metadata->setScenario('nonAutoBuild');
118  $metadata->name = 'aName';
119  $metadata->modelClassName = 'Account';
120  $metadata->serializedMetadata = serialize(array('attributeLabels' => array('a' => 'b'),
121  'mappingData' => $mappingData));
122  $this->assertTrue($metadata->save());
123 
124  //Change b3 to b3New
125  $attributeForm = AttributesFormFactory::
126  createAttributeFormByAttributeName($account, 'bbbCstm');
127  $attributeForm->customFieldDataDataExistingValues = array('b1', 'b2', 'b3', 'b4');
128  $attributeForm->customFieldDataData = array('b1', 'b2', 'b3New', 'b4');
129  $modelAttributesAdapterClassName = $attributeForm::
130  getModelAttributeAdapterNameForSavingAttributeFormData();
131  $adapter = new $modelAttributesAdapterClassName($account);
132  try
133  {
134  $adapter->setAttributeMetadataFromForm($attributeForm);
135  }
137  {
138  echo $e->getMessage();
139  $this->fail();
140  }
141 
142  //Confirm b3 values changed correctly to b3New
143  $metadata = DropDownDependencyDerivedAttributeMetadata::getById($metadata->id);
144  $unserializedData = unserialize($metadata->serializedMetadata);
145 
146  $compareData = array(array('attributeName' => 'aaaCstm'),
147  array('attributeName' => 'bbbCstm',
148  'valuesToParentValues' =>
149  array('b1' => 'a1',
150  'b2' => 'a2',
151  'b3New' => 'a3',
152  'b4' => 'a4'
153  )),
154  array('attributeName' => 'cccCstm',
155  'valuesToParentValues' =>
156  array('c1' => 'b1',
157  'c2' => 'b2',
158  'c3' => 'b3New',
159  'c4' => 'b4'
160  )));
161  $this->assertEquals(array('a' => 'b'), $unserializedData['attributeLabels']);
162  $this->assertEquals($compareData, $unserializedData['mappingData']);
163 
164  //Now change a value for a dropdown that is the first level of the dependency. This will only change the
165  //value when it is a parent value.
166  $attributeForm = AttributesFormFactory::
167  createAttributeFormByAttributeName($account, 'aaaCstm');
168  $attributeForm->customFieldDataDataExistingValues = array('a1', 'a2', 'a3', 'a4');
169  $attributeForm->customFieldDataData = array('a1', 'a2New', 'a3', 'a4');
170  $modelAttributesAdapterClassName = $attributeForm::
171  getModelAttributeAdapterNameForSavingAttributeFormData();
172  $adapter = new $modelAttributesAdapterClassName($account);
173  try
174  {
175  $adapter->setAttributeMetadataFromForm($attributeForm);
176  }
178  {
179  echo $e->getMessage();
180  $this->fail();
181  }
182 
183  //Confirm a2 values changed correctly to a2New
184  $metadata = DropDownDependencyDerivedAttributeMetadata::getById($metadata->id);
185  $unserializedData = unserialize($metadata->serializedMetadata);
186 
187  $compareData = array(array('attributeName' => 'aaaCstm'),
188  array('attributeName' => 'bbbCstm',
189  'valuesToParentValues' =>
190  array('b1' => 'a1',
191  'b2' => 'a2New',
192  'b3New' => 'a3',
193  'b4' => 'a4'
194  )),
195  array('attributeName' => 'cccCstm',
196  'valuesToParentValues' =>
197  array('c1' => 'b1',
198  'c2' => 'b2',
199  'c3' => 'b3New',
200  'c4' => 'b4'
201  )));
202  $this->assertEquals(array('a' => 'b'), $unserializedData['attributeLabels']);
203  $this->assertEquals($compareData, $unserializedData['mappingData']);
204 
205  //Now change 2 values at once.
206  $attributeForm = AttributesFormFactory::
207  createAttributeFormByAttributeName($account, 'cccCstm');
208  $attributeForm->customFieldDataDataExistingValues = array('c1', 'c2', 'c3', 'c4');
209  $attributeForm->customFieldDataData = array('c1New', 'c2New', 'c3', 'c4');
210  $modelAttributesAdapterClassName = $attributeForm::
211  getModelAttributeAdapterNameForSavingAttributeFormData();
212  $adapter = new $modelAttributesAdapterClassName($account);
213  try
214  {
215  $adapter->setAttributeMetadataFromForm($attributeForm);
216  }
218  {
219  echo $e->getMessage();
220  $this->fail();
221  }
222 
223  //Confirm c1, c2 both changed.
224  $metadata = DropDownDependencyDerivedAttributeMetadata::getById($metadata->id);
225  $unserializedData = unserialize($metadata->serializedMetadata);
226 
227  $compareData = array(array('attributeName' => 'aaaCstm'),
228  array('attributeName' => 'bbbCstm',
229  'valuesToParentValues' =>
230  array('b1' => 'a1',
231  'b2' => 'a2New',
232  'b3New' => 'a3',
233  'b4' => 'a4'
234  )),
235  array('attributeName' => 'cccCstm',
236  'valuesToParentValues' =>
237  array('c1New' => 'b1',
238  'c2New' => 'b2',
239  'c3' => 'b3New',
240  'c4' => 'b4'
241  )));
242  $this->assertEquals(array('a' => 'b'), $unserializedData['attributeLabels']);
243  $this->assertEquals($compareData, $unserializedData['mappingData']);
244  }
245 
250  {
251  Yii::app()->user->userModel = User::getByUsername('super');
252  $account = new Account();
253 
254  //Remove a1
255  $attributeForm = AttributesFormFactory::
256  createAttributeFormByAttributeName($account, 'aaaCstm');
257  $attributeForm->customFieldDataData = array('a2New', 'a3', 'a4');
258  $modelAttributesAdapterClassName = $attributeForm::
259  getModelAttributeAdapterNameForSavingAttributeFormData();
260  $adapter = new $modelAttributesAdapterClassName($account);
261  try
262  {
263  $adapter->setAttributeMetadataFromForm($attributeForm);
264  }
266  {
267  echo $e->getMessage();
268  $this->fail();
269  }
270 
271  //Confirm a1 has been removed from the mapping.
273  getByNameAndModelClassName('aName', 'Account');
274  $unserializedData = unserialize($metadata->serializedMetadata);
275 
276  $compareData = array(array('attributeName' => 'aaaCstm'),
277  array('attributeName' => 'bbbCstm',
278  'valuesToParentValues' =>
279  array('b1' => null,
280  'b2' => 'a2New',
281  'b3New' => 'a3',
282  'b4' => 'a4'
283  )),
284  array('attributeName' => 'cccCstm',
285  'valuesToParentValues' =>
286  array('c1New' => 'b1',
287  'c2New' => 'b2',
288  'c3' => 'b3New',
289  'c4' => 'b4'
290  )));
291  $this->assertEquals(array('a' => 'b'), $unserializedData['attributeLabels']);
292  $this->assertEquals($compareData, $unserializedData['mappingData']);
293 
294  //Remove b4
295  $attributeForm = AttributesFormFactory::
296  createAttributeFormByAttributeName($account, 'bbbCstm');
297  $attributeForm->customFieldDataData = array('b1', 'b2', 'b3New');
298  $modelAttributesAdapterClassName = $attributeForm::
299  getModelAttributeAdapterNameForSavingAttributeFormData();
300  $adapter = new $modelAttributesAdapterClassName($account);
301  try
302  {
303  $adapter->setAttributeMetadataFromForm($attributeForm);
304  }
306  {
307  echo $e->getMessage();
308  $this->fail();
309  }
310 
311  //Confirm b4 has been removed from the mapping.
313  getByNameAndModelClassName('aName', 'Account');
314  $unserializedData = unserialize($metadata->serializedMetadata);
315 
316  $compareData = array(array('attributeName' => 'aaaCstm'),
317  array('attributeName' => 'bbbCstm',
318  'valuesToParentValues' =>
319  array('b1' => null,
320  'b2' => 'a2New',
321  'b3New' => 'a3',
322  )),
323  array('attributeName' => 'cccCstm',
324  'valuesToParentValues' =>
325  array('c1New' => 'b1',
326  'c2New' => 'b2',
327  'c3' => 'b3New',
328  'c4' => null
329  )));
330  $this->assertEquals(array('a' => 'b'), $unserializedData['attributeLabels']);
331  $this->assertEquals($compareData, $unserializedData['mappingData']);
332  }
333  }
334 ?>
static getByUsername($username)
Definition: User.php:49
static getByName($name, $shouldCache=true)
static getById($id, $modelClassName=null)
static getByNameAndModelClassName($name, $modelClassName)
Generated on Sun Aug 9 2020 07:10:30
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.