All Data Structures Functions Variables Pages
UserImportTest.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  }
44 
45  public function testSimpleUserImportWhereAllRowsSucceed()
46  {
47  Yii::app()->user->userModel = User::getByUsername('super');
48 
49  $users = User::getAll();
50  $this->assertEquals(1, count($users));
51  $import = new Import();
52  $serializedData['importRulesType'] = 'User';
53  $serializedData['firstRowIsHeaderRow'] = true;
54  $import->serializedData = serialize($serializedData);
55  $this->assertTrue($import->save());
56 
57  ImportTestHelper::
58  createTempTableByFileNameAndTableName('importAnalyzerTest.csv', $import->getTempTableName(), true,
59  Yii::getPathOfAlias('application.modules.users.tests.unit.files'));
60 
61  $this->assertEquals(11, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
62 
63  $defaultLanguage = Yii::app()->language;
65  $defaultLocale = $localeIds[0];
66 
67  $timezoneIdentifiers = DateTimeZone::listIdentifiers();
68  $defaultTimeZone = $timezoneIdentifiers[0];
69  $defaultCurrency = Yii::app()->currencyHelper->getActiveCurrencyForCurrentUser();
70  $defaultCurrencyId = $defaultCurrency->id;
71 
72  $mappingData = array(
73  'column_0' => array('attributeIndexOrDerivedType' => 'username',
74  'type' => 'importColumn',
75  'mappingRulesData' => array()),
76 
77  'column_1' => array('attributeIndexOrDerivedType' => 'Password',
78  'type' => 'importColumn',
79  'mappingRulesData' => array(
80  'PasswordDefaultValueModelAttributeMappingRuleForm' =>
81  array('defaultValue' => null))),
82  'column_2' => array('attributeIndexOrDerivedType' => 'UserStatus',
83  'type' => 'importColumn',
84  'mappingRulesData' => array(
85  'UserStatusDefaultValueMappingRuleForm' =>
86  array('defaultValue' => UserStatusUtil::ACTIVE))),
87  'column_3' => ImportMappingUtil::makeStringColumnMappingData('firstName'),
88  'column_4' => ImportMappingUtil::makeStringColumnMappingData('lastName'),
89  'column_5' => array('attributeIndexOrDerivedType' => 'role',
90  'type' => 'importColumn',
91  'mappingRulesData' => array(
92  'DefaultModelNameIdMappingRuleForm' =>
93  array('defaultModelId' => null),
94  'RelatedModelValueTypeMappingRuleForm' =>
95  array('type' => RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_ID))),
96  'column_6' => array('attributeIndexOrDerivedType' => 'language',
97  'type' => 'importColumn',
98  'mappingRulesData' => array(
99  'DefaultValueModelAttributeMappingRuleForm' =>
100  array('defaultValue' => $defaultLanguage))),
101  'column_7' => array('attributeIndexOrDerivedType' => 'locale',
102  'type' => 'importColumn',
103  'mappingRulesData' => array(
104  'DefaultValueModelAttributeMappingRuleForm' =>
105  array('defaultValue' => $defaultLocale))),
106  'column_8' => array('attributeIndexOrDerivedType' => 'timeZone',
107  'type' => 'importColumn',
108  'mappingRulesData' => array(
109  'DefaultValueModelAttributeMappingRuleForm' =>
110  array('defaultValue' => $defaultTimeZone))),
111  'column_9' => array('attributeIndexOrDerivedType' => 'currency',
112  'type' => 'importColumn',
113  'mappingRulesData' => array(
114  'DefaultValueModelAttributeMappingRuleForm' =>
115  array('defaultValue' => $defaultCurrencyId))),
116  );
117 
118  $importRules = ImportRulesUtil::makeImportRulesByType('Users');
119  $page = 0;
120  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
121  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
122  $dataProvider->getPagination()->setCurrentPage($page);
123  $importResultsUtil = new ImportResultsUtil($import);
124  $messageLogger = new ImportMessageLogger();
125  ImportUtil::importByDataProvider($dataProvider,
126  $importRules,
127  $mappingData,
128  $importResultsUtil,
130  $messageLogger);
131  $importResultsUtil->processStatusAndMessagesForEachRow();
132 
133  //Confirm that 10 models where created.
134  $this->assertEquals(11, User::getCount());
135  $activeUser = User::getByUsername('myusername7');
136  $userStatus = UserStatusUtil::makeByUser($activeUser);
137  $this->assertTrue($userStatus->isActive());
138  $this->assertEquals($defaultLanguage, $activeUser->language);
139  $this->assertEquals($defaultLocale, $activeUser->locale);
140  $this->assertEquals($defaultTimeZone, $activeUser->timeZone);
141  $this->assertEquals($defaultCurrency, $activeUser->currency);
142  $inactiveUser = User::getByUsername('myusername8');
143  $userStatus = UserStatusUtil::makeByUser($inactiveUser);
144  $this->assertEquals($defaultLanguage, $inactiveUser->language);
145  $this->assertEquals($defaultLocale, $inactiveUser->locale);
146  $this->assertEquals($defaultTimeZone, $inactiveUser->timeZone);
147  $this->assertEquals($defaultCurrency, $inactiveUser->currency);
148  $this->assertFalse($userStatus->isActive());
149 
150  //Confirm 10 rows were processed as 'created'.
151  $this->assertEquals(10, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
152  . ImportRowDataResultsUtil::CREATED));
153 
154  //Confirm that 0 rows were processed as 'updated'.
155  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
156  . ImportRowDataResultsUtil::UPDATED));
157 
158  //Confirm 2 rows were processed as 'errors'.
159  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
160  . ImportRowDataResultsUtil::ERROR));
161 
162  $beansWithErrors = ImportDatabaseUtil::getSubset($import->getTempTableName(), "status = "
163  . ImportRowDataResultsUtil::ERROR);
164  $this->assertEquals(0, count($beansWithErrors));
165  }
166 
171  {
172  Yii::app()->user->userModel = User::getByUsername('super');
173  $users = User::getAll();
174  $this->assertEquals(11, count($users));
175  $import = new Import();
176  $serializedData['importRulesType'] = 'User';
177  $serializedData['firstRowIsHeaderRow'] = true;
178  $import->serializedData = serialize($serializedData);
179  $this->assertTrue($import->save());
180 
181  ImportTestHelper::
182  createTempTableByFileNameAndTableName('importAnalyzerWithOptionalFields.csv',
183  $import->getTempTableName(), true,
184  Yii::getPathOfAlias('application.modules.users.tests.unit.files'));
185 
186  $this->assertEquals(11, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
187 
188  $defaultLanguage = Yii::app()->language;
190  $defaultLocale = $localeIds[0];
191 
192  $timezoneIdentifiers = DateTimeZone::listIdentifiers();
193  $defaultTimeZone = $timezoneIdentifiers[0];
194  $defaultCurrency = Yii::app()->currencyHelper->getActiveCurrencyForCurrentUser();
195  $defaultCurrencyId = $defaultCurrency->id;
196 
197  $mappingData = array(
198  'column_0' => array('attributeIndexOrDerivedType' => 'username',
199  'type' => 'importColumn',
200  'mappingRulesData' => array()),
201  'column_1' => array('attributeIndexOrDerivedType' => 'Password',
202  'type' => 'importColumn',
203  'mappingRulesData' => array(
204  'PasswordDefaultValueModelAttributeMappingRuleForm' =>
205  array('defaultValue' => null))),
206  'column_3' => ImportMappingUtil::makeStringColumnMappingData('firstName'),
207  'column_4' => ImportMappingUtil::makeStringColumnMappingData('lastName'),
208  'column_5' => array('attributeIndexOrDerivedType' => 'language',
209  'type' => 'extraColumn',
210  'mappingRulesData' => array(
211  'DefaultValueModelAttributeMappingRuleForm' =>
212  array('defaultValue' => $defaultLanguage))),
213  'column_6' => array('attributeIndexOrDerivedType' => 'locale',
214  'type' => 'extraColumn',
215  'mappingRulesData' => array(
216  'DefaultValueModelAttributeMappingRuleForm' =>
217  array('defaultValue' => $defaultLocale))),
218  'column_7' => array('attributeIndexOrDerivedType' => 'timeZone',
219  'type' => 'extraColumn',
220  'mappingRulesData' => array(
221  'DefaultValueModelAttributeMappingRuleForm' =>
222  array('defaultValue' => $defaultTimeZone))),
223  'column_8' => array('attributeIndexOrDerivedType' => 'currency',
224  'type' => 'extraColumn',
225  'mappingRulesData' => array(
226  'DefaultValueModelAttributeMappingRuleForm' =>
227  array('defaultValue' => $defaultCurrencyId))),
228  );
229 
230  $importRules = ImportRulesUtil::makeImportRulesByType('Users');
231  $page = 0;
232  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
233  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
234  $dataProvider->getPagination()->setCurrentPage($page);
235  $importResultsUtil = new ImportResultsUtil($import);
236  $messageLogger = new ImportMessageLogger();
237  ImportUtil::importByDataProvider($dataProvider, $importRules, $mappingData, $importResultsUtil,
238  new ExplicitReadWriteModelPermissions(), $messageLogger);
239  $importResultsUtil->processStatusAndMessagesForEachRow();
240 
241  //Confirm that 10 new models are created.
242  $users = User::getAll();
243  $this->assertEquals(21, count($users));
244  $user = User::getByUsername('myusername11');
245  $this->assertEquals($defaultLanguage, $user->language);
246  $this->assertEquals($defaultLocale, $user->locale);
247  $this->assertEquals($defaultTimeZone, $user->timeZone);
248  $this->assertEquals($defaultCurrency, $user->currency);
249  //Confirm 10 rows were processed as 'created'.
250  $this->assertEquals(10, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
251  . ImportRowDataResultsUtil::CREATED));
252  //Confirm that 0 rows were processed as 'updated'.
253  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
254  . ImportRowDataResultsUtil::UPDATED));
255  //Confirm 2 rows were processed as 'errors'.
256  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
257  . ImportRowDataResultsUtil::ERROR));
258  $beansWithErrors = ImportDatabaseUtil::getSubset($import->getTempTableName(), "status = "
259  . ImportRowDataResultsUtil::ERROR);
260  $this->assertEquals(0, count($beansWithErrors));
261  }
262  }
263 ?>
testUserImportWithOptionalFields()
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
static makeStringColumnMappingData($attributeName, $defaultValue=null)
static getSelectableLocaleIds()
static getSubset($tableName, $where=null, $count=null, $offset=null)
static getCount(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $where=null, $modelClassName=null, $selectDistinct=false)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
static makeByUser(User $user)
Generated on Sun May 31 2020 07:10:35