Skip to content

Commit

Permalink
Refactoring test and add rollback for fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Echo committed Feb 24, 2020
1 parent 28fa345 commit bf514b0
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="ChangeSeoUrlKeyForSubCategoryWithoutRedirectActionGroup" extends="ChangeSeoUrlKeyForSubCategoryActionGroup">
<actionGroup name="AdminChangeSeoUrlKeyForSubCategoryWithoutRedirectActionGroup" extends="ChangeSeoUrlKeyForSubCategoryActionGroup">
<annotations>
<description>Requires navigation to subcategory creation/edit. Updates the Search Engine Optimization with uncheck Redirect Checkbox .</description>
</annotations>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
-->
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
<actionGroup name="StorefrontAssertProductRewriteUrlSubCategoryActionGroup">
<actionGroup name="AssertStorefrontProductRewriteUrlSubCategoryActionGroup">
<annotations>
<description>Validates that the provided Product Title is present on the Rewrite URL with a subcategory page.</description>
</annotations>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
<actionGroup ref="AdminSwitchStoreViewActionGroup" stepKey="AdminSwitchDefaultStoreViewForDefaultCategory">
<argument name="storeView" value="_defaultStore.name"/>
</actionGroup>
<actionGroup ref="ChangeSeoUrlKeyForSubCategoryWithoutRedirectActionGroup" stepKey="changeSeoUrlKeyForDefaultCategoryDefaultStore">
<actionGroup ref="AdminChangeSeoUrlKeyForSubCategoryWithoutRedirectActionGroup" stepKey="changeSeoUrlKeyForDefaultCategoryDefaultStore">
<argument name="value" value="{{_defaultCategoryDifferentUrlStore.url_key_default_store}}"/>
</actionGroup>
<actionGroup ref="AdminSwitchStoreViewActionGroup" stepKey="AdminSwitchCustomStoreViewForDefaultCategory">
<argument name="storeView" value="customStore.name"/>
</actionGroup>
<actionGroup ref="ChangeSeoUrlKeyForSubCategoryWithoutRedirectActionGroup" stepKey="changeSeoUrlKeyForDefaultCategoryCustomStore">
<actionGroup ref="AdminChangeSeoUrlKeyForSubCategoryWithoutRedirectActionGroup" stepKey="changeSeoUrlKeyForDefaultCategoryCustomStore">
<argument name="value" value="{{_defaultCategoryDifferentUrlStore.url_key_custom_store}}"/>
</actionGroup>

Expand All @@ -59,26 +59,25 @@
<actionGroup ref="AdminSwitchStoreViewActionGroup" stepKey="AdminSwitchDefaultStoreViewForSubCategory">
<argument name="storeView" value="_defaultStore.name"/>
</actionGroup>
<actionGroup ref="ChangeSeoUrlKeyForSubCategoryWithoutRedirectActionGroup" stepKey="changeSeoUrlKeyForSubCategoryDefaultStore">
<actionGroup ref="AdminChangeSeoUrlKeyForSubCategoryWithoutRedirectActionGroup" stepKey="changeSeoUrlKeyForSubCategoryDefaultStore">
<argument name="value" value="{{SimpleSubCategoryDifferentUrlStore.url_key_default_store}}"/>
</actionGroup>
<actionGroup ref="AdminSwitchStoreViewActionGroup" stepKey="AdminSwitchCustomStoreViewForSubCategory">
<argument name="storeView" value="customStore.name"/>
</actionGroup>
<actionGroup ref="ChangeSeoUrlKeyForSubCategoryWithoutRedirectActionGroup" stepKey="changeSeoUrlKeyForSubCategoryCustomStore">

<actionGroup ref="AdminChangeSeoUrlKeyForSubCategoryWithoutRedirectActionGroup" stepKey="changeSeoUrlKeyForSubCategoryCustomStore">
<argument name="value" value="{{SimpleSubCategoryDifferentUrlStore.url_key_custom_store}}"/>
</actionGroup>

<actionGroup ref="StorefrontAssertProductRewriteUrlSubCategoryActionGroup" stepKey="validatesRewriteUrlDefaultStore">
<actionGroup ref="AssertStorefrontProductRewriteUrlSubCategoryActionGroup" stepKey="validatesRewriteUrlDefaultStore">
<argument name="category" value="{{_defaultCategoryDifferentUrlStore.url_key_default_store}}"/>
<argument name="product" value="SimpleProduct" />
</actionGroup>

<actionGroup ref="StorefrontSwitchStoreViewActionGroup" stepKey="switchStore">
<argument name="storeView" value="customStore" />
</actionGroup>

<actionGroup ref="StorefrontAssertProductRewriteUrlSubCategoryActionGroup" stepKey="validatesRewriteUrlCustomStore">
<actionGroup ref="AssertStorefrontProductRewriteUrlSubCategoryActionGroup" stepKey="validatesRewriteUrlCustomStore">
<argument name="category" value="{{_defaultCategoryDifferentUrlStore.url_key_custom_store}}"/>
<argument name="product" value="SimpleProduct" />
</actionGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Magento\CatalogUrlRewrite\Model\Product;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\CatalogUrlRewrite\Model\ObjectRegistryFactory;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\Store;
Expand All @@ -31,11 +30,6 @@ class AnchorUrlRewriteGeneratorTest extends TestCase
*/
private $productRepository;

/**
* @var Category
*/
private $collectionCategory;

/**
* @var ObjectRegistryFactory
*/
Expand All @@ -50,32 +44,49 @@ public function setUp()

$this->objectManager = Bootstrap::getObjectManager();
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
$this->collectionCategory = $this->objectManager->create(Category::class);
$this->objectRegistryFactory = $this->objectManager->create(ObjectRegistryFactory::class);
}

/**
* Verify correct generate of the relative "StoreId"
*
* @param string $expect
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_with_stores.php
* @magentoDbIsolation disabled
*
* @return void
* @dataProvider getConfigGenerate
*/
public function testGenerate(): void
public function testGenerate(string $expect): void
{
$product = $this->productRepository->get('simple');
$categories = $product->getCategoryCollection();
$productCategories = $this->objectRegistryFactory->create(['entities' => $categories]);

/** @var Store $store */
/** @var AnchorUrlRewriteGenerator $generator */
$generator = $this->objectManager->get(AnchorUrlRewriteGenerator::class);

/** @var $store Store */
$store = Bootstrap::getObjectManager()->get(Store::class);
$store->load('fixture_second_store', 'code');

/** @var AnchorUrlRewriteGenerator $generator */
$generator = $this->objectManager->get(AnchorUrlRewriteGenerator::class);
$urls = $generator->generate($store->getId(), $product, $productCategories);

$this->assertEquals($expect, $urls[0]->getRequestPath());
}

$this->assertEquals([], $generator->generate(1, $product, $productCategories));
$this->assertNotEquals([], $generator->generate($store->getId(), $product, $productCategories));
/**
* Data provider for testGenerate
*
* @return array
*/
public function getConfigGenerate(): array
{
return [
[
'expect' => 'category-1-custom/simple-product.html'
]
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@
require __DIR__ . '/../../../Magento/Store/_files/second_store.php';
Bootstrap::getInstance()->loadArea(FrontNameResolver::AREA_CODE);

/**
* After installation system has categories:
*
* root one with ID:1 and Default category with ID:3 both with StoreId:1,
*
* root one with ID:1 and Default category with ID:2 both with StoreId:2
*/

$store = Bootstrap::getObjectManager()->get(Store::class);
$store->load('fixture_second_store', 'code');

Expand All @@ -29,11 +21,13 @@
$category->isObjectNew(true);
$category->setId(3)
->setName('Category 1')
->setParentId(1)
->setPath('1/2')
->setLevel(1)
->setParentId(2)
->setPath('1/2/3')
->setLevel(2)
->setAvailableSortBy('name')
->setDefaultSortBy('name')
->setUrlPath('category-1-default')
->setUrlKey('category-1-default')
->setIsActive(true)
->setPosition(1)
->save();
Expand All @@ -43,10 +37,12 @@
$category->setId(4)
->setName('Category 1.1')
->setParentId(3)
->setPath('1/2/3')
->setLevel(2)
->setPath('1/2/3/4')
->setLevel(3)
->setAvailableSortBy('name')
->setDefaultSortBy('name')
->setUrlPath('category-1-1-default')
->setUrlKey('category-1-1-default')
->setIsActive(true)
->setPosition(1)
->save();
Expand All @@ -61,6 +57,8 @@
->setAvailableSortBy('name')
->setDefaultSortBy('name')
->setStoreId($store->getId())
->setUrlPath('category-1-custom')
->setUrlKey('category-1-custom')
->setIsActive(true)
->setPosition(1)
->save();
Expand All @@ -75,6 +73,8 @@
->setAvailableSortBy('name')
->setDefaultSortBy('name')
->setStoreId($store->getId())
->setUrlPath('category-1-1-custom')
->setUrlKey('category-1-1-custom')
->setIsActive(true)
->setPosition(1)
->save();
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

/** @var \Magento\Framework\Registry $registry */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$registry = $objectManager->get(\Magento\Framework\Registry::class);

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);

/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $objectManager->create(\Magento\Catalog\Model\ResourceModel\Category\Collection::class);
$collection
->addAttributeToFilter('level', 2)
->load()
->delete();

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
$installer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Setup\CategorySetup::class
);

require __DIR__ . '/categories_with_stores.php';

$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
Expand All @@ -33,8 +34,6 @@
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))
->setStoreId(1)
->setWebsiteIds([1])
->setName('Simple Product')
->setSku('simple')
->setPrice(10)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Framework\Exception\NoSuchEntityException;

\Magento\TestFramework\Helper\Bootstrap::getInstance()->getInstance()->reinitialize();

/** @var \Magento\Framework\Registry $registry */
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);

/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
try {
$product = $productRepository->get('simple', true);
if ($product->getId()) {
$productRepository->delete($product);
}
} catch (NoSuchEntityException $e) {
}
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);

0 comments on commit bf514b0

Please sign in to comment.