init
This commit is contained in:
+365
@@ -0,0 +1,365 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\UrlRewrite\Block\Catalog\Category;
|
||||
|
||||
use Magento\Catalog\Model\Category;
|
||||
use Magento\Framework\View\LayoutFactory;
|
||||
use Magento\Framework\View\LayoutInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Helper\Xpath;
|
||||
use Magento\UrlRewrite\Block\Catalog\Edit\Form;
|
||||
use Magento\UrlRewrite\Block\Link;
|
||||
use Magento\UrlRewrite\Block\Selector;
|
||||
use Magento\UrlRewrite\Model\UrlRewrite;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test for \Magento\UrlRewrite\Block\Catalog\Category\Edit
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class EditTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Test prepare layout
|
||||
*
|
||||
* @dataProvider prepareLayoutDataProvider
|
||||
*
|
||||
* @param array $blockAttributes
|
||||
* @param array $expected
|
||||
*
|
||||
* @return void
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testPrepareLayout($blockAttributes, $expected): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$layoutFactory = $objectManager->get(LayoutFactory::class);
|
||||
/** @var $layout LayoutInterface */
|
||||
$layout = $layoutFactory->create();
|
||||
|
||||
/** @var $block Edit */
|
||||
$block = $layout->createBlock(
|
||||
Edit::class,
|
||||
'',
|
||||
['data' => $blockAttributes]
|
||||
);
|
||||
|
||||
$this->checkSelector($block, $expected);
|
||||
$this->checkLinks($block, $expected);
|
||||
$this->checkButtons($block, $expected);
|
||||
$this->checkForm($block, $expected);
|
||||
$this->checkCategoriesTree($block, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check selector
|
||||
*
|
||||
* @param Edit $block
|
||||
* @param array $expected
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function checkSelector($block, $expected): void
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
$blockName = $block->getNameInLayout();
|
||||
|
||||
/** @var $selectorBlock Selector|bool */
|
||||
$selectorBlock = $layout->getChildBlock($blockName, 'selector');
|
||||
|
||||
if ($expected['selector']) {
|
||||
$this->assertInstanceOf(
|
||||
Selector::class,
|
||||
$selectorBlock,
|
||||
'Child block with entity selector is invalid'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($selectorBlock, 'Child block with entity selector should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check links
|
||||
*
|
||||
* @param Edit $block
|
||||
* @param array $expected
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function checkLinks($block, $expected): void
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
$blockName = $block->getNameInLayout();
|
||||
|
||||
/** @var $categoryBlock Link|bool */
|
||||
$categoryBlock = $layout->getChildBlock($blockName, 'category_link');
|
||||
|
||||
if ($expected['category_link']) {
|
||||
$this->assertInstanceOf(
|
||||
Link::class,
|
||||
$categoryBlock,
|
||||
'Child block with category link is invalid'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'Category:',
|
||||
$categoryBlock->getLabel(),
|
||||
'Child block with category has invalid item label'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$expected['category_link']['name'],
|
||||
$categoryBlock->getItemName(),
|
||||
'Child block with category has invalid item name'
|
||||
);
|
||||
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/http:\/\/localhost\/index.php\/.*\/category/',
|
||||
$categoryBlock->getItemUrl(),
|
||||
'Child block with category contains invalid URL'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($categoryBlock, 'Child block with category link should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check buttons
|
||||
*
|
||||
* @param Edit $block
|
||||
* @param array $expected
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function checkButtons($block, $expected): void
|
||||
{
|
||||
$buttonsHtml = $block->getButtonsHtml();
|
||||
|
||||
if (isset($expected['back_button'])) {
|
||||
if ($expected['back_button']) {
|
||||
if ($block->getCategory()->getId()) {
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/setLocation\([\\\'\"]\S+?\/category/i',
|
||||
$buttonsHtml,
|
||||
'Back button is not present in category URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
$this->assertEquals(
|
||||
1,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"back")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Back button is not present in category URL rewrite edit block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"back")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Back button should not present in category URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($expected['save_button']) {
|
||||
$this->assertEquals(
|
||||
1,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"save")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Save button is not present in category URL rewrite edit block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"save")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Save button should not present in category URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
|
||||
if ($expected['reset_button']) {
|
||||
$this->assertEquals(
|
||||
1,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[@title="Reset"]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Reset button is not present in category URL rewrite edit block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[@title="Reset"]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Reset button should not present in category URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
|
||||
if ($expected['delete_button']) {
|
||||
$this->assertEquals(
|
||||
1,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"delete")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Delete button is not present in category URL rewrite edit block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"delete")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Delete button should not present in category URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check form
|
||||
*
|
||||
* @param Edit $block
|
||||
* @param array $expected
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function checkForm($block, $expected): void
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
$blockName = $block->getNameInLayout();
|
||||
|
||||
/** @var $formBlock Form|bool */
|
||||
$formBlock = $layout->getChildBlock($blockName, 'form');
|
||||
|
||||
if ($expected['form']) {
|
||||
$this->assertInstanceOf(
|
||||
Form::class,
|
||||
$formBlock,
|
||||
'Child block with form is invalid'
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
$expected['form']['category'],
|
||||
$formBlock->getCategory(),
|
||||
'Form block should have same category attribute'
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
$expected['form']['url_rewrite'],
|
||||
$formBlock->getUrlRewrite(),
|
||||
'Form block should have same URL rewrite attribute'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($formBlock, 'Child block with form should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check categories tree
|
||||
*
|
||||
* @param Edit $block
|
||||
* @param array $expected
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function checkCategoriesTree($block, $expected): void
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
$blockName = $block->getNameInLayout();
|
||||
|
||||
/** @var $categoriesTreeBlock Tree|bool */
|
||||
$categoriesTreeBlock = $layout->getChildBlock($blockName, 'categories_tree');
|
||||
|
||||
if ($expected['categories_tree']) {
|
||||
$this->assertInstanceOf(
|
||||
Tree::class,
|
||||
$categoriesTreeBlock,
|
||||
'Child block with categories tree is invalid'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($categoriesTreeBlock, 'Child block with category_tree should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function prepareLayoutDataProvider(): array
|
||||
{
|
||||
/** @var $urlRewrite UrlRewrite */
|
||||
$urlRewrite = Bootstrap::getObjectManager()->create(
|
||||
UrlRewrite::class
|
||||
);
|
||||
/** @var $category Category */
|
||||
$category = Bootstrap::getObjectManager()->create(
|
||||
Category::class,
|
||||
['data' => ['entity_id' => 1, 'name' => 'Test category']]
|
||||
);
|
||||
/** @var $existingUrlRewrite UrlRewrite */
|
||||
$existingUrlRewrite = Bootstrap::getObjectManager()->create(
|
||||
UrlRewrite::class,
|
||||
['data' => ['url_rewrite_id' => 1]]
|
||||
);
|
||||
|
||||
return [
|
||||
// Creating URL rewrite when category selected
|
||||
[
|
||||
['category' => $category, 'url_rewrite' => $urlRewrite],
|
||||
[
|
||||
'selector' => false,
|
||||
'category_link' => ['name' => $category->getName()],
|
||||
'back_button' => true,
|
||||
'save_button' => true,
|
||||
'reset_button' => false,
|
||||
'delete_button' => false,
|
||||
'form' => ['category' => $category, 'url_rewrite' => $urlRewrite],
|
||||
'categories_tree' => false
|
||||
]
|
||||
],
|
||||
// Creating URL rewrite when category not selected
|
||||
[
|
||||
['url_rewrite' => $urlRewrite],
|
||||
[
|
||||
'selector' => true,
|
||||
'category_link' => false,
|
||||
'back_button' => true,
|
||||
'save_button' => false,
|
||||
'reset_button' => false,
|
||||
'delete_button' => false,
|
||||
'form' => false,
|
||||
'categories_tree' => true
|
||||
]
|
||||
],
|
||||
// Editing URL rewrite with category
|
||||
[
|
||||
['url_rewrite' => $existingUrlRewrite, 'category' => $category],
|
||||
[
|
||||
'selector' => false,
|
||||
'category_link' => ['name' => $category->getName()],
|
||||
'save_button' => true,
|
||||
'reset_button' => true,
|
||||
'delete_button' => true,
|
||||
'form' => ['category' => $category, 'url_rewrite' => $existingUrlRewrite],
|
||||
'categories_tree' => false
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\UrlRewrite\Block\Catalog\Category;
|
||||
|
||||
/**
|
||||
* Test for \Magento\UrlRewrite\Block\Catalog\Category\Tree
|
||||
*
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class TreeTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\UrlRewrite\Block\Catalog\Category\Tree
|
||||
*/
|
||||
private $_treeBlock;
|
||||
|
||||
/**
|
||||
* Set up
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->_treeBlock = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
)->createBlock(
|
||||
\Magento\UrlRewrite\Block\Catalog\Category\Tree::class
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for method \Magento\UrlRewrite\Block\Catalog\Category\Tree::getTreeArray()
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/Catalog/_files/indexer_catalog_category.php
|
||||
* @magentoDataFixture Magento/Catalog/_files/indexer_catalog_products.php
|
||||
*/
|
||||
public function testGetTreeArray()
|
||||
{
|
||||
$tree = $this->_treeBlock->getTreeArray();
|
||||
$this->assertEquals('Root', (string)$tree['name']);
|
||||
$this->assertTrue($tree['expanded']);
|
||||
$this->assertCount(1, $tree['children']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the getTreeArray() method scrubs single quotes and apostrophes from names
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/Catalog/_files/catalog_category_with_apostrophe.php
|
||||
*/
|
||||
public function testGetTreeArrayApostropheReplaced()
|
||||
{
|
||||
$tree = $this->_treeBlock->getTreeArray();
|
||||
|
||||
$this->assertStringNotContainsString('\'', $tree['children'][0]['children'][0]['children'][0]['name']);
|
||||
$this->assertEquals(
|
||||
''Category 6'',
|
||||
$tree['children'][0]['children'][0]['children'][0]['name']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the getTreeArray() method scrubs single quotes and apostrophes from names
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/Catalog/_files/catalog_category_with_doublequotes.php
|
||||
*/
|
||||
public function testGetTreeArrayDoubleQuotesReplaced()
|
||||
{
|
||||
$tree = $this->_treeBlock->getTreeArray();
|
||||
|
||||
$this->assertStringNotContainsString('\"', $tree['children'][0]['children'][0]['children'][0]['name']);
|
||||
$this->assertEquals(
|
||||
'"Category 6"',
|
||||
$tree['children'][0]['children'][0]['children'][0]['name']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test prepare grid
|
||||
*/
|
||||
public function testGetLoadTreeUrl()
|
||||
{
|
||||
$row = new \Magento\Framework\DataObject(['id' => 1]);
|
||||
$this->assertStringStartsWith(
|
||||
'http://localhost/index.php',
|
||||
$this->_treeBlock->getLoadTreeUrl($row),
|
||||
'Tree load URL is invalid'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for method \Magento\UrlRewrite\Block\Catalog\Category\Tree::getCategoryCollection()
|
||||
*/
|
||||
public function testGetCategoryCollection()
|
||||
{
|
||||
$collection = $this->_treeBlock->getCategoryCollection();
|
||||
$this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Category\Collection::class, $collection);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\UrlRewrite\Block\Catalog\Edit;
|
||||
|
||||
/**
|
||||
* Test for \Magento\UrlRewrite\Block\Catalog\Edit\Form
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class FormTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get form instance
|
||||
*
|
||||
* @param array $args
|
||||
* @return \Magento\Framework\Data\Form
|
||||
*/
|
||||
protected function _getFormInstance($args = [])
|
||||
{
|
||||
/** @var $layout \Magento\Framework\View\Layout */
|
||||
$layout = $this->objectManager->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
);
|
||||
/** @var $block \Magento\UrlRewrite\Block\Catalog\Edit\Form */
|
||||
$block = $layout->createBlock(
|
||||
\Magento\UrlRewrite\Block\Catalog\Edit\Form::class,
|
||||
'block',
|
||||
['data' => $args]
|
||||
);
|
||||
$block->setTemplate(null);
|
||||
$block->toHtml();
|
||||
return $block->getForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check _formPostInit set expected fields values
|
||||
*
|
||||
* @covers \Magento\UrlRewrite\Block\Catalog\Edit\Form::_formPostInit
|
||||
*
|
||||
* @dataProvider formPostInitDataProvider
|
||||
*
|
||||
* @param array $productData
|
||||
* @param array $categoryData
|
||||
* @param string $action
|
||||
* @param string $requestPath
|
||||
* @param string $targetPath
|
||||
* @magentoConfigFixture current_store general/single_store_mode/enabled 1
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testFormPostInitNew($productData, $categoryData, $action, $requestPath, $targetPath)
|
||||
{
|
||||
$args = [];
|
||||
if ($productData) {
|
||||
$args['product'] = $this->objectManager->create(
|
||||
\Magento\Catalog\Model\Product::class,
|
||||
['data' => $productData]
|
||||
);
|
||||
}
|
||||
if ($categoryData) {
|
||||
$args['category'] = $this->objectManager->create(
|
||||
\Magento\Catalog\Model\Category::class,
|
||||
['data' => $categoryData]
|
||||
);
|
||||
}
|
||||
$form = $this->_getFormInstance($args);
|
||||
$this->assertStringContainsString($action, $form->getAction());
|
||||
|
||||
$this->assertEquals($requestPath, $form->getElement('request_path')->getValue());
|
||||
$this->assertEquals($targetPath, $form->getElement('target_path')->getValue());
|
||||
|
||||
$this->assertTrue($form->getElement('target_path')->getData('disabled'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test entity stores
|
||||
*
|
||||
* @dataProvider getEntityStoresDataProvider
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
|
||||
*
|
||||
* @param array $productData
|
||||
* @param array $categoryData
|
||||
* @param array $expectedStores
|
||||
*/
|
||||
public function testGetEntityStores($productData, $categoryData, $expectedStores)
|
||||
{
|
||||
$args = [];
|
||||
if ($productData) {
|
||||
$args['product'] = $this->objectManager->create(
|
||||
\Magento\Catalog\Model\Product::class,
|
||||
['data' => $productData]
|
||||
);
|
||||
}
|
||||
if ($categoryData) {
|
||||
$args['category'] = $this->objectManager->create(
|
||||
\Magento\Catalog\Model\Category::class,
|
||||
['data' => $categoryData]
|
||||
);
|
||||
}
|
||||
$form = $this->_getFormInstance($args);
|
||||
$this->assertEquals($expectedStores, $form->getElement('store_id')->getValues());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check exception is thrown when product does not associated with stores
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
|
||||
*/
|
||||
public function testGetEntityStoresProductStoresException()
|
||||
{
|
||||
$args = [
|
||||
'product' => $this->objectManager->create(
|
||||
\Magento\Catalog\Model\Product::class,
|
||||
['data' => ['entity_id' => 1, 'name' => 'product1', 'url_key' => 'product2']]
|
||||
),
|
||||
];
|
||||
$form = $this->_getFormInstance($args);
|
||||
$this->assertEquals([], $form->getElement('store_id')->getValues());
|
||||
$this->assertEquals(
|
||||
'We can\'t set up a URL rewrite because the product you chose is not associated with a website.',
|
||||
$form->getElement('store_id')->getAfterElementHtml()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check exception is thrown when product stores in intersection with category stores is empty
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
|
||||
*
|
||||
*/
|
||||
public function testGetEntityStoresProductCategoryStoresException()
|
||||
{
|
||||
$args = [
|
||||
'product' => $this->objectManager->create(
|
||||
\Magento\Catalog\Model\Product::class,
|
||||
['data' => ['entity_id' => 1, 'name' => 'product1', 'url_key' => 'product1', 'store_ids' => [1]]]
|
||||
),
|
||||
'category' => $this->objectManager->create(
|
||||
\Magento\Catalog\Model\Category::class,
|
||||
['data' => ['entity_id' => 1, 'name' => 'category1', 'url_key' => 'category1', 'store_ids' => [3]]]
|
||||
),
|
||||
];
|
||||
$form = $this->_getFormInstance($args);
|
||||
$this->assertEquals([], $form->getElement('store_id')->getValues());
|
||||
$this->assertEquals(
|
||||
'We can\'t set up a URL rewrite because the product you chose is not associated with a website.',
|
||||
$form->getElement('store_id')->getAfterElementHtml()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check exception is thrown when category does not associated with stores
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
|
||||
*/
|
||||
public function testGetEntityStoresCategoryStoresException()
|
||||
{
|
||||
$args = ['category' => $this->objectManager->create(
|
||||
\Magento\Catalog\Model\Category::class,
|
||||
['data' => ['entity_id' => 1, 'name' => 'product1', 'url_key' => 'product', 'initial_setup_flag' => true]]
|
||||
)];
|
||||
$form = $this->_getFormInstance($args);
|
||||
$this->assertEquals([], $form->getElement('store_id')->getValues());
|
||||
$this->assertEquals(
|
||||
'Please assign a website to the selected category.',
|
||||
$form->getElement('store_id')->getAfterElementHtml()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testing formPostInit
|
||||
* 1) Category selected
|
||||
* 2) Product selected
|
||||
* 3) Product with category selected
|
||||
*
|
||||
* @static
|
||||
* @return array
|
||||
* phpcs:disable Magento2.Functions.StaticFunction
|
||||
*/
|
||||
public static function formPostInitDataProvider()
|
||||
{
|
||||
return [
|
||||
[
|
||||
null,
|
||||
['entity_id' => 3, 'level' => 2, 'parent_id' => 2, 'url_key' => 'category', 'store_id' => 1],
|
||||
'category/3',
|
||||
'category.html',
|
||||
'catalog/category/view/id/3',
|
||||
],
|
||||
[
|
||||
['entity_id' => 2, 'level' => 2, 'url_key' => 'product', 'store_id' => 1],
|
||||
null,
|
||||
'product/2',
|
||||
'product.html',
|
||||
'catalog/product/view/id/2'
|
||||
],
|
||||
[
|
||||
['entity_id' => 2, 'name' => 'product', 'url_key' => 'product', 'store_id' => 1],
|
||||
['entity_id' => 3, 'parent_id' => 2, 'level' => 2, 'url_key' => 'category', 'store_id' => 1],
|
||||
'product/2/category/3',
|
||||
'category/product.html',
|
||||
'catalog/product/view/id/2/category/3'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Entity stores data provider
|
||||
* 1) Category assigned to 1 store
|
||||
* 2) Product assigned to 1 store
|
||||
* 3) Product and category are assigned to same store
|
||||
*
|
||||
* @static
|
||||
* @return array
|
||||
* phpcs:disable Magento2.Functions.StaticFunction
|
||||
*/
|
||||
public static function getEntityStoresDataProvider()
|
||||
{
|
||||
return [
|
||||
[
|
||||
null,
|
||||
['entity_id' => 3, 'store_ids' => [1]],
|
||||
[
|
||||
['label' => 'Main Website', 'value' => [], '__disableTmpl' => true],
|
||||
[
|
||||
'label' => ' Main Website Store',
|
||||
'value' => [['label' => ' Default Store View', 'value' => 1]],
|
||||
'__disableTmpl' => true
|
||||
]
|
||||
],
|
||||
],
|
||||
[
|
||||
['entity_id' => 2, 'name' => 'product2', 'url_key' => 'product2', 'store_ids' => [1]],
|
||||
null,
|
||||
[
|
||||
['label' => 'Main Website', 'value' => [], '__disableTmpl' => true],
|
||||
[
|
||||
'label' => ' Main Website Store',
|
||||
'value' => [['label' => ' Default Store View', 'value' => 1]],
|
||||
'__disableTmpl' => true
|
||||
]
|
||||
]
|
||||
],
|
||||
[
|
||||
['entity_id' => 2, 'name' => 'product2', 'url_key' => 'product2', 'store_ids' => [1]],
|
||||
['entity_id' => 3, 'name' => 'product3', 'url_key' => 'product3', 'store_ids' => [1]],
|
||||
[
|
||||
['label' => 'Main Website', 'value' => [], '__disableTmpl' => true],
|
||||
[
|
||||
'label' => ' Main Website Store',
|
||||
'value' => [['label' => ' Default Store View', 'value' => 1]],
|
||||
'__disableTmpl' => true
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
+488
@@ -0,0 +1,488 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\UrlRewrite\Block\Catalog\Product;
|
||||
|
||||
use Magento\Backend\Block\Widget\Button;
|
||||
use Magento\Catalog\Model\Category;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Framework\View\LayoutInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Helper\Xpath;
|
||||
use Magento\UrlRewrite\Block\Catalog\Category\Tree;
|
||||
use Magento\UrlRewrite\Block\Catalog\Edit\Form;
|
||||
use Magento\UrlRewrite\Block\Link;
|
||||
use Magento\UrlRewrite\Block\Selector;
|
||||
use Magento\UrlRewrite\Model\UrlRewrite;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class EditTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Test prepare layout.
|
||||
*
|
||||
* @dataProvider prepareLayoutDataProvider
|
||||
*
|
||||
* @param array $blockAttributes
|
||||
* @param array $expected
|
||||
*
|
||||
* @return void
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testPrepareLayout($blockAttributes, $expected): void
|
||||
{
|
||||
/** @var $layout LayoutInterface */
|
||||
$layout = Bootstrap::getObjectManager()->get(
|
||||
LayoutInterface::class
|
||||
);
|
||||
|
||||
/** @var $block Edit */
|
||||
$block = $layout->createBlock(
|
||||
Edit::class,
|
||||
'',
|
||||
['data' => $blockAttributes]
|
||||
);
|
||||
|
||||
$this->checkSelector($block, $expected);
|
||||
$this->checkLinks($block, $expected);
|
||||
$this->checkButtons($block, $expected);
|
||||
$this->checkForm($block, $expected);
|
||||
$this->checkGrid($block, $expected);
|
||||
$this->checkCategories($block, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check selector.
|
||||
*
|
||||
* @param Edit $block
|
||||
* @param array $expected
|
||||
*/
|
||||
private function checkSelector($block, $expected)
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
$blockName = $block->getNameInLayout();
|
||||
|
||||
/** @var $selectorBlock Selector|bool */
|
||||
$selectorBlock = $layout->getChildBlock($blockName, 'selector');
|
||||
|
||||
if ($expected['selector']) {
|
||||
$this->assertInstanceOf(
|
||||
Selector::class,
|
||||
$selectorBlock,
|
||||
'Child block with entity selector is invalid'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($selectorBlock, 'Child block with entity selector should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check links
|
||||
*
|
||||
* @param Edit $block
|
||||
* @param array $expected
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function checkLinks($block, $expected): void
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
$blockName = $block->getNameInLayout();
|
||||
|
||||
/** @var $productLinkBlock Link|bool */
|
||||
$productLinkBlock = $layout->getChildBlock($blockName, 'product_link');
|
||||
|
||||
if ($expected['product_link']) {
|
||||
$this->assertInstanceOf(
|
||||
Link::class,
|
||||
$productLinkBlock,
|
||||
'Child block with product link is invalid'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'Product:',
|
||||
$productLinkBlock->getLabel(),
|
||||
'Child block with product link has invalid item label'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$expected['product_link']['name'],
|
||||
$productLinkBlock->getItemName(),
|
||||
'Child block with product link has invalid item name'
|
||||
);
|
||||
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/http:\/\/localhost\/index.php\/.*\/product/',
|
||||
$productLinkBlock->getItemUrl(),
|
||||
'Child block with product link contains invalid URL'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($productLinkBlock, 'Child block with product link should not present in block');
|
||||
}
|
||||
|
||||
/** @var $categoryLinkBlock Link|bool */
|
||||
$categoryLinkBlock = $layout->getChildBlock($blockName, 'category_link');
|
||||
|
||||
if ($expected['category_link']) {
|
||||
$this->assertInstanceOf(
|
||||
Link::class,
|
||||
$categoryLinkBlock,
|
||||
'Child block with category link is invalid'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'Category:',
|
||||
$categoryLinkBlock->getLabel(),
|
||||
'Child block with category link has invalid item label'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$expected['category_link']['name'],
|
||||
$categoryLinkBlock->getItemName(),
|
||||
'Child block with category link has invalid item name'
|
||||
);
|
||||
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/http:\/\/localhost\/index.php\/.*\/category/',
|
||||
$categoryLinkBlock->getItemUrl(),
|
||||
'Child block with category link contains invalid URL'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($categoryLinkBlock, 'Child block with category link should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check buttons
|
||||
*
|
||||
* @param Edit $block
|
||||
* @param array $expected
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function checkButtons($block, $expected): void
|
||||
{
|
||||
$buttonsHtml = $block->getButtonsHtml();
|
||||
|
||||
if (isset($expected['back_button'])) {
|
||||
if ($expected['back_button']) {
|
||||
if ($block->getProduct()->getId()) {
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/setLocation\([\\\'\"]\S+?\/product/i',
|
||||
$buttonsHtml,
|
||||
'Back button is not present in category URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
$this->assertEquals(
|
||||
1,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"back")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Back button is not present in product URL rewrite edit block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"back")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Back button should not present in product URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($expected['save_button']) {
|
||||
$this->assertEquals(
|
||||
1,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"save")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Save button is not present in product URL rewrite edit block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"save")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Save button should not present in product URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
|
||||
if ($expected['reset_button']) {
|
||||
$this->assertEquals(
|
||||
1,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[@title="Reset"]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Reset button is not present in product URL rewrite edit block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[@title="Reset"]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Reset button should not present in product URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
|
||||
if ($expected['delete_button']) {
|
||||
$this->assertEquals(
|
||||
1,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"delete")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Delete button is not present in product URL rewrite edit block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"delete")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Delete button should not present in product URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check form
|
||||
*
|
||||
* @param Edit $block
|
||||
* @param array $expected
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function checkForm($block, $expected): void
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
$blockName = $block->getNameInLayout();
|
||||
|
||||
/** @var $formBlock Form|bool */
|
||||
$formBlock = $layout->getChildBlock($blockName, 'form');
|
||||
|
||||
if ($expected['form']) {
|
||||
$this->assertInstanceOf(
|
||||
Form::class,
|
||||
$formBlock,
|
||||
'Child block with form is invalid'
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
$block->getProduct(),
|
||||
$formBlock->getProduct(),
|
||||
'Form block should have same product attribute'
|
||||
);
|
||||
|
||||
if ($block->getCategory()) {
|
||||
$this->assertSame(
|
||||
$block->getCategory(),
|
||||
$formBlock->getCategory(),
|
||||
'Form block should have same category attribute'
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertSame(
|
||||
$block->getUrlRewrite(),
|
||||
$formBlock->getUrlRewrite(),
|
||||
'Form block should have same URL rewrite attribute'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($formBlock, 'Child block with form should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check grid
|
||||
*
|
||||
* @param Edit $block
|
||||
* @param array $expected
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function checkGrid($block, $expected): void
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
$blockName = $block->getNameInLayout();
|
||||
|
||||
/** @var $gridBlock Grid|bool */
|
||||
$gridBlock = $layout->getChildBlock($blockName, 'products_grid');
|
||||
|
||||
if ($expected['products_grid']) {
|
||||
$this->assertInstanceOf(
|
||||
Grid::class,
|
||||
$gridBlock,
|
||||
'Child block with product grid is invalid'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($gridBlock, 'Child block with product grid should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check categories
|
||||
*
|
||||
* @param Edit $block
|
||||
* @param array $expected
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function checkCategories($block, $expected): void
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
$blockName = $block->getNameInLayout();
|
||||
|
||||
/** @var $categoriesTreeBlock Tree|bool */
|
||||
$categoriesTreeBlock = $layout->getChildBlock($blockName, 'categories_tree');
|
||||
|
||||
if ($expected['categories_tree']) {
|
||||
$this->assertInstanceOf(
|
||||
Tree::class,
|
||||
$categoriesTreeBlock,
|
||||
'Child block with categories tree is invalid'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($categoriesTreeBlock, 'Child block with categories tree should not present in block');
|
||||
}
|
||||
|
||||
/** @var $skipCategoriesBlock Button|bool */
|
||||
$skipCategoriesBlock = $layout->getChildBlock($blockName, 'skip_categories');
|
||||
|
||||
if ($expected['skip_categories']) {
|
||||
$this->assertInstanceOf(
|
||||
Button::class,
|
||||
$skipCategoriesBlock,
|
||||
'Child block with skip categories is invalid'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($skipCategoriesBlock, 'Child block with skip categories should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider
|
||||
*
|
||||
* @return array
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function prepareLayoutDataProvider(): array
|
||||
{
|
||||
/** @var $urlRewrite UrlRewrite */
|
||||
$urlRewrite = Bootstrap::getObjectManager()->create(
|
||||
UrlRewrite::class
|
||||
);
|
||||
/** @var $product Product */
|
||||
$product = Bootstrap::getObjectManager()->create(
|
||||
Product::class,
|
||||
['data' => ['entity_id' => 1, 'name' => 'Test product']]
|
||||
);
|
||||
/** @var $category Category */
|
||||
$category = Bootstrap::getObjectManager()->create(
|
||||
Category::class,
|
||||
['data' => ['entity_id' => 1, 'name' => 'Test category']]
|
||||
);
|
||||
/** @var $existingUrlRewrite UrlRewrite */
|
||||
$existingUrlRewrite = Bootstrap::getObjectManager()->create(
|
||||
UrlRewrite::class,
|
||||
['data' => ['url_rewrite_id' => 1]]
|
||||
);
|
||||
return [
|
||||
[ // Creating URL rewrite when product and category are not selected
|
||||
['url_rewrite' => $urlRewrite],
|
||||
[
|
||||
'selector' => true,
|
||||
'product_link' => false,
|
||||
'category_link' => false,
|
||||
'back_button' => true,
|
||||
'reset_button' => false,
|
||||
'delete_button' => false,
|
||||
'save_button' => false,
|
||||
'form' => false,
|
||||
'products_grid' => true,
|
||||
'categories_tree' => false,
|
||||
'skip_categories' => false
|
||||
],
|
||||
],
|
||||
[ // Creating URL rewrite when product selected and category tree active
|
||||
['product' => $product, 'url_rewrite' => $urlRewrite, 'is_category_mode' => true],
|
||||
[
|
||||
'selector' => false,
|
||||
'product_link' => ['name' => $product->getName()],
|
||||
'category_link' => false,
|
||||
'back_button' => true,
|
||||
'reset_button' => false,
|
||||
'delete_button' => false,
|
||||
'save_button' => false,
|
||||
'form' => false,
|
||||
'products_grid' => false,
|
||||
'categories_tree' => true,
|
||||
'skip_categories' => true
|
||||
]
|
||||
],
|
||||
[ // Creating URL rewrite when product selected and category tree inactive
|
||||
['product' => $product, 'url_rewrite' => $urlRewrite],
|
||||
[
|
||||
'selector' => false,
|
||||
'product_link' => ['name' => $product->getName()],
|
||||
'category_link' => false,
|
||||
'back_button' => true,
|
||||
'reset_button' => false,
|
||||
'delete_button' => false,
|
||||
'save_button' => true,
|
||||
'form' => true,
|
||||
'products_grid' => false,
|
||||
'categories_tree' => false,
|
||||
'skip_categories' => false
|
||||
]
|
||||
],
|
||||
[ // Creating URL rewrite when product selected and category selected
|
||||
['product' => $product, 'category' => $category, 'url_rewrite' => $urlRewrite],
|
||||
[
|
||||
'selector' => false,
|
||||
'product_link' => ['name' => $product->getName()],
|
||||
'category_link' => ['name' => $category->getName()],
|
||||
'back_button' => true,
|
||||
'reset_button' => false,
|
||||
'delete_button' => false,
|
||||
'save_button' => true,
|
||||
'form' => true,
|
||||
'products_grid' => false,
|
||||
'categories_tree' => false,
|
||||
'skip_categories' => false
|
||||
]
|
||||
],
|
||||
[ // Editing existing URL rewrite with product and category
|
||||
['product' => $product, 'category' => $category, 'url_rewrite' => $existingUrlRewrite],
|
||||
[
|
||||
'selector' => false,
|
||||
'product_link' => ['name' => $product->getName()],
|
||||
'category_link' => ['name' => $category->getName()],
|
||||
'reset_button' => true,
|
||||
'delete_button' => true,
|
||||
'save_button' => true,
|
||||
'form' => true,
|
||||
'products_grid' => false,
|
||||
'categories_tree' => false,
|
||||
'skip_categories' => false
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\UrlRewrite\Block\Catalog\Product;
|
||||
|
||||
/**
|
||||
* Test for \Magento\UrlRewrite\Block\Catalog\Product\Grid
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class GridTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* Test prepare grid
|
||||
*/
|
||||
public function testPrepareGrid()
|
||||
{
|
||||
/** @var $gridBlock \Magento\UrlRewrite\Block\Catalog\Product\Grid */
|
||||
$gridBlock = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
)->createBlock(
|
||||
\Magento\UrlRewrite\Block\Catalog\Product\Grid::class
|
||||
);
|
||||
$gridBlock->toHtml();
|
||||
|
||||
foreach (['entity_id', 'name', 'sku', 'status'] as $key) {
|
||||
$this->assertInstanceOf(
|
||||
\Magento\Backend\Block\Widget\Grid\Column::class,
|
||||
$gridBlock->getColumn($key),
|
||||
'Column with key "' . $key . '" is invalid'
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertStringStartsWith('http://localhost/index.php', $gridBlock->getGridUrl(), 'Grid URL is invalid');
|
||||
|
||||
$row = new \Magento\Framework\DataObject(['id' => 1]);
|
||||
$this->assertStringStartsWith(
|
||||
'http://localhost/index.php/backend/admin/index/edit/product/1',
|
||||
$gridBlock->getRowUrl($row),
|
||||
'Grid row URL is invalid'
|
||||
);
|
||||
$this->assertStringEndsWith('/category', $gridBlock->getRowUrl($row), 'Grid row URL is invalid');
|
||||
|
||||
$this->assertEmpty($gridBlock->getMassactionBlock()->getItems(), 'Grid should not have mass action items');
|
||||
$this->assertTrue($gridBlock->getUseAjax(), '"use_ajax" value of grid is incorrect');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\UrlRewrite\Block\Cms\Page\Edit;
|
||||
|
||||
/**
|
||||
* Test for \Magento\UrlRewrite\Block\Cms\Page\Edit\FormTest
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class FormTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* Get form instance
|
||||
*
|
||||
* @param array $args
|
||||
* @return \Magento\Framework\Data\Form
|
||||
*/
|
||||
protected function _getFormInstance($args = [])
|
||||
{
|
||||
/** @var $layout \Magento\Framework\View\Layout */
|
||||
$layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
);
|
||||
/** @var $block \Magento\UrlRewrite\Block\Cms\Page\Edit\Form */
|
||||
$block = $layout->createBlock(
|
||||
\Magento\UrlRewrite\Block\Cms\Page\Edit\Form::class,
|
||||
'block',
|
||||
['data' => $args]
|
||||
);
|
||||
$block->setTemplate(null);
|
||||
$block->toHtml();
|
||||
return $block->getForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check _formPostInit set expected fields values
|
||||
*
|
||||
* @covers \Magento\UrlRewrite\Block\Cms\Page\Edit\Form::_formPostInit
|
||||
*
|
||||
* @dataProvider formPostInitDataProvider
|
||||
*
|
||||
* @param array $cmsPageData
|
||||
* @param string $action
|
||||
* @param string $requestPath
|
||||
* @param string $targetPath
|
||||
* @magentoConfigFixture current_store general/single_store_mode/enabled 1
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testFormPostInit($cmsPageData, $action, $requestPath, $targetPath)
|
||||
{
|
||||
$args = [];
|
||||
if ($cmsPageData) {
|
||||
$args['cms_page'] = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Cms\Model\Page::class,
|
||||
['data' => $cmsPageData]
|
||||
);
|
||||
}
|
||||
$form = $this->_getFormInstance($args);
|
||||
$this->assertStringContainsString($action, $form->getAction());
|
||||
|
||||
$this->assertEquals($requestPath, $form->getElement('request_path')->getValue());
|
||||
$this->assertEquals($targetPath, $form->getElement('target_path')->getValue());
|
||||
|
||||
$this->assertTrue($form->getElement('target_path')->getData('disabled'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test entity stores
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
|
||||
*/
|
||||
public function testGetEntityStores()
|
||||
{
|
||||
$args = ['cms_page' => $this->_getCmsPageWithStoresMock([1])];
|
||||
$form = $this->_getFormInstance($args);
|
||||
|
||||
$expectedStores = [
|
||||
['label' => 'Main Website', 'value' => [], '__disableTmpl' => true],
|
||||
[
|
||||
'label' => ' Main Website Store',
|
||||
'value' => [['label' => ' Default Store View', 'value' => 1]],
|
||||
'__disableTmpl' => true
|
||||
],
|
||||
];
|
||||
$this->assertEquals($expectedStores, $form->getElement('store_id')->getValues());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check exception is thrown when product does not associated with stores
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
|
||||
*/
|
||||
public function testGetEntityStoresProductStoresException()
|
||||
{
|
||||
$args = ['cms_page' => $this->_getCmsPageWithStoresMock([])];
|
||||
$form = $this->_getFormInstance($args);
|
||||
$this->assertEquals([], $form->getElement('store_id')->getValues());
|
||||
$this->assertEquals(
|
||||
'Please assign a website to the selected CMS page.',
|
||||
$form->getElement('store_id')->getAfterElementHtml()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testing formPostInit
|
||||
* 1) Cms page is selected
|
||||
*
|
||||
* @static
|
||||
* @return array
|
||||
* phpcs:disable Magento2.Functions.StaticFunction
|
||||
*/
|
||||
public static function formPostInitDataProvider()
|
||||
{
|
||||
return [
|
||||
[
|
||||
['page_id' => 3, 'identifier' => 'cms-page'],
|
||||
'cms_page/3',
|
||||
'cms-page',
|
||||
'cms/page/view/page_id/3',
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get CMS page model mock
|
||||
*
|
||||
* @param $stores
|
||||
* @return \PHPUnit\Framework\MockObject\MockObject|\Magento\Cms\Model\Page
|
||||
*/
|
||||
protected function _getCmsPageWithStoresMock($stores)
|
||||
{
|
||||
$resourceMock = $this->getMockBuilder(
|
||||
\Magento\Cms\Model\ResourceModel\Page::class
|
||||
)->setMethods(
|
||||
['lookupStoreIds']
|
||||
)->disableOriginalConstructor()->getMock();
|
||||
$resourceMock->expects($this->any())->method('lookupStoreIds')->willReturn($stores);
|
||||
|
||||
$cmsPageMock = $this->getMockBuilder(
|
||||
\Magento\Cms\Model\Page::class
|
||||
)->setMethods(
|
||||
['getResource', 'getId']
|
||||
)->disableOriginalConstructor()->getMock();
|
||||
$cmsPageMock->expects($this->any())->method('getId')->willReturn(1);
|
||||
$cmsPageMock->expects($this->any())->method('getResource')->willReturn($resourceMock);
|
||||
|
||||
return $cmsPageMock;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\UrlRewrite\Block\Cms\Page;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test for \Magento\UrlRewrite\Block\Cms\Page\Edit
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class EditTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Test prepare layout
|
||||
*
|
||||
* @dataProvider prepareLayoutDataProvider
|
||||
*
|
||||
* @param array $blockAttributes
|
||||
* @param array $expected
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testPrepareLayout($blockAttributes, $expected)
|
||||
{
|
||||
/** @var $layout \Magento\Framework\View\LayoutInterface */
|
||||
$layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
);
|
||||
|
||||
/** @var $block \Magento\UrlRewrite\Block\Cms\Page\Edit */
|
||||
$block = $layout->createBlock(
|
||||
\Magento\UrlRewrite\Block\Cms\Page\Edit::class,
|
||||
'',
|
||||
['data' => $blockAttributes]
|
||||
);
|
||||
|
||||
$this->_checkSelector($block, $expected);
|
||||
$this->_checkLinks($block, $expected);
|
||||
$this->_checkButtons($block, $expected);
|
||||
$this->_checkForm($block, $expected);
|
||||
$this->_checkGrid($block, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check selector
|
||||
*
|
||||
* @param \Magento\UrlRewrite\Block\Cms\Page\Edit $block
|
||||
* @param array $expected
|
||||
*/
|
||||
private function _checkSelector($block, $expected)
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
$blockName = $block->getNameInLayout();
|
||||
|
||||
/** @var $selectorBlock \Magento\UrlRewrite\Block\Selector|bool */
|
||||
$selectorBlock = $layout->getChildBlock($blockName, 'selector');
|
||||
|
||||
if ($expected['selector']) {
|
||||
$this->assertInstanceOf(
|
||||
\Magento\UrlRewrite\Block\Selector::class,
|
||||
$selectorBlock,
|
||||
'Child block with entity selector is invalid'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($selectorBlock, 'Child block with entity selector should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check links
|
||||
*
|
||||
* @param \Magento\UrlRewrite\Block\Cms\Page\Edit $block
|
||||
* @param array $expected
|
||||
*/
|
||||
private function _checkLinks($block, $expected)
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
$blockName = $block->getNameInLayout();
|
||||
|
||||
/** @var $cmsPageLinkBlock \Magento\UrlRewrite\Block\Link|bool */
|
||||
$cmsPageLinkBlock = $layout->getChildBlock($blockName, 'cms_page_link');
|
||||
|
||||
if ($expected['cms_page_link']) {
|
||||
$this->assertInstanceOf(
|
||||
\Magento\UrlRewrite\Block\Link::class,
|
||||
$cmsPageLinkBlock,
|
||||
'Child block with CMS page link is invalid'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'CMS page:',
|
||||
$cmsPageLinkBlock->getLabel(),
|
||||
'Child block with CMS page has invalid item label'
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
$expected['cms_page_link']['name'],
|
||||
$cmsPageLinkBlock->getItemName(),
|
||||
'Child block with CMS page has invalid item name'
|
||||
);
|
||||
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/http:\/\/localhost\/index.php\/.*\/cms_page/',
|
||||
$cmsPageLinkBlock->getItemUrl(),
|
||||
'Child block with CMS page contains invalid URL'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($cmsPageLinkBlock, 'Child block with CMS page link should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check buttons
|
||||
*
|
||||
* @param \Magento\UrlRewrite\Block\Cms\Page\Edit $block
|
||||
* @param array $expected
|
||||
*/
|
||||
private function _checkButtons($block, $expected)
|
||||
{
|
||||
$buttonsHtml = $block->getButtonsHtml();
|
||||
|
||||
if (isset($expected['back_button'])) {
|
||||
if ($expected['back_button']) {
|
||||
if ($block->getCmsPage()->getId()) {
|
||||
$this->assertMatchesRegularExpression(
|
||||
'/setLocation\([\\\'\"]\S+?\/cms_page/i',
|
||||
$buttonsHtml,
|
||||
'Back button is not present in category URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
$this->assertEquals(
|
||||
1,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class, "back")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Back button is not present in CMS page URL rewrite edit block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"back")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Back button should not present in CMS page URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($expected['save_button']) {
|
||||
$this->assertEquals(
|
||||
1,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"save")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Save button is not present in CMS page URL rewrite edit block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"save")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Save button should not present in CMS page URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
|
||||
if ($expected['reset_button']) {
|
||||
$this->assertEquals(
|
||||
1,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[@title="Reset"]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Reset button is not present in CMS page URL rewrite edit block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[@title="Reset"]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Reset button should not present in CMS page URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
|
||||
if ($expected['delete_button']) {
|
||||
$this->assertEquals(
|
||||
1,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"delete")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Delete button is not present in CMS page URL rewrite edit block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"delete")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Delete button should not present in CMS page URL rewrite edit block'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check form
|
||||
*
|
||||
* @param \Magento\UrlRewrite\Block\Cms\Page\Edit $block
|
||||
* @param array $expected
|
||||
*/
|
||||
private function _checkForm($block, $expected)
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
$blockName = $block->getNameInLayout();
|
||||
|
||||
/** @var $formBlock \Magento\UrlRewrite\Block\Cms\Page\Edit\Form|bool */
|
||||
$formBlock = $layout->getChildBlock($blockName, 'form');
|
||||
|
||||
if ($expected['form']) {
|
||||
$this->assertInstanceOf(
|
||||
\Magento\UrlRewrite\Block\Cms\Page\Edit\Form::class,
|
||||
$formBlock,
|
||||
'Child block with form is invalid'
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
$expected['form']['cms_page'],
|
||||
$formBlock->getCmsPage(),
|
||||
'Form block should have same CMS page attribute'
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
$expected['form']['url_rewrite'],
|
||||
$formBlock->getUrlRewrite(),
|
||||
'Form block should have same URL rewrite attribute'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($formBlock, 'Child block with form should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check grid
|
||||
*
|
||||
* @param \Magento\UrlRewrite\Block\Cms\Page\Edit $block
|
||||
* @param array $expected
|
||||
*/
|
||||
private function _checkGrid($block, $expected)
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
$blockName = $block->getNameInLayout();
|
||||
|
||||
/** @var $gridBlock \Magento\UrlRewrite\Block\Cms\Page\Grid|bool */
|
||||
$gridBlock = $layout->getChildBlock($blockName, 'cms_pages_grid');
|
||||
|
||||
if ($expected['cms_pages_grid']) {
|
||||
$this->assertInstanceOf(
|
||||
\Magento\UrlRewrite\Block\Cms\Page\Grid::class,
|
||||
$gridBlock,
|
||||
'Child block with CMS pages grid is invalid'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($gridBlock, 'Child block with CMS pages grid should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function prepareLayoutDataProvider()
|
||||
{
|
||||
/** @var $urlRewrite \Magento\UrlRewrite\Model\UrlRewrite */
|
||||
$urlRewrite = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\UrlRewrite\Model\UrlRewrite::class
|
||||
);
|
||||
/** @var $cmsPage \Magento\Cms\Model\Page */
|
||||
$cmsPage = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Cms\Model\Page::class,
|
||||
['data' => ['page_id' => 1, 'title' => 'Test CMS Page']]
|
||||
);
|
||||
/** @var $existingUrlRewrite \Magento\UrlRewrite\Model\UrlRewrite */
|
||||
$existingUrlRewrite = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\UrlRewrite\Model\UrlRewrite::class,
|
||||
['data' => ['url_rewrite_id' => 1]]
|
||||
);
|
||||
|
||||
return [
|
||||
// Creating URL rewrite when CMS page selected
|
||||
[
|
||||
['cms_page' => $cmsPage, 'url_rewrite' => $urlRewrite],
|
||||
[
|
||||
'selector' => false,
|
||||
'cms_page_link' => ['name' => $cmsPage->getTitle()],
|
||||
'back_button' => true,
|
||||
'save_button' => true,
|
||||
'reset_button' => false,
|
||||
'delete_button' => false,
|
||||
'form' => ['cms_page' => $cmsPage, 'url_rewrite' => $urlRewrite],
|
||||
'cms_pages_grid' => false
|
||||
]
|
||||
],
|
||||
// Creating URL rewrite when CMS page not selected
|
||||
[
|
||||
['url_rewrite' => $urlRewrite],
|
||||
[
|
||||
'selector' => true,
|
||||
'cms_page_link' => false,
|
||||
'back_button' => true,
|
||||
'save_button' => false,
|
||||
'reset_button' => false,
|
||||
'delete_button' => false,
|
||||
'form' => false,
|
||||
'cms_pages_grid' => true
|
||||
]
|
||||
],
|
||||
// Editing existing URL rewrite with CMS page
|
||||
[
|
||||
['url_rewrite' => $existingUrlRewrite, 'cms_page' => $cmsPage],
|
||||
[
|
||||
'selector' => false,
|
||||
'cms_page_link' => ['name' => $cmsPage->getTitle()],
|
||||
'save_button' => true,
|
||||
'reset_button' => true,
|
||||
'delete_button' => true,
|
||||
'form' => ['cms_page' => $cmsPage, 'url_rewrite' => $existingUrlRewrite],
|
||||
'cms_pages_grid' => false
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\UrlRewrite\Block\Cms\Page;
|
||||
|
||||
/**
|
||||
* Test for \Magento\UrlRewrite\Block\Cms\Page\Grid
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class GridTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* Test prepare grid
|
||||
*/
|
||||
public function testPrepareGrid()
|
||||
{
|
||||
/** @var \Magento\UrlRewrite\Block\Cms\Page\Grid $gridBlock */
|
||||
$gridBlock = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
)->createBlock(
|
||||
\Magento\UrlRewrite\Block\Cms\Page\Grid::class
|
||||
);
|
||||
$gridBlock->toHtml();
|
||||
|
||||
foreach (['title', 'identifier', 'is_active'] as $key) {
|
||||
$this->assertInstanceOf(
|
||||
\Magento\Backend\Block\Widget\Grid\Column::class,
|
||||
$gridBlock->getColumn($key),
|
||||
'Column with key "' . $key . '" is invalid'
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertStringStartsWith('http://localhost/index.php', $gridBlock->getGridUrl(), 'Grid URL is invalid');
|
||||
|
||||
$row = new \Magento\Framework\DataObject(['id' => 1]);
|
||||
$this->assertStringStartsWith(
|
||||
'http://localhost/index.php/backend/admin/index/edit/cms_page/1',
|
||||
$gridBlock->getRowUrl($row),
|
||||
'Grid row URL is invalid'
|
||||
);
|
||||
|
||||
$this->assertEmpty($gridBlock->getMassactionBlock()->getItems(), 'Grid should not have mass action items');
|
||||
$this->assertTrue($gridBlock->getUseAjax(), '"use_ajax" value of grid is incorrect');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test prepare grid when there is more than one store
|
||||
*
|
||||
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
|
||||
*/
|
||||
public function testPrepareGridForMultipleStores()
|
||||
{
|
||||
/** @var \Magento\UrlRewrite\Block\Cms\Page\Grid $gridBlock */
|
||||
$gridBlock = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
)->createBlock(
|
||||
\Magento\UrlRewrite\Block\Cms\Page\Grid::class
|
||||
);
|
||||
$gridBlock->toHtml();
|
||||
$this->assertInstanceOf(
|
||||
\Magento\Backend\Block\Widget\Grid\Column::class,
|
||||
$gridBlock->getColumn('store_id'),
|
||||
'When there is more than one store column with key "store_id" should be present'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\UrlRewrite\Block\Edit;
|
||||
|
||||
/**
|
||||
* Test for \Magento\UrlRewrite\Block\Edit\FormTest
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class FormTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* Get form instance
|
||||
*
|
||||
* @param array $args
|
||||
* @return \Magento\Framework\Data\Form
|
||||
*/
|
||||
protected function _getFormInstance($args = [])
|
||||
{
|
||||
/** @var $layout \Magento\Framework\View\Layout */
|
||||
$layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
);
|
||||
/** @var $block \Magento\UrlRewrite\Block\Edit\Form */
|
||||
$block = $layout->createBlock(\Magento\UrlRewrite\Block\Edit\Form::class, 'block', ['data' => $args]);
|
||||
$block->setTemplate(null);
|
||||
$block->toHtml();
|
||||
return $block->getForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that form was prepared correctly
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testPrepareForm()
|
||||
{
|
||||
// Test form was configured correctly
|
||||
$form = $this->_getFormInstance(['url_rewrite' => new \Magento\Framework\DataObject(['id' => 3])]);
|
||||
$this->assertInstanceOf(\Magento\Framework\Data\Form::class, $form);
|
||||
$this->assertNotEmpty($form->getAction());
|
||||
$this->assertEquals('edit_form', $form->getId());
|
||||
$this->assertEquals('post', $form->getMethod());
|
||||
$this->assertTrue($form->getUseContainer());
|
||||
$this->assertStringContainsString('/id/3', $form->getAction());
|
||||
|
||||
// Check all expected form elements are present
|
||||
$expectedElements = [
|
||||
'store_id',
|
||||
'entity_type',
|
||||
'entity_id',
|
||||
'request_path',
|
||||
'target_path',
|
||||
'redirect_type',
|
||||
'description',
|
||||
];
|
||||
foreach ($expectedElements as $expectedElement) {
|
||||
$this->assertNotNull($form->getElement($expectedElement));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check session data restoring
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testSessionRestore()
|
||||
{
|
||||
// Set urlrewrite data to session
|
||||
$sessionValues = [
|
||||
'store_id' => 1,
|
||||
'entity_type' => 'entity_type',
|
||||
'entity_id' => 'entity_id',
|
||||
'request_path' => 'request_path',
|
||||
'target_path' => 'target_path',
|
||||
'redirect_type' => 'redirect_type',
|
||||
'description' => 'description',
|
||||
];
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\Backend\Model\Session::class
|
||||
)->setUrlRewriteData(
|
||||
$sessionValues
|
||||
);
|
||||
// Re-init form to use newly set session data
|
||||
$form = $this->_getFormInstance(['url_rewrite' => new \Magento\Framework\DataObject()]);
|
||||
|
||||
// Check that all fields values are restored from session
|
||||
foreach ($sessionValues as $field => $value) {
|
||||
$this->assertEquals($value, $form->getElement($field)->getValue());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test store element is hidden when only one store available
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoConfigFixture current_store general/single_store_mode/enabled 1
|
||||
*/
|
||||
public function testStoreElementSingleStore()
|
||||
{
|
||||
$form = $this->_getFormInstance(['url_rewrite' => new \Magento\Framework\DataObject(['id' => 3])]);
|
||||
/** @var $storeElement \Magento\Framework\Data\Form\Element\AbstractElement */
|
||||
$storeElement = $form->getElement('store_id');
|
||||
$this->assertInstanceOf(\Magento\Framework\Data\Form\Element\Hidden::class, $storeElement);
|
||||
|
||||
// Check that store value set correctly
|
||||
$defaultStore = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Store\Model\StoreManagerInterface::class
|
||||
)->getStore(
|
||||
true
|
||||
)->getId();
|
||||
$this->assertEquals($defaultStore, $storeElement->getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test store selection is available and correctly configured
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDataFixture Magento/Store/_files/core_fixturestore.php
|
||||
*/
|
||||
public function testStoreElementMultiStores()
|
||||
{
|
||||
$form = $this->_getFormInstance(['url_rewrite' => new \Magento\Framework\DataObject(['id' => 3])]);
|
||||
/** @var $storeElement \Magento\Framework\Data\Form\Element\AbstractElement */
|
||||
$storeElement = $form->getElement('store_id');
|
||||
|
||||
// Check store selection elements has correct type
|
||||
$this->assertInstanceOf(\Magento\Framework\Data\Form\Element\Select::class, $storeElement);
|
||||
|
||||
// Check store selection elements has correct renderer
|
||||
$this->assertInstanceOf(
|
||||
\Magento\Backend\Block\Store\Switcher\Form\Renderer\Fieldset\Element::class,
|
||||
$storeElement->getRenderer()
|
||||
);
|
||||
|
||||
// Check store elements has expected values
|
||||
$storesList = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Store\Model\System\Store::class
|
||||
)->getStoreValuesForForm();
|
||||
$this->assertIsArray($storeElement->getValues());
|
||||
$this->assertNotEmpty($storeElement->getValues());
|
||||
$this->assertEquals($storesList, $storeElement->getValues());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test fields disabled status
|
||||
* @dataProvider fieldsStateDataProvider
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoConfigFixture current_store general/single_store_mode/enabled 0
|
||||
*/
|
||||
public function testReadonlyFields($urlRewrite, $fields)
|
||||
{
|
||||
$form = $this->_getFormInstance(['url_rewrite' => $urlRewrite]);
|
||||
foreach ($fields as $fieldKey => $expected) {
|
||||
$this->assertEquals($expected, $form->getElement($fieldKey)->getReadonly());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for checking fields state
|
||||
*/
|
||||
public function fieldsStateDataProvider()
|
||||
{
|
||||
return [
|
||||
[
|
||||
new \Magento\Framework\DataObject(),
|
||||
[
|
||||
'store_id' => false,
|
||||
],
|
||||
],
|
||||
[
|
||||
new \Magento\Framework\DataObject(['id' => 3, 'is_autogenerated' => true]),
|
||||
[
|
||||
'store_id' => true,
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\UrlRewrite\Block;
|
||||
|
||||
/**
|
||||
* Test for \Magento\UrlRewrite\Block\Edit
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class EditTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* Test prepare layout
|
||||
*
|
||||
* @dataProvider prepareLayoutDataProvider
|
||||
*
|
||||
* @param array $blockAttributes
|
||||
* @param array $expected
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testPrepareLayout($blockAttributes, $expected)
|
||||
{
|
||||
/** @var $layout \Magento\Framework\View\LayoutInterface */
|
||||
$layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
);
|
||||
|
||||
/** @var $block \Magento\UrlRewrite\Block\Edit */
|
||||
$block = $layout->createBlock(\Magento\UrlRewrite\Block\Edit::class, '', ['data' => $blockAttributes]);
|
||||
|
||||
$this->_checkSelector($block, $expected);
|
||||
$this->_checkButtons($block, $expected);
|
||||
$this->_checkForm($block, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check entity selector
|
||||
*
|
||||
* @param \Magento\UrlRewrite\Block\Edit $block
|
||||
* @param array $expected
|
||||
*/
|
||||
private function _checkSelector($block, $expected)
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
|
||||
/** @var $selectorBlock \Magento\UrlRewrite\Block\Selector|bool */
|
||||
$selectorBlock = $layout->getChildBlock($block->getNameInLayout(), 'selector');
|
||||
|
||||
if ($expected['selector']) {
|
||||
$this->assertInstanceOf(
|
||||
\Magento\UrlRewrite\Block\Selector::class,
|
||||
$selectorBlock,
|
||||
'Child block with entity selector is invalid'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($selectorBlock, 'Child block with entity selector should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check form
|
||||
*
|
||||
* @param \Magento\UrlRewrite\Block\Edit $block
|
||||
* @param array $expected
|
||||
*/
|
||||
private function _checkForm($block, $expected)
|
||||
{
|
||||
$layout = $block->getLayout();
|
||||
$blockName = $block->getNameInLayout();
|
||||
|
||||
/** @var $formBlock \Magento\UrlRewrite\Block\Edit\Form|bool */
|
||||
$formBlock = $layout->getChildBlock($blockName, 'form');
|
||||
|
||||
if ($expected['form']) {
|
||||
$this->assertInstanceOf(
|
||||
\Magento\UrlRewrite\Block\Edit\Form::class,
|
||||
$formBlock,
|
||||
'Child block with form is invalid'
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
$expected['form']['url_rewrite'],
|
||||
$formBlock->getUrlRewrite(),
|
||||
'Form block should have same URL rewrite attribute'
|
||||
);
|
||||
} else {
|
||||
$this->assertFalse($formBlock, 'Child block with form should not present in block');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check buttons
|
||||
*
|
||||
* @param \Magento\UrlRewrite\Block\Edit $block
|
||||
* @param array $expected
|
||||
*/
|
||||
private function _checkButtons($block, $expected)
|
||||
{
|
||||
$buttonsHtml = $block->getButtonsHtml();
|
||||
|
||||
if ($expected['back_button']) {
|
||||
$this->assertEquals(
|
||||
1,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"back")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Back button is not present in block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"back")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Back button should not present in block'
|
||||
);
|
||||
}
|
||||
|
||||
if ($expected['save_button']) {
|
||||
$this->assertEquals(
|
||||
1,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"save")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Save button is not present in block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"save")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Save button should not present in block'
|
||||
);
|
||||
}
|
||||
|
||||
if ($expected['reset_button']) {
|
||||
$this->assertEquals(
|
||||
1,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[@title="Reset"]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Reset button is not present in block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[@title="Reset"]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Reset button should not present in block'
|
||||
);
|
||||
}
|
||||
|
||||
if ($expected['delete_button']) {
|
||||
$this->assertEquals(
|
||||
1,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"delete")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Delete button is not present in block'
|
||||
);
|
||||
} else {
|
||||
$this->assertEquals(
|
||||
0,
|
||||
\Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
|
||||
'//button[contains(@class,"delete")]',
|
||||
$buttonsHtml
|
||||
),
|
||||
'Delete button should not present in block'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function prepareLayoutDataProvider()
|
||||
{
|
||||
/** @var $urlRewrite \Magento\UrlRewrite\Model\UrlRewrite */
|
||||
$urlRewrite = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\UrlRewrite\Model\UrlRewrite::class
|
||||
);
|
||||
/** @var $existingUrlRewrite \Magento\UrlRewrite\Model\UrlRewrite */
|
||||
$existingUrlRewrite = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
|
||||
\Magento\UrlRewrite\Model\UrlRewrite::class,
|
||||
['data' => ['url_rewrite_id' => 1]]
|
||||
);
|
||||
|
||||
return [
|
||||
// Creating new URL rewrite
|
||||
[
|
||||
['url_rewrite' => $urlRewrite],
|
||||
[
|
||||
'selector' => true,
|
||||
'back_button' => true,
|
||||
'save_button' => true,
|
||||
'reset_button' => false,
|
||||
'delete_button' => false,
|
||||
'form' => ['url_rewrite' => $urlRewrite]
|
||||
]
|
||||
],
|
||||
// Editing URL rewrite
|
||||
[
|
||||
['url_rewrite' => $existingUrlRewrite],
|
||||
[
|
||||
'selector' => true,
|
||||
'back_button' => true,
|
||||
'save_button' => true,
|
||||
'reset_button' => true,
|
||||
'delete_button' => true,
|
||||
'form' => ['url_rewrite' => $existingUrlRewrite]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\UrlRewrite\Block;
|
||||
|
||||
/**
|
||||
* Test for \Magento\UrlRewrite\Block\Selector
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class SelectorTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGetModeUrl()
|
||||
{
|
||||
/** @var $layout \Magento\Framework\View\LayoutInterface */
|
||||
$layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
);
|
||||
|
||||
/** @var $block \Magento\UrlRewrite\Block\Selector */
|
||||
$block = $layout->createBlock(\Magento\UrlRewrite\Block\Selector::class);
|
||||
|
||||
$modeUrl = $block->getModeUrl('mode');
|
||||
$this->assertEquals(1, preg_match('/admin\/index\/index\/key\/[0-9a-zA-Z]+\/mode/', $modeUrl));
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\UrlRewrite\Controller\Adminhtml;
|
||||
|
||||
use Magento\Framework\App\Request\Http as HttpRequest;
|
||||
|
||||
/**
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
class SaveRewriteTest extends \Magento\TestFramework\TestCase\AbstractBackendController
|
||||
{
|
||||
/**
|
||||
* Test create url rewrite with invalid target path
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSaveRewriteWithInvalidRequestPath() : void
|
||||
{
|
||||
$requestPath = 'admin';
|
||||
$reservedWords = 'admin, soap, rest, graphql, standard';
|
||||
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
|
||||
$this->getRequest()->setPostValue(
|
||||
[
|
||||
'description' => 'Some URL rewrite description',
|
||||
'options' => 'R',
|
||||
'request_path' => 'admin',
|
||||
'target_path' => "target_path",
|
||||
'store_id' => 1,
|
||||
]
|
||||
);
|
||||
$this->dispatch('backend/admin/url_rewrite/save');
|
||||
|
||||
$this->assertSessionMessages(
|
||||
$this->containsEqual(__(sprintf(
|
||||
'URL key "%s" matches a reserved endpoint name (%s). Use another URL key.',
|
||||
$requestPath,
|
||||
$reservedWords
|
||||
))),
|
||||
\Magento\Framework\Message\MessageInterface::TYPE_ERROR
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\UrlRewrite\Controller;
|
||||
|
||||
use Magento\TestFramework\TestCase\AbstractController;
|
||||
use Magento\Framework\App\Response\Http as HttpResponse;
|
||||
|
||||
/**
|
||||
* Class to test Match corresponding URL Rewrite
|
||||
*/
|
||||
class UrlRewriteTest extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @magentoDataFixture Magento/UrlRewrite/_files/url_rewrite.php
|
||||
* @magentoDbIsolation disabled
|
||||
*
|
||||
* @covers \Magento\UrlRewrite\Controller\Router::match
|
||||
* @covers \Magento\UrlRewrite\Model\Storage\DbStorage::doFindOneByData
|
||||
*
|
||||
* @param string $request
|
||||
* @param string $redirect
|
||||
* @param int $expectedCode
|
||||
* @return void
|
||||
*
|
||||
* @dataProvider requestDataProvider
|
||||
*/
|
||||
public function testMatchUrlRewrite(
|
||||
string $request,
|
||||
string $redirect,
|
||||
int $expectedCode = HttpResponse::STATUS_CODE_301
|
||||
): void {
|
||||
$this->dispatch($request);
|
||||
/** @var HttpResponse $response */
|
||||
$response = $this->getResponse();
|
||||
$code = $response->getHttpResponseCode();
|
||||
$this->assertEquals($expectedCode, $code, 'Invalid response code');
|
||||
|
||||
if ($expectedCode !== HttpResponse::STATUS_CODE_200) {
|
||||
$location = $response->getHeader('Location')->getFieldValue();
|
||||
$this->assertStringEndsWith(
|
||||
$redirect,
|
||||
$location
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function requestDataProvider(): array
|
||||
{
|
||||
return [
|
||||
'Use Case #1: Rewrite: page-one/ --(301)--> page-a/; Request: page-one/ --(301)--> page-a/' => [
|
||||
'request' => '/page-one/',
|
||||
'redirect' => '/page-a/',
|
||||
],
|
||||
'Use Case #2: Rewrite: page-one/ --(301)--> page-a/; Request: page-one --(301)--> page-a/' => [
|
||||
'request' => '/page-one',
|
||||
'redirect' => '/page-a/',
|
||||
],
|
||||
'Use Case #3: Rewrite: page-two --(301)--> page-b; Request: page-two --(301)--> page-b' => [
|
||||
'request' => '/page-two',
|
||||
'redirect' => '/page-b',
|
||||
],
|
||||
'Use Case #4: Rewrite: page-two --(301)--> page-b; Request: page-two --(301)--> page-b' => [
|
||||
'request' => '/page-two/',
|
||||
'redirect' => '/page-b',
|
||||
],
|
||||
'Use Case #5: Rewrite: page-similar --(301)--> page-a; Request: page-similar --(301)--> page-a' => [
|
||||
'request' => '/page-similar',
|
||||
'redirect' => '/page-a',
|
||||
],
|
||||
'Use Case #6: Rewrite: page-similar/ --(301)--> page-b; Request: page-similar/ --(301)--> page-b' => [
|
||||
'request' => '/page-similar/',
|
||||
'redirect' => '/page-b',
|
||||
],
|
||||
'Use Case #7: Rewrite: page-similar --(301)--> page-a; '
|
||||
. 'Request: page-similar?param=1 --(301)--> page-a?param=1' => [
|
||||
'request' => '/page-similar?param=1',
|
||||
'redirect' => '/page-a?param=1',
|
||||
],
|
||||
'Use Case #8: Rewrite: page-similar/ --(301)--> page-b; '
|
||||
. 'Request: page-similar/?param=1 --(301)--> page-b?param=1' => [
|
||||
'request' => '/page-similar/?param=1',
|
||||
'redirect' => '/page-b?param=1',
|
||||
],
|
||||
'Use Case #9: Rewrite: page-similar-query-param --(301)--> page-d?param1=1;'
|
||||
. 'Request: page-similar-query-param --(301)--> page-d?param1=1' => [
|
||||
'request' => '/page-similar-query-param',
|
||||
'redirect' => '/page-d?param1=1',
|
||||
],
|
||||
'Use Case #10: Rewrite: page-similar-query-param --(301)--> page-d?param1=1; '
|
||||
. 'Request: page-similar-query-param?param2=1 --(301)--> page-d?param1=1¶m2=1' => [
|
||||
'request' => '/page-similar-query-param?param2=1',
|
||||
'redirect' => '/page-d?param1=1¶m2=1',
|
||||
],
|
||||
'Use Case #11: Rewrite: page-similar-query-param/ --(301)--> page-e?param1=1; '
|
||||
. 'Request: page-similar-query-param/ --(301)--> page-e?param1=1' => [
|
||||
'request' => '/page-similar-query-param/',
|
||||
'redirect' => '/page-e?param1=1',
|
||||
],
|
||||
'Use Case #12: Rewrite: page-similar-query-param/ --(301)--> page-e?param1=1;'
|
||||
. 'Request: page-similar-query-param/?param2=1 --(301)--> page-e?param1=1¶m2=1' => [
|
||||
'request' => '/page-similar-query-param/?param2=1',
|
||||
'redirect' => '/page-e?param1=1¶m2=1',
|
||||
],
|
||||
'Use Case #13: Rewrite: page-external1 --(301)--> http://example.com/external;'
|
||||
. 'Request: page-external1?param1=1 --(301)--> http://example.com/external (not fills get params)' => [
|
||||
'request' => '/page-external1?param1=1',
|
||||
'redirect' => 'http://example.com/external',
|
||||
],
|
||||
'Use Case #14: Rewrite: page-external2/ --(301)--> https://example.com/external2/;'
|
||||
. 'Request: page-external2?param2=1 --(301)--> https://example.com/external2/ (not fills get params)' => [
|
||||
'request' => '/page-external2?param2=1',
|
||||
'redirect' => 'https://example.com/external2/',
|
||||
],
|
||||
'Use Case #15: Rewrite: page-external3 --(301)--> http://example.com/external?param1=value1;'
|
||||
. 'Request: page-external3?param1=custom1¶m2=custom2 --(301)--> '
|
||||
. 'http://example.com/external?param1=value1'
|
||||
. ' (fills get param from target path)' => [
|
||||
'request' => '/page-external3?param1=custom1¶m2=custom2',
|
||||
'redirect' => 'http://example.com/external?param1=value1',
|
||||
],
|
||||
'Use Case #16: Rewrite: page-external4/ --(301)--> https://example.com/external2/?param2=value2;'
|
||||
. 'Request: page-external4?param1=custom1¶m2=custom2 --(301)--> '
|
||||
. 'https://example.com/external2/?param2=value2 '
|
||||
. ' (fills get param from target path)' => [
|
||||
'request' => '/page-external4?param1=custom1¶m2=custom2',
|
||||
'redirect' => 'https://example.com/external2/?param2=value2',
|
||||
],
|
||||
'Use Case #17: Rewrite: / --(301)--> /; No redirect' => [
|
||||
'request' => '/',
|
||||
'redirect' => '/',
|
||||
'expectedCode' => HttpResponse::STATUS_CODE_200,
|
||||
],
|
||||
'Use Case #18: Rewrite: contact/ --(301)--> contact?param1=1; '
|
||||
. 'Request: contact/ --(301)--> contact?param1=1' => [
|
||||
'request' => 'contact/',
|
||||
'redirect' => 'contact?param1=1',
|
||||
],
|
||||
'Use Case #19: Rewrite: contact/?param2=2 --(301)--> contact?param1=1¶m2=2; '
|
||||
. 'Request: contact/?¶m2=2 --(301)--> contact?param1=1¶m2=2' => [
|
||||
'request' => 'contact/?¶m2=2',
|
||||
'redirect' => 'contact?param1=1¶m2=2',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
+239
@@ -0,0 +1,239 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\UrlRewrite\Model\StoreSwitcher;
|
||||
|
||||
use Exception;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface;
|
||||
use Magento\Customer\Api\Data\CustomerInterface;
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Framework\App\Config\ReinitableConfigInterface;
|
||||
use Magento\Framework\App\Config\Value;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\ObjectManagerInterface as ObjectManager;
|
||||
use Magento\Store\Api\Data\StoreInterface;
|
||||
use Magento\Store\Api\StoreRepositoryInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Store\Model\StoreSwitcher;
|
||||
use Magento\Store\Model\StoreSwitcher\CannotSwitchStoreException;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test store switching
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class RewriteUrlTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var StoreSwitcher
|
||||
*/
|
||||
private $storeSwitcher;
|
||||
|
||||
/**
|
||||
* @var ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* Class dependencies initialization
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->storeSwitcher = $this->objectManager->get(StoreSwitcher::class);
|
||||
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
|
||||
$this->storeManager = $this->objectManager->create(StoreManagerInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test switching stores with non-existent cms pages and then redirecting to the homepage
|
||||
*
|
||||
* @magentoDataFixture Magento/Catalog/_files/category_product.php
|
||||
* @magentoDataFixture Magento/UrlRewrite/_files/url_rewrite.php
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoAppIsolation enabled
|
||||
* @return void
|
||||
* @throws StoreSwitcher\CannotSwitchStoreException
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function testSwitchToNonExistingPage(): void
|
||||
{
|
||||
$fromStore = $this->getStoreByCode('default');
|
||||
$toStore = $this->getStoreByCode('fixture_second_store');
|
||||
|
||||
$this->setBaseUrl($toStore, 'http://domain.com/');
|
||||
|
||||
$product = $this->productRepository->get('simple333');
|
||||
|
||||
$redirectUrl = "http://domain.com/{$product->getUrlKey()}.html";
|
||||
$expectedUrl = $toStore->getBaseUrl();
|
||||
|
||||
$this->assertEquals($expectedUrl, $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl));
|
||||
$this->setBaseUrl($toStore, 'http://localhost/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing store switching with existing cms pages
|
||||
*
|
||||
* @magentoDataFixture Magento/UrlRewrite/_files/url_rewrite.php
|
||||
* @magentoDbIsolation disabled
|
||||
* @return void
|
||||
* @throws StoreSwitcher\CannotSwitchStoreException
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function testSwitchToExistingPage(): void
|
||||
{
|
||||
$fromStore = $this->getStoreByCode('default');
|
||||
$toStore = $this->getStoreByCode('fixture_second_store');
|
||||
|
||||
$redirectUrl = "http://localhost/index.php/page-c/";
|
||||
$expectedUrl = "http://localhost/index.php/page-c-on-2nd-store";
|
||||
|
||||
$this->assertEquals($expectedUrl, $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl));
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing store switching with existing cms pages with non-existing url keys
|
||||
*
|
||||
* @magentoDataFixture Magento/UrlRewrite/_files/url_rewrite.php
|
||||
* @magentoDbIsolation disabled
|
||||
* @return void
|
||||
* @throws StoreSwitcher\CannotSwitchStoreException|NoSuchEntityException
|
||||
*/
|
||||
public function testSwitchToExistingPageToNonExistingUrlKeys(): void
|
||||
{
|
||||
$fromStore = $this->getStoreByCode('default');
|
||||
$toStore = $this->getStoreByCode('fixture_second_store');
|
||||
|
||||
//test with CMS page with url rewrite for from and target store
|
||||
$redirectUrl1 = "http://localhost/index.php/page-c/";
|
||||
$expectedUrl1 = "http://localhost/index.php/page-c-on-2nd-store";
|
||||
|
||||
$this->assertEquals($expectedUrl1, $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl1));
|
||||
|
||||
//test with CMS page without url rewrite for second/target store
|
||||
$redirectUrl2 = "http://localhost/index.php/fixture_second_store/page-e/";
|
||||
$expectedUrl2 = "http://localhost/index.php/fixture_second_store/page-e/";
|
||||
|
||||
$this->assertEquals($expectedUrl2, $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl2));
|
||||
|
||||
//test with custom url rewrite without CMS page
|
||||
$redirectUrl3 = "http://localhost/index.php/fixture_second_store/contact/";
|
||||
$expectedUrl3 = "http://localhost/index.php/fixture_second_store/contact/";
|
||||
|
||||
$this->assertEquals($expectedUrl3, $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl3));
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing store switching using cms pages with the same url_key but with different page_id
|
||||
*
|
||||
* @magentoDataFixture Magento/Cms/_files/two_cms_page_with_same_url_for_different_stores.php
|
||||
* @magentoDbIsolation disabled
|
||||
* @return void
|
||||
* @throws CannotSwitchStoreException|NoSuchEntityException
|
||||
*/
|
||||
public function testSwitchCmsPageToAnotherStore(): void
|
||||
{
|
||||
$fromStore = $this->getStoreByCode('default');
|
||||
$toStore = $this->getStoreByCode('fixture_second_store');
|
||||
$redirectUrl = "http://localhost/index.php/page100/";
|
||||
$expectedUrl = "http://localhost/index.php/page100/";
|
||||
$this->assertEquals($expectedUrl, $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test store switching with logged in customer on cms page with different url_key
|
||||
*
|
||||
* @magentoDataFixture Magento/UrlRewrite/_files/url_rewrite.php
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoAppArea frontend
|
||||
* @return void
|
||||
* @throws CannotSwitchStoreException
|
||||
* @throws NoSuchEntityException
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function testSwitchCmsPageToAnotherStoreAsCustomer(): void
|
||||
{
|
||||
/** @var CustomerRepositoryInterface $repository */
|
||||
$repository = $this->objectManager->create(CustomerRepositoryInterface::class);
|
||||
$this->loginAsCustomer($repository->get('customer@example.com'));
|
||||
$fromStore = $this->getStoreByCode('default');
|
||||
$toStore = $this->getStoreByCode('fixture_second_store');
|
||||
|
||||
$redirectUrl = "http://localhost/index.php/page-c/";
|
||||
$expectedUrl = "http://localhost/index.php/page-c-on-2nd-store";
|
||||
|
||||
$secureRedirectUrl = $this->storeSwitcher->switch($fromStore, $toStore, $redirectUrl);
|
||||
$this->assertEquals($expectedUrl, $secureRedirectUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Login as customer
|
||||
*
|
||||
* @param CustomerInterface $customer
|
||||
*/
|
||||
private function loginAsCustomer($customer)
|
||||
{
|
||||
/** @var Session $session */
|
||||
$session = $this->objectManager->get(Session::class);
|
||||
$session->setCustomerDataAsLoggedIn($customer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set base url to store.
|
||||
*
|
||||
* @param StoreInterface $targetStore
|
||||
* @param string $baseUrl
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
private function setBaseUrl(StoreInterface $targetStore, string $baseUrl): void
|
||||
{
|
||||
$configValue = $this->objectManager->create(Value::class);
|
||||
$configValue->load('web/unsecure/base_url', 'path');
|
||||
if (!$configValue->getPath()) {
|
||||
$configValue->setPath('web/unsecure/base_url');
|
||||
}
|
||||
$configValue->setValue($baseUrl);
|
||||
$configValue->setScope(ScopeInterface::SCOPE_STORES);
|
||||
$configValue->setScopeId($targetStore->getId());
|
||||
$configValue->save();
|
||||
|
||||
$reinitibleConfig = $this->objectManager->create(ReinitableConfigInterface::class);
|
||||
$reinitibleConfig->reinit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get store object by storeCode
|
||||
*
|
||||
* @param string $storeCode
|
||||
* @return StoreInterface
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
private function getStoreByCode(string $storeCode): StoreInterface
|
||||
{
|
||||
/** @var StoreRepositoryInterface */
|
||||
return $this->storeManager->getStore($storeCode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\UrlRewrite\Model;
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/UrlRewrite/_files/url_rewrites.php
|
||||
*/
|
||||
class UrlFinderInterfaceTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var UrlFinderInterface
|
||||
*/
|
||||
private $urlFinder;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->urlFinder = Bootstrap::getObjectManager()->create(UrlFinderInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider findOneDataProvider
|
||||
* @param string $requestPath
|
||||
* @param string $targetPath
|
||||
* @param int $redirectType
|
||||
*/
|
||||
public function testFindOneByData(string $requestPath, string $targetPath, int $redirectType)
|
||||
{
|
||||
$data = [
|
||||
UrlRewrite::REQUEST_PATH => $requestPath,
|
||||
];
|
||||
$urlRewrite = $this->urlFinder->findOneByData($data);
|
||||
$this->assertEquals($targetPath, $urlRewrite->getTargetPath());
|
||||
$this->assertEquals($redirectType, $urlRewrite->getRedirectType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function findOneDataProvider(): array
|
||||
{
|
||||
return [
|
||||
['string', 'test_page1', 0],
|
||||
['string/', 'string', 301],
|
||||
['string_permanent', 'test_page1', 301],
|
||||
['string_permanent/', 'test_page1', 301],
|
||||
['string_temporary', 'test_page1', 302],
|
||||
['string_temporary/', 'test_page1', 302],
|
||||
['строка', 'test_page1', 0],
|
||||
['строка/', 'строка', 301],
|
||||
[urlencode('строка'), 'test_page2', 0],
|
||||
[urlencode('строка') . '/', urlencode('строка'), 301],
|
||||
['другая_строка', 'test_page1', 302],
|
||||
['другая_строка/', 'test_page1', 302],
|
||||
[urlencode('другая_строка'), 'test_page1', 302],
|
||||
[urlencode('другая_строка') . '/', 'test_page1', 302],
|
||||
['السلسلة', 'test_page1', 0],
|
||||
[urlencode('السلسلة'), 'test_page1', 0],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
use Magento\Cms\Model\Page;
|
||||
use Magento\Cms\Model\ResourceModel\Page as PageResource;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Store\Model\Store;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
|
||||
use Magento\UrlRewrite\Model\OptionProvider;
|
||||
use Magento\UrlRewrite\Model\ResourceModel\UrlRewrite as UrlRewriteResource;
|
||||
use Magento\UrlRewrite\Model\UrlRewrite;
|
||||
|
||||
Resolver::getInstance()->requireDataFixture('Magento/Store/_files/second_store.php');
|
||||
|
||||
/** @var UrlRewrite $rewrite */
|
||||
/** @var ObjectManagerInterface $objectManager */
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
/** @var UrlRewriteResource $rewriteResource */
|
||||
$rewriteResource = $objectManager->create(UrlRewriteResource::class);
|
||||
/** @var PageResource $pageResource */
|
||||
$pageResource = $objectManager->create(PageResource::class);
|
||||
/** @var StoreManagerInterface $storeManager */
|
||||
$storeManager = Bootstrap::getObjectManager()
|
||||
->get(StoreManagerInterface::class);
|
||||
|
||||
/** @var Store $secondStore */
|
||||
$secondStore = Bootstrap::getObjectManager()->create(Store::class);
|
||||
$secondStore->load('fixture_second_store');
|
||||
$secondStoreId = $secondStore->getId();
|
||||
$storeID = 1;
|
||||
|
||||
/** @var $page Page */
|
||||
$page = $objectManager->create(Page::class);
|
||||
$page->setTitle('Cms Page A')
|
||||
->setIdentifier('page-a')
|
||||
->setIsActive(1)
|
||||
->setContent('<h1>Cms Page A</h1>')
|
||||
->setPageLayout('1column')
|
||||
->setStores([$storeID, $secondStoreId]);
|
||||
$pageResource->save($page);
|
||||
|
||||
$page = $objectManager->create(Page::class);
|
||||
$page->setTitle('Cms B')
|
||||
->setIdentifier('page-b')
|
||||
->setIsActive(1)
|
||||
->setContent('<h1>Cms Page B</h1>')
|
||||
->setPageLayout('1column')
|
||||
->setCustomTheme('Magento/blank')
|
||||
->setStores([$storeID, $secondStoreId]);
|
||||
$pageResource->save($page);
|
||||
|
||||
$page = $objectManager->create(Page::class);
|
||||
$page->setTitle('Cms C')
|
||||
->setIdentifier('page-c')
|
||||
->setIsActive(1)
|
||||
->setContent('<h1>Cms Page C</h1>')
|
||||
->setPageLayout('1column')
|
||||
->setCustomTheme('Magento/blank')
|
||||
->setStores([$storeID, $secondStoreId]);
|
||||
$pageResource->save($page);
|
||||
|
||||
$page = $objectManager->create(Page::class);
|
||||
$page->setTitle('Cms D')
|
||||
->setIdentifier('page-d')
|
||||
->setIsActive(1)
|
||||
->setContent('<h1>Cms Page D</h1>')
|
||||
->setPageLayout('1column')
|
||||
->setCustomTheme('Magento/blank')
|
||||
->setStores([$storeID, $secondStoreId]);
|
||||
$pageResource->save($page);
|
||||
|
||||
$page = $objectManager->create(Page::class);
|
||||
$page->setTitle('Cms E')
|
||||
->setIdentifier('page-e')
|
||||
->setIsActive(1)
|
||||
->setContent('<h1>Cms Page E</h1>')
|
||||
->setPageLayout('1column')
|
||||
->setCustomTheme('Magento/blank')
|
||||
->setStores([$storeID, $secondStoreId]);
|
||||
$pageResource->save($page);
|
||||
|
||||
$rewrite = $objectManager->create(UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath('page-one/')
|
||||
->setTargetPath('page-a/')
|
||||
->setRedirectType(OptionProvider::PERMANENT)
|
||||
->setStoreId($storeID)
|
||||
->setDescription('From page-one/ to page-a/');
|
||||
$rewriteResource->save($rewrite);
|
||||
|
||||
$rewrite = $objectManager->create(UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath('page-two')
|
||||
->setTargetPath('page-b')
|
||||
->setRedirectType(OptionProvider::PERMANENT)
|
||||
->setStoreId($storeID)
|
||||
->setDescription('From page-two to page-b');
|
||||
$rewriteResource->save($rewrite);
|
||||
|
||||
$rewrite = $objectManager->create(UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath('page-similar')
|
||||
->setTargetPath('page-a')
|
||||
->setRedirectType(OptionProvider::PERMANENT)
|
||||
->setStoreId($storeID)
|
||||
->setDescription('From page-similar without trailing slash to page-a');
|
||||
$rewriteResource->save($rewrite);
|
||||
|
||||
$rewrite = $objectManager->create(UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath('page-similar/')
|
||||
->setTargetPath('page-b')
|
||||
->setRedirectType(OptionProvider::PERMANENT)
|
||||
->setStoreId($storeID)
|
||||
->setDescription('From page-similar with trailing slash to page-b');
|
||||
$rewriteResource->save($rewrite);
|
||||
|
||||
//Emulating auto-generated aliases (like the ones used for categories).
|
||||
//Rewrite rule for the 1st store.
|
||||
$rewrite = $objectManager->create(UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath('page-c-on-1st-store')
|
||||
->setTargetPath('page-c')
|
||||
->setRedirectType(0)
|
||||
->setStoreId($storeID);
|
||||
$rewriteResource->save($rewrite);
|
||||
//Rewrite rule for the 2nd store.
|
||||
$rewrite = $objectManager->create(UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath('page-c-on-2nd-store')
|
||||
->setTargetPath('page-c')
|
||||
->setRedirectType(0)
|
||||
->setStoreId($secondStoreId);
|
||||
$rewriteResource->save($rewrite);
|
||||
|
||||
$rewrite = $objectManager->create(UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath('page-similar-query-param')
|
||||
->setTargetPath('page-d?param1=1')
|
||||
->setRedirectType(OptionProvider::PERMANENT)
|
||||
->setStoreId($storeID)
|
||||
->setDescription('From page-similar-query-param to page-d with query param');
|
||||
$rewriteResource->save($rewrite);
|
||||
|
||||
$rewrite = $objectManager->create(UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath('page-similar-query-param/')
|
||||
->setTargetPath('page-e?param1=1')
|
||||
->setRedirectType(OptionProvider::PERMANENT)
|
||||
->setStoreId($storeID)
|
||||
->setDescription('From page-similar-query-param with trailing slash to page-e with query param');
|
||||
$rewriteResource->save($rewrite);
|
||||
|
||||
$rewrite = $objectManager->create(UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath('/')
|
||||
->setTargetPath('/')
|
||||
->setRedirectType(OptionProvider::PERMANENT)
|
||||
->setStoreId($storeID)
|
||||
->setDescription('From / to /');
|
||||
$rewriteResource->save($rewrite);
|
||||
|
||||
$rewrite = $objectManager->create(UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath('contact/')
|
||||
->setTargetPath('contact?param1=1')
|
||||
->setRedirectType(OptionProvider::PERMANENT)
|
||||
->setStoreId($storeID)
|
||||
->setDescription('From contact with trailing slash to contact with query param');
|
||||
$rewriteResource->save($rewrite);
|
||||
|
||||
$rewrite = $objectManager->create(UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath('page-external1')
|
||||
->setTargetPath('http://example.com/external')
|
||||
->setRedirectType(OptionProvider::PERMANENT)
|
||||
->setStoreId($storeID)
|
||||
->setDescription('From page-external to external URL');
|
||||
$rewriteResource->save($rewrite);
|
||||
|
||||
$rewrite = $objectManager->create(UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath('page-external2/')
|
||||
->setTargetPath('https://example.com/external2/')
|
||||
->setRedirectType(OptionProvider::PERMANENT)
|
||||
->setStoreId($storeID)
|
||||
->setDescription('From page-external with trailing slash to external URL');
|
||||
$rewriteResource->save($rewrite);
|
||||
|
||||
$rewrite = $objectManager->create(UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath('page-external3')
|
||||
->setTargetPath('http://example.com/external?param1=value1')
|
||||
->setRedirectType(OptionProvider::PERMANENT)
|
||||
->setStoreId($storeID)
|
||||
->setDescription('From page-external to external URL');
|
||||
$rewriteResource->save($rewrite);
|
||||
|
||||
$rewrite = $objectManager->create(UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath('page-external4/')
|
||||
->setTargetPath('https://example.com/external2/?param2=value2')
|
||||
->setRedirectType(OptionProvider::PERMANENT)
|
||||
->setStoreId($storeID)
|
||||
->setDescription('From page-external with trailing slash to external URL');
|
||||
$rewriteResource->save($rewrite);
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\UrlRewrite\Model\UrlRewrite;
|
||||
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
|
||||
/** @var int Product Id */
|
||||
$productId = 708579;
|
||||
|
||||
/** @var UrlRewrite $urlRewrite */
|
||||
$urlRewrite = $objectManager->create(UrlRewrite::class);
|
||||
$urlRewrite->setEntityType('custom')
|
||||
->setRequestPath('non-exist-entity.html')
|
||||
->setTargetPath('catalog/product/view/id/' . $productId)
|
||||
->setRedirectType(0)
|
||||
->setStoreId(1)
|
||||
->setDescription(null)
|
||||
->setIsAutogenerated(0);
|
||||
|
||||
$urlRewrite->save();
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\UrlRewrite\Model\UrlRewrite;
|
||||
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
|
||||
/** @var UrlRewrite $urlRewrite */
|
||||
$urlRewrite = $objectManager->create(UrlRewrite::class);
|
||||
$urlRewrite->load('non-exist-entity.html', 'request_path');
|
||||
$urlRewrite->delete();
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
|
||||
|
||||
/** @var \Magento\Framework\Registry $registry */
|
||||
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
|
||||
|
||||
Resolver::getInstance()->requireDataFixture('Magento/Store/_files/second_store_rollback.php');
|
||||
|
||||
$registry->unregister('isSecureArea');
|
||||
$registry->register('isSecureArea', true);
|
||||
|
||||
/** @var Magento\Cms\Api\PageRepositoryInterface $pageRepository */
|
||||
$pageRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
Magento\Cms\Api\PageRepositoryInterface::class
|
||||
);
|
||||
|
||||
$pageRepository->deleteById('page-a');
|
||||
$pageRepository->deleteById('page-b');
|
||||
$pageRepository->deleteById('page-c');
|
||||
$pageRepository->deleteById('page-d');
|
||||
$pageRepository->deleteById('page-e');
|
||||
|
||||
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
|
||||
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
|
||||
|
||||
/** @var \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection $urlRewriteCollection */
|
||||
$urlRewriteCollection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
|
||||
->create(\Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection::class);
|
||||
$collection = $urlRewriteCollection
|
||||
->addFieldToFilter('entity_type', 'custom')
|
||||
->addFieldToFilter(
|
||||
'target_path',
|
||||
[
|
||||
'page-a/',
|
||||
'page-a',
|
||||
'page-b',
|
||||
'page-c',
|
||||
'page-d?param1=1',
|
||||
'page-e?param1=1',
|
||||
'http://example.com/external',
|
||||
'https://example.com/external2/',
|
||||
'http://example.com/external?param1=value1',
|
||||
'https://example.com/external2/?param2=value2',
|
||||
'/',
|
||||
'contact?param1=1'
|
||||
]
|
||||
)
|
||||
->load()
|
||||
->walk('delete');
|
||||
|
||||
$registry->unregister('isSecureArea');
|
||||
$registry->register('isSecureArea', false);
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
|
||||
$rewritesData = [
|
||||
[
|
||||
'string', 'test_page1', 0
|
||||
],
|
||||
[
|
||||
'string_permanent', 'test_page1', \Magento\UrlRewrite\Model\OptionProvider::PERMANENT
|
||||
],
|
||||
[
|
||||
'string_temporary', 'test_page1', \Magento\UrlRewrite\Model\OptionProvider::TEMPORARY
|
||||
],
|
||||
[
|
||||
'строка', 'test_page1', 0
|
||||
],
|
||||
[
|
||||
urlencode('строка'), 'test_page2', 0
|
||||
],
|
||||
[
|
||||
'другая_строка', 'test_page1', \Magento\UrlRewrite\Model\OptionProvider::TEMPORARY
|
||||
],
|
||||
[
|
||||
'السلسلة', 'test_page1', 0
|
||||
],
|
||||
];
|
||||
|
||||
$rewriteResource = $objectManager->create(\Magento\UrlRewrite\Model\ResourceModel\UrlRewrite::class);
|
||||
foreach ($rewritesData as $rewriteData) {
|
||||
list ($requestPath, $targetPath, $redirectType) = $rewriteData;
|
||||
$rewrite = $objectManager->create(\Magento\UrlRewrite\Model\UrlRewrite::class);
|
||||
$rewrite->setEntityType('custom')
|
||||
->setRequestPath($requestPath)
|
||||
->setTargetPath($targetPath)
|
||||
->setRedirectType($redirectType);
|
||||
$rewriteResource->save($rewrite);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
|
||||
$registry = $objectManager->get(\Magento\Framework\Registry::class);
|
||||
$registry->unregister('isSecureArea');
|
||||
$registry->register('isSecureArea', true);
|
||||
|
||||
$urlRewriteCollection = $objectManager->create(\Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection::class);
|
||||
$collection = $urlRewriteCollection
|
||||
->addFieldToFilter('target_path', ['test_page1', 'test_page2'])
|
||||
->load()
|
||||
->walk('delete');
|
||||
|
||||
$registry->unregister('isSecureArea');
|
||||
$registry->register('isSecureArea', false);
|
||||
Reference in New Issue
Block a user