Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ProductImportTest.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  Yii::import('application.core.data.*');
44  Yii::import('application.modules.products.data.*');
45  $defaultDataMaker = new ProductsDefaultDataMaker();
46  $defaultDataMaker->make();
47  }
48 
49  public function testSimpleUserImportWhereAllRowsSucceed()
50  {
51  Yii::app()->user->userModel = User::getByUsername('super');
52  //Create account
53  $sampleAccount = AccountTestHelper::
54  createAccountByNameForOwner('sampleAccount',
55  Yii::app()->user->userModel);
56  $accountId = $sampleAccount->id;
57 
58  $import = new Import();
59  $serializedData['importRulesType'] = 'Products';
60  $serializedData['firstRowIsHeaderRow'] = true;
61  $import->serializedData = serialize($serializedData);
62  $this->assertTrue($import->save());
63 
64  ImportTestHelper::
65  createTempTableByFileNameAndTableName('productsSample.csv', $import->getTempTableName(), true,
66  Yii::getPathOfAlias('application.modules.products.tests.unit.files'));
67 
68  $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
69 
70  $currencies = Currency::getAll();
71 
72  $ownerColumnMappingData = array('attributeIndexOrDerivedType' => 'owner',
73  'type' => 'importColumn', 'mappingRulesData' => array(
74  'DefaultModelNameIdMappingRuleForm' =>
75  array('defaultModelId' => null),
76  'UserValueTypeModelAttributeMappingRuleForm' =>
77  array('type' =>
78  UserValueTypeModelAttributeMappingRuleForm::ZURMO_USERNAME)));
79 
80  $mappingData = array(
81  'column_0' => $ownerColumnMappingData,
83  'column_2' => ImportMappingUtil::makeTextAreaColumnMappingData ('description'),
84  'column_3' => ImportMappingUtil::makeIntegerColumnMappingData ('quantity'),
85  'column_4' => ImportMappingUtil::makeHasOneColumnMappingData ('account',
86  RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_NAME),
87  'column_5' => ImportMappingUtil::makeStringColumnMappingData ('stage'),
88  'column_6' => ImportMappingUtil::makeCurrencyColumnMappingData ('sellPrice', $currencies[0]),
89  'column_7' => ImportMappingUtil::makeIntegerColumnMappingData ('priceFrequency'),
90  'column_8' => ImportMappingUtil::makeIntegerColumnMappingData ('type'),
91  );
92 
93  $importRules = ImportRulesUtil::makeImportRulesByType('Products');
94  $page = 0;
95  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
96  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
97  $dataProvider->getPagination()->setCurrentPage($page);
98  $importResultsUtil = new ImportResultsUtil($import);
99  $messageLogger = new ImportMessageLogger();
100  ImportUtil::importByDataProvider($dataProvider,
101  $importRules,
102  $mappingData,
103  $importResultsUtil,
105  $messageLogger);
106  $importResultsUtil->processStatusAndMessagesForEachRow();
107 
108  //Confirm that 3 models where created.
109  $products = Product::getAll();
110  $this->assertEquals(2, count($products));
111 
112  $products = Product::getByName('A Bend in the River November Issue import');
113  $this->assertEquals(1, count($products[0]));
114  $this->assertEquals('super', $products[0]->owner->username);
115  $this->assertEquals('A Bend in the River November Issue import', $products[0]->name);
116  $this->assertEquals(6, $products[0]->quantity);
117  $this->assertEquals('sampleAccount', $products[0]->account->name);
118  $this->assertEquals('Open', $products[0]->stage->value);
119  $this->assertEquals('Test Desc', $products[0]->description);
120  $this->assertEquals(210, $products[0]->sellPrice->value);
121  $this->assertEquals(2, $products[0]->priceFrequency);
122  $this->assertEquals(2, $products[0]->type);
123 
124  $products[0]->delete();
125 
126  $products = Product::getByName('A Bend in the River November Issue import copy');
127  $this->assertEquals(1, count($products[0]));
128  $this->assertEquals('super', $products[0]->owner->username);
129  $this->assertEquals('A Bend in the River November Issue import copy', $products[0]->name);
130  $this->assertEquals(6, $products[0]->quantity);
131  $this->assertEquals('sampleAccount', $products[0]->account->name);
132  $this->assertEquals('Open', $products[0]->stage->value);
133  $this->assertEquals('Test Desc 1', $products[0]->description);
134  $this->assertEquals(210, $products[0]->sellPrice->value);
135  $this->assertEquals(2, $products[0]->priceFrequency);
136  $this->assertEquals(2, $products[0]->type);
137 
138  $products[0]->delete();
139 
140  //Confirm 10 rows were processed as 'created'.
141 // $this->assertEquals(2, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
142 // . ImportRowDataResultsUtil::CREATED));
143 
144  //Confirm that 2 rows were processed as 'updated'.
145  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
146  . ImportRowDataResultsUtil::UPDATED));
147 
148  //Confirm 2 rows were processed as 'errors'.
149  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
150  . ImportRowDataResultsUtil::ERROR));
151 
152  $beansWithErrors = ImportDatabaseUtil::getSubset($import->getTempTableName(), "status = "
153  . ImportRowDataResultsUtil::ERROR);
154  $this->assertEquals(0, count($beansWithErrors));
155  }
156 
161  {
162  Yii::app()->user->userModel = User::getByUsername('super');
163  //Create account
164  $sampleAccount = Account::getByName('sampleAccount');
165  $accountId = $sampleAccount[0]->id;
166 
167  //Create Contact
168  $contact = ContactTestHelper::createContactByNameForOwner("My Contact", Yii::app()->user->userModel);
169 
170  $import = new Import();
171  $serializedData['importRulesType'] = 'Products';
172  $serializedData['firstRowIsHeaderRow'] = true;
173  $import->serializedData = serialize($serializedData);
174  $this->assertTrue($import->save());
175 
176  ImportTestHelper::
177  createTempTableByFileNameAndTableName('productsSampleWithRelations.csv', $import->getTempTableName(), true,
178  Yii::getPathOfAlias('application.modules.products.tests.unit.files'));
179 
180  //update the ids of the account column to match the parent account.
181  ZurmoRedBean::exec("update " . $import->getTempTableName() . " set column_9 = " .
182  $contact->id . " where id != 1 limit 3");
183 
184  $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows.
185 
186  $currencies = Currency::getAll();
187 
188  $ownerColumnMappingData = array('attributeIndexOrDerivedType' => 'owner',
189  'type' => 'importColumn', 'mappingRulesData' => array(
190  'DefaultModelNameIdMappingRuleForm' =>
191  array('defaultModelId' => null),
192  'UserValueTypeModelAttributeMappingRuleForm' =>
193  array('type' =>
194  UserValueTypeModelAttributeMappingRuleForm::ZURMO_USERNAME)));
195  $mappingData = array(
196  'column_0' => $ownerColumnMappingData,
197  'column_1' => ImportMappingUtil::makeStringColumnMappingData ('name'),
198  'column_2' => ImportMappingUtil::makeTextAreaColumnMappingData ('description'),
199  'column_3' => ImportMappingUtil::makeIntegerColumnMappingData ('quantity'),
200  'column_4' => ImportMappingUtil::makeHasOneColumnMappingData ('account',
201  RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_NAME),
202  'column_5' => ImportMappingUtil::makeStringColumnMappingData ('stage'),
203  'column_6' => ImportMappingUtil::makeCurrencyColumnMappingData ('sellPrice', $currencies[0]),
204  'column_7' => ImportMappingUtil::makeIntegerColumnMappingData ('priceFrequency'),
205  'column_8' => ImportMappingUtil::makeIntegerColumnMappingData ('type'),
206  //'column_9' => ImportMappingUtil::makeHasOneColumnMappingData ('contact'),
207  );
208 
209  $importRules = ImportRulesUtil::makeImportRulesByType('Products');
210  $page = 0;
211  $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed.
212  $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config);
213  $dataProvider->getPagination()->setCurrentPage($page);
214  $importResultsUtil = new ImportResultsUtil($import);
215  $messageLogger = new ImportMessageLogger();
216  ImportUtil::importByDataProvider($dataProvider,
217  $importRules,
218  $mappingData,
219  $importResultsUtil,
221  $messageLogger);
222  $importResultsUtil->processStatusAndMessagesForEachRow();
223 
224  //Confirm that 3 models where created.
225  $products = Product::getAll();
226  $this->assertEquals(2, count($products));
227 
228  $products = Product::getByName('A Bend in the River November Issue import');
229 
230  $this->assertEquals(1, count($products));
231  $this->assertEquals('super', $products[0]->owner->username);
232  $this->assertEquals('A Bend in the River November Issue import', $products[0]->name);
233  $this->assertEquals(6, $products[0]->quantity);
234  $this->assertEquals('sampleAccount', $products[0]->account->name);
235  $this->assertEquals('Open', $products[0]->stage->value);
236  $this->assertEquals('Test Desc', $products[0]->description);
237  $this->assertEquals(210, $products[0]->sellPrice->value);
238  $this->assertEquals(2, $products[0]->priceFrequency);
239  $this->assertEquals(2, $products[0]->type);
240  //$this->assertEquals('My Contact', $products[0]->contact->firstName);
241 
242  $products = Product::getByName('A Bend in the River November Issue import copy');
243  $this->assertEquals(1, count($products));
244  $this->assertEquals('super', $products[0]->owner->username);
245  $this->assertEquals('A Bend in the River November Issue import copy', $products[0]->name);
246  $this->assertEquals(6, $products[0]->quantity);
247  $this->assertEquals('sampleAccount', $products[0]->account->name);
248  $this->assertEquals('Open', $products[0]->stage->value);
249  $this->assertEquals('Test Desc 1', $products[0]->description);
250  $this->assertEquals(210, $products[0]->sellPrice->value);
251  $this->assertEquals(2, $products[0]->priceFrequency);
252  $this->assertEquals(2, $products[0]->type);
253  //$this->assertEquals('My Contact', $products[0]->contact->firstName);
254 
255  //Confirm that 2 rows were processed as 'updated'.
256  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
257  . ImportRowDataResultsUtil::UPDATED));
258 
259  //Confirm 2 rows were processed as 'errors'.
260  $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = "
261  . ImportRowDataResultsUtil::ERROR));
262 
263  $beansWithErrors = ImportDatabaseUtil::getSubset($import->getTempTableName(), "status = "
264  . ImportRowDataResultsUtil::ERROR);
265  $this->assertEquals(0, count($beansWithErrors));
266  }
267  }
268 ?>
static makeCurrencyColumnMappingData($attributeName, $currency, $defaultValue=null, $rateToBase=1)
static makeImportRulesByType($importRulesType)
testSimpleUserImportWithRelationsWhereAllRowsSucceed()
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 getByName($name)
Definition: Product.php:45
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null, $buildFirstCurrency=true)
Definition: Currency.php:107
static getSubset($tableName, $where=null, $count=null, $offset=null)
static makeHasOneColumnMappingData($attributeName, $type=RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_ID)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
static makeTextAreaColumnMappingData($attributeName)
Generated on Thu Jul 9 2020 07:10:34
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.