This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,270 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Ui\Component;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Component\ComponentFile;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Component\DirSearch;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\ValidatorException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\ReadInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Ui\Config\Reader\DefinitionMap;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ConfigurationTest extends \PHPUnit\Framework\TestCase
{
/**
* @var DirSearch
*/
private $dirSearch;
/**
* @var ReadInterface
*/
private $appDir;
/**
* @var ReadInterface
*/
private $rootDir;
/**
* @var \DOMDocument
*/
private $dom;
/**
* @var array
*/
private $map;
/**
* @var string
*/
private $currentComponent;
/**
* @var ComponentFile
*/
private $currentFile;
/**
* @var array
*/
private $whiteList = [
'argument[@name="data"]/item[@name="config"]/item[@name="multiple"]' => [
'//*[@formElement="select"]',
'//*[substring(@component, string-length(@component) - string-length("ui-group") +1) = "ui-group"]'
]
];
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$mapReader = $objectManager->create(DefinitionMap::class);
$this->map = $mapReader->read();
$this->dirSearch = $objectManager->create(DirSearch::class);
/** @var Filesystem $filesystem */
$filesystem = $objectManager->create(Filesystem::class);
$this->appDir = $filesystem->getDirectoryRead(DirectoryList::APP);
$this->rootDir = $filesystem->getDirectoryRead(DirectoryList::ROOT);
}
/**
* @return void
*/
public function testConfiguration()
{
$uiConfigurationFiles = $this->dirSearch->collectFilesWithContext(
ComponentRegistrar::MODULE,
'view/*/ui_component/*.xml'
);
$this->generateXpaths();
$result = [];
/** @var ComponentFile $file */
foreach ($uiConfigurationFiles as $file) {
$this->currentFile = $file;
$fullPath = $file->getFullPath();
// by default search files in `app` directory but Magento can be installed via composer
// or some modules can be in `vendor` directory (like bundled extensions)
try {
$content = $this->appDir->readFile($this->appDir->getRelativePath($fullPath));
} catch (ValidatorException $e) {
$content = $this->rootDir->readFile($this->rootDir->getRelativePath($fullPath));
}
$this->assertConfigurationSemantic($this->getDom($content), $result);
}
if (!empty($result)) {
$this->fail(implode("\n\n", $result));
}
}
/**
* @return void
*/
private function generateXpaths()
{
foreach ($this->map as $name => &$map) {
$this->currentComponent = $name;
$xpaths = [];
$counter = 0;
while (!empty($map)) {
$this->hasXpaths($map, $xpaths, $counter);
$counter++;
}
$this->map[$name]['xpaths'] = $xpaths;
}
}
/**
* @param \DOMNode $node
* @param array $result
* @return void
*/
private function assertConfigurationSemantic(\DOMNode $node, &$result = [])
{
foreach ($node->childNodes as $child) {
if ($child->nodeType === XML_ELEMENT_NODE) {
if (isset($this->map[$child->localName])) {
$xpaths = [];
$this->currentComponent = $child->localName;
if (isset($this->map[$this->currentComponent]['xpaths'])) {
$xpaths = $this->map[$this->currentComponent]['xpaths'];
}
$domXpath = new \DOMXPath($this->getDom());
foreach ($xpaths as $xpathData) {
if ($domXpath->query($xpathData['xpath'], $child)->length !== 0
&& !$this->isAvailable($xpathData['xpath'], $child)
) {
$result[] = 'Xpath: "' . $xpathData['xpath'] . '" is a forbidden.' . "\n" .
'This node should migrate to "' . trim($xpathData['target']) . "\"\n" .
'File: ' . $this->currentFile->getFullPath() . "\n";
}
}
}
$this->assertConfigurationSemantic($child, $result);
}
}
}
/**
* @param string $targetXpath
* @param \DOMElement $node
* @return bool
*/
private function isAvailable($targetXpath, \DOMElement $node)
{
$domXpath = new \DOMXPath($this->getDom());
if (isset($this->whiteList[$targetXpath])) {
$availableForXpath = $this->whiteList[$targetXpath];
foreach ($availableForXpath as $xpath) {
$result = $domXpath->query($xpath, $node);
if ($result->length != 0) {
return true;
}
}
}
return false;
}
/**
* @param array $data
* @param array $result
* @param int $counter
* @return bool
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function hasXpaths(array &$data, array &$result, $counter)
{
foreach ($data as $name => &$child) {
if (!is_array($child)) {
continue;
}
if (isset($child['value'])) {
$result[$counter]['xpath'] .= '[@name="' . $child['name'] . '"]';
$result[$counter]['target'] = $child['value'];
unset($data[$name]);
$this->deleteEmptyNodes($this->map[$this->currentComponent]);
return true;
}
if (isset($child['name']) && is_string($child['name'])) {
$result[$counter]['xpath'] .= '[@name="' . $child['name'] . '"]';
$break = false;
if (isset($child['item'])) {
$result[$counter]['xpath'] .= '/item';
$break = $this->hasXpaths($child['item'], $result, $counter);
} elseif (isset($child['argument'])) {
$result[$counter]['xpath'] .= '/argument';
$break = $this->hasXpaths($child['argument'], $result, $counter);
}
if ($break) {
return true;
}
}
if (!isset($result[$counter]['xpath'])) {
$result[$counter]['xpath'] = '';
}
$result[$counter]['xpath'] .= $name;
$this->hasXpaths($child, $result, $counter);
}
return true;
}
/**
* @param array $map
* @param bool $isRemoveParentNode
* @return bool
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function deleteEmptyNodes(array &$map, $isRemoveParentNode = false)
{
if (empty($map)) {
return true;
}
foreach ($map as $name => &$child) {
if (is_array($child)) {
$isRemoveParentNode = $this->deleteEmptyNodes($map[$name]);
}
if (empty($map[$name]) || $isRemoveParentNode) {
if ((isset($map['item']) && empty($map['item']))
|| (isset($map['argument']) && empty($map['argument']))
) {
unset($map[$name]);
return true;
}
unset($map[$name]);
}
}
return false;
}
/**
* @param string|null $content
* @return \DOMDocument
*/
private function getDom($content = null)
{
if ($content) {
$this->dom = new \DOMDocument();
$this->dom->loadXML($content);
}
return $this->dom;
}
}
@@ -0,0 +1,83 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Ui\Component\Control;
use Magento\Framework\View\LayoutInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Test for the button control.
*
* @magentoAppArea frontend
*/
class ButtonTest extends TestCase
{
/**
* @var LayoutInterface
*/
private $layout;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->layout = $objectManager->get(LayoutInterface::class);
}
/**
* Create the block.
*
* @return Button
*/
private function createBlock(): Button
{
/** @var Button $block */
$block = $this->layout->createBlock(Button::class, 'button_block');
$block->setLayout($this->layout);
return $block;
}
/**
* Test resulting button HTML.
*
* @return void
*/
public function testToHtml(): void
{
$block = $this->createBlock();
$block->addData(
[
'type' => 'button',
'on_click' => $onclick = 'console.log("Button pressed!")',
'disabled' => false,
'title' => 'A button control',
'label' => 'A button control',
'class' => 'button',
'id' => 'button',
'element_name' => 'some-name',
'value' => 'Press a button',
'data-style' => 'width: 100px',
'style' => 'height: 200px'
]
);
$html = $block->toHtml();
$this->assertStringContainsString('<button ', $html);
$this->assertStringContainsString('<span>A button control</span>', $html);
$this->assertStringNotContainsString('onclick=', $html);
$this->assertStringNotContainsString('style=', $html);
$this->assertMatchesRegularExpression('/\<script.*?\>.*?' .preg_quote($onclick) .'.*?\<\/script\>/ims', $html);
$this->assertStringContainsString('height', $html);
$this->assertStringContainsString('200px', $html);
}
}
@@ -0,0 +1,97 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Ui\Component\Control;
use Magento\Framework\View\LayoutInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Testing SplitButton widget
*
* @magentoAppArea frontend
*/
class SplitButtonTest extends TestCase
{
/**
* @var LayoutInterface
*/
private $layout;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
$this->layout = $objectManager->get(LayoutInterface::class);
}
/**
* Create the block.
*
* @return SplitButton
*/
private function createBlock(): SplitButton
{
/** @var SplitButton $block */
$block = $this->layout->createBlock(SplitButton::class, 'button_block');
$block->setLayout($this->layout);
return $block;
}
/**
* Test resulting button HTML.
*
* @return void
*/
public function testToHtml(): void
{
$block = $this->createBlock();
$block->addData(
[
'title' => 'Split button control',
'label' => 'Split button control',
'has_split' => true,
'button_class' => 'aclass',
'id' => 'split-button',
'disabled' => false,
'class' => 'aclass',
'data_attribute' => ['bind' => ['var' => 'val']],
'id_hard' => 'split-button',
'options' => [
[
'id' => 'an-option',
'disabled' => false,
'title' => 'An option',
'label' => 'An option',
'onclick' => $onclick = 'console.log("option")',
'style' => 'width: 100px'
]
],
'dropdown_button_aria_label' => 'Split button options',
]
);
$html = $block->toHtml();
$this->assertStringContainsString('<button ', $html);
$this->assertStringContainsString('<span>Split button control</span>', $html);
$this->assertStringNotContainsString('onclick=', $html);
$this->assertStringNotContainsString('style=', $html);
$this->assertMatchesRegularExpression(
'/\<script.*?\>.*?' . preg_quote($onclick) . '.*?\<\/script\>/ims',
$html
);
$this->assertStringContainsString('width', $html);
$this->assertStringContainsString('100px', $html);
$this->assertStringContainsString('aria-label="Split button options"', $html);
}
}
@@ -0,0 +1,65 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Ui\Component\Form\Element\DataType;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Test for date component.
*/
class DateTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var DateFactory */
private $dateFactory;
/** @var ResolverInterface */
private $localeResolver;
/**
* @inheritDoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->dateFactory = $this->objectManager->get(DateFactory::class);
$this->localeResolver = $this->objectManager->get(ResolverInterface::class);
}
/**
* @dataProvider localeDataProvider
*
* @param string $locale
* @param string $dateFormat
* @return void
*/
public function testDateFormat(string $locale, string $dateFormat): void
{
$this->localeResolver->setLocale($locale);
$date = $this->dateFactory->create();
$date->prepare();
$this->assertEquals($dateFormat, $date->getData('config')['options']['dateFormat']);
}
/**
* @return array
*/
public function localeDataProvider(): array
{
return [
['en_GB', 'dd/MM/y'], ['en_US', 'M/d/yy'],
];
}
}
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Ui\Component\Form\Element;
use Magento\Framework\Data\OptionSourceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Test MultiSelect component.
*/
class MultiSelectTest extends TestCase
{
/**
* @var MultiSelectFactory
*/
private $factory;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->factory = Bootstrap::getObjectManager()->get(MultiSelectFactory::class);
}
/**
* Options data to verify
*
* @return array
*/
public function getTestOptions(): array
{
return [
'List' => [
[
['value' => '${\'my-value\'}', 'label' => 'My label'],
['value' => '1', 'label' => 'Label'],
['value' => '${\'my-value-2\'}', 'label' => 'This is ${\'My label\'}']
],
[
['value' => '${\'my-value\'}', 'label' => 'My label', '__disableTmpl' => ['value' => true]],
['value' => '1', 'label' => 'Label'],
[
'value' => '${\'my-value-2\'}',
'label' => 'This is ${\'My label\'}',
'__disableTmpl' => ['value' => true, 'label' => true]
]
]
],
'provider' => [
new class implements OptionSourceInterface
{
/**
* @inheritDoc
*/
public function toOptionArray()
{
return [['value' => '${\'value\'}', 'label' => 'Test']];
}
},
[['value' => '${\'value\'}', 'label' => 'Test', '__disableTmpl' => ['value' => true]]]
]
];
}
/**
* Check that options received from an options provider properly initiated.
*
* @param array|OptionSourceInterface $options Options provided
* @param array $expected Expected initialized options
* @return void
* @dataProvider getTestOptions
*/
public function testOptions($options, array $expected): void
{
/** @var MultiSelect $component */
$component = $this->factory->create(['options' => $options]);
$component->prepare();
$this->assertEquals($expected, $component->getData('config')['options']);
}
}