ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,678 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\View\Element;
|
||||
|
||||
use Magento\Framework\Math\Random;
|
||||
use Magento\Framework\Session\SessionManagerInterface;
|
||||
use Magento\Framework\Session\SidResolverInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class AbstractBlockTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var AbstractBlock
|
||||
*/
|
||||
protected $_block;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\View\LayoutInterface
|
||||
*/
|
||||
protected $_layout = null;
|
||||
|
||||
protected static $_mocks = [];
|
||||
|
||||
/**
|
||||
* @var SessionManagerInterface
|
||||
*/
|
||||
private $session;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
/** @var \Magento\Framework\App\State $state */
|
||||
$state = Bootstrap::getObjectManager()->get(\Magento\Framework\App\State::class);
|
||||
$state->setAreaCode('frontend');
|
||||
/** @var \Magento\Framework\View\DesignInterface $design */
|
||||
$design = Bootstrap::getObjectManager()->get(\Magento\Framework\View\DesignInterface::class);
|
||||
$design->setDefaultDesignTheme();
|
||||
$this->_block = $this->getMockForAbstractClass(
|
||||
AbstractBlock::class,
|
||||
[
|
||||
Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\Element\Context::class
|
||||
),
|
||||
['module_name' => 'Magento_Theme']
|
||||
]
|
||||
);
|
||||
$this->session = Bootstrap::getObjectManager()->get(SessionManagerInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks, that not existing image in CSS not affected own publication
|
||||
*
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testCssWithWrongImage()
|
||||
{
|
||||
$cssUrl = $this->_block->getViewFileUrl(
|
||||
'css/wrong.css',
|
||||
['area' => 'frontend', 'theme' => 'Magento/luma', 'locale' => 'en_US']
|
||||
);
|
||||
$this->assertStringMatchesFormat('%s/css/wrong.css', $cssUrl);
|
||||
}
|
||||
|
||||
public function testGetRequest()
|
||||
{
|
||||
$this->assertInstanceOf(\Magento\Framework\App\RequestInterface::class, $this->_block->getRequest());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGetParentBlock()
|
||||
{
|
||||
// Without layout
|
||||
$this->assertFalse($this->_block->getParentBlock());
|
||||
|
||||
// Need to create blocks through layout
|
||||
$parentBlock = $this->_createBlockWithLayout('block1', 'block1', \Magento\Framework\View\Element\Text::class);
|
||||
$childBlock = $this->_createBlockWithLayout('block2', 'block2');
|
||||
|
||||
$this->assertEmpty($childBlock->getParentBlock());
|
||||
$parentBlock->setChild('block2', $childBlock);
|
||||
$this->assertSame($parentBlock, $childBlock->getParentBlock());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Magento\Framework\View\Element\AbstractBlock::addChild
|
||||
*/
|
||||
public function testAddChild()
|
||||
{
|
||||
$parentBlock = $this->_createBlockWithLayout(
|
||||
'testAddChild',
|
||||
'testAddChild',
|
||||
\Magento\Framework\View\Element\Text::class
|
||||
);
|
||||
$child = $parentBlock->addChild(
|
||||
'testAddChildAlias',
|
||||
\Magento\Framework\View\Element\Text::class,
|
||||
['content' => 'content']
|
||||
);
|
||||
$this->assertInstanceOf(\Magento\Framework\View\Element\Text::class, $child);
|
||||
$this->assertEquals('testAddChild.testAddChildAlias', $child->getNameInLayout());
|
||||
$this->assertEquals($child, $parentBlock->getChildBlock('testAddChildAlias'));
|
||||
$this->assertEquals('content', $child->getContent());
|
||||
}
|
||||
|
||||
public function testSetGetNameInLayout()
|
||||
{
|
||||
// Basic setting/getting
|
||||
$this->assertEmpty($this->_block->getNameInLayout());
|
||||
$name = uniqid('name');
|
||||
$this->_block->setNameInLayout($name);
|
||||
$this->assertEquals($name, $this->_block->getNameInLayout());
|
||||
|
||||
// Setting second time, along with the layout
|
||||
$layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
);
|
||||
$layout->createBlock(\Magento\Framework\View\Element\Template::class, $name);
|
||||
$block = $layout->getBlock($name);
|
||||
$this->assertInstanceOf(AbstractBlock::class, $block);
|
||||
$block->setNameInLayout($name);
|
||||
$this->assertInstanceOf(AbstractBlock::class, $layout->getBlock($name));
|
||||
$this->assertEquals($name, $block->getNameInLayout());
|
||||
$this->assertTrue($layout->hasElement($name));
|
||||
$newName = 'new_name';
|
||||
$block->setNameInLayout($newName);
|
||||
$this->assertTrue($layout->hasElement($newName));
|
||||
$this->assertFalse($layout->hasElement($name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @covers \Magento\Framework\View\Element\AbstractBlock::getChildNames
|
||||
* @covers \Magento\Framework\View\Element\AbstractBlock::insert
|
||||
*/
|
||||
public function testGetChildNames()
|
||||
{
|
||||
// Without layout
|
||||
$this->assertEquals([], $this->_block->getChildNames());
|
||||
|
||||
// With layout
|
||||
$parent = $this->_createBlockWithLayout('parent', 'parent');
|
||||
$block1 = $this->_createBlockWithLayout('block1');
|
||||
$block2 = $this->_createBlockWithLayout('block2');
|
||||
$block3 = $this->_createBlockWithLayout('block3');
|
||||
$block4 = $this->_createBlockWithLayout('block4');
|
||||
|
||||
$parent->insert($block1);
|
||||
// add one block
|
||||
$parent->insert($block2, 'block1', false);
|
||||
// add second to the 1st position
|
||||
$parent->insert($block3, 'block1', false);
|
||||
// add third to the 2nd position
|
||||
$parent->insert($block4, 'block3', true);
|
||||
// add fourth block to the 3rd position
|
||||
|
||||
$this->assertEquals(['block2', 'block3', 'block4', 'block1'], $parent->getChildNames());
|
||||
}
|
||||
|
||||
public function testSetAttribute()
|
||||
{
|
||||
$this->assertEmpty($this->_block->getSomeValue());
|
||||
$this->_block->setAttribute('some_value', 'value');
|
||||
$this->assertEquals('value', $this->_block->getSomeValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testSetGetUnsetChild()
|
||||
{
|
||||
// With layout
|
||||
$parent = $this->_createBlockWithLayout('parent', 'parent');
|
||||
|
||||
// Regular block
|
||||
$nameOne = uniqid('block.');
|
||||
$blockOne = $this->_createBlockWithLayout($nameOne, $nameOne);
|
||||
$parent->setChild('block1', $blockOne);
|
||||
$this->assertSame($blockOne, $parent->getChildBlock('block1'));
|
||||
|
||||
// Block factory name
|
||||
$blockTwo = $this->_createBlockWithLayout('parent_block2', 'parent_block2');
|
||||
$blockTwo->setChild('block2', $nameOne);
|
||||
$this->assertSame($blockOne, $blockTwo->getChildBlock('block2'));
|
||||
|
||||
// No name block
|
||||
$blockThree = $this->_createBlockWithLayout('');
|
||||
$parent->setChild('block3', $blockThree);
|
||||
$this->assertSame($blockThree, $parent->getChildBlock('block3'));
|
||||
|
||||
// Unset
|
||||
$parent->unsetChild('block3');
|
||||
$this->assertNotSame($blockThree, $parent->getChildBlock('block3'));
|
||||
$parent->insert($blockOne, '', true, 'block1');
|
||||
$this->assertContains($nameOne, $parent->getChildNames());
|
||||
$parent->unsetChild('block1');
|
||||
$this->assertNotSame($blockOne, $parent->getChildBlock('block1'));
|
||||
$this->assertNotContains($nameOne, $parent->getChildNames());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testUnsetCallChild()
|
||||
{
|
||||
$blockParent = $this->_createBlockWithLayout('parent', 'parent');
|
||||
$block = $this->_createBlockWithLayout('block1', 'block1');
|
||||
$block->setSomeValue(true);
|
||||
$blockParent->setChild('block1', $block);
|
||||
$this->assertSame($block, $blockParent->getChildBlock('block1'));
|
||||
$blockParent->unsetCallChild('block1', 'getSomeValue', true, []);
|
||||
$this->assertNotSame($block, $blockParent->getChildBlock('block1'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @covers \Magento\Framework\View\Element\AbstractBlock::unsetChildren
|
||||
* @covers \Magento\Framework\View\Element\AbstractBlock::getChildBlock
|
||||
*/
|
||||
public function testUnsetChildren()
|
||||
{
|
||||
$parent = $this->_createBlockWithLayout('block', 'block');
|
||||
$this->assertEquals([], $parent->getChildNames());
|
||||
$blockOne = $this->_createBlockWithLayout('block1', 'block1');
|
||||
$blockTwo = $this->_createBlockWithLayout('block2', 'block2');
|
||||
$parent->setChild('block1', $blockOne);
|
||||
$parent->setChild('block2', $blockTwo);
|
||||
$this->assertSame($blockOne, $parent->getChildBlock('block1'));
|
||||
$this->assertSame($blockTwo, $parent->getChildBlock('block2'));
|
||||
$parent->unsetChildren();
|
||||
$this->assertEquals([], $parent->getChildNames());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGetChildBlock()
|
||||
{
|
||||
$childAlias = 'child_alias';
|
||||
$childName = 'child';
|
||||
$parentName = 'parent';
|
||||
|
||||
// Without layout
|
||||
$this->assertFalse($this->_block->getChildBlock($childAlias));
|
||||
|
||||
// With layout
|
||||
/** @var $layout \Magento\Framework\View\Layout */
|
||||
$layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
);
|
||||
$child = $layout->createBlock(\Magento\Framework\View\Element\Text::class, $childName);
|
||||
$layout->addBlock($this->_block, $parentName);
|
||||
|
||||
$this->_block->setChild($childAlias, $child);
|
||||
$result = $this->_block->getChildBlock($childAlias);
|
||||
|
||||
$this->assertInstanceOf(\Magento\Framework\View\Element\Text::class, $result);
|
||||
$this->assertEquals($childName, $result->getNameInLayout());
|
||||
$this->assertEquals($child, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @covers \Magento\Framework\View\Element\AbstractBlock::getChildHtml
|
||||
* @covers \Magento\Framework\View\Element\AbstractBlock::getChildChildHtml
|
||||
*/
|
||||
public function testGetChildHtml()
|
||||
{
|
||||
// Without layout
|
||||
$this->assertEmpty($this->_block->getChildHtml());
|
||||
$this->assertEmpty($this->_block->getChildHtml('block'));
|
||||
|
||||
// With layout
|
||||
$parent = $this->_createBlockWithLayout('parent', 'parent');
|
||||
$blockOne = $this->_createBlockWithLayout('block1', 'block1', \Magento\Framework\View\Element\Text::class);
|
||||
$blockTwo = $this->_createBlockWithLayout('block2', 'block2', \Magento\Framework\View\Element\Text::class);
|
||||
$blockOne->setText('one');
|
||||
$blockTwo->setText('two');
|
||||
$parent->insert($blockTwo, '-', false, 'block2');
|
||||
// make block2 1st
|
||||
$parent->insert($blockOne, '-', false, 'block1');
|
||||
// make block1 1st
|
||||
|
||||
$this->assertEquals('one', $parent->getChildHtml('block1'));
|
||||
$this->assertEquals('two', $parent->getChildHtml('block2'));
|
||||
|
||||
// GetChildChildHtml
|
||||
$blockTwo->setChild('block11', $blockOne);
|
||||
$this->assertEquals('one', $parent->getChildChildHtml('block2'));
|
||||
$this->assertEquals('', $parent->getChildChildHtml(''));
|
||||
$this->assertEquals('', $parent->getChildChildHtml('block3'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testGetChildChildHtml()
|
||||
{
|
||||
// Without layout
|
||||
$this->assertEmpty($this->_block->getChildChildHtml('alias'));
|
||||
|
||||
// With layout
|
||||
$parent1 = $this->_createBlockWithLayout('parent1', 'parent1');
|
||||
$parent2 = $this->_createBlockWithLayout('parent2', 'parent2');
|
||||
|
||||
$block1 = $this->_createBlockWithLayout('block1', 'block1', \Magento\Framework\View\Element\Text::class);
|
||||
$block2 = $this->_createBlockWithLayout('block2', 'block2', \Magento\Framework\View\Element\Text::class);
|
||||
$block3 = $this->_createBlockWithLayout('block3', 'block3', \Magento\Framework\View\Element\Text::class);
|
||||
$block4 = $this->_createBlockWithLayout('block4', 'block4', \Magento\Framework\View\Element\Text::class);
|
||||
|
||||
$block1->setText('one');
|
||||
$block2->setText('two');
|
||||
$block3->setText('three');
|
||||
$block4->setText('four');
|
||||
|
||||
$parent1->insert($parent2);
|
||||
$parent2->insert($block1, '-', false, 'block1');
|
||||
$parent2->insert($block2, '-', false, 'block2');
|
||||
$parent2->insert($block3, '-', true, 'block3');
|
||||
$parent1->insert($block4);
|
||||
$this->assertEquals('twoonethree', $parent1->getChildChildHtml('parent2'));
|
||||
}
|
||||
|
||||
public function testGetBlockHtml()
|
||||
{
|
||||
// Without layout
|
||||
/** @var $blockFactory \Magento\Framework\View\Element\BlockFactory */
|
||||
$blockFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\Element\BlockFactory::class
|
||||
);
|
||||
$block1 = $blockFactory->createBlock(\Magento\Framework\View\Element\Text::class);
|
||||
$block1->setText('Block text');
|
||||
$block1->setNameInLayout('block');
|
||||
$html = $this->_block->getBlockHtml('block');
|
||||
$this->assertIsString($html);
|
||||
$this->assertEmpty($html);
|
||||
|
||||
// With layout
|
||||
$expected = 'Block2';
|
||||
$block2 = $this->_createBlockWithLayout('block2', 'block2', \Magento\Framework\View\Element\Text::class);
|
||||
$block3 = $this->_createBlockWithLayout('block3', 'block3');
|
||||
$block2->setText($expected);
|
||||
$html = $block3->getBlockHtml('block2');
|
||||
$this->assertIsString($html);
|
||||
$this->assertEquals($expected, $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testInsertBlockWithoutName()
|
||||
{
|
||||
$parent = $this->_createBlockWithLayout('parent', 'parent');
|
||||
$block = $this->_createBlockWithLayout('');
|
||||
$parent->setChild('', $block);
|
||||
$this->assertContains('abstractblockmock_0', $parent->getChildNames());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testInsertBlockWithAlias()
|
||||
{
|
||||
$parent = $this->_createBlockWithLayout('parent', 'parent');
|
||||
$block = $this->_createBlockWithLayout('block_name');
|
||||
$parent->insert($block, '', true, 'block_alias');
|
||||
$this->assertContains('block_name', $parent->getChildNames());
|
||||
$this->assertSame($block, $parent->getChildBlock('block_alias'));
|
||||
}
|
||||
|
||||
public function testInsertWithSibling()
|
||||
{
|
||||
$name1 = 'block_one';
|
||||
$parent = $this->_createBlockWithLayout('parent', 'parent');
|
||||
$blockOne = $this->_createBlockWithLayout($name1);
|
||||
$parent->insert($blockOne);
|
||||
$this->assertContains($name1, $parent->getChildNames());
|
||||
|
||||
$name2 = 'block_two';
|
||||
$blockTwo = $this->_createBlockWithLayout($name2);
|
||||
$parent->insert($blockTwo, 'wrong_sibling', false);
|
||||
$this->assertSame(1, array_search($name2, $parent->getChildNames()));
|
||||
|
||||
$name3 = 'block_three';
|
||||
$blockThree = $this->_createBlockWithLayout($name3);
|
||||
$parent->insert($blockThree, $name2, false);
|
||||
$this->assertSame(1, array_search($name3, $parent->getChildNames()));
|
||||
|
||||
$name4 = 'block_four';
|
||||
$blockFour = $this->_createBlockWithLayout($name4);
|
||||
$parent->insert($blockFour, $name1, true);
|
||||
$this->assertSame(1, array_search($name4, $parent->getChildNames()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testInsertWithoutCreateBlock()
|
||||
{
|
||||
$this->expectException(\OutOfBoundsException::class);
|
||||
|
||||
$parent = $this->_createBlockWithLayout('parent', 'parent');
|
||||
$parent->insert('block');
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testInsertContainer()
|
||||
{
|
||||
$parentName = 'parent';
|
||||
$name = 'container';
|
||||
$parent = $this->_createBlockWithLayout($parentName, $parentName);
|
||||
$layout = $parent->getLayout();
|
||||
|
||||
$this->assertEmpty($layout->getChildNames($parentName));
|
||||
$layout->addContainer($name, 'Container');
|
||||
$parent->insert($name);
|
||||
$this->assertEquals([$name], $layout->getChildNames($parentName));
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
*/
|
||||
public function testAppend()
|
||||
{
|
||||
$parent = $this->_createBlockWithLayout('parent', 'parent');
|
||||
$child1 = $this->_createBlockWithLayout('child1');
|
||||
$parent->append($child1, 'child1');
|
||||
$child2 = $this->_createBlockWithLayout('child2');
|
||||
$parent->append($child2);
|
||||
$this->assertEquals(['child1', 'child2'], $parent->getChildNames());
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoAppIsolation enabled
|
||||
* @covers \Magento\Framework\View\Element\AbstractBlock::getGroupChildNames
|
||||
* @covers \Magento\Framework\View\Layout::addToParentGroup
|
||||
*/
|
||||
public function testAddToParentGroup()
|
||||
{
|
||||
$parent = $this->_createBlockWithLayout('parent', 'parent');
|
||||
$block1 = $this->_createBlockWithLayout('block1', 'block1');
|
||||
$block2 = $this->_createBlockWithLayout('block2', 'block2');
|
||||
$parent->setChild('block1', $block1)->setChild('block2', $block2);
|
||||
$this->_layout->addToParentGroup('block1', 'group');
|
||||
$this->_layout->addToParentGroup('block2', 'group');
|
||||
$group = $parent->getGroupChildNames('group');
|
||||
$this->assertContains('block1', $group);
|
||||
$this->assertContains('block2', $group);
|
||||
$this->assertSame($group[0], 'block1');
|
||||
$this->assertSame($group[1], 'block2');
|
||||
}
|
||||
|
||||
public function testGetChildData()
|
||||
{
|
||||
$parent = $this->_createBlockWithLayout('parent', 'parent');
|
||||
$block = $this->_createBlockWithLayout('block', 'block');
|
||||
$block->setSomeProperty('some_value');
|
||||
$parent->setChild('block1', $block);
|
||||
|
||||
// all child data
|
||||
$actualChildData = $parent->getChildData('block1');
|
||||
$this->assertArrayHasKey('some_property', $actualChildData);
|
||||
$this->assertEquals('some_value', $actualChildData['some_property']);
|
||||
|
||||
// specific child data key
|
||||
$this->assertEquals('some_value', $parent->getChildData('block1', 'some_property'));
|
||||
|
||||
// non-existing child block
|
||||
$this->assertNull($parent->getChildData('unknown_block'));
|
||||
}
|
||||
|
||||
public function testGetUrl()
|
||||
{
|
||||
$base = 'http://localhost/index.php/';
|
||||
$withRoute = "{$base}catalog/product/view/id/10/";
|
||||
$this->assertEquals($base, $this->_block->getUrl());
|
||||
$this->assertEquals($withRoute, $this->_block->getUrl('catalog/product/view', ['id' => 10]));
|
||||
}
|
||||
|
||||
public function testGetViewFileUrl()
|
||||
{
|
||||
$actualResult = $this->_block->getViewFileUrl('css/styles.css');
|
||||
$this->assertStringMatchesFormat(
|
||||
'http://localhost/static/%s/frontend/%s/en_US/css/styles.css',
|
||||
$actualResult
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetModuleName()
|
||||
{
|
||||
$this->assertEquals('Magento_Theme', $this->_block->getModuleName());
|
||||
$this->assertEquals('Magento_Theme', $this->_block->getData('module_name'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider escapeHtmlDataProvider
|
||||
*/
|
||||
public function testEscapeHtml($data, $expected)
|
||||
{
|
||||
$actual = $this->_block->escapeHtml($data);
|
||||
$this->assertEquals($expected, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function escapeHtmlDataProvider()
|
||||
{
|
||||
return [
|
||||
'array data' => [
|
||||
'data' => ['one', '<two>three</two>'],
|
||||
'expected' => ['one', '<two>three</two>'],
|
||||
],
|
||||
'string data conversion' => [
|
||||
'data' => '<two>three</two>',
|
||||
'expected' => '<two>three</two>',
|
||||
],
|
||||
'string data no conversion' => ['data' => 'one', 'expected' => 'one']
|
||||
];
|
||||
}
|
||||
|
||||
public function testStripTags()
|
||||
{
|
||||
$str = '<p>text</p>';
|
||||
$this->assertEquals('text', $this->_block->stripTags($str));
|
||||
}
|
||||
|
||||
public function testEscapeUrl()
|
||||
{
|
||||
$url = 'http://example.com/?wsdl=1';
|
||||
$this->assertEquals($url, $this->_block->escapeUrl($url));
|
||||
}
|
||||
|
||||
public function testEscapeJsQuote()
|
||||
{
|
||||
$script = "var s = 'text';";
|
||||
$this->assertEquals('var s = \\\'text\\\';', $this->_block->escapeJsQuote($script));
|
||||
}
|
||||
|
||||
public function testGetCacheKeyInfo()
|
||||
{
|
||||
$name = uniqid('block.');
|
||||
$block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
)->createBlock(
|
||||
\Magento\Framework\View\Element\Text::class
|
||||
);
|
||||
$block->setNameInLayout($name);
|
||||
$this->assertEquals([$name], $block->getCacheKeyInfo());
|
||||
}
|
||||
|
||||
public function testGetCacheKey()
|
||||
{
|
||||
$name = uniqid('block.');
|
||||
$block = Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
)->createBlock(
|
||||
\Magento\Framework\View\Element\Text::class
|
||||
);
|
||||
$block->setNameInLayout($name);
|
||||
$key = $block->getCacheKey();
|
||||
$this->assertNotEmpty($key);
|
||||
$this->assertNotEquals('key', $key);
|
||||
$this->assertNotEquals($name, $key);
|
||||
|
||||
$block->setCacheKey('key');
|
||||
$this->assertEquals(AbstractBlock::CACHE_KEY_PREFIX . 'key', $block->getCacheKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that SIDs inside blocks are not being replaced.
|
||||
*
|
||||
* @return void
|
||||
* @magentoCache block_html enabled
|
||||
*/
|
||||
public function testNoSid(): void
|
||||
{
|
||||
$blockId = 'block-with-sid' .Random::getRandomNumber(1, 9999);
|
||||
$block = $this->_createBlockWithLayout(
|
||||
$blockId,
|
||||
$blockId,
|
||||
\Magento\Framework\View\Element\Text::class
|
||||
);
|
||||
$outerId = 'block-outer' .Random::getRandomNumber(1, 9999);
|
||||
$outer = $this->_createBlockWithLayout($outerId, $outerId);
|
||||
$block->setText(
|
||||
$text = 'Some text with ' .SidResolverInterface::SESSION_ID_QUERY_PARAM
|
||||
.'=' .$this->session->getSessionId()
|
||||
);
|
||||
$block->setData('cache_lifetime', 3600);
|
||||
//Caching the block's content
|
||||
$outer->getBlockHtml($blockId);
|
||||
//New ID generated, must not be replace in block's content.
|
||||
$this->session->regenerateId();
|
||||
$html = $outer->getBlockHtml($blockId);
|
||||
$this->assertEquals($text, $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create <N> sample blocks
|
||||
*
|
||||
* @param int $qty
|
||||
* @param bool $withLayout
|
||||
* @param string $className
|
||||
* @return array
|
||||
*/
|
||||
protected function _createSampleBlocks(
|
||||
$qty,
|
||||
$withLayout = true,
|
||||
$className = \Magento\Framework\View\Element\Template::class
|
||||
) {
|
||||
$blocks = [];
|
||||
$names = [];
|
||||
$layout = false;
|
||||
if ($withLayout) {
|
||||
$layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
);
|
||||
}
|
||||
for ($i = 0; $i < $qty; $i++) {
|
||||
$name = uniqid('block.');
|
||||
if ($layout) {
|
||||
$block = $layout->createBlock($className, $name);
|
||||
} else {
|
||||
$block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create($className);
|
||||
$block->setNameInLayout($name);
|
||||
}
|
||||
$blocks[] = $block;
|
||||
$names[] = $name;
|
||||
}
|
||||
return [$blocks, $names];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Block with Layout
|
||||
*
|
||||
* @param string $name
|
||||
* @param null|string $alias
|
||||
* @param null|string $type
|
||||
* @return \Magento\Framework\View\Element\AbstractBlock
|
||||
*/
|
||||
protected function _createBlockWithLayout(
|
||||
$name = 'block',
|
||||
$alias = null,
|
||||
$type = AbstractBlock::class
|
||||
) {
|
||||
$typePart = explode('\\', $type);
|
||||
$mockClass = array_pop($typePart) . 'Mock';
|
||||
if (!isset(self::$_mocks[$mockClass])) {
|
||||
self::$_mocks[$mockClass] = $this->getMockForAbstractClass(
|
||||
$type,
|
||||
[
|
||||
Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\Element\Context::class
|
||||
),
|
||||
['module_name' => 'Magento_Theme']
|
||||
],
|
||||
$mockClass
|
||||
);
|
||||
}
|
||||
if ($this->_layout === null) {
|
||||
$this->_layout = Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
);
|
||||
}
|
||||
$block = $this->_layout->addBlock($mockClass, $name, '', $alias);
|
||||
return $block;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\View\Element;
|
||||
|
||||
use Magento\Framework\App\Area;
|
||||
|
||||
class TemplateTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\View\Element\Template
|
||||
*/
|
||||
protected $_block;
|
||||
|
||||
/**
|
||||
* @var \Magento\TestFramework\ObjectManager
|
||||
*/
|
||||
protected $objectManager;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $origMode;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$params = ['layout' => $this->objectManager->create(\Magento\Framework\View\Layout::class, [])];
|
||||
$context = $this->objectManager->create(\Magento\Framework\View\Element\Template\Context::class, $params);
|
||||
$this->_block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
)->createBlock(
|
||||
\Magento\Framework\View\Element\Template::class,
|
||||
'',
|
||||
['context' => $context]
|
||||
);
|
||||
|
||||
/** @var \Magento\TestFramework\App\State $appState */
|
||||
$appState = $this->objectManager->get(\Magento\TestFramework\App\State::class);
|
||||
$this->origMode = $appState->getMode();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
/** @var \Magento\TestFramework\App\State $appState */
|
||||
$appState = $this->objectManager->get(\Magento\TestFramework\App\State::class);
|
||||
$appState->setMode($this->origMode);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testConstruct()
|
||||
{
|
||||
$block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
)->createBlock(
|
||||
\Magento\Framework\View\Element\Template::class,
|
||||
'',
|
||||
['data' => ['template' => 'value']]
|
||||
);
|
||||
$this->assertEquals('value', $block->getTemplate());
|
||||
}
|
||||
|
||||
public function testSetGetTemplate()
|
||||
{
|
||||
$this->assertEmpty($this->_block->getTemplate());
|
||||
$this->_block->setTemplate('value');
|
||||
$this->assertEquals('value', $this->_block->getTemplate());
|
||||
}
|
||||
|
||||
public function testGetArea()
|
||||
{
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\App\State::class)
|
||||
->setAreaCode('frontend');
|
||||
$this->assertEquals('frontend', $this->_block->getArea());
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\App\State::class
|
||||
)->setAreaCode(Area::AREA_ADMINHTML);
|
||||
$this->assertEquals(Area::AREA_ADMINHTML, $this->_block->getArea());
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\App\State::class
|
||||
)->setAreaCode(Area::AREA_GLOBAL);
|
||||
$this->assertEquals(Area::AREA_GLOBAL, $this->_block->getArea());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Magento\Framework\View\Element\AbstractBlock::toHtml
|
||||
* @see testAssign()
|
||||
*/
|
||||
public function testToHtml()
|
||||
{
|
||||
\Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\App\State::class)
|
||||
->setAreaCode(Area::AREA_GLOBAL);
|
||||
/** @var \Magento\TestFramework\App\State $appState */
|
||||
$appState = $this->objectManager->get(\Magento\TestFramework\App\State::class);
|
||||
$appState->setMode(\Magento\TestFramework\App\State::MODE_DEFAULT);
|
||||
$this->assertEmpty($this->_block->toHtml());
|
||||
$this->_block->setTemplate(uniqid('invalid_filename.phtml'));
|
||||
$this->assertEmpty($this->_block->toHtml());
|
||||
}
|
||||
|
||||
public function testGetBaseUrl()
|
||||
{
|
||||
$this->assertEquals('http://localhost/index.php/', $this->_block->getBaseUrl());
|
||||
}
|
||||
|
||||
public function testGetObjectData()
|
||||
{
|
||||
$object = new \Magento\Framework\DataObject(['key' => 'value']);
|
||||
$this->assertEquals('value', $this->_block->getObjectData($object, 'key'));
|
||||
}
|
||||
|
||||
public function testGetCacheKeyInfo()
|
||||
{
|
||||
$this->_block->setTemplate('non-existing-template.phtml');
|
||||
$this->assertArrayHasKey('template', $this->_block->getCacheKeyInfo());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\View\Element\Text;
|
||||
|
||||
class ListTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\View\LayoutInterface
|
||||
*/
|
||||
protected $_layout;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\View\Element\Text\ListText
|
||||
*/
|
||||
protected $_block;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
);
|
||||
$this->_block = $this->_layout->createBlock(\Magento\Framework\View\Element\Text\ListText::class);
|
||||
}
|
||||
|
||||
public function testToHtml()
|
||||
{
|
||||
$children = [
|
||||
['block1', \Magento\Framework\View\Element\Text::class, 'text1'],
|
||||
['block2', \Magento\Framework\View\Element\Text::class, 'text2'],
|
||||
['block3', \Magento\Framework\View\Element\Text::class, 'text3'],
|
||||
];
|
||||
foreach ($children as $child) {
|
||||
$this->_layout->addBlock($child[1], $child[0], $this->_block->getNameInLayout())->setText($child[2]);
|
||||
}
|
||||
$html = $this->_block->toHtml();
|
||||
$this->assertEquals('text1text2text3', $html);
|
||||
}
|
||||
|
||||
public function testToHtmlWithContainer()
|
||||
{
|
||||
$listName = $this->_block->getNameInLayout();
|
||||
$block1 = $this->_layout->addBlock(\Magento\Framework\View\Element\Text::class, '', $listName);
|
||||
$this->_layout->addContainer('container', 'Container', [], $listName);
|
||||
$block2 = $this->_layout->addBlock(\Magento\Framework\View\Element\Text::class, '', 'container');
|
||||
$block3 = $this->_layout->addBlock(\Magento\Framework\View\Element\Text::class, '', $listName);
|
||||
$block1->setText('text1');
|
||||
$block2->setText('text2');
|
||||
$block3->setText('text3');
|
||||
$html = $this->_block->toHtml();
|
||||
$this->assertEquals('text1text2text3', $html);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\View\Element;
|
||||
|
||||
class TextTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\View\Element\Text
|
||||
*/
|
||||
protected $_block;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
|
||||
\Magento\Framework\View\LayoutInterface::class
|
||||
)->createBlock(
|
||||
\Magento\Framework\View\Element\Text::class
|
||||
);
|
||||
}
|
||||
|
||||
public function testSetGetText()
|
||||
{
|
||||
$this->_block->setText('text');
|
||||
$this->assertEquals('text', $this->_block->getText());
|
||||
}
|
||||
|
||||
public function testAddText()
|
||||
{
|
||||
$this->_block->addText('a');
|
||||
$this->assertEquals('a', $this->_block->getText());
|
||||
|
||||
$this->_block->addText('b');
|
||||
$this->assertEquals('ab', $this->_block->getText());
|
||||
|
||||
$this->_block->addText('c', false);
|
||||
$this->assertEquals('abc', $this->_block->getText());
|
||||
|
||||
$this->_block->addText('-', true);
|
||||
$this->assertEquals('-abc', $this->_block->getText());
|
||||
}
|
||||
|
||||
public function testToHtml()
|
||||
{
|
||||
$this->_block->setText('test');
|
||||
$this->assertEquals('test', $this->_block->toHtml());
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Framework\View\Element\UiComponent\Config\Provider;
|
||||
|
||||
use \Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Helper\CacheCleaner;
|
||||
|
||||
/**
|
||||
* @magentoComponentsDir Magento/Framework/View/_files/UiComponent/theme
|
||||
* @magentoAppIsolation enabled
|
||||
* @magentoDbIsolation enabled
|
||||
*/
|
||||
class TemplateTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\View\Element\UiComponent\Config\Provider\Template
|
||||
*/
|
||||
private $model;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->registerThemes();
|
||||
$this->objectManager->addSharedInstance(
|
||||
$this->objectManager->create(
|
||||
\Magento\Framework\App\Arguments\ValidationState::class,
|
||||
['appMode' => 'default']
|
||||
),
|
||||
\Magento\Framework\App\Arguments\ValidationState::class
|
||||
);
|
||||
$this->model = $this->objectManager->create(
|
||||
\Magento\Framework\View\Element\UiComponent\Config\Provider\Template::class
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetTemplate()
|
||||
{
|
||||
$expected = file_get_contents(__DIR__ . '/../../../../_files/UiComponent/expected/config.xml');
|
||||
|
||||
\Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea('adminhtml');
|
||||
$this->objectManager->get(\Magento\Framework\View\DesignInterface::class)
|
||||
->setDesignTheme('FrameworkViewUiComponent/default');
|
||||
|
||||
$resultOne = $this->model->getTemplate('test.xml');
|
||||
$resultTwo = $this->model->getTemplate('test.xml');
|
||||
|
||||
$this->assertXmlStringEqualsXmlString($expected, $resultOne);
|
||||
$this->assertXmlStringEqualsXmlString($expected, $resultTwo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register themes in the fixture folder
|
||||
*/
|
||||
protected function registerThemes()
|
||||
{
|
||||
/** @var \Magento\Theme\Model\Theme\Registration $registration */
|
||||
$registration = $this->objectManager->get(
|
||||
\Magento\Theme\Model\Theme\Registration::class
|
||||
);
|
||||
$registration->register();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->objectManager->removeSharedInstance(
|
||||
\Magento\Framework\App\Arguments\ValidationState::class
|
||||
);
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Framework\View\Element\UiComponent;
|
||||
|
||||
use Magento\Framework\Api\Search\SearchCriteria;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Magento\Ui\Component\Form;
|
||||
use Magento\Ui\Component\FormFactory;
|
||||
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface;
|
||||
|
||||
/**
|
||||
* Test UI component context.
|
||||
*/
|
||||
class ContextTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var RequestInterface
|
||||
*/
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* @var ContextFactory
|
||||
*/
|
||||
private $contextFactory;
|
||||
|
||||
/**
|
||||
* @var FormFactory
|
||||
*/
|
||||
private $componentFactory;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->request = Bootstrap::getObjectManager()->get(RequestInterface::class);
|
||||
$this->contextFactory = Bootstrap::getObjectManager()->get(ContextFactory::class);
|
||||
$this->componentFactory = Bootstrap::getObjectManager()->get(FormFactory::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate provider for the test.
|
||||
*
|
||||
* @return DataProviderInterface
|
||||
*/
|
||||
private function generateMockProvider(): DataProviderInterface
|
||||
{
|
||||
/** @var DataProviderInterface|MockObject $mock */
|
||||
$mock = $this->getMockForAbstractClass(DataProviderInterface::class);
|
||||
$mock->method('getName')->willReturn('test');
|
||||
$mock->method('getPrimaryFieldName')->willReturn('id');
|
||||
$mock->method('getRequestFieldName')->willReturn('id');
|
||||
$mock->method('getData')->willReturn(['id' => ['some_field' => '${\'some_value\'}']]);
|
||||
$mock->method('getConfigData')->willReturn([]);
|
||||
$mock->method('getFieldMetaInfo')->willReturn([]);
|
||||
$mock->method('getFieldSetMetaInfo')->willReturn('id');
|
||||
$mock->method('getFieldsMetaInfo')->willReturn('id');
|
||||
$mock->method('getSearchCriteria')->willReturn(new SearchCriteria());
|
||||
$mock->method('getSearchResult')->willReturn([]);
|
||||
|
||||
return $mock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check processed provider data.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testGetDataSourceData(): void
|
||||
{
|
||||
$dataProvider = $this->generateMockProvider();
|
||||
$context = $this->contextFactory->create(['dataProvider' => $dataProvider]);
|
||||
/** @var Form $component */
|
||||
$component = $this->componentFactory->create(['context' => $context]);
|
||||
$this->request->setParams(['id' => 'id']);
|
||||
|
||||
$data = $context->getDataSourceData($component);
|
||||
$this->assertEquals(
|
||||
[
|
||||
'test' => [
|
||||
'type' => 'dataSource',
|
||||
'name' => 'test',
|
||||
'dataScope' => null,
|
||||
'config' => [
|
||||
'data' => ['some_field' => '${\'some_value\'}', '__disableTmpl' => ['some_field' => true]],
|
||||
'params' => [
|
||||
'namespace' => null,
|
||||
'id' => 'id'
|
||||
]]
|
||||
]
|
||||
],
|
||||
$data
|
||||
);
|
||||
}
|
||||
}
|
||||
+175
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Framework\View\Element\UiComponent\DataProvider;
|
||||
|
||||
use Magento\Framework\Api\Filter;
|
||||
use Magento\Framework\Api\Search\FilterGroup;
|
||||
use Magento\Framework\Api\Search\SearchCriteriaInterface;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Represents FilterPool methods test class
|
||||
*/
|
||||
class ReportingTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Customer/_files/five_repository_customers.php
|
||||
* @magentoDbIsolation disabled
|
||||
* @dataProvider filtersDataProvider
|
||||
* @param array $filters
|
||||
* @param int $expectedCount
|
||||
*/
|
||||
public function testSearchItemsByOrCondition(array $filters, int $expectedCount): void
|
||||
{
|
||||
$filterGroups = [];
|
||||
$filterGroups[] = $this->objectManager->create(FilterGroup::class)
|
||||
->setFilters(
|
||||
[
|
||||
$this->objectManager->create(Filter::class, ['data' => $filters[0]]),
|
||||
$this->objectManager->create(Filter::class, ['data' => $filters[1]]),
|
||||
]
|
||||
);
|
||||
$filterGroups[] = $this->objectManager->create(FilterGroup::class)
|
||||
->setFilters([$this->objectManager->create(Filter::class, ['data' => $filters[2]])]);
|
||||
if (isset($filters[3], $filters[4])) {
|
||||
$filterGroups[] = $this->objectManager->create(FilterGroup::class)
|
||||
->setFilters(
|
||||
[
|
||||
$this->objectManager->create(Filter::class, ['data' => $filters[3]]),
|
||||
$this->objectManager->create(Filter::class, ['data' => $filters[4]]),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/** @var SearchCriteriaInterface $searchCriteria */
|
||||
$searchCriteria = $this->objectManager->get(SearchCriteriaInterface::class);
|
||||
$searchCriteria->setFilterGroups($filterGroups);
|
||||
$searchCriteria->setRequestName('customer_listing_data_source');
|
||||
$searchCriteria->setSortOrders([]);
|
||||
|
||||
/** @var Reporting $reporting */
|
||||
$reporting = $this->objectManager->get(Reporting::class);
|
||||
$collection = $reporting->search($searchCriteria);
|
||||
self::assertCount($expectedCount, $collection->getItems(), 'Wrong collection filters applied');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array[]
|
||||
*/
|
||||
public function filtersDataProvider()
|
||||
{
|
||||
return [
|
||||
'variation 1 (filter OR filter) AND filter' => [
|
||||
'filters' => [
|
||||
[
|
||||
'field' => 'email',
|
||||
'value' => '%1%',
|
||||
'condition_type' => 'like',
|
||||
],
|
||||
[
|
||||
'field' => 'email',
|
||||
'value' => '%2%',
|
||||
'condition_type' => 'like',
|
||||
],
|
||||
[
|
||||
'field' => 'name',
|
||||
'value' => 'John Smith',
|
||||
'condition_type' => 'eq',
|
||||
],
|
||||
],
|
||||
'expected_count' => 2,
|
||||
],
|
||||
'variation 2 (filter OR filter) AND filter' => [
|
||||
'filters' => [
|
||||
[
|
||||
'field' => 'email',
|
||||
'value' => '%1%',
|
||||
'condition_type' => 'like',
|
||||
],
|
||||
[
|
||||
'field' => 'name',
|
||||
'value' => 'John Smith',
|
||||
'condition_type' => 'eq',
|
||||
],
|
||||
[
|
||||
'field' => 'email',
|
||||
'value' => '%example%',
|
||||
'condition_type' => 'like',
|
||||
],
|
||||
],
|
||||
'expected_count' => 5,
|
||||
],
|
||||
'variation 3 (filter OR filter) AND filter' => [
|
||||
'filters' => [
|
||||
[
|
||||
'field' => 'email',
|
||||
'value' => 'customer%',
|
||||
'condition_type' => 'like',
|
||||
],
|
||||
[
|
||||
'field' => 'name',
|
||||
'value' => 'John%',
|
||||
'condition_type' => 'like',
|
||||
],
|
||||
[
|
||||
'field' => 'email',
|
||||
'value' => 'customer2@example.com',
|
||||
'condition_type' => 'eq',
|
||||
],
|
||||
],
|
||||
'expected_count' => 1,
|
||||
],
|
||||
'variation (filter OR filter) AND filter AND (filter OR filter)' => [
|
||||
'filters' => [
|
||||
[
|
||||
'field' => 'email',
|
||||
'value' => 'customer%',
|
||||
'condition_type' => 'like',
|
||||
],
|
||||
[
|
||||
'field' => 'name',
|
||||
'value' => 'Test',
|
||||
'condition_type' => 'eq',
|
||||
],
|
||||
[
|
||||
'field' => 'email',
|
||||
'value' => 'customer%@example.com',
|
||||
'condition_type' => 'like',
|
||||
],
|
||||
[
|
||||
'field' => 'name',
|
||||
'value' => 'non existing',
|
||||
'condition_type' => 'like',
|
||||
],
|
||||
[
|
||||
'field' => 'email',
|
||||
'value' => 'customer3%',
|
||||
'condition_type' => 'like',
|
||||
],
|
||||
],
|
||||
'expected_count' => 1,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Framework\View\Element;
|
||||
|
||||
use Magento\Framework\Api\Search\SearchCriteria;
|
||||
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Magento\Framework\Config\DataInterface as ConfigData;
|
||||
use Magento\Framework\Config\DataInterfaceFactory as ConfigDataFactory;
|
||||
|
||||
/**
|
||||
* Test the component factory.
|
||||
*/
|
||||
class UiComponentFactoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var UiComponentFactoryFactory
|
||||
*/
|
||||
private $factory;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->factory = Bootstrap::getObjectManager()->get(UiComponentFactoryFactory::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create factory with mock config provided.
|
||||
*
|
||||
* @param array $mockConfig
|
||||
* @return UiComponentFactory
|
||||
*/
|
||||
private function createFactory(array $mockConfig): UiComponentFactory
|
||||
{
|
||||
$dataMock = $this->getMockForAbstractClass(ConfigData::class);
|
||||
$dataMock->method('get')->willReturnCallback(
|
||||
function (string $id) use ($mockConfig) : array {
|
||||
return $mockConfig[$id];
|
||||
}
|
||||
);
|
||||
$dataFactoryMock = $this->getMockBuilder(ConfigDataFactory::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$dataFactoryMock->method('create')->willReturn($dataMock);
|
||||
|
||||
return $this->factory->create(['configFactory' => $dataFactoryMock]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creating a component.
|
||||
*
|
||||
* @return void
|
||||
* @magentoAppArea adminhtml
|
||||
*/
|
||||
public function testCreate(): void
|
||||
{
|
||||
//Mocking component config.
|
||||
$factory = $this->createFactory([
|
||||
'test' => [
|
||||
'arguments' => ['data' => ['config' => ['component' => 'uiComponent']]],
|
||||
'attributes' => [
|
||||
'name' => 'test',
|
||||
'sorting' => true,
|
||||
'class' => 'Magento\Ui\Component\Listing',
|
||||
'component' => 'uiComponent'
|
||||
],
|
||||
'children' => [
|
||||
'test_child' => [
|
||||
'arguments' => [
|
||||
'data' => ['config' => ['component' => 'uiComponent']],
|
||||
'dataProvider' => $this->generateMockProvider()
|
||||
],
|
||||
'attributes' => [
|
||||
'name' => 'test_child',
|
||||
'sorting' => true,
|
||||
'class' => 'Magento\Ui\Component\Listing',
|
||||
'component' => 'uiComponent'
|
||||
],
|
||||
'children' => []
|
||||
]
|
||||
]
|
||||
],
|
||||
'test_child_child' => [
|
||||
'arguments' => [
|
||||
'data' => [
|
||||
'config' => [
|
||||
'component' => 'uiComponent',
|
||||
'label' => '${\'Label\'}',
|
||||
'componentType' => 'component'
|
||||
]
|
||||
],
|
||||
],
|
||||
'attributes' => [
|
||||
'name' => 'test_child_child',
|
||||
'sorting' => true,
|
||||
'class' => 'Magento\Ui\Component\Listing',
|
||||
'component' => 'uiComponent'
|
||||
],
|
||||
'children' => []
|
||||
]
|
||||
]);
|
||||
$component = $factory->create('test', null, ['data' => ['label' => '${\'Label\'}']]);
|
||||
|
||||
$componentData = $component->getData();
|
||||
//Arguments passed must be sanitized
|
||||
$this->assertArrayHasKey('__disableTmpl', $componentData);
|
||||
$this->assertEquals(['label' => true], $componentData['__disableTmpl']);
|
||||
//Metadata provided by the dataProvider must be sanitized as well.
|
||||
$this->assertArrayHasKey('test_child_child', $childData = $component->getChildComponents());
|
||||
$childData = $component->getChildComponents()['test_child_child']->getData()['config'];
|
||||
$this->assertArrayHasKey('__disableTmpl', $childData);
|
||||
$this->assertEquals(['label' => true], $childData['__disableTmpl']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate provider for the test.
|
||||
*
|
||||
* @return DataProviderInterface
|
||||
*/
|
||||
private function generateMockProvider(): DataProviderInterface
|
||||
{
|
||||
/** @var DataProviderInterface|MockObject $mock */
|
||||
$mock = $this->getMockForAbstractClass(DataProviderInterface::class);
|
||||
$mock->method('getName')->willReturn('test');
|
||||
$mock->method('getPrimaryFieldName')->willReturn('id');
|
||||
$mock->method('getRequestFieldName')->willReturn('id');
|
||||
$mock->method('getData')->willReturn([]);
|
||||
$mock->method('getConfigData')->willReturn([]);
|
||||
$mock->method('getFieldMetaInfo')->willReturn([]);
|
||||
$mock->method('getFieldSetMetaInfo')->willReturn('id');
|
||||
$mock->method('getFieldsMetaInfo')->willReturn('id');
|
||||
$mock->method('getSearchCriteria')->willReturn(new SearchCriteria());
|
||||
$mock->method('getSearchResult')->willReturn([]);
|
||||
$mock->method('getMeta')->willReturn(
|
||||
[
|
||||
'test_child_child' => [
|
||||
'arguments' => [
|
||||
'data' => [
|
||||
'config' => [
|
||||
'component' => 'uiComponent',
|
||||
'label' => '${\'Label\'}',
|
||||
'componentType' => 'component'
|
||||
]
|
||||
],
|
||||
],
|
||||
'attributes' => [
|
||||
'name' => 'test_child_child',
|
||||
'sorting' => true,
|
||||
'class' => 'Magento\Ui\Component\Listing',
|
||||
'component' => 'uiComponent'
|
||||
]
|
||||
]
|
||||
]
|
||||
);
|
||||
|
||||
return $mock;
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
.test {background: url(../images/nonexistent_file.png);}
|
||||
Reference in New Issue
Block a user