/** * Assert configurable product on Frontend * * @param ConfigurableProduct $product * @return void */ protected function assertOnFrontend(ConfigurableProduct $product) { //Pages $frontendHomePage = Factory::getPageFactory()->getCmsIndexIndex(); $categoryPage = Factory::getPageFactory()->getCatalogCategoryView(); $productPage = Factory::getPageFactory()->getCatalogProductView(); //Steps $frontendHomePage->open(); $frontendHomePage->getTopmenu()->selectCategoryByName($product->getCategoryName()); //Verification on category product list $productListBlock = $categoryPage->getListProductBlock(); $this->assertTrue($productListBlock->isProductVisible($product->getName()), 'Product is absent on category page.'); //Verification on product detail page $productViewBlock = $productPage->getViewBlock(); $productListBlock->openProductViewPage($product->getName()); $this->assertEquals($product->getName(), $productViewBlock->getProductName(), 'Product name does not correspond to specified.'); $price = $product->getProductPrice(); $priceOnPage = $productViewBlock->getPriceBlock()->getPrice(); $this->assertEquals(number_format($price, 2), number_format($priceOnPage, 2), 'Product price does not correspond to specified.'); $pageOptions = $productViewBlock->getOptions($product); $configurableOptions = []; foreach ($pageOptions['configurable_options'] as $attribute) { $configurableOption = []; foreach ($attribute['options'] as $option) { $configurableOption[] = $option['title']; } $configurableOptions[$attribute['title']] = $configurableOption; } $this->assertEquals($product->getConfigurableOptions(), $configurableOptions); }
/** * Assert that deleted configurable attributes are absent on product page on frontend. * * @param CatalogProductAttribute[] $deletedProductAttributes * @param BrowserInterface $browser * @param CatalogProductView $catalogProductView * @param ConfigurableProduct $product * @return void */ public function processAssert(array $deletedProductAttributes, BrowserInterface $browser, CatalogProductView $catalogProductView, ConfigurableProduct $product) { $browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html'); $pageOptions = $catalogProductView->getViewBlock()->getOptions($product)['configurable_options']; foreach ($deletedProductAttributes as $attribute) { $attributeLabel = $attribute->getFrontendLabel(); \PHPUnit_Framework_Assert::assertFalse(isset($pageOptions[$attributeLabel]), "Configurable attribute '{$attributeLabel}' found on product page on frontend."); } }
/** * Assertion that the product is correctly displayed in cart * * @param BrowserInterface $browser * @param CatalogProductView $catalogProductView * @param CheckoutCart $checkoutCart * @param ConfigurableProduct $product * @return void */ public function processAssert(BrowserInterface $browser, CatalogProductView $catalogProductView, CheckoutCart $checkoutCart, ConfigurableProduct $product) { $browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html'); $catalogProductView->getViewBlock()->addToCart($product); $catalogProductView->getMessagesBlock()->waitSuccessMessage(); $checkoutCart->open(); $checkoutData = $product->getCheckoutData(); $price = $checkoutCart->getCartBlock()->getCartItem($product)->getPrice(); \PHPUnit_Framework_Assert::assertEquals($checkoutData['cartItem']['price'], $price, 'Product price in shopping cart is not correct.'); }
/** * Run Delete used in configurable product attribute test * * @param ConfigurableProduct $product * @return array */ public function test(ConfigurableProduct $product) { // Precondition $product->persist(); /** @var CatalogProductAttribute $attribute */ $attribute = $product->getDataFieldConfig('configurable_attributes_data')['source']->getAttributes()['attribute_key_0']; // Steps $this->attributeIndex->open(); $this->attributeIndex->getGrid()->searchAndOpen(['attribute_code' => $attribute->getAttributeCode()]); $this->attributeNew->getPageActions()->delete(); return ['attribute' => $attribute]; }
/** * Assert that child products generated during configurable product are present in products grid * * @param CatalogProductIndex $productGrid * @param ConfigurableProduct $product * @return void */ public function processAssert(CatalogProductIndex $productGrid, ConfigurableProduct $product) { $configurableAttributesData = $product->getConfigurableAttributesData(); $errors = []; $productGrid->open(); foreach ($configurableAttributesData['matrix'] as $variation) { $filter = ['name' => $variation['name'], 'type' => isset($variation['weight']) && (int) $variation['weight'] > 0 ? 'Simple Product' : 'Virtual Product', 'sku' => $variation['sku'], 'visibility' => self::NOT_VISIBLE_INDIVIDUALLY]; if (!$productGrid->getProductGrid()->isRowVisible($filter)) { $errors[] = sprintf('Child product with name: "%s" and sku:"%s" is absent in grid.', $filter['name'], $filter['sku']); } } \PHPUnit_Framework_Assert::assertEmpty($errors, implode(' ', $errors)); }
/** * Assert that products generated during configurable product creation - are not visible on frontend(by default). * * @param CatalogSearchResult $catalogSearchResult * @param CmsIndex $cmsIndex * @param ConfigurableProduct $product * @return void */ public function processAssert(CatalogsearchResult $catalogSearchResult, CmsIndex $cmsIndex, ConfigurableProduct $product) { $configurableAttributesData = $product->getConfigurableAttributesData(); $errors = []; $cmsIndex->open(); foreach ($configurableAttributesData['matrix'] as $variation) { $cmsIndex->getSearchBlock()->search($variation['sku']); $isVisibleProduct = $catalogSearchResult->getListProductBlock()->isProductVisible($variation['name']); while (!$isVisibleProduct && $catalogSearchResult->getBottomToolbar()->nextPage()) { $isVisibleProduct = $catalogSearchResult->getListProductBlock()->isProductVisible($product->getName()); } if ($isVisibleProduct) { $errors[] = sprintf("\nChild product with sku: \"%s\" is visible on frontend(by default).", $variation['sku']); } } \PHPUnit_Framework_Assert::assertEmpty($errors, implode(' ', $errors)); }
/** * Verify displayed product short description on product page(front-end) equals passed from fixture * * @return string|null */ protected function verifyShortDescription() { $fixtureProductShortDescription = $this->product->getShortDescription(); $formProductShortDescription = $this->productView->getProductShortDescription(); if ($fixtureProductShortDescription == $formProductShortDescription) { return null; } return "Displayed product short description on product page(front-end) not equals passed from fixture. " . "Actual: {$formProductShortDescription}, expected: {$fixtureProductShortDescription}."; }
/** * Update product. * * @param ConfigurableProduct $product * @return void */ protected function updateProduct(ConfigurableProduct $product) { //open product $filter = ['sku' => $this->initialProduct->getSku()]; $this->productGrid->open(); $this->productGrid->getProductGrid()->searchAndOpen($filter); //update $productForm = $this->catalogProductEdit->getProductForm(); $productForm->openSection('variations'); /** @var Config $variationsSection */ $variationsSection = $productForm->getSection('variations'); $variationsSection->deleteVariations(); $this->catalogProductEdit->getProductForm()->fill($product); }
/** * Prepare new product for update. * * @param ConfigurableProduct $initialProduct * @param ConfigurableProduct $product * @param string $attributeTypeAction * @return ConfigurableProduct */ protected function prepareProduct(ConfigurableProduct $initialProduct, ConfigurableProduct $product, $attributeTypeAction) { if ($attributeTypeAction == 'deleteAll') { $this->deletedAttributes = $initialProduct->getDataFieldConfig('configurable_attributes_data')['source']->getAttributes(); return $product; } $dataProduct = $product->getData(); $dataInitialProduct = $initialProduct->getData(); $oldMatrix = []; if ($attributeTypeAction == 'deleteLast') { array_pop($dataInitialProduct['configurable_attributes_data']['attributes_data']); $attributes = $initialProduct->getDataFieldConfig('configurable_attributes_data')['source']->getAttributes(); $this->deletedAttributes[] = array_pop($attributes); } $attributesData = $dataInitialProduct['configurable_attributes_data']['attributes_data']; if ($attributeTypeAction == 'addOptions') { $oldMatrix = $dataInitialProduct['configurable_attributes_data']['matrix']; $this->addOptions($attributesData, $dataProduct['configurable_attributes_data']['attributes_data']); } else { $this->addAttributes($attributesData, $dataProduct['configurable_attributes_data']['attributes_data']); } $dataProduct['configurable_attributes_data'] = ['attributes_data' => $attributesData, 'matrix' => $oldMatrix]; if ($product->hasData('category_ids')) { $dataProduct['category_ids']['category'] = $product->getDataFieldConfig('category_ids')['source']->getCategories()[0]; } return $this->fixtureFactory->createByCode('configurableProduct', ['data' => $dataProduct]); }
/** * Prepare data for configurable product. * * @param ConfigurableProduct $product * @return array */ protected function prepareConfigurableData(ConfigurableProduct $product) { $result = []; $checkoutData = $product->getCheckoutData(); $result['qty'] = $checkoutData['qty']; $attributesData = $product->hasData('configurable_attributes_data') ? $product->getDataFieldConfig('configurable_attributes_data')['source']->getAttributesData() : null; if ($attributesData == null) { return $result; } foreach ($checkoutData['options']['configurable_options'] as $option) { $attributeId = $attributesData[$option['title']]['attribute_id']; $optionId = $attributesData[$option['title']]['options'][$option['value']]['id']; $result['super_attribute'][$attributeId] = $optionId; } return $result; }
/** * Fill product variations and save product * * @param ConfigurableProduct $variations * @return void */ protected function fillProductVariationsAndSave(ConfigurableProduct $variations) { $variationsData = $variations->getData(); $matrix = []; foreach ($variationsData['fields']['variations-matrix']['value'] as $variation) { $matrix[] = ['quantity_and_stock_status' => ['qty' => $variation['value']['qty']['value']]]; } $createProductPage = Factory::getPageFactory()->getCatalogProductNew(); $productForm = $createProductPage->getProductForm(); $productForm->openTab('variations'); /** @var \Magento\ConfigurableProduct\Test\Block\Adminhtml\Product\Edit\Tab\Super\Config $variationsTab */ $variationsTab = $productForm->getTabElement('variations'); $variationsTab->generateVariations(); $variationsTab->getVariationsBlock()->fillVariations($matrix); $createProductPage->getFormPageActions()->save($variations); }
/** * Assert configurable product on Frontend * * @param ConfigurableProduct $product * @return void */ protected function assertOnFrontend(ConfigurableProduct $product) { //Pages $frontendHomePage = Factory::getPageFactory()->getCmsIndexIndex(); $categoryPage = Factory::getPageFactory()->getCatalogCategoryView(); $productPage = Factory::getPageFactory()->getCatalogProductView(); //Steps $frontendHomePage->open(); $frontendHomePage->getTopmenu()->selectCategoryByName($product->getCategoryName()); //Verification on category product list $productListBlock = $categoryPage->getListProductBlock(); $this->assertTrue($productListBlock->isProductVisible($product->getName()), 'Product is absent on category page.'); //Verification on product detail page $productViewBlock = $productPage->getViewBlock(); $productListBlock->openProductViewPage($product->getName()); $this->assertEquals($product->getName(), $productViewBlock->getProductName(), 'Product name does not correspond to specified.'); $price = $product->getProductPrice(); $blockPrice = $productViewBlock->getProductPrice(); $this->assertEquals(number_format($price, 2), number_format($blockPrice['price_regular_price'], 2), 'Product price does not correspond to specified.'); $this->assertTrue($productViewBlock->verifyProductOptions($product), 'Added configurable options are absent'); }
/** * Get variations data for curl * * @param ConfigurableProduct $fixture * @return array */ protected function _getVariationMatrix(ConfigurableProduct $fixture) { $config = $fixture->getDataConfig(); $variationData = $fixture->getData('fields/variations-matrix/value'); $curlData = []; $variationNumber = 0; foreach ($config['options'] as $attributeId => $options) { foreach ($options['id'] as $option) { foreach ($variationData[$variationNumber]['value'] as $fieldName => $fieldData) { if ($fieldName == 'qty') { $curlData[$option]['quantity_and_stock_status'][$fieldName] = $fieldData['value']; } else { $curlData[$option][$fieldName] = $fieldData['value']; } } if (!isset($curlData[$option]['weight']) && $fixture->getData('fields/weight/value')) { $curlData[$option]['weight'] = $fixture->getData('fields/weight/value'); } $curlData[$option]['configurable_attribute'] = '{"' . $config['attributes'][$attributeId]['code'] . '":"' . $option . '"}'; ++$variationNumber; } } return $curlData; }
/** * Prepare configurable product options. * * @param ConfigurableProduct $product * @return array */ protected function prepareConfigurableOptions(ConfigurableProduct $product) { $options = []; $attributesData = $product->getDataFieldConfig('configurable_attributes_data')['source']->getAttributesData(); foreach ($product->getCheckoutData()['options']['configurable_options'] as $checkoutOption) { $options[] = ['option_id' => $attributesData[$checkoutOption['title']]['attribute_id'], 'option_value' => $attributesData[$checkoutOption['title']]['options'][$checkoutOption['value']]['id']]; } return ['extension_attributes' => ['configurable_item_options' => $options]]; }
/** * Assert that all configurable attributes is absent on product page on frontend. * * @param BrowserInterface $browser * @param CatalogProductView $catalogProductView * @param ConfigurableProduct $product * @return void */ public function processAssert(BrowserInterface $browser, CatalogProductView $catalogProductView, ConfigurableProduct $product) { $browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html'); \PHPUnit_Framework_Assert::assertFalse($catalogProductView->getConfigurableAttributesBlock()->isVisible(), "Configurable attributes are present on product page on frontend."); }