Skip to content

Commit 97bd66e

Browse files
andrewbessAndrii Beziazychnyi
authored and
Andrii Beziazychnyi
committed
magento/magento2: fixes for the cache configuration schema
1 parent 8eb9deb commit 97bd66e

File tree

11 files changed

+286
-150
lines changed

11 files changed

+286
-150
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types = 1);
7+
8+
namespace Magento\Test\Integrity\Magento\Framework\Cache;
9+
10+
use Magento\Framework\Config\Dom\UrnResolver;
11+
use Magento\Framework\TestFramework\Unit\Utility\XsdValidator;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Unit test of the cache configuration
16+
*/
17+
class ConfigTest extends TestCase
18+
{
19+
/**
20+
* Path to xsd schema file
21+
* @var string
22+
*/
23+
private $xsdSchema;
24+
25+
/**
26+
* @var UrnResolver
27+
*/
28+
private $urnResolver;
29+
30+
/**
31+
* @var XsdValidator
32+
*/
33+
private $xsdValidator;
34+
35+
/**
36+
* Setup environment for test
37+
*/
38+
protected function setUp(): void
39+
{
40+
if (!function_exists('libxml_set_external_entity_loader')) {
41+
$this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
42+
}
43+
$this->urnResolver = new UrnResolver();
44+
$this->xsdSchema = $this->urnResolver->getRealPath(
45+
'urn:magento:framework:Cache/etc/cache.xsd'
46+
);
47+
$this->xsdValidator = new XsdValidator();
48+
}
49+
50+
/**
51+
* Tests invalid configurations
52+
*
53+
* @param string $xmlString
54+
* @param array $expectedError
55+
* @dataProvider schemaCorrectlyIdentifiesInvalidXmlDataProvider
56+
*/
57+
public function testSchemaCorrectlyIdentifiesInvalidXml(
58+
string $xmlString,
59+
array $expectedError
60+
): void {
61+
$actualError = $this->xsdValidator->validate(
62+
$this->xsdSchema,
63+
$xmlString
64+
);
65+
$this->assertEquals($expectedError, $actualError);
66+
}
67+
68+
/**
69+
* Tests valid configurations
70+
*/
71+
public function testSchemaCorrectlyIdentifiesValidXml(): void
72+
{
73+
$xmlString = file_get_contents(__DIR__ . '/_files/valid_cache_config.xml');
74+
$actualResult = $this->xsdValidator->validate(
75+
$this->xsdSchema,
76+
$xmlString
77+
);
78+
79+
$this->assertEmpty($actualResult);
80+
}
81+
82+
/**
83+
* Data provider with invalid xml array according to cache.xsd
84+
*/
85+
public function schemaCorrectlyIdentifiesInvalidXmlDataProvider(): array
86+
{
87+
return include __DIR__ . '/_files/invalidCacheConfigXmlArray.php';
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
return [
7+
'without_type_handle' => [
8+
'<?xml version="1.0"?><config></config>',
9+
["Element 'config': Missing child element(s). Expected is ( type ).\nLine: 1\n"],
10+
],
11+
'cache_config_with_notallowed_attribute' => [
12+
'<?xml version="1.0"?><config>' .
13+
'<type name="test" translate="label,description" instance="Class\Name" notallowed="some value">' .
14+
'<label>Test</label><description>Test</description></type></config>',
15+
["Element 'type', attribute 'notallowed': The attribute 'notallowed' is not allowed.\nLine: 1\n"],
16+
],
17+
'cache_config_without_name_attribute' => [
18+
'<?xml version="1.0"?><config><type translate="label,description" instance="Class\Name">' .
19+
'<label>Test</label><description>Test</description></type></config>',
20+
["Element 'type': The attribute 'name' is required but missing.\nLine: 1\n"],
21+
],
22+
'cache_config_without_instance_attribute' => [
23+
'<?xml version="1.0"?><config><type name="test" translate="label,description">' .
24+
'<label>Test</label><description>Test</description></type></config>',
25+
["Element 'type': The attribute 'instance' is required but missing.\nLine: 1\n"],
26+
],
27+
'cache_config_without_label_element' => [
28+
'<?xml version="1.0"?><config><type name="test" translate="label,description" instance="Class\Name">' .
29+
'<description>Test</description></type></config>',
30+
["Element 'type': Missing child element(s). Expected is ( label ).\nLine: 1\n"],
31+
],
32+
'cache_config_without_description_element' => [
33+
'<?xml version="1.0"?><config><type name="test" translate="label,description" instance="Class\Name">' .
34+
'<label>Test</label></type></config>',
35+
["Element 'type': Missing child element(s). Expected is ( description ).\nLine: 1\n"],
36+
],
37+
'cache_config_without_child_elements' => [
38+
'<?xml version="1.0"?><config><type name="test" translate="label,description" instance="Class\Name">' .
39+
'</type></config>',
40+
["Element 'type': Missing child element(s). Expected is one of ( label, description ).\nLine: 1\n"],
41+
],
42+
'cache_config_cache_name_not_unique' => [
43+
'<?xml version="1.0"?><config><type name="test" translate="label,description" instance="Class\Name1">' .
44+
'<label>Test1</label><description>Test1</description></type>' .
45+
'<type name="test" translate="label,description" instance="Class\Name2">' .
46+
'<label>Test2</label><description>Test2</description></type></config>',
47+
[
48+
"Element 'type': Duplicate key-sequence ['test'] in unique identity-constraint"
49+
. " 'uniqueCacheName'.\nLine: 1\n"
50+
],
51+
],
52+
];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
9+
<type name="type_name_1" translate="label,description" instance="Instance1\Class\Name">
10+
<label>Type1</label>
11+
<description>Type1</description>
12+
</type>
13+
<type name="type_name_2" translate="label,description" instance="Instance2\Class\Name">
14+
<label>Type2</label>
15+
<description>Type2</description>
16+
</type>
17+
</config>

lib/internal/Magento/Framework/App/Cache/TypeList.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Magento\Framework\App\ObjectManager;
99
use Magento\Framework\Serialize\SerializerInterface;
1010

11+
/**
12+
* Application cache type list
13+
*/
1114
class TypeList implements TypeListInterface
1215
{
1316
const INVALIDATED_TYPES = 'core_cache_invalidate';
@@ -68,9 +71,7 @@ public function __construct(
6871
protected function _getTypeInstance($type)
6972
{
7073
$config = $this->_config->getType($type);
71-
if (!isset($config['instance'])) {
72-
return null;
73-
}
74+
7475
return $this->_factory->get($config['instance']);
7576
}
7677

@@ -132,7 +133,7 @@ public function getTypes()
132133
}
133134

134135
/**
135-
* {@inheritdoc}
136+
* @inheritdoc
136137
*/
137138
public function getTypeLabels()
138139
{

0 commit comments

Comments
 (0)