ltsecurityinc安防产品在线商城代码
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user