Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

magento/magento2: Fixes for the schema cache.xsd #27307

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento\Test\Integrity\Magento\Framework\Cache;

use Magento\Framework\Config\Dom\UrnResolver;
use Magento\Framework\TestFramework\Unit\Utility\XsdValidator;
use PHPUnit\Framework\TestCase;

/**
* Unit test of the cache configuration
*/
class ConfigTest extends TestCase
{
/**
* Path to xsd schema file
* @var string
*/
private $xsdSchema;

/**
* @var UrnResolver
*/
private $urnResolver;

/**
* @var XsdValidator
*/
private $xsdValidator;

/**
* Setup environment for test
*/
protected function setUp(): void
{
if (!function_exists('libxml_set_external_entity_loader')) {
$this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
}
$this->urnResolver = new UrnResolver();
$this->xsdSchema = $this->urnResolver->getRealPath(
'urn:magento:framework:Cache/etc/cache.xsd'
);
$this->xsdValidator = new XsdValidator();
}

/**
* Tests invalid configurations
*
* @param string $xmlString
* @param array $expectedError
* @dataProvider schemaCorrectlyIdentifiesInvalidXmlDataProvider
*/
public function testSchemaCorrectlyIdentifiesInvalidXml(
string $xmlString,
array $expectedError
): void {
$actualError = $this->xsdValidator->validate(
$this->xsdSchema,
$xmlString
);
$this->assertEquals($expectedError, $actualError);
}

/**
* Tests valid configurations
*/
public function testSchemaCorrectlyIdentifiesValidXml(): void
{
$xmlString = file_get_contents(__DIR__ . '/_files/valid_cache_config.xml');
$actualResult = $this->xsdValidator->validate(
$this->xsdSchema,
$xmlString
);

$this->assertEmpty($actualResult);
}

/**
* Data provider with invalid xml array according to cache.xsd
*/
public function schemaCorrectlyIdentifiesInvalidXmlDataProvider(): array
{
return include __DIR__ . '/_files/invalidCacheConfigXmlArray.php';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting approach to provide code samples.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @lbajsarowicz
Thank you for your remark.
Magento already has the same data providers. Please take a look at the screenshot
interesting-data-providers
Thank you in advance.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
return [
'without_type_handle' => [
'<?xml version="1.0"?><config></config>',
["Element 'config': Missing child element(s). Expected is ( type ).\nLine: 1\n"],
],
'cache_config_with_notallowed_attribute' => [
'<?xml version="1.0"?><config>' .
'<type name="test" translate="label,description" instance="Class\Name" notallowed="some value">' .
'<label>Test</label><description>Test</description></type></config>',
["Element 'type', attribute 'notallowed': The attribute 'notallowed' is not allowed.\nLine: 1\n"],
],
'cache_config_without_name_attribute' => [
'<?xml version="1.0"?><config><type translate="label,description" instance="Class\Name">' .
'<label>Test</label><description>Test</description></type></config>',
["Element 'type': The attribute 'name' is required but missing.\nLine: 1\n"],
],
'cache_config_without_instance_attribute' => [
'<?xml version="1.0"?><config><type name="test" translate="label,description">' .
'<label>Test</label><description>Test</description></type></config>',
["Element 'type': The attribute 'instance' is required but missing.\nLine: 1\n"],
],
'cache_config_without_label_element' => [
'<?xml version="1.0"?><config><type name="test" translate="label,description" instance="Class\Name">' .
'<description>Test</description></type></config>',
["Element 'type': Missing child element(s). Expected is ( label ).\nLine: 1\n"],
],
'cache_config_without_description_element' => [
'<?xml version="1.0"?><config><type name="test" translate="label,description" instance="Class\Name">' .
'<label>Test</label></type></config>',
["Element 'type': Missing child element(s). Expected is ( description ).\nLine: 1\n"],
],
'cache_config_without_child_elements' => [
'<?xml version="1.0"?><config><type name="test" translate="label,description" instance="Class\Name">' .
'</type></config>',
["Element 'type': Missing child element(s). Expected is one of ( label, description ).\nLine: 1\n"],
],
'cache_config_cache_name_not_unique' => [
'<?xml version="1.0"?><config><type name="test" translate="label,description" instance="Class\Name1">' .
'<label>Test1</label><description>Test1</description></type>' .
'<type name="test" translate="label,description" instance="Class\Name2">' .
'<label>Test2</label><description>Test2</description></type></config>',
[
"Element 'type': Duplicate key-sequence ['test'] in unique identity-constraint"
. " 'uniqueCacheName'.\nLine: 1\n"
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
<type name="type_name_1" translate="label,description" instance="Instance1\Class\Name">
<label>Type1</label>
<description>Type1</description>
</type>
<type name="type_name_2" translate="label,description" instance="Instance2\Class\Name">
<label>Type2</label>
<description>Type2</description>
</type>
</config>
9 changes: 5 additions & 4 deletions lib/internal/Magento/Framework/App/Cache/TypeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\SerializerInterface;

/**
* Application cache type list
*/
class TypeList implements TypeListInterface
{
const INVALIDATED_TYPES = 'core_cache_invalidate';
Expand Down Expand Up @@ -68,9 +71,7 @@ public function __construct(
protected function _getTypeInstance($type)
{
$config = $this->_config->getType($type);
if (!isset($config['instance'])) {
return null;
}

return $this->_factory->get($config['instance']);
}

Expand Down Expand Up @@ -132,7 +133,7 @@ public function getTypes()
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getTypeLabels()
{
Expand Down
Loading