public function testSimpleUserImportWhereAllRowsSucceed() { Yii::app()->user->userModel = User::getByUsername('super'); $import = new Import(); $serializedData['importRulesType'] = 'ProductTemplates'; $serializedData['firstRowIsHeaderRow'] = true; $import->serializedData = serialize($serializedData); $this->assertTrue($import->save()); ImportTestHelper::createTempTableByFileNameAndTableName('productTemplates.csv', $import->getTempTableName(), Yii::getPathOfAlias('application.modules.productTemplates.tests.unit.files')); $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows. $currencies = Currency::getAll(); $mappingData = array('column_0' => ImportMappingUtil::makeStringColumnMappingData('name'), 'column_1' => ImportMappingUtil::makeTextAreaColumnMappingData('description'), 'column_2' => ImportMappingUtil::makeIntegerColumnMappingData('sellPriceFormula__type'), 'column_3' => ImportMappingUtil::makeFloatColumnMappingData('sellPriceFormula__discountOrMarkupPercentage'), 'column_4' => ImportMappingUtil::makeCurrencyColumnMappingData('cost', $currencies[0]), 'column_5' => ImportMappingUtil::makeCurrencyColumnMappingData('listPrice', $currencies[0]), 'column_6' => ImportMappingUtil::makeCurrencyColumnMappingData('sellPrice', $currencies[0]), 'column_7' => ImportMappingUtil::makeIntegerColumnMappingData('priceFrequency'), 'column_8' => ImportMappingUtil::makeIntegerColumnMappingData('type'), 'column_9' => ImportMappingUtil::makeIntegerColumnMappingData('status')); $importRules = ImportRulesUtil::makeImportRulesByType('ProductTemplates'); $page = 0; $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed. $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config); $dataProvider->getPagination()->setCurrentPage($page); $importResultsUtil = new ImportResultsUtil($import); $messageLogger = new ImportMessageLogger(); ImportUtil::importByDataProvider($dataProvider, $importRules, $mappingData, $importResultsUtil, new ExplicitReadWriteModelPermissions(), $messageLogger); $importResultsUtil->processStatusAndMessagesForEachRow(); //Confirm that 3 models where created. $productTemplates = ProductTemplate::getAll(); $this->assertEquals(2, count($productTemplates)); $productTemplates = ProductTemplate::getByName('A Gift of Monotheists import'); $this->assertEquals(1, count($productTemplates[0])); $this->assertEquals('A Gift of Monotheists import', $productTemplates[0]->name); $this->assertEquals(2, $productTemplates[0]->sellPriceFormula->type); $this->assertEquals(10, $productTemplates[0]->sellPriceFormula->discountOrMarkupPercentage); $this->assertEquals(180, $productTemplates[0]->sellPrice->value); $this->assertEquals(200, $productTemplates[0]->listPrice->value); $this->assertEquals(200, $productTemplates[0]->cost->value); $this->assertEquals(2, $productTemplates[0]->priceFrequency); $this->assertEquals(2, $productTemplates[0]->type); $this->assertEquals(2, $productTemplates[0]->status); $productTemplates[0]->delete(); $productTemplates = ProductTemplate::getByName('A Gift of Monotheists import Copy'); $this->assertEquals(1, count($productTemplates[0])); $this->assertEquals('A Gift of Monotheists import Copy', $productTemplates[0]->name); $this->assertEquals(2, $productTemplates[0]->sellPriceFormula->type); $this->assertEquals(5, $productTemplates[0]->sellPriceFormula->discountOrMarkupPercentage); $this->assertEquals(180, $productTemplates[0]->sellPrice->value); $this->assertEquals(200, $productTemplates[0]->listPrice->value); $this->assertEquals(200, $productTemplates[0]->cost->value); $this->assertEquals(2, $productTemplates[0]->priceFrequency); $this->assertEquals(2, $productTemplates[0]->type); $this->assertEquals(2, $productTemplates[0]->status); $productTemplates[0]->delete(); //Confirm that 2 rows were processed as 'updated'. $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::UPDATED)); //Confirm 2 rows were processed as 'errors'. $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::ERROR)); $beansWithErrors = ImportDatabaseUtil::getSubset($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::ERROR); $this->assertEquals(0, count($beansWithErrors)); }
/** * @depends testSimpleUserImportWhereAllRowsSucceed */ public function testSimpleUserImportWithRelationsWhereAllRowsSucceed() { Yii::app()->user->userModel = User::getByUsername('super'); //Create account $sampleAccount = Account::getByName('sampleAccount'); $accountId = $sampleAccount[0]->id; //Create Contact $contact = ContactTestHelper::createContactByNameForOwner("My Contact", Yii::app()->user->userModel); $import = new Import(); $serializedData['importRulesType'] = 'Products'; $serializedData['firstRowIsHeaderRow'] = true; $import->serializedData = serialize($serializedData); $this->assertTrue($import->save()); ImportTestHelper::createTempTableByFileNameAndTableName('productsSampleWithRelations.csv', $import->getTempTableName(), true, Yii::getPathOfAlias('application.modules.products.tests.unit.files')); //update the ids of the account column to match the parent account. ZurmoRedBean::exec("update " . $import->getTempTableName() . " set column_9 = " . $contact->id . " where id != 1 limit 3"); $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows. $currencies = Currency::getAll(); $ownerColumnMappingData = array('attributeIndexOrDerivedType' => 'owner', 'type' => 'importColumn', 'mappingRulesData' => array('DefaultModelNameIdMappingRuleForm' => array('defaultModelId' => null), 'UserValueTypeModelAttributeMappingRuleForm' => array('type' => UserValueTypeModelAttributeMappingRuleForm::ZURMO_USERNAME))); $mappingData = array('column_0' => $ownerColumnMappingData, 'column_1' => ImportMappingUtil::makeStringColumnMappingData('name'), 'column_2' => ImportMappingUtil::makeTextAreaColumnMappingData('description'), 'column_3' => ImportMappingUtil::makeIntegerColumnMappingData('quantity'), 'column_4' => ImportMappingUtil::makeHasOneColumnMappingData('account', RelatedModelValueTypeMappingRuleForm::ZURMO_MODEL_NAME), 'column_5' => ImportMappingUtil::makeStringColumnMappingData('stage'), 'column_6' => ImportMappingUtil::makeCurrencyColumnMappingData('sellPrice', $currencies[0]), 'column_7' => ImportMappingUtil::makeIntegerColumnMappingData('priceFrequency'), 'column_8' => ImportMappingUtil::makeIntegerColumnMappingData('type')); $importRules = ImportRulesUtil::makeImportRulesByType('Products'); $page = 0; $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed. $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config); $dataProvider->getPagination()->setCurrentPage($page); $importResultsUtil = new ImportResultsUtil($import); $messageLogger = new ImportMessageLogger(); ImportUtil::importByDataProvider($dataProvider, $importRules, $mappingData, $importResultsUtil, new ExplicitReadWriteModelPermissions(), $messageLogger); $importResultsUtil->processStatusAndMessagesForEachRow(); //Confirm that 3 models where created. $products = Product::getAll(); $this->assertEquals(2, count($products)); $products = Product::getByName('A Bend in the River November Issue import'); $this->assertEquals(1, count($products)); $this->assertEquals('super', $products[0]->owner->username); $this->assertEquals('A Bend in the River November Issue import', $products[0]->name); $this->assertEquals(6, $products[0]->quantity); $this->assertEquals('sampleAccount', $products[0]->account->name); $this->assertEquals('Open', $products[0]->stage->value); $this->assertEquals('Test Desc', $products[0]->description); $this->assertEquals(210, $products[0]->sellPrice->value); $this->assertEquals(2, $products[0]->priceFrequency); $this->assertEquals(2, $products[0]->type); //$this->assertEquals('My Contact', $products[0]->contact->firstName); $products = Product::getByName('A Bend in the River November Issue import copy'); $this->assertEquals(1, count($products)); $this->assertEquals('super', $products[0]->owner->username); $this->assertEquals('A Bend in the River November Issue import copy', $products[0]->name); $this->assertEquals(6, $products[0]->quantity); $this->assertEquals('sampleAccount', $products[0]->account->name); $this->assertEquals('Open', $products[0]->stage->value); $this->assertEquals('Test Desc 1', $products[0]->description); $this->assertEquals(210, $products[0]->sellPrice->value); $this->assertEquals(2, $products[0]->priceFrequency); $this->assertEquals(2, $products[0]->type); //$this->assertEquals('My Contact', $products[0]->contact->firstName); //Confirm that 2 rows were processed as 'updated'. $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::UPDATED)); //Confirm 2 rows were processed as 'errors'. $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::ERROR)); $beansWithErrors = ImportDatabaseUtil::getSubset($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::ERROR); $this->assertEquals(0, count($beansWithErrors)); }
/** * Process csv and get data * @param array $mappingData * @param array $data */ protected function processSantizerCSVAndGetData(&$mappingData, &$data) { Yii::app()->user->userModel = User::getByUsername('super'); $import = new Import(); $serializedData['importRulesType'] = 'ProductTemplates'; $serializedData['firstRowIsHeaderRow'] = true; $import->serializedData = serialize($serializedData); $this->assertTrue($import->save()); ImportTestHelper::createTempTableByFileNameAndTableName('productTemplatesSanitizer.csv', $import->getTempTableName(), true, Yii::getPathOfAlias('application.modules.productTemplates.tests.unit.files')); $this->assertEquals(5, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows. $currencies = Currency::getAll(); $mappingData = array('column_0' => ImportMappingUtil::makeStringColumnMappingData('name'), 'column_1' => ImportMappingUtil::makeTextAreaColumnMappingData('description'), 'column_2' => ImportMappingUtil::makeIntegerColumnMappingData('sellPriceFormula__type'), 'column_3' => ImportMappingUtil::makeFloatColumnMappingData('sellPriceFormula__discountOrMarkupPercentage'), 'column_4' => ImportMappingUtil::makeCurrencyColumnMappingData('cost', $currencies[0]), 'column_5' => ImportMappingUtil::makeCurrencyColumnMappingData('listPrice', $currencies[0]), 'column_6' => ImportMappingUtil::makeCurrencyColumnMappingData('sellPrice', $currencies[0]), 'column_7' => ImportMappingUtil::makeIntegerColumnMappingData('priceFrequency'), 'column_8' => ImportMappingUtil::makeIntegerColumnMappingData('type'), 'column_9' => ImportMappingUtil::makeIntegerColumnMappingData('status')); $importRules = ImportRulesUtil::makeImportRulesByType('ProductTemplates'); $page = 0; $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed. $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config); $dataProvider->getPagination()->setCurrentPage($page); $data = $dataProvider->getData(true); }
/** * There is a special way you can import rateToBase and currencyCode for an amount attribute. * if the column data is formatted like: $54.67__1.2__USD then it will split the column and properly * handle rate and currency code. Eventually this will be exposed in the user interface */ public function testImportWithRateAndCurrencyCodeSpecified() { Yii::app()->user->userModel = User::getByUsername('super'); $account = AccountTestHelper::createAccountByNameForOwner('Account', Yii::app()->user->userModel); $accountId = $account->id; $contracts = Contract::getAll(); $this->assertEquals(0, count($contracts)); $import = new Import(); $serializedData['importRulesType'] = 'Contracts'; $serializedData['firstRowIsHeaderRow'] = true; $import->serializedData = serialize($serializedData); $this->assertTrue($import->save()); ImportTestHelper::createTempTableByFileNameAndTableName('importTestIncludingRateAndCurrencyCode.csv', $import->getTempTableName(), true, Yii::getPathOfAlias('application.modules.contracts.tests.unit.files')); //update the ids of the account column to match the parent account. ZurmoRedBean::exec("update " . $import->getTempTableName() . " set column_3 = " . $account->id . " where id != 1 limit 4"); $this->assertEquals(4, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows. $currency = Currency::getByCode(Yii::app()->currencyHelper->getBaseCode()); $mappingData = array('column_0' => ImportMappingUtil::makeStringColumnMappingData('name'), 'column_1' => ImportMappingUtil::makeDateColumnMappingData('closeDate'), 'column_2' => ImportMappingUtil::makeIntegerColumnMappingData('description'), 'column_3' => ImportMappingUtil::makeHasOneColumnMappingData('account'), 'column_4' => ImportMappingUtil::makeDropDownColumnMappingData('stage'), 'column_5' => ImportMappingUtil::makeDropDownColumnMappingData('source'), 'column_6' => ImportMappingUtil::makeCurrencyColumnMappingData('amount', $currency)); $importRules = ImportRulesUtil::makeImportRulesByType('Contracts'); $page = 0; $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed. $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config); $dataProvider->getPagination()->setCurrentPage($page); $importResultsUtil = new ImportResultsUtil($import); $messageLogger = new ImportMessageLogger(); ImportUtil::importByDataProvider($dataProvider, $importRules, $mappingData, $importResultsUtil, new ExplicitReadWriteModelPermissions(), $messageLogger); $importResultsUtil->processStatusAndMessagesForEachRow(); //Confirm that 3 models where created. $contracts = Contract::getAll(); $this->assertEquals(3, count($contracts)); $contracts = Contract::getByName('opp1'); $this->assertEquals(1, count($contracts[0])); $this->assertEquals('opp1', $contracts[0]->name); $this->assertEquals('1980-06-03', $contracts[0]->closeDate); $this->assertEquals(10, $contracts[0]->probability); $this->assertEquals('desc1', $contracts[0]->description); $this->assertTrue($contracts[0]->account->isSame($account)); $this->assertEquals('Prospecting', $contracts[0]->stage->value); $this->assertEquals('Self-Generated', $contracts[0]->source->value); $this->assertEquals(500, $contracts[0]->amount->value); $this->assertEquals(1, $contracts[0]->amount->rateToBase); $this->assertEquals('USD', $contracts[0]->amount->currency->code); $contracts = Contract::getByName('opp2'); $this->assertEquals(1, count($contracts[0])); $this->assertEquals('opp2', $contracts[0]->name); $this->assertEquals('1980-06-04', $contracts[0]->closeDate); $this->assertEquals(25, $contracts[0]->probability); $this->assertEquals('desc2', $contracts[0]->description); $this->assertTrue($contracts[0]->account->isSame($account)); $this->assertEquals('Qualification', $contracts[0]->stage->value); $this->assertEquals('Inbound Call', $contracts[0]->source->value); $this->assertEquals(501, $contracts[0]->amount->value); // $this->assertEquals(2.7, $contracts[0]->amount->rateToBase); $this->assertEquals('GBP', $contracts[0]->amount->currency->code); $contracts = Contract::getByName('opp3'); $this->assertEquals(1, count($contracts[0])); $this->assertEquals('opp3', $contracts[0]->name); $this->assertEquals('1980-06-05', $contracts[0]->closeDate); $this->assertEquals(50, $contracts[0]->probability); $this->assertEquals('desc3', $contracts[0]->description); $this->assertTrue($contracts[0]->account->isSame($account)); $this->assertEquals('Negotiating', $contracts[0]->stage->value); $this->assertEquals('Tradeshow', $contracts[0]->source->value); $this->assertEquals(502, $contracts[0]->amount->value); // $this->assertEquals(3.2, $contracts[0]->amount->rateToBase); $this->assertEquals('EUR', $contracts[0]->amount->currency->code); //Confirm 10 rows were processed as 'created'. $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::CREATED)); //Confirm that 0 rows were processed as 'updated'. $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::UPDATED)); //Confirm 2 rows were processed as 'errors'. $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::ERROR)); $beansWithErrors = ImportDatabaseUtil::getSubset($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::ERROR); $this->assertEquals(0, count($beansWithErrors)); //test the account has 3 contracts $account->forget(); $account = Account::getById($accountId); $this->assertEquals(3, $account->contracts->count()); }
/** * @depends testParentAccountHasCorrectAttributeImportType */ public function testSimpleUserImportWhereAllRowsSucceed() { Yii::app()->user->userModel = User::getByUsername('super'); $parentAccount = AccountTestHelper::createAccountByNameForOwner('parentAccount', Yii::app()->user->userModel); $parentAccountId = $parentAccount->id; $accounts = Account::getAll(); $this->assertEquals(1, count($accounts)); $import = new Import(); $serializedData['importRulesType'] = 'Accounts'; $serializedData['firstRowIsHeaderRow'] = true; $import->serializedData = serialize($serializedData); $this->assertTrue($import->save()); ImportTestHelper::createTempTableByFileNameAndTableName('importTest.csv', $import->getTempTableName(), Yii::getPathOfAlias('application.modules.accounts.tests.unit.files')); //update the ids of the account column to match the parent account. R::exec("update " . $import->getTempTableName() . " set column_16 = " . $parentAccount->id . " where id != 1 limit 4"); $this->assertEquals(4, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows. $mappingData = array('column_0' => ImportMappingUtil::makeStringColumnMappingData('name'), 'column_1' => ImportMappingUtil::makeStringColumnMappingData('officePhone'), 'column_2' => ImportMappingUtil::makeStringColumnMappingData('officeFax'), 'column_3' => ImportMappingUtil::makeIntegerColumnMappingData('employees'), 'column_4' => ImportMappingUtil::makeUrlColumnMappingData('website'), 'column_5' => ImportMappingUtil::makeFloatColumnMappingData('annualRevenue'), 'column_6' => ImportMappingUtil::makeTextAreaColumnMappingData('description'), 'column_7' => ImportMappingUtil::makeStringColumnMappingData('billingAddress__city'), 'column_8' => ImportMappingUtil::makeStringColumnMappingData('billingAddress__country'), 'column_9' => ImportMappingUtil::makeStringColumnMappingData('billingAddress__postalCode'), 'column_10' => ImportMappingUtil::makeStringColumnMappingData('billingAddress__state'), 'column_11' => ImportMappingUtil::makeStringColumnMappingData('billingAddress__street1'), 'column_12' => ImportMappingUtil::makeStringColumnMappingData('billingAddress__street2'), 'column_13' => ImportMappingUtil::makeEmailColumnMappingData('primaryEmail__emailAddress'), 'column_14' => ImportMappingUtil::makeBooleanColumnMappingData('primaryEmail__isInvalid'), 'column_15' => ImportMappingUtil::makeBooleanColumnMappingData('primaryEmail__optOut'), 'column_16' => ImportMappingUtil::makeHasOneColumnMappingData('account'), 'column_17' => ImportMappingUtil::makeDropDownColumnMappingData('industry'), 'column_18' => ImportMappingUtil::makeDropDownColumnMappingData('type')); $importRules = ImportRulesUtil::makeImportRulesByType('Accounts'); $page = 0; $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed. $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config); $dataProvider->getPagination()->setCurrentPage($page); $importResultsUtil = new ImportResultsUtil($import); $messageLogger = new ImportMessageLogger(); ImportUtil::importByDataProvider($dataProvider, $importRules, $mappingData, $importResultsUtil, new ExplicitReadWriteModelPermissions(), $messageLogger); $importResultsUtil->processStatusAndMessagesForEachRow(); //Confirm that 3 models where created. $accounts = Account::getAll(); $this->assertEquals(4, count($accounts)); $accounts = Account::getByName('account1'); $this->assertEquals(1, count($accounts[0])); $this->assertEquals(123456, $accounts[0]->officePhone); $this->assertEquals(555, $accounts[0]->officeFax); $this->assertEquals(1, $accounts[0]->employees); $this->assertEquals('http://www.account1.com', $accounts[0]->website); $this->assertEquals(100, $accounts[0]->annualRevenue); $this->assertEquals('desc1', $accounts[0]->description); $this->assertEquals('city1', $accounts[0]->billingAddress->city); $this->assertEquals('country1', $accounts[0]->billingAddress->country); $this->assertEquals('postal1', $accounts[0]->billingAddress->postalCode); $this->assertEquals('state1', $accounts[0]->billingAddress->state); $this->assertEquals('street11', $accounts[0]->billingAddress->street1); $this->assertEquals('street21', $accounts[0]->billingAddress->street2); $this->assertEquals('*****@*****.**', $accounts[0]->primaryEmail->emailAddress); $this->assertEquals(null, $accounts[0]->primaryEmail->isInvalid); $this->assertEquals(null, $accounts[0]->primaryEmail->optOut); $this->assertTrue($accounts[0]->account->isSame($parentAccount)); $this->assertEquals('Automotive', $accounts[0]->industry->value); $this->assertEquals('Prospect', $accounts[0]->type->value); $accounts = Account::getByName('account2'); $this->assertEquals(1, count($accounts[0])); $this->assertEquals(223456, $accounts[0]->officePhone); $this->assertEquals(666, $accounts[0]->officeFax); $this->assertEquals(2, $accounts[0]->employees); $this->assertEquals('http://www.account2.com', $accounts[0]->website); $this->assertEquals(200, $accounts[0]->annualRevenue); $this->assertEquals('desc2', $accounts[0]->description); $this->assertEquals('city2', $accounts[0]->billingAddress->city); $this->assertEquals('country2', $accounts[0]->billingAddress->country); $this->assertEquals('postal2', $accounts[0]->billingAddress->postalCode); $this->assertEquals('state2', $accounts[0]->billingAddress->state); $this->assertEquals('street12', $accounts[0]->billingAddress->street1); $this->assertEquals('street22', $accounts[0]->billingAddress->street2); $this->assertEquals('*****@*****.**', $accounts[0]->primaryEmail->emailAddress); $this->assertEquals('1', $accounts[0]->primaryEmail->isInvalid); $this->assertEquals('1', $accounts[0]->primaryEmail->optOut); $this->assertTrue($accounts[0]->account->isSame($parentAccount)); $this->assertEquals('Banking', $accounts[0]->industry->value); $this->assertEquals('Customer', $accounts[0]->type->value); $accounts = Account::getByName('account3'); $this->assertEquals(1, count($accounts[0])); $this->assertEquals(323456, $accounts[0]->officePhone); $this->assertEquals(777, $accounts[0]->officeFax); $this->assertEquals(3, $accounts[0]->employees); $this->assertEquals('http://www.account3.com', $accounts[0]->website); $this->assertEquals(300, $accounts[0]->annualRevenue); $this->assertEquals('desc3', $accounts[0]->description); $this->assertEquals('city3', $accounts[0]->billingAddress->city); $this->assertEquals('country3', $accounts[0]->billingAddress->country); $this->assertEquals('postal3', $accounts[0]->billingAddress->postalCode); $this->assertEquals('state3', $accounts[0]->billingAddress->state); $this->assertEquals('street13', $accounts[0]->billingAddress->street1); $this->assertEquals('street23', $accounts[0]->billingAddress->street2); $this->assertEquals('*****@*****.**', $accounts[0]->primaryEmail->emailAddress); $this->assertEquals(null, $accounts[0]->primaryEmail->isInvalid); $this->assertEquals(null, $accounts[0]->primaryEmail->optOut); $this->assertTrue($accounts[0]->account->isSame($parentAccount)); $this->assertEquals('Energy', $accounts[0]->industry->value); $this->assertEquals('Vendor', $accounts[0]->type->value); //Confirm 10 rows were processed as 'created'. $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::CREATED)); //Confirm that 0 rows were processed as 'updated'. $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::UPDATED)); //Confirm 2 rows were processed as 'errors'. $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::ERROR)); $beansWithErrors = ImportDatabaseUtil::getSubset($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::ERROR); $this->assertEquals(0, count($beansWithErrors)); //test the parent account has 3 children $parentAccount->forget(); $parentAccount = Account::getById($parentAccountId); $this->assertEquals(3, $parentAccount->accounts->count()); }
public function testSimpleUserImportWhereAllRowsSucceed() { Yii::app()->user->userModel = User::getByUsername('super'); $tasks = Task::getAll(); $this->assertEquals(0, count($tasks)); $import = new Import(); $serializedData['importRulesType'] = 'Tasks'; $serializedData['firstRowIsHeaderRow'] = true; $import->serializedData = serialize($serializedData); $this->assertTrue($import->save()); ImportTestHelper::createTempTableByFileNameAndTableName('simpleImportTest.csv', $import->getTempTableName(), true, Yii::getPathOfAlias('application.modules.tasks.tests.unit.files')); $this->assertEquals(4, ImportDatabaseUtil::getCount($import->getTempTableName())); // includes header rows. $mappingData = array('column_0' => ImportMappingUtil::makeStringColumnMappingData('name'), 'column_1' => ImportMappingUtil::makeDateTimeColumnMappingData('dueDateTime'), 'column_2' => ImportMappingUtil::makeDateTimeColumnMappingData('completedDateTime'), 'column_3' => ImportMappingUtil::makeIntegerColumnMappingData('status'), 'column_4' => ImportMappingUtil::makeModelDerivedColumnMappingData('AccountDerived'), 'column_5' => ImportMappingUtil::makeModelDerivedColumnMappingData('ContactDerived'), 'column_6' => ImportMappingUtil::makeModelDerivedColumnMappingData('OpportunityDerived'), 'column_7' => ImportMappingUtil::makeTextAreaColumnMappingData('description')); $importRules = ImportRulesUtil::makeImportRulesByType('Tasks'); $page = 0; $config = array('pagination' => array('pageSize' => 50)); //This way all rows are processed. $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config); $dataProvider->getPagination()->setCurrentPage($page); $importResultsUtil = new ImportResultsUtil($import); $actionDateTime = substr(DateTimeUtil::convertTimestampToDbFormatDateTime(time()), 0, -3); $messageLogger = new ImportMessageLogger(); ImportUtil::importByDataProvider($dataProvider, $importRules, $mappingData, $importResultsUtil, new ExplicitReadWriteModelPermissions(), $messageLogger); $importResultsUtil->processStatusAndMessagesForEachRow(); //Confirm that 3 models where created. $tasks = Task::getAll(); $this->assertEquals(3, count($tasks)); $tasks = Task::getByName('task1'); $this->assertEquals(1, count($tasks[0])); $this->assertEquals(1, count($tasks[0]->activityItems)); $this->assertEquals('testAccount', $tasks[0]->activityItems[0]->name); $this->assertEquals('Account', get_class($tasks[0]->activityItems[0])); $this->assertFalse((bool) $tasks[0]->completed); $this->assertEquals(Task::STATUS_NEW, $tasks[0]->status); $this->assertEquals($actionDateTime, substr($tasks[0]->latestDateTime, 0, -3)); $tasks = Task::getByName('task2'); $this->assertEquals(1, count($tasks[0])); $this->assertEquals(1, count($tasks[0]->activityItems)); $this->assertEquals('testContact', $tasks[0]->activityItems[0]->firstName); $this->assertEquals('Contact', get_class($tasks[0]->activityItems[0])); $this->assertTrue((bool) $tasks[0]->completed); $this->assertEquals(Task::STATUS_COMPLETED, $tasks[0]->status); $this->assertEquals('2011-12-22 06:03', substr($tasks[0]->latestDateTime, 0, -3)); $tasks = Task::getByName('task3'); $this->assertEquals(1, count($tasks[0])); $this->assertEquals(1, count($tasks[0]->activityItems)); $this->assertEquals('testOpportunity', $tasks[0]->activityItems[0]->name); $this->assertEquals('Opportunity', get_class($tasks[0]->activityItems[0])); $this->assertFalse((bool) $tasks[0]->completed); $this->assertEquals(Task::STATUS_IN_PROGRESS, $tasks[0]->status); //Not checking the minutes because sometimes it was failing for one minute $this->assertEquals(substr($actionDateTime, 0, -2), substr($tasks[0]->latestDateTime, 0, -5)); //Confirm 10 rows were processed as 'created'. $this->assertEquals(3, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::CREATED)); //Confirm that 0 rows were processed as 'updated'. $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::UPDATED)); //Confirm 2 rows were processed as 'errors'. $this->assertEquals(0, ImportDatabaseUtil::getCount($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::ERROR)); $beansWithErrors = ImportDatabaseUtil::getSubset($import->getTempTableName(), "status = " . ImportRowDataResultsUtil::ERROR); $this->assertEquals(0, count($beansWithErrors)); }
public function testImportDataAnalysisResults() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; $import = new Import(); $serializedData['importRulesType'] = 'Tasks'; $import->serializedData = serialize($serializedData); $this->assertTrue($import->save()); $account1 = AccountTestHelper::createAccountByNameForOwner('account1', $super); $account2 = AccountTestHelper::createAccountByNameForOwner('account2', $super); $account3 = AccountTestHelper::createAccountByNameForOwner('account3', $super); $contact1 = ContactTestHelper::createContactByNameForOwner('contact1', $super); $contact2 = ContactTestHelper::createContactByNameForOwner('contact2', $super); $contact3 = ContactTestHelper::createContactByNameForOwner('contact3', $super); $opportunity1 = OpportunityTestHelper::createOpportunityByNameForOwner('opportunity1', $super); $opportunity2 = OpportunityTestHelper::createOpportunityByNameForOwner('opportunity2', $super); $opportunity3 = OpportunityTestHelper::createOpportunityByNameForOwner('opportunity3', $super); //Make models externally linked for testing. ImportTestHelper::updateModelsExternalId($account2, 'ACC'); ImportTestHelper::updateModelsExternalId($contact2, 'CON'); ImportTestHelper::updateModelsExternalId($opportunity2, 'OPP'); ImportTestHelper::createTempTableByFileNameAndTableName('importAnalyzerTest.csv', $import->getTempTableName(), true, Yii::getPathOfAlias('application.modules.tasks.tests.unit.files')); ZurmoRedBean::exec("update " . $import->getTempTableName() . " set column_0 = " . $account3->id . " where id != 1 limit 3"); ZurmoRedBean::exec("update " . $import->getTempTableName() . " set column_2 = " . $contact3->id . " where id != 1 limit 4"); ZurmoRedBean::exec("update " . $import->getTempTableName() . " set column_4 = " . $opportunity3->id . " where id != 1 limit 5"); $mappingData = array('column_0' => ImportMappingUtil::makeModelDerivedColumnMappingData('AccountDerived', IdValueTypeMappingRuleForm::ZURMO_MODEL_ID), 'column_1' => ImportMappingUtil::makeModelDerivedColumnMappingData('AccountDerived'), 'column_2' => ImportMappingUtil::makeModelDerivedColumnMappingData('ContactDerived', IdValueTypeMappingRuleForm::ZURMO_MODEL_ID), 'column_3' => ImportMappingUtil::makeModelDerivedColumnMappingData('ContactDerived'), 'column_4' => ImportMappingUtil::makeModelDerivedColumnMappingData('OpportunityDerived', IdValueTypeMappingRuleForm::ZURMO_MODEL_ID), 'column_5' => ImportMappingUtil::makeModelDerivedColumnMappingData('OpportunityDerived'), 'column_6' => ImportMappingUtil::makeIntegerColumnMappingData('status')); $serializedData = unserialize($import->serializedData); $serializedData['mappingData'] = $mappingData; $import->serializedData = serialize($serializedData); $this->assertTrue($import->save()); $importRules = ImportRulesUtil::makeImportRulesByType('Tasks'); $config = array('pagination' => array('pageSize' => 15)); //This test csv has a header row. $dataProvider = new ImportDataProvider($import->getTempTableName(), true, $config); //Run data analyzer $importDataAnalyzer = new ImportDataAnalyzer($importRules, $dataProvider, $mappingData, array('column_0', 'column_1', 'column_2', 'column_3', 'column_4', 'column_5', 'column_6')); $importDataAnalyzer->analyzePage(); $data = $dataProvider->getData(); $this->assertEquals(10, count($data)); $compareData = array(); $compareData['column_0'][] = 'Is an existing record and will be updated.'; $compareData['column_1'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_2'][] = 'Is an existing record and will be updated.'; $compareData['column_3'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_4'][] = 'Is an existing record and will be updated.'; $compareData['column_5'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_6'][] = 'Status specified is invalid and this row will be skipped during import.'; $this->assertEquals($compareData, unserialize($data[0]->serializedAnalysisMessages)); $this->assertEquals(ImportDataAnalyzer::STATUS_SKIP, $data[0]->analysisStatus); $compareData = array(); $compareData['column_0'][] = 'Is an existing record and will be updated.'; $compareData['column_1'][] = 'Is an existing record and will be updated.'; $compareData['column_2'][] = 'Is an existing record and will be updated.'; $compareData['column_3'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_4'][] = 'Is an existing record and will be updated.'; $compareData['column_5'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_6'][] = 'Status specified is invalid and this row will be skipped during import.'; $this->assertEquals($compareData, unserialize($data[1]->serializedAnalysisMessages)); $this->assertEquals(ImportDataAnalyzer::STATUS_SKIP, $data[1]->analysisStatus); $compareData = array(); $compareData['column_0'][] = 'Is an existing record and will be updated.'; $compareData['column_1'][] = 'Is an existing record and will be updated.'; $compareData['column_2'][] = 'Is an existing record and will be updated.'; $compareData['column_3'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_4'][] = 'Is an existing record and will be updated.'; $compareData['column_5'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_6'][] = 'Status specified is invalid and this row will be skipped during import.'; $this->assertEquals($compareData, unserialize($data[2]->serializedAnalysisMessages)); $this->assertEquals(ImportDataAnalyzer::STATUS_SKIP, $data[2]->analysisStatus); $compareData = array(); $compareData['column_0'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_1'][] = 'Is an existing record and will be updated.'; $compareData['column_2'][] = 'Is an existing record and will be updated.'; $compareData['column_3'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_4'][] = 'Is an existing record and will be updated.'; $compareData['column_5'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_6'][] = 'Status specified is invalid and this row will be skipped during import.'; $this->assertEquals($compareData, unserialize($data[3]->serializedAnalysisMessages)); $this->assertEquals(ImportDataAnalyzer::STATUS_SKIP, $data[3]->analysisStatus); $compareData = array(); $compareData['column_0'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_1'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_2'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_3'][] = 'Is an existing record and will be updated.'; $compareData['column_4'][] = 'Is an existing record and will be updated.'; $compareData['column_5'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_6'][] = 'Status specified is invalid and this row will be skipped during import.'; $this->assertEquals($compareData, unserialize($data[4]->serializedAnalysisMessages)); $this->assertEquals(ImportDataAnalyzer::STATUS_SKIP, $data[4]->analysisStatus); $compareData = array(); $compareData['column_0'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_1'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_2'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_3'][] = 'Is an existing record and will be updated.'; $compareData['column_4'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_5'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_6'][] = 'Status specified is invalid and this row will be skipped during import.'; $this->assertEquals($compareData, unserialize($data[5]->serializedAnalysisMessages)); $this->assertEquals(ImportDataAnalyzer::STATUS_SKIP, $data[5]->analysisStatus); $compareData = array(); $compareData['column_0'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_1'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_2'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_3'][] = 'Is an existing record and will be updated.'; $compareData['column_4'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_6'][] = 'Status specified is invalid and this row will be skipped during import.'; $compareData['column_5'][] = 'Is an existing record and will be updated.'; $this->assertEquals($compareData, unserialize($data[6]->serializedAnalysisMessages)); $this->assertEquals(ImportDataAnalyzer::STATUS_SKIP, $data[6]->analysisStatus); $compareData = array(); $compareData['column_0'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_1'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_2'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_3'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_4'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_6'][] = 'Status specified is invalid and this row will be skipped during import.'; $compareData['column_5'][] = 'Is an existing record and will be updated.'; $this->assertEquals($compareData, unserialize($data[7]->serializedAnalysisMessages)); $this->assertEquals(ImportDataAnalyzer::STATUS_SKIP, $data[7]->analysisStatus); $compareData = array(); $compareData['column_0'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_1'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_2'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_3'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_4'][] = 'Was not found and this row will be skipped during import.'; $compareData['column_5'][] = 'Is an existing record and will be updated.'; $compareData['column_6'][] = 'Status specified is invalid and this row will be skipped during import.'; $this->assertEquals($compareData, unserialize($data[8]->serializedAnalysisMessages)); $this->assertEquals(ImportDataAnalyzer::STATUS_SKIP, $data[8]->analysisStatus); //Will result with no problems since it is all blank. $this->assertFalse(unserialize($data[9]->serializedAnalysisMessages)); $this->assertEquals(ImportDataAnalyzer::STATUS_CLEAN, $data[9]->analysisStatus); }