/**
  * @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));
 }
Example #2
0
 /**
  * @depends testGetProductsByName
  */
 public function testGetProductByNameForNonExistentName()
 {
     Yii::app()->user->userModel = User::getByUsername('super');
     $products = Product::getByName('Red Widget 1');
     $this->assertEquals(0, count($products));
 }
 public function testSuperUserCreateAction()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     Yii::app()->user->userModel = $super;
     $this->resetGetArray();
     $currency = new Currency();
     $currency->code = 'USD';
     $currency->rateToBase = 1;
     $currency->save();
     $currencyRec = Currency::getByCode('USD');
     $currencyValue1Array = array('currency' => array('id' => $currencyRec->id), 'value' => 500.54);
     ProductTemplateTestHelper::createProductTemplateByName("My Catalog Item 1");
     $productTemplate = ProductTemplate::getByName('My Catalog Item 1');
     $product = array();
     $product['name'] = 'Red Widget';
     $product['quantity'] = 5;
     $product['priceFrequency'] = ProductTemplate::PRICE_FREQUENCY_ONE_TIME;
     $product['sellPrice'] = $currencyValue1Array;
     $product['type'] = ProductTemplate::TYPE_PRODUCT;
     $product['stage']['value'] = Product::OPEN_STAGE;
     $product['productTemplate'] = array('id' => $productTemplate[0]->id);
     $this->setPostArray(array('Product' => $product, 'Product_owner_name' => 'Super User'));
     $redirectUrl = $this->runControllerWithRedirectExceptionAndGetUrl('products/default/create');
     $products = Product::getByName('Red Widget');
     $this->assertEquals(1, count($products));
     $this->assertTrue($products[0]->id > 0);
     $this->assertEquals(500.54, $products[0]->sellPrice->value);
     $compareRedirectUrl = Yii::app()->createUrl('products/default/details', array('id' => $products[0]->id));
     $this->assertEquals($compareRedirectUrl, $redirectUrl);
 }
 public function testDeleteProductTemplate()
 {
     Yii::app()->user->userModel = User::getByUsername('super');
     $productTemplates = ProductTemplate::getAll();
     $this->assertEquals(4, count($productTemplates));
     $productTemplates[0]->delete();
     $productTemplates = ProductTemplate::getAll();
     $this->assertEquals(3, count($productTemplates));
     $products = Product::getByName('Product 1');
     $this->assertFalse($productTemplates[0]->delete());
     $productTemplates[0]->products->remove($products[0]);
     $this->assertTrue($productTemplates[0]->delete());
     $productTemplates = ProductTemplate::getAll();
     $this->assertEquals(2, count($productTemplates));
     $products = Product::getByName('Product 2');
     $productTemplates[0]->products->remove($products[0]);
     $productTemplates[0]->delete();
     $productTemplates = ProductTemplate::getAll();
     $this->assertEquals(1, count($productTemplates));
     $productTemplates[0]->delete();
 }
 /**
  * @depends testWhetherSearchWorksForTheCustomFieldsPlacedForProductsModuleAfterEditingTheProduct
  */
 public function testDeleteOfTheProductUserForTheCustomFieldsPlacedForProductsModule()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     //Get the product id from the recently edited product.
     $productId = self::getModelIdByModelNameAndName('Product', 'myEditProduct');
     //Set the product id so as to delete the product.
     $this->setGetArray(array('id' => $productId));
     $this->runControllerWithRedirectExceptionAndGetUrl('products/default/delete');
     //Check wether the product is deleted.
     $product = Product::getByName('myEditProduct');
     $this->assertEquals(0, count($product));
 }
 public function testSuperUserCreateFromRelationAction()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $products = Product::getAll();
     $productsCount = count($products);
     $accountId = self::getModelIdByModelNameAndName('Account', 'superAccount');
     $contactId = self::getModelIdByModelNameAndName('Contact', 'superContact superContactson');
     $opportunityId = self::getModelIdByModelNameAndName('Opportunity', 'superOpportunity');
     //Create a new product from a related account.
     $this->setGetArray(array('relationAttributeName' => 'account', 'relationModelId' => $accountId, 'relationModuleId' => 'accounts', 'redirectUrl' => 'someRedirect'));
     $currency = new Currency();
     $currency->code = 'USD';
     $currency->rateToBase = 1;
     $currency->save();
     $currencyRec = Currency::getByCode('USD');
     $currencyValue1Array = array('currency' => array('id' => $currencyRec->id), 'value' => 500.54);
     $product['name'] = 'myUltraNewProduct';
     $product['quantity'] = 50;
     $product['priceFrequency'] = ProductTemplate::PRICE_FREQUENCY_ONE_TIME;
     $product['sellPrice'] = $currencyValue1Array;
     $product['type'] = ProductTemplate::TYPE_PRODUCT;
     $product['stage']['value'] = Product::OPEN_STAGE;
     $this->setPostArray(array('Product' => $product));
     $this->runControllerWithRedirectExceptionAndGetContent('products/default/createFromRelation');
     $products = Product::getByName('myUltraNewProduct');
     $this->assertEquals(1, count($products));
     $this->assertTrue($products[0]->id > 0);
     $this->assertTrue($products[0]->owner == $super);
     $this->assertEquals($accountId, $products[0]->account->id);
     $this->assertEquals('50', $products[0]->quantity);
     $this->assertEquals('500.54', $products[0]->sellPrice->value);
     $this->assertEquals(ProductTemplate::PRICE_FREQUENCY_ONE_TIME, $products[0]->priceFrequency);
     $this->assertEquals(ProductTemplate::TYPE_PRODUCT, $products[0]->type);
     $this->assertEquals(Product::OPEN_STAGE, $products[0]->stage->value);
     $products = Product::getAll();
     $this->assertEquals(++$productsCount, count($products));
     //Create a new product from a related contact.
     $this->setGetArray(array('relationAttributeName' => 'contact', 'relationModelId' => $contactId, 'relationModuleId' => 'contacts', 'redirectUrl' => 'someRedirect'));
     $product['name'] = 'myUltraNewProduct2';
     $product['quantity'] = 51;
     $product['priceFrequency'] = ProductTemplate::PRICE_FREQUENCY_ANNUALLY;
     $product['sellPrice'] = $currencyValue1Array;
     $product['type'] = ProductTemplate::TYPE_SERVICE;
     $product['stage']['value'] = Product::OPEN_STAGE;
     $this->setPostArray(array('Product' => $product));
     $this->runControllerWithRedirectExceptionAndGetContent('products/default/createFromRelation');
     $products = Product::getByName('myUltraNewProduct2');
     $this->assertEquals(1, count($products));
     $this->assertTrue($products[0]->id > 0);
     $this->assertTrue($products[0]->owner == $super);
     $this->assertEquals($accountId, $products[0]->account->id);
     $this->assertEquals($contactId, $products[0]->contact->id);
     $this->assertEquals('51', $products[0]->quantity);
     $this->assertEquals('500.54', $products[0]->sellPrice->value);
     $this->assertEquals(ProductTemplate::PRICE_FREQUENCY_ANNUALLY, $products[0]->priceFrequency);
     $this->assertEquals(ProductTemplate::TYPE_SERVICE, $products[0]->type);
     $this->assertEquals(Product::OPEN_STAGE, $products[0]->stage->value);
     $products = Product::getAll();
     $this->assertEquals(++$productsCount, count($products));
     //Create a new product from a related opportunity.
     $this->setGetArray(array('relationAttributeName' => 'opportunity', 'relationModelId' => $opportunityId, 'relationModuleId' => 'opportunities', 'redirectUrl' => 'someRedirect'));
     $product['name'] = 'myUltraNewProduct3';
     $product['quantity'] = 51;
     $product['priceFrequency'] = ProductTemplate::PRICE_FREQUENCY_ANNUALLY;
     $product['sellPrice'] = $currencyValue1Array;
     $product['type'] = ProductTemplate::TYPE_SERVICE;
     $product['stage']['value'] = Product::OPEN_STAGE;
     $this->setPostArray(array('Product' => $product));
     $this->runControllerWithRedirectExceptionAndGetContent('products/default/createFromRelation');
     $products = Product::getByName('myUltraNewProduct3');
     $this->assertEquals(1, count($products));
     $this->assertTrue($products[0]->id > 0);
     $this->assertTrue($products[0]->owner == $super);
     $this->assertEquals($accountId, $products[0]->account->id);
     $this->assertEquals($opportunityId, $products[0]->opportunity->id);
     $this->assertEquals('51', $products[0]->quantity);
     $this->assertEquals('500.54', $products[0]->sellPrice->value);
     $this->assertEquals(ProductTemplate::PRICE_FREQUENCY_ANNUALLY, $products[0]->priceFrequency);
     $this->assertEquals(ProductTemplate::TYPE_SERVICE, $products[0]->type);
     $this->assertEquals(Product::OPEN_STAGE, $products[0]->stage->value);
     $products = Product::getAll();
     $this->assertEquals(++$productsCount, count($products));
 }