All Data Structures Functions Variables Pages
ImportUtilTest.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  $jim = UserTestHelper::createBasicUser('jim');
44 
45  $values = array(
46  'Multi 1',
47  'Multi 2',
48  'Multi 3',
49  );
50  $customFieldData = CustomFieldData::getByName('ImportTestMultiDropDown');
51  $customFieldData->serializedData = serialize($values);
52  $saved = $customFieldData->save();
53  assert($saved); // Not Coding Standard
54 
55  $values = array(
56  'Cloud 1',
57  'Cloud 2',
58  'Cloud 3',
59  );
60  $customFieldData = CustomFieldData::getByName('ImportTestTagCloud');
61  $customFieldData->serializedData = serialize($values);
62  $saved = $customFieldData->save();
63  assert($saved); // Not Coding Standard
66  }
67 
68  public static function getDependentTestModelClassNames()
69  {
70  return array('ImportModelTestItem', 'ImportModelTestItem2');
71  }
72 
73  public function testResolveLinkMessageToModel()
74  {
75  Yii::app()->user->userModel = User::getByUsername('super');
76  $account = AccountTestHelper::createAccountByNameForOwner('account', Yii::app()->user->userModel);
77  $content = ImportUtil::resolveLinkMessageToModel($account);
78  $this->assertContains('accounts/default/details?id', $content);
79  $contact = ContactTestHelper::createContactByNameForOwner('contact', Yii::app()->user->userModel);
80  $content = ImportUtil::resolveLinkMessageToModel($contact);
81  $this->assertContains('contacts/default/details?id', $content);
82  $lead = LeadTestHelper::createLeadByNameForOwner('lead', Yii::app()->user->userModel);
83  $content = ImportUtil::resolveLinkMessageToModel($lead);
84  $this->assertContains('leads/default/details?id', $content);
85  }
86 
87  public function testImportNameAndRelatedNameWithApostrophes()
88  {
89  Yii::app()->user->userModel = User::getByUsername('super');
90 
91  $testModels = ImportModelTestItem::getAll();
92  $this->assertEquals(0, count($testModels));
93  $import = new Import();
94  $serializedData['importRulesType'] = 'ImportModelTestItem';
95  $serializedData['firstRowIsHeaderRow'] = true;
96  $import->serializedData = serialize($serializedData);
97  $this->assertTrue($import->save());
98 
99  ImportTestHelper::createTempTableByFileNameAndTableName('importApostropheTest.csv', $import->getTempTableName(), true);
100 
101  $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
102 
103  $mappingData = array(
104  'column_0' => ImportMappingUtil::makeStringColumnMappingData ('string'),
105  'column_1' => ImportMappingUtil::makeStringColumnMappingData ('lastName'),
106  'column_2' => array('attributeIndexOrDerivedType' => 'hasOne', 'type' => 'importColumn',
107  'mappingRulesData' => array(
108  'RelatedModelValueTypeMappingRuleForm' =>
109  array('type' => RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_NAME))),
110  );
111 
112  $importRules = ImportRulesUtil::makeImportRulesByType('ImportModelTestItem');
113  $page = 0;
114  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
115  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
116  $dataProvider->getPagination()->setCurrentPage($page);
117  $importResultsUtil = new ImportResultsUtil($import);
118  $messageLogger = new ImportMessageLogger();
119  ImportUtil::importByDataProvider($dataProvider,
120  $importRules,
121  $mappingData,
122  $importResultsUtil,
124  $messageLogger);
125  $importResultsUtil->processStatusAndMessagesForEachRow();
126 
127  //Confirm that 2 ImportModelTestItem models were created and 2 ImportModelTestItem2 were created
128  $testModels = ImportModelTestItem::getAll();
129  $this->assertEquals(2, count($testModels));
130  $this->assertEquals("Barrel'o Fun", $testModels[0]->lastName);
131  $this->assertEquals('bLastName', $testModels[1]->lastName);
132  $testModels = ImportModelTestItem2::getAll();
133  $this->assertEquals(2, count($testModels));
134  $this->assertEquals("D'Angelo Inc", $testModels[0]->name);
135  $this->assertEquals('Dartmouth Financial Services', $testModels[1]->name);
136 
137  //Clear out data in table
139  }
140 
146  {
147  $super = User::getByUsername('super');
148  $jim = User::getByUsername('jim');
149  Yii::app()->user->userModel = $jim;
150 
151  $testModels = ImportModelTestItem::getAll();
152  $this->assertEquals(0, count($testModels));
153  $import = new Import();
154  $serializedData['importRulesType'] = 'ImportModelTestItem';
155  $serializedData['firstRowIsHeaderRow'] = true;
156  $import->serializedData = serialize($serializedData);
157  $this->assertTrue($import->save());
158 
159  ImportTestHelper::createTempTableByFileNameAndTableName('importMultiSelectDropDownTest.csv', $import->getTempTableName(), true);
160 
161  $this->assertEquals(6, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
162 
163  $multiDropDownInstructionsData = array(CustomFieldsInstructionData::ADD_MISSING_VALUES =>
164  array('Multi 5', 'Multi 4'),
165  CustomFieldsInstructionData::MAP_MISSING_VALUES => array());
166  $tagCloudInstructionsData = array(CustomFieldsInstructionData::ADD_MISSING_VALUES =>
167  array('Cloud 5', 'Cloud 4'),
168  CustomFieldsInstructionData::MAP_MISSING_VALUES => array());
169  $mappingData = array(
170  'column_0' => ImportMappingUtil::makeStringColumnMappingData ('string'),
171  'column_1' => ImportMappingUtil::makeStringColumnMappingData ('lastName'),
172  'column_2' => ImportMappingUtil::
173  makeMultiSelectDropDownColumnMappingData('multiDropDown', null,
174  $multiDropDownInstructionsData),
175  'column_3' => ImportMappingUtil::
176  makeTagCloudColumnMappingData('tagCloud', null, $tagCloudInstructionsData)
177  );
178 
179  $importRules = ImportRulesUtil::makeImportRulesByType('ImportModelTestItem');
180  $page = 0;
181  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
182  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
183  $dataProvider->getPagination()->setCurrentPage($page);
184  $importResultsUtil = new ImportResultsUtil($import);
185  $messageLogger = new ImportMessageLogger();
186  ImportUtil::importByDataProvider($dataProvider,
187  $importRules,
188  $mappingData,
189  $importResultsUtil,
191  $messageLogger);
192  $importResultsUtil->processStatusAndMessagesForEachRow();
193 
194  //Confirm the missing custom field values were properly added.
195  $customFieldData = CustomFieldData::getByName('ImportTestMultiDropDown');
196  $values = array(
197  'Multi 1',
198  'Multi 2',
199  'Multi 3',
200  'Multi 4',
201  'Multi 5',
202  );
203  $this->assertEquals($values, unserialize($customFieldData->serializedData));
204  $customFieldData = CustomFieldData::getByName('ImportTestTagCloud');
205  $values = array(
206  'Cloud 1',
207  'Cloud 2',
208  'Cloud 3',
209  'Cloud 4',
210  'Cloud 5',
211  );
212  $this->assertEquals($values, unserialize($customFieldData->serializedData));
213 
214  //Confirm that 5 models where created.
215  $testModels = ImportModelTestItem::getAll();
216  $this->assertEquals(5, count($testModels));
217 
218  foreach ($testModels as $model)
219  {
220  $this->assertEquals(array(Permission::NONE, Permission::NONE), $model->getExplicitActualPermissions ($jim));
221  }
222 
223  //Confirm the values of the multi-select and tag cloud are as expected.
224  $this->assertEquals(0, $testModels[2]->multiDropDown->values->count());
225  $this->assertEquals(2, $testModels[2]->tagCloud->values->count());
226  $this->assertEquals(2, $testModels[3]->multiDropDown->values->count());
227  $this->assertEquals(0, $testModels[3]->tagCloud->values->count());
228 
229  $this->assertEquals(2, $testModels[1]->multiDropDown->values->count());
230  $this->assertEquals('Multi 4', $testModels[1]->multiDropDown->values[0]->value);
231  $this->assertEquals('Multi 2', $testModels[1]->multiDropDown->values[1]->value);
232  $this->assertEquals(2, $testModels[1]->tagCloud->values->count());
233  $this->assertEquals('Cloud 1', $testModels[1]->tagCloud->values[0]->value);
234  $this->assertEquals('Cloud 4', $testModels[1]->tagCloud->values[1]->value);
235 
236  $this->assertEquals(2, $testModels[4]->multiDropDown->values->count());
237  $this->assertEquals('Multi 1', $testModels[4]->multiDropDown->values[0]->value);
238  $this->assertEquals('Multi 5', $testModels[4]->multiDropDown->values[1]->value);
239  $this->assertEquals(2, $testModels[4]->tagCloud->values->count());
240  $this->assertEquals('Cloud 5', $testModels[4]->tagCloud->values[0]->value);
241  $this->assertEquals('Cloud 2', $testModels[4]->tagCloud->values[1]->value);
242  //Confirm 10 rows were processed as 'created'.
243  $this->assertEquals(5, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
244  . ImportRowDataResultsUtil::CREATED));
245 
246  //Confirm that 0 rows were processed as 'updated'.
247  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
248  . ImportRowDataResultsUtil::UPDATED));
249 
250  //Confirm 0 rows were processed as 'errors'.
251  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
252  . ImportRowDataResultsUtil::ERROR));
253 
254  $beansWithErrors = ImportDatabaseUtil::getSubset($import->getTempTableName(), "status = "
255  . ImportRowDataResultsUtil::ERROR);
256  $this->assertEquals(0, count($beansWithErrors));
257 
258  //Clear out data in table
260  }
261 
267  {
268  $super = User::getByUsername('super');
269  $jim = User::getByUsername('jim');
270  Yii::app()->user->userModel = $jim;
271 
272  //Confirm Jim can can only view ImportModelTestItems he owns.
273  $item = NamedSecurableItem::getByName('ImportModule');
274  $this->assertEquals(Permission::NONE, $item->getEffectivePermissions($jim));
275 
276  $testModels = ImportModelTestItem::getAll();
277  $this->assertEquals(0, count($testModels));
278  $import = new Import();
279  $serializedData['importRulesType'] = 'ImportModelTestItem';
280  $serializedData['firstRowIsHeaderRow'] = true;
281  $import->serializedData = serialize($serializedData);
282  $this->assertTrue($import->save());
283 
284  ImportTestHelper::createTempTableByFileNameAndTableName('importEmptyCurrencyTest.csv', $import->getTempTableName(), true);
285 
286  $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
287 
288  $columnMappingData = array('attributeIndexOrDerivedType' => 'owner',
289  'type' => 'extraColumn',
290  'mappingRulesData' => array(
291  'DefaultModelNameIdMappingRuleForm' =>
292  array('defaultModelId' => $super->id),
293  'UserValueTypeModelAttributeMappingRuleForm' =>
294  array('type' =>
295  UserValueTypeModelAttributeMappingRuleForm::ZURMO_USER_ID)));
296 
297  $mappingData = array(
298  'column_0' => ImportMappingUtil::makeStringColumnMappingData ('lastName'),
299  'column_1' => ImportMappingUtil::makeStringColumnMappingData ('string'),
300  'column_2' => $columnMappingData
301  );
302 
303  $importRules = ImportRulesUtil::makeImportRulesByType('ImportModelTestItem');
304  $page = 0;
305  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
306  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
307  $dataProvider->getPagination()->setCurrentPage($page);
308  $importResultsUtil = new ImportResultsUtil($import);
309  $messageLogger = new ImportMessageLogger();
310  ImportUtil::importByDataProvider($dataProvider,
311  $importRules,
312  $mappingData,
313  $importResultsUtil,
315  $messageLogger);
316  $importResultsUtil->processStatusAndMessagesForEachRow();
317 
318  //Confirm that 2 models where created.
319  $testModels = ImportModelTestItem::getAll();
320  $this->assertEquals(2, count($testModels));
321 
322  foreach ($testModels as $model)
323  {
324  $this->assertEquals(array(Permission::NONE, Permission::NONE), $model->getExplicitActualPermissions ($jim));
325  }
326 
327  //Confirm 10 rows were processed as 'created'.
328  $this->assertEquals(2, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
329  . ImportRowDataResultsUtil::CREATED));
330 
331  //Confirm that 0 rows were processed as 'updated'.
332  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
333  . ImportRowDataResultsUtil::UPDATED));
334 
335  //Confirm 2 rows were processed as 'errors'.
336  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
337  . ImportRowDataResultsUtil::ERROR));
338 
339  $beansWithErrors = ImportDatabaseUtil::getSubset($import->getTempTableName(), "status = "
340  . ImportRowDataResultsUtil::ERROR);
341  $this->assertEquals(0, count($beansWithErrors));
342 
343  //Clear out data in table
344  Yii::app()->user->userModel = $super;
346  }
347 
352  {
353  Yii::app()->user->userModel = User::getByUsername('super');
354 
355  $testModels = ImportModelTestItem::getAll();
356  $this->assertEquals(0, count($testModels));
357  $import = new Import();
358  $serializedData['importRulesType'] = 'ImportModelTestItem';
359  $serializedData['firstRowIsHeaderRow'] = true;
360  $import->serializedData = serialize($serializedData);
361  $this->assertTrue($import->save());
362 
363  ImportTestHelper::createTempTableByFileNameAndTableName('importEmptyCurrencyTest.csv', $import->getTempTableName(), true);
364 
365  $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
366 
367  $mappingData = array(
368  'column_0' => ImportMappingUtil::makeStringColumnMappingData ('lastName'),
369  'column_1' => ImportMappingUtil::makeStringColumnMappingData ('string')
370  );
371 
372  $importRules = ImportRulesUtil::makeImportRulesByType('ImportModelTestItem');
373  $page = 0;
374  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
375  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
376  $dataProvider->getPagination()->setCurrentPage($page);
377  $importResultsUtil = new ImportResultsUtil($import);
378  $messageLogger = new ImportMessageLogger();
379  ImportUtil::importByDataProvider($dataProvider,
380  $importRules,
381  $mappingData,
382  $importResultsUtil,
384  $messageLogger);
385  $importResultsUtil->processStatusAndMessagesForEachRow();
386 
387  //Confirm that 2 models where created.
388  $testModels = ImportModelTestItem::getAll();
389  $this->assertEquals(2, count($testModels));
390  $jim = User::getByUsername('jim');
391  foreach ($testModels as $model)
392  {
393  $this->assertEquals(array(Permission::NONE, Permission::NONE), $model->getExplicitActualPermissions ($jim));
394  }
395 
396  //Confirm 10 rows were processed as 'created'.
397  $this->assertEquals(2, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
398  . ImportRowDataResultsUtil::CREATED));
399 
400  //Confirm that 0 rows were processed as 'updated'.
401  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
402  . ImportRowDataResultsUtil::UPDATED));
403 
404  //Confirm 2 rows were processed as 'errors'.
405  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
406  . ImportRowDataResultsUtil::ERROR));
407 
408  $beansWithErrors = ImportDatabaseUtil::getSubset($import->getTempTableName(), "status = "
409  . ImportRowDataResultsUtil::ERROR);
410  $this->assertEquals(0, count($beansWithErrors));
411 
412  //Confirm the base code is USD
413  $this->assertEquals('USD', Yii::app()->currencyHelper->getBaseCode());
414 
415  //Creating an object produces the correct currency code.
416  $testItem = new ImportModelTestItem();
417  $this->assertEquals('USD', $testItem->currencyValue->currency->code);
418  $testItem->string = 'test';
419  $testItem->lastName = 'testAlso';
420  $this->assertTrue($testItem->save());
421  $testItemId = $testItem->id;
422  $testItem->forget();
423 
424  //The currency code, even though not set, shows up correctly based on the base code.
425  $testItem = ImportModelTestItem::getById($testItemId);
426  $this->assertEquals('USD', $testItem->currencyValue->currency->code);
427 
428  //Test that the related currency information is not empty for the imported objects.
429  $this->assertEquals('USD', $testModels[0]->currencyValue->currency->code);
430 
431  //Clear out data in table
433  }
434 
439  {
440  Yii::app()->user->userModel = User::getByUsername('super');
441 
442  $testModels = ImportModelTestItem::getAll();
443  $this->assertEquals(0, count($testModels));
444  $import = new Import();
445  $serializedData['importRulesType'] = 'ImportModelTestItem';
446  $serializedData['firstRowIsHeaderRow'] = true;
447  $import->serializedData = serialize($serializedData);
448  $this->assertTrue($import->save());
449 
450  ImportTestHelper::createTempTableByFileNameAndTableName('importAnalyzerTest.csv', $import->getTempTableName(), true);
451 
452  $this->assertEquals(13, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
453 
454  $mappingData = array(
455  'column_0' => array('attributeIndexOrDerivedType' => 'string', 'type' => 'importColumn',
456  'mappingRulesData' => array(
457  'DefaultValueModelAttributeMappingRuleForm' =>
458  array('defaultValue' => null))),
459  'column_23' => array('attributeIndexOrDerivedType' => 'FullName', 'type' => 'importColumn',
460  'mappingRulesData' => array(
461  'FullNameDefaultValueModelAttributeMappingRuleForm' =>
462  array('defaultValue' => null))),
463  );
464 
465  $importRules = ImportRulesUtil::makeImportRulesByType('ImportModelTestItem');
466  $page = 0;
467  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
468  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
469  $dataProvider->getPagination()->setCurrentPage($page);
470  $importResultsUtil = new ImportResultsUtil($import);
471  $messageLogger = new ImportMessageLogger();
472  ImportUtil::importByDataProvider($dataProvider,
473  $importRules,
474  $mappingData,
475  $importResultsUtil,
477  $messageLogger);
478  $importResultsUtil->processStatusAndMessagesForEachRow();
479 
480  //Confirm that 10 models where created.
481  $testModels = ImportModelTestItem::getAll();
482  $this->assertEquals(10, count($testModels));
483  $jim = User::getByUsername('jim');
484  foreach ($testModels as $model)
485  {
486  $this->assertEquals(array(Permission::NONE, Permission::NONE), $model->getExplicitActualPermissions ($jim));
487  }
488 
489  //Confirm 10 rows were processed as 'created'.
490  $this->assertEquals(10, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
491  . ImportRowDataResultsUtil::CREATED));
492 
493  //Confirm that 0 rows were processed as 'updated'.
494  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
495  . ImportRowDataResultsUtil::UPDATED));
496 
497  //Confirm 2 rows were processed as 'errors'.
498  $this->assertEquals(2, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
499  . ImportRowDataResultsUtil::ERROR));
500 
501  $beansWithErrors = ImportDatabaseUtil::getSubset($import->getTempTableName(), "status = "
502  . ImportRowDataResultsUtil::ERROR);
503  $this->assertEquals(2, count($beansWithErrors));
504 
505  //Confirm the messages are as expected.
506  $compareMessages = array(
507  'Import - Last Name specified is too long.',
508  'Import - Last Name - Last Name cannot be blank.',
509  );
510  $this->assertEquals($compareMessages, unserialize(current($beansWithErrors)->serializedMessages));
511 
512  $compareMessages = array(
513  'Import - String This field is required and neither a value nor a default value was specified.',
514  'Import - Full name value required, but missing.',
515  'Import - Last Name - Last Name cannot be blank.',
516  'Import - String - String cannot be blank.',
517  );
518  $this->assertEquals($compareMessages, unserialize(next($beansWithErrors)->serializedMessages));
519 
520  //Clear out data in table
522  }
523 
528  {
529  Yii::app()->user->userModel = User::getByUsername('super');
530 
531  $testModels = ImportModelTestItem::getAll();
532  $this->assertEquals(0, count($testModels));
533 
534  //Add a read only user for import. Then all models should be readable by jim in addition to super.
535  $explicitReadWriteModelPermissions = new ExplicitReadWriteModelPermissions();
536  $explicitReadWriteModelPermissions->addReadOnlyPermitable(User::getByUsername('jim'));
537 
538  $testModels = ImportModelTestItem::getAll();
539  $this->assertEquals(0, count($testModels));
540  $import = new Import();
541  $serializedData['importRulesType'] = 'ImportModelTestItem';
542  $serializedData['firstRowIsHeaderRow'] = true;
543  $import->serializedData = serialize($serializedData);
544  $this->assertTrue($import->save());
545 
546  ImportTestHelper::createTempTableByFileNameAndTableName('importAnalyzerTest.csv', $import->getTempTableName(), true);
547 
548  $this->assertEquals(13, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
549 
550  $mappingData = array(
551  'column_0' => array('attributeIndexOrDerivedType' => 'string', 'type' => 'importColumn',
552  'mappingRulesData' => array(
553  'DefaultValueModelAttributeMappingRuleForm' =>
554  array('defaultValue' => null))),
555  'column_23' => array('attributeIndexOrDerivedType' => 'FullName', 'type' => 'importColumn',
556  'mappingRulesData' => array(
557  'FullNameDefaultValueModelAttributeMappingRuleForm' =>
558  array('defaultValue' => null))),
559  );
560 
561  $importRules = ImportRulesUtil::makeImportRulesByType('ImportModelTestItem');
562  $page = 0;
563  $config = array('pagination' => array('pageSize' => 3)); //This way all rows are processed.
564  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
565  $dataProvider->getPagination()->setCurrentPage($page);
566  $importResultsUtil = new ImportResultsUtil($import);
567  $messageLogger = new ImportMessageLogger();
568  ImportUtil::importByDataProvider($dataProvider,
569  $importRules,
570  $mappingData,
571  $importResultsUtil,
572  $explicitReadWriteModelPermissions,
573  $messageLogger);
574  $importResultsUtil->processStatusAndMessagesForEachRow();
575 
576  //Confirm that 3 models where created.
577  $testModels = ImportModelTestItem::getAll();
578  $this->assertEquals(3, count($testModels));
579  $jim = User::getByUsername('jim');
580  foreach ($testModels as $model)
581  {
582  $this->assertEquals(array(Permission::READ, Permission::NONE), $model->getExplicitActualPermissions ($jim));
583  }
584 
585  //Clear out data in table
587 
588  //Now test with read/write permissions being set.
589  $explicitReadWriteModelPermissions = new ExplicitReadWriteModelPermissions();
590  $explicitReadWriteModelPermissions->addReadWritePermitable(User::getByUsername('jim'));
591  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
592  $dataProvider->getPagination()->setCurrentPage($page);
593  $importResultsUtil = new ImportResultsUtil($import);
594  $messageLogger = new ImportMessageLogger();
595  ImportUtil::importByDataProvider($dataProvider,
596  $importRules,
597  $mappingData,
598  $importResultsUtil,
599  $explicitReadWriteModelPermissions,
600  $messageLogger);
601  $importResultsUtil->processStatusAndMessagesForEachRow();
602 
603  //Confirm that 3 models where created.
604  $testModels = ImportModelTestItem::getAll();
605  $this->assertEquals(3, count($testModels));
606  $jim = User::getByUsername('jim');
607  foreach ($testModels as $model)
608  {
609  $this->assertEquals(array(Permission::READ_WRITE_CHANGE_PERMISSIONS_CHANGE_OWNER, Permission::NONE), $model->getExplicitActualPermissions ($jim));
610  }
612  }
613 
614  public function testSettingExplicitReadWriteModelPermissionsDuringImportDontAffectTheModifiedByUserAttribute()
615  {
616  Yii::app()->user->userModel = User::getByUsername('super');
617 
618  $testModels = ImportModelTestItem::getAll();
619  $this->assertEquals(0, count($testModels));
620 
621  //Add a read only user for import. Then all models should be readable by jim in addition to super.
622  $explicitReadWriteModelPermissions = new ExplicitReadWriteModelPermissions();
623  $explicitReadWriteModelPermissions->addReadOnlyPermitable(User::getByUsername('jim'));
624 
625  $testModels = ImportModelTestItem::getAll();
626  $this->assertEquals(0, count($testModels));
627  $import = new Import();
628  $serializedData['importRulesType'] = 'ImportModelTestItem';
629  $serializedData['firstRowIsHeaderRow'] = true;
630  $import->serializedData = serialize($serializedData);
631  $this->assertTrue($import->save());
632 
633  ImportTestHelper::createTempTableByFileNameAndTableName('importAnalyzerTest3.csv', $import->getTempTableName(), true);
634 
635  $this->assertEquals(2, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
636 
637  $mappingData = array(
638  'column_0' => array('attributeIndexOrDerivedType' => 'string', 'type' => 'importColumn',
639  'mappingRulesData' => array(
640  'DefaultValueModelAttributeMappingRuleForm' =>
641  array('defaultValue' => null))),
642  'column_7' => array('attributeIndexOrDerivedType' => 'modifiedByUser', 'type' => 'importColumn',
643  'mappingRulesData' => array(
644  'UserValueTypeModelAttributeMappingRuleForm' =>
645  array('type' => UserValueTypeModelAttributeMappingRuleForm::ZURMO_USERNAME))),
646  'column_23' => array('attributeIndexOrDerivedType' => 'FullName', 'type' => 'importColumn',
647  'mappingRulesData' => array(
648  'FullNameDefaultValueModelAttributeMappingRuleForm' =>
649  array('defaultValue' => null))),
650  );
651 
652  $importRules = ImportRulesUtil::makeImportRulesByType('ImportModelTestItem');
653  $page = 0;
654  $config = array('pagination' => array('pageSize' => 3)); //This way all rows are processed.
655  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
656  $dataProvider->getPagination()->setCurrentPage($page);
657  $importResultsUtil = new ImportResultsUtil($import);
658  $messageLogger = new ImportMessageLogger();
659  ImportUtil::importByDataProvider($dataProvider,
660  $importRules,
661  $mappingData,
662  $importResultsUtil,
663  $explicitReadWriteModelPermissions,
664  $messageLogger);
665  $importResultsUtil->processStatusAndMessagesForEachRow();
666 
667  //Confirm that 1 models where created.
668  $testModels = ImportModelTestItem::getAll();
669  $this->assertEquals(1, count($testModels));
670  $jim = User::getByUsername('jim');
671  foreach ($testModels as $model)
672  {
673  $this->assertEquals(array(Permission::READ, Permission::NONE), $model->getExplicitActualPermissions ($jim));
674  $this->assertEquals('jim', $model->modifiedByUser->username);
675  }
676 
677  //Clear out data in table
679 
680  //Now test with read/write permissions being set.
681  $explicitReadWriteModelPermissions = new ExplicitReadWriteModelPermissions();
682  $explicitReadWriteModelPermissions->addReadWritePermitable(User::getByUsername('jim'));
683  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
684  $dataProvider->getPagination()->setCurrentPage($page);
685  $importResultsUtil = new ImportResultsUtil($import);
686  $messageLogger = new ImportMessageLogger();
687  ImportUtil::importByDataProvider($dataProvider,
688  $importRules,
689  $mappingData,
690  $importResultsUtil,
691  $explicitReadWriteModelPermissions,
692  $messageLogger);
693  $importResultsUtil->processStatusAndMessagesForEachRow();
694 
695  //Confirm that 1 models where created.
696  $testModels = ImportModelTestItem::getAll();
697  $this->assertEquals(1, count($testModels));
698  $jim = User::getByUsername('jim');
699  foreach ($testModels as $model)
700  {
701  $this->assertEquals(array(Permission::READ_WRITE_CHANGE_PERMISSIONS_CHANGE_OWNER, Permission::NONE), $model->getExplicitActualPermissions ($jim));
702  $this->assertEquals('jim', $model->modifiedByUser->username);
703  }
704  }
705 
706  public function testCreateImportDataForEmailDedupe()
707  {
708  $super = User::getByUsername('super');
709  $jim = User::getByUsername('jim');
710  Yii::app()->user->userModel = $jim;
711 
712  //Confirm Jim can can only view ImportModelTestItems he owns.
713  $item = NamedSecurableItem::getByName('ImportModule');
714  $this->assertEquals(Permission::NONE, $item->getEffectivePermissions($jim));
715 
716  $import = new Import();
717  $serializedData['importRulesType'] = 'Accounts';
718  $serializedData['firstRowIsHeaderRow'] = true;
719  $import->serializedData = serialize($serializedData);
720  $this->assertTrue($import->save());
721 
722  ImportTestHelper::createTempTableByFileNameAndTableName('importDedupeTest.csv', $import->getTempTableName(), true);
723 
724  $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
725 
726  $emailMappingData = ImportMappingUtil::makeEmailColumnMappingData ('primaryEmail__emailAddress');
727  $emailMappingData['mappingRulesData']['EmailModelAttributeDedupeMappingRuleForm']
728  = array('dedupeRule' => ImportDedupeRulesRadioDropDownElement::DO_NOT_DEDUPE);
729  $mappingData = array(
730  'column_0' => ImportMappingUtil::makeStringColumnMappingData ('name'),
731  'column_1' => $emailMappingData
732  );
733 
734  $importRules = ImportRulesUtil::makeImportRulesByType('Accounts');
735  $page = 0;
736  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
737  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
738  $dataProvider->getPagination()->setCurrentPage($page);
739  $importResultsUtil = new ImportResultsUtil($import);
740  $messageLogger = new ImportMessageLogger();
741  ImportUtil::importByDataProvider($dataProvider,
742  $importRules,
743  $mappingData,
744  $importResultsUtil,
746  $messageLogger);
747  $importResultsUtil->processStatusAndMessagesForEachRow();
748 
749  //Confirm that 2 models where created.
750  $testModels = Account::getAll();
751  $this->assertEquals(2, count($testModels));
752  }
753 
757  //On inport of new set containing duplicate data
759  {
760  $jim = User::getByUsername('jim');
761  Yii::app()->user->userModel = $jim;
762 
763  //Confirm Jim can can only view ImportModelTestItems he owns.
764  $item = NamedSecurableItem::getByName('ImportModule');
765  $this->assertEquals(Permission::NONE, $item->getEffectivePermissions($jim));
766 
767  $testModels = Account::getAll();
768  $this->assertEquals(2, count($testModels));
769  $import = new Import();
770  $serializedData['importRulesType'] = 'Accounts';
771  $serializedData['firstRowIsHeaderRow'] = true;
772  $import->serializedData = serialize($serializedData);
773  $this->assertTrue($import->save());
774 
775  ImportTestHelper::createTempTableByFileNameAndTableName('importEmailDedupeTest.csv', $import->getTempTableName(), true);
776 
777  $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
778 
779  $emailMappingData = ImportMappingUtil::makeEmailColumnMappingData ('primaryEmail__emailAddress');
780  $emailMappingData['mappingRulesData']['EmailModelAttributeDedupeMappingRuleForm']
781  = array('dedupeRule' =>
782  ImportDedupeRulesRadioDropDownElement::SKIP_ROW_ON_MATCH_FOUND);
783  $mappingData = array(
784  'column_0' => ImportMappingUtil::makeStringColumnMappingData ('name'),
785  'column_1' => $emailMappingData
786  );
787 
788  $importRules = ImportRulesUtil::makeImportRulesByType('Accounts');
789  $page = 0;
790  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
791  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
792  $dataProvider->getPagination()->setCurrentPage($page);
793  $importResultsUtil = new ImportResultsUtil($import);
794  $messageLogger = new ImportMessageLogger();
795  ImportUtil::importByDataProvider($dataProvider,
796  $importRules,
797  $mappingData,
798  $importResultsUtil,
800  $messageLogger);
801  $importResultsUtil->processStatusAndMessagesForEachRow();
802 
803  //Confirm that 3 models where created.
804  $testModels = Account::getAll();
805  $this->assertEquals(3, count($testModels));
806  }
807 
811  //On inport of new set containing duplicate data
813  {
814  $jim = User::getByUsername('jim');
815  Yii::app()->user->userModel = $jim;
816 
817  //Confirm Jim can can only view ImportModelTestItems he owns.
818  $item = NamedSecurableItem::getByName('ImportModule');
819  $this->assertEquals(Permission::NONE, $item->getEffectivePermissions($jim));
820 
821  $testModels = Account::getByName('abc');
822  $this->assertEquals(1, count($testModels));
823  $testModels = Account::getByName('mom');
824  $this->assertEquals(1, count($testModels));
825  $testModels = Account::getByName('hello');
826  $this->assertEquals(0, count($testModels));
827  $testModels = Account::getByName('dear');
828  $this->assertEquals(0, count($testModels));
829  $import = new Import();
830  $serializedData['importRulesType'] = 'Accounts';
831  $serializedData['firstRowIsHeaderRow'] = true;
832  $import->serializedData = serialize($serializedData);
833  $this->assertTrue($import->save());
834 
835  ImportTestHelper::createTempTableByFileNameAndTableName('importEmailUpdateDedupeTest.csv', $import->getTempTableName(), true);
836 
837  $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
838 
839  $emailMappingData = ImportMappingUtil::makeEmailColumnMappingData ('primaryEmail__emailAddress');
840  $emailMappingData['mappingRulesData']['EmailModelAttributeDedupeMappingRuleForm']
841  = array('dedupeRule' =>
842  ImportDedupeRulesRadioDropDownElement::UPDATE_ROW_ON_MATCH_FOUND);
843  $mappingData = array(
844  'column_0' => ImportMappingUtil::makeStringColumnMappingData ('name'),
845  'column_1' => $emailMappingData
846  );
847 
848  $importRules = ImportRulesUtil::makeImportRulesByType('Accounts');
849  $page = 0;
850  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
851  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
852  $dataProvider->getPagination()->setCurrentPage($page);
853  $importResultsUtil = new ImportResultsUtil($import);
854  $messageLogger = new ImportMessageLogger();
855  ImportUtil::importByDataProvider($dataProvider,
856  $importRules,
857  $mappingData,
858  $importResultsUtil,
860  $messageLogger);
861  $importResultsUtil->processStatusAndMessagesForEachRow();
862 
863  //Confirm that 3 models where created.
864  $testModels = Account::getAll();
865  $this->assertEquals(3, count($testModels));
866 
867  $testModels = Account::getByName('abc');
868  $this->assertEquals(0, count($testModels));
869  $testModels = Account::getByName('mom');
870  $this->assertEquals(0, count($testModels));
871  $testModels = Account::getByName('hello');
872  $this->assertEquals(1, count($testModels));
873  $testModels = Account::getByName('dear');
874  $this->assertEquals(1, count($testModels));
875  }
876 
880  //On inport of new set containing duplicate data for name
882  {
883  $jim = User::getByUsername('jim');
884  Yii::app()->user->userModel = $jim;
885 
886  //Confirm Jim can can only view ImportModelTestItems he owns.
887  $item = NamedSecurableItem::getByName('ImportModule');
888  $this->assertEquals(Permission::NONE, $item->getEffectivePermissions($jim));
889 
890  $testModels = Account::getByName('hello');
891  $this->assertEquals(1, count($testModels));
892  $import = new Import();
893  $serializedData['importRulesType'] = 'Accounts';
894  $serializedData['firstRowIsHeaderRow'] = true;
895  $import->serializedData = serialize($serializedData);
896  $this->assertTrue($import->save());
897 
898  ImportTestHelper::createTempTableByFileNameAndTableName('importDedupeNameTest.csv', $import->getTempTableName(), true);
899 
900  $this->assertEquals(2, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
901 
902  $emailMappingData = ImportMappingUtil::makeEmailColumnMappingData ('primaryEmail__emailAddress');
903  $emailMappingData['mappingRulesData']['EmailModelAttributeDedupeMappingRuleForm']
904  = array('dedupeRule' =>
905  ImportDedupeRulesRadioDropDownElement::DO_NOT_DEDUPE);
906  $nameMappingData = ImportMappingUtil::makeEmailColumnMappingData ('name');
907  $nameMappingData['mappingRulesData']['NameModelAttributeDedupeMappingRuleForm']
908  = array('dedupeRule' =>
909  ImportDedupeRulesRadioDropDownElement::SKIP_ROW_ON_MATCH_FOUND);
910  $mappingData = array(
911  'column_0' => $nameMappingData,
912  'column_1' => $emailMappingData
913  );
914 
915  $importRules = ImportRulesUtil::makeImportRulesByType('Accounts');
916  $page = 0;
917  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
918  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
919  $dataProvider->getPagination()->setCurrentPage($page);
920  $importResultsUtil = new ImportResultsUtil($import);
921  $messageLogger = new ImportMessageLogger();
922  ImportUtil::importByDataProvider($dataProvider,
923  $importRules,
924  $mappingData,
925  $importResultsUtil,
927  $messageLogger);
928  $importResultsUtil->processStatusAndMessagesForEachRow();
929 
930  $testModels = Account::getByName('hello');
931  $this->assertEquals(1, count($testModels));
932  $this->assertEquals('a@a.com', $testModels[0]->primaryEmail->emailAddress);
933  }
934 
939  {
940  $jim = User::getByUsername('jim');
941  Yii::app()->user->userModel = $jim;
942 
943  //Confirm Jim can can only view ImportModelTestItems he owns.
944  $item = NamedSecurableItem::getByName('ImportModule');
945  $this->assertEquals(Permission::NONE, $item->getEffectivePermissions($jim));
946 
947  $testModels = Account::getByName('hello');
948  $this->assertEquals(1, count($testModels));
949  $import = new Import();
950  $serializedData['importRulesType'] = 'Accounts';
951  $serializedData['firstRowIsHeaderRow'] = true;
952  $import->serializedData = serialize($serializedData);
953  $this->assertTrue($import->save());
954 
955  ImportTestHelper::createTempTableByFileNameAndTableName('importDedupeNameTest.csv', $import->getTempTableName(), true);
956 
957  $this->assertEquals(2, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
958 
959  $emailMappingData = ImportMappingUtil::makeEmailColumnMappingData ('primaryEmail__emailAddress');
960  $emailMappingData['mappingRulesData']['EmailModelAttributeDedupeMappingRuleForm']
961  = array('dedupeRule' =>
962  ImportDedupeRulesRadioDropDownElement::DO_NOT_DEDUPE);
963  $nameMappingData = ImportMappingUtil::makeEmailColumnMappingData ('name');
964  $nameMappingData['mappingRulesData']['NameModelAttributeDedupeMappingRuleForm']
965  = array('dedupeRule' =>
966  ImportDedupeRulesRadioDropDownElement::UPDATE_ROW_ON_MATCH_FOUND);
967  $mappingData = array(
968  'column_0' => $nameMappingData,
969  'column_1' => $emailMappingData
970  );
971 
972  $importRules = ImportRulesUtil::makeImportRulesByType('Accounts');
973  $page = 0;
974  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
975  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
976  $dataProvider->getPagination()->setCurrentPage($page);
977  $importResultsUtil = new ImportResultsUtil($import);
978  $messageLogger = new ImportMessageLogger();
979  ImportUtil::importByDataProvider($dataProvider,
980  $importRules,
981  $mappingData,
982  $importResultsUtil,
984  $messageLogger);
985  $importResultsUtil->processStatusAndMessagesForEachRow();
986 
987  $testModels = Account::getByName('hello');
988  $this->assertEquals(1, count($testModels));
989  $this->assertEquals('test@a.com', $testModels[0]->primaryEmail->emailAddress);
990  }
991  }
992 ?>
static makeEmailColumnMappingData($attributeName, $defaultValue=null)
testVerifyNewlyImportedDataForEmailDedupe()
static makeImportRulesByType($importRulesType)
static getByUsername($username)
Definition: User.php:49
static getCount($tableName, $where=null)
static importByDataProvider(ImportDataProvider $dataProvider, ImportRules $importRules, $mappingData, ImportResultsUtil $importResultsUtil, ExplicitReadWriteModelPermissions $explicitReadWriteModelPermissions, ImportMessageLogger $messageLogger)
Definition: ImportUtil.php:51
testNewlyImportedDataForUpdateEmailDedupe()
static makeStringColumnMappingData($attributeName, $defaultValue=null)
static resolveLinkMessageToModel(RedBeanModel $model)
Definition: ImportUtil.php:283
testSettingExplicitReadWriteModelPermissionsDuringImport()
static getByName($name, $shouldCache=true)
testImportSwitchingOwnerButShouldStillCreate()
testSimpleImportWithStringAndFullNameWhichAreRequiredAttributeOnImportTestModelItem()
static getSubset($tableName, $where=null, $count=null, $offset=null)
static deleteAll()
static getById($id, $modelClassName=null)
testImportedDataForNameSkipDedupe()
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
testSetDataAnalyzerMultiSelectAndTagCloudImport()
testImportedDataForNameUpdateDedupe()
Generated on Wed Feb 26 2020 07:10:38