Issue #3205480 by alexpott, andypost, Wim Leers, catch, kim.pepper, longwave: Drop PECL YAML library support in favor of only Symfony YAML

(cherry picked from commit 25dc2f1480ddd83896b5cba9cd0488b9ea8abe9c)
merge-requests/6880/merge
catch 2024-03-03 17:05:28 +00:00
parent d67efa58fc
commit 47d0389079
13 changed files with 138 additions and 290 deletions

View File

@ -9,6 +9,9 @@ modules/system/tests/fixtures/HtaccessTest/access_test.yml
modules/system/tests/themes/test_theme_libraries_empty/test_theme_libraries_empty.info.yml modules/system/tests/themes/test_theme_libraries_empty/test_theme_libraries_empty.info.yml
tests/Drupal/Tests/Core/Asset/library_test_files/empty.libraries.yml tests/Drupal/Tests/Core/Asset/library_test_files/empty.libraries.yml
tests/Drupal/Tests/Core/Asset/library_test_files/invalid_file.libraries.yml tests/Drupal/Tests/Core/Asset/library_test_files/invalid_file.libraries.yml
tests/Drupal/Tests/Composer/Plugin/Scaffold/fixtures/drupal-assets-fixture/assets/default.services.yml
tests/Drupal/Tests/Composer/Plugin/Scaffold/fixtures/drupal-profile/assets/profile.default.services.yml
modules/sdc/tests/themes/sdc_theme_test/components/bar/bar.component.yml
# Temporary until they are brought up to standards # Temporary until they are brought up to standards
scripts/**/* scripts/**/*

View File

@ -2,37 +2,43 @@
namespace Drupal\Component\Serialization; namespace Drupal\Component\Serialization;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml as SymfonyYaml;
/** /**
* Provides a YAML serialization implementation. * Provides a YAML serialization implementation using symfony/yaml.
*
* Proxy implementation that will choose the best library based on availability.
*/ */
class Yaml implements SerializationInterface { class Yaml implements SerializationInterface {
/**
* The YAML implementation to use.
*
* @var \Drupal\Component\Serialization\SerializationInterface
*/
protected static $serializer;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function encode($data) { public static function encode($data) {
// Instead of using \Drupal\Component\Serialization\Yaml::getSerializer(), try {
// always using Symfony for writing the data, to reduce the risk of having // Set the indentation to 2 to match Drupal's coding standards.
// differences if different environments (like production and development) $yaml = new Dumper(2);
// do not match in terms of what YAML implementation is available. return $yaml->dump($data, PHP_INT_MAX, 0, SymfonyYaml::DUMP_EXCEPTION_ON_INVALID_TYPE | SymfonyYaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
return YamlSymfony::encode($data); }
catch (\Exception $e) {
throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
}
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function decode($raw) { public static function decode($raw) {
$serializer = static::getSerializer(); try {
return $serializer::decode($raw); $yaml = new Parser();
// Make sure we have a single trailing newline. A very simple config like
// 'foo: bar' with no newline will fail to parse otherwise.
return $yaml->parse($raw, SymfonyYaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
}
catch (\Exception $e) {
throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
}
} }
/** /**
@ -44,21 +50,15 @@ class Yaml implements SerializationInterface {
/** /**
* Determines which implementation to use for parsing YAML. * Determines which implementation to use for parsing YAML.
*
* @deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. There is no
* replacement.
*
* @see https://www.drupal.org/node/3415489
*/ */
protected static function getSerializer() { protected static function getSerializer() {
@trigger_error('Calling ' . __METHOD__ . '() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3415489', E_USER_DEPRECATED);
if (!isset(static::$serializer)) { return YamlSymfony::class;
// Use the PECL YAML extension if it is available. It has better
// performance for file reads and is YAML compliant.
if (extension_loaded('yaml')) {
static::$serializer = YamlPecl::class;
}
else {
// Otherwise, fallback to the Symfony implementation.
static::$serializer = YamlSymfony::class;
}
}
return static::$serializer;
} }
} }

View File

@ -2,6 +2,8 @@
namespace Drupal\Component\Serialization; namespace Drupal\Component\Serialization;
@trigger_error('The ' . __NAMESPACE__ . '\YamlSymfony is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use \Drupal\Component\Serialization\Yaml instead. See https://www.drupal.org/node/3415489', E_USER_DEPRECATED);
use Drupal\Component\Serialization\Exception\InvalidDataTypeException; use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Symfony\Component\Yaml\Parser; use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Dumper; use Symfony\Component\Yaml\Dumper;
@ -9,6 +11,11 @@ use Symfony\Component\Yaml\Yaml as SymfonyYaml;
/** /**
* Default serialization for YAML using the Symfony component. * Default serialization for YAML using the Symfony component.
*
* @deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use
* \Drupal\Component\Serialization\Yaml instead.
*
* @see https://www.drupal.org/node/3415489
*/ */
class YamlSymfony implements SerializationInterface { class YamlSymfony implements SerializationInterface {
@ -16,6 +23,7 @@ class YamlSymfony implements SerializationInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function encode($data) { public static function encode($data) {
@trigger_error('Calling ' . __METHOD__ . '() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use \Drupal\Component\Serialization\Yaml::encode() instead. See https://www.drupal.org/node/3415489', E_USER_DEPRECATED);
try { try {
// Set the indentation to 2 to match Drupal's coding standards. // Set the indentation to 2 to match Drupal's coding standards.
$yaml = new Dumper(2); $yaml = new Dumper(2);
@ -30,6 +38,7 @@ class YamlSymfony implements SerializationInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function decode($raw) { public static function decode($raw) {
@trigger_error('Calling ' . __METHOD__ . '() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use \Drupal\Component\Serialization\Yaml::decode() instead. See https://www.drupal.org/node/3415489', E_USER_DEPRECATED);
try { try {
$yaml = new Parser(); $yaml = new Parser();
// Make sure we have a single trailing newline. A very simple config like // Make sure we have a single trailing newline. A very simple config like
@ -45,6 +54,7 @@ class YamlSymfony implements SerializationInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function getFileExtension() { public static function getFileExtension() {
@trigger_error('Calling ' . __METHOD__ . '() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use \Drupal\Component\Serialization\Yaml::getFileExtension() instead. See https://www.drupal.org/node/3415489', E_USER_DEPRECATED);
return 'yml'; return 'yml';
} }

View File

@ -1,28 +1,7 @@
<?php <?php
// phpcs:ignoreFile
namespace Drupal\Core\Serialization;
use Drupal\Core\Site\Settings;
use Drupal\Component\Serialization\Yaml as ComponentYaml;
/** /**
* Provides a YAML serialization implementation. * Provides a BC layer for Drupal\Core\Serialization\Yaml.
*
* Allow settings to override the YAML implementation resolution.
*/ */
class Yaml extends ComponentYaml { class_alias('\Drupal\Component\Serialization\Yaml', '\Drupal\Core\Serialization\Yaml');
/**
* {@inheritdoc}
*/
protected static function getSerializer() {
// Allow settings.php to override the YAML serializer.
if (!isset(static::$serializer) &&
$class = Settings::get('yaml_parser_class')) {
static::$serializer = $class;
}
return parent::getSerializer();
}
}

View File

@ -42,6 +42,10 @@ final class Settings {
'replacement' => '', 'replacement' => '',
'message' => 'The "block_interest_cohort" setting is deprecated in drupal:9.5.0. This setting should be removed from the settings file, since its usage has been removed. See https://www.drupal.org/node/3320787.', 'message' => 'The "block_interest_cohort" setting is deprecated in drupal:9.5.0. This setting should be removed from the settings file, since its usage has been removed. See https://www.drupal.org/node/3320787.',
], ],
'yaml_parser_class' => [
'replacement' => '',
'message' => 'The "yaml_parser_class" setting is deprecated in drupal:10.3.0. This setting should be removed from the settings file, since its usage has been removed. See https://www.drupal.org/node/3415489.',
],
]; ];
/** /**

View File

@ -192,22 +192,10 @@ EOD;
]; ];
$this->drupalGet('admin/config/development/configuration/single/import'); $this->drupalGet('admin/config/development/configuration/single/import');
$this->submitForm($edit, 'Import'); $this->submitForm($edit, 'Import');
if (extension_loaded('yaml')) { // @see \Drupal\Tests\Component\Serialization\YamlSymfonyTest:: testDecodeObjectSupportDisabled()
// If the yaml extension is loaded it will work but not create the PHP
// object.
$this->assertSession()->pageTextContains('Are you sure you want to update the second test configuration?');
$this->submitForm([], 'Confirm');
$entity = $storage->load('second');
$this->assertSession()->pageTextContains('The configuration was imported successfully.');
$this->assertIsString($entity->label());
$this->assertStringContainsString('ObjectSerialization', $entity->label(), 'Label contains serialized object');
}
else {
// If the Symfony parser is used there will be an error.
$this->assertSession()->responseContains('The import failed with the following message:'); $this->assertSession()->responseContains('The import failed with the following message:');
$this->assertSession()->responseContains('Object support when parsing a YAML file has been disabled'); $this->assertSession()->responseContains('Object support when parsing a YAML file has been disabled');
} }
}
/** /**
* Tests importing a simple configuration file. * Tests importing a simple configuration file.

View File

@ -1,2 +1 @@
# YAML linter does not allow empty files, setting it to NULL tests the same. # This file is intentionally left empty
null

View File

@ -15,6 +15,10 @@ parameters:
- . - .
- ../composer - ../composer
bootstrapFiles:
# Load aliases.
- lib/Drupal/Core/Serialization/Yaml.php
excludePaths: excludePaths:
# Skip sites directory. # Skip sites directory.
- ../sites - ../sites

View File

@ -6,15 +6,18 @@ namespace Drupal\Tests\Component\Serialization;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException; use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Serialization\YamlSymfony; use Drupal\Component\Serialization\YamlSymfony;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
/** /**
* Tests the YamlSymfony serialization implementation. * Tests the YamlSymfony serialization implementation.
* *
* @group Drupal * @group Drupal
* @group Serialization * @group Serialization
* @group legacy
* @coversDefaultClass \Drupal\Component\Serialization\YamlSymfony * @coversDefaultClass \Drupal\Component\Serialization\YamlSymfony
*/ */
class YamlSymfonyTest extends YamlTestBase { class YamlSymfonyTest extends YamlTestBase {
use ExpectDeprecationTrait;
/** /**
* Tests encoding and decoding basic data structures. * Tests encoding and decoding basic data structures.
@ -24,6 +27,8 @@ class YamlSymfonyTest extends YamlTestBase {
* @dataProvider providerEncodeDecodeTests * @dataProvider providerEncodeDecodeTests
*/ */
public function testEncodeDecode($data) { public function testEncodeDecode($data) {
$this->expectDeprecation("Calling Drupal\Component\Serialization\YamlSymfony::encode() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use \Drupal\Component\Serialization\Yaml::encode() instead. See https://www.drupal.org/node/3415489");
$this->expectDeprecation("Calling Drupal\Component\Serialization\YamlSymfony::decode() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use \Drupal\Component\Serialization\Yaml::decode() instead. See https://www.drupal.org/node/3415489");
$this->assertEquals($data, YamlSymfony::decode(YamlSymfony::encode($data))); $this->assertEquals($data, YamlSymfony::decode(YamlSymfony::encode($data)));
} }
@ -34,6 +39,7 @@ class YamlSymfonyTest extends YamlTestBase {
* @dataProvider providerDecodeTests * @dataProvider providerDecodeTests
*/ */
public function testDecode($string, $data) { public function testDecode($string, $data) {
$this->expectDeprecation("Calling Drupal\Component\Serialization\YamlSymfony::decode() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use \Drupal\Component\Serialization\Yaml::decode() instead. See https://www.drupal.org/node/3415489");
$this->assertEquals($data, YamlSymfony::decode($string)); $this->assertEquals($data, YamlSymfony::decode($string));
} }
@ -43,6 +49,7 @@ class YamlSymfonyTest extends YamlTestBase {
* @covers ::encode * @covers ::encode
*/ */
public function testEncode() { public function testEncode() {
$this->expectDeprecation("Calling Drupal\Component\Serialization\YamlSymfony::encode() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use \Drupal\Component\Serialization\Yaml::encode() instead. See https://www.drupal.org/node/3415489");
// cSpell:disable // cSpell:disable
$this->assertEquals('foo: $this->assertEquals('foo:
bar: \'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis\' bar: \'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis\'
@ -54,6 +61,7 @@ class YamlSymfonyTest extends YamlTestBase {
* @covers ::getFileExtension * @covers ::getFileExtension
*/ */
public function testGetFileExtension() { public function testGetFileExtension() {
$this->expectDeprecation("Calling Drupal\Component\Serialization\YamlSymfony::getFileExtension() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use \Drupal\Component\Serialization\Yaml::getFileExtension() instead. See https://www.drupal.org/node/3415489");
$this->assertEquals('yml', YamlSymfony::getFileExtension()); $this->assertEquals('yml', YamlSymfony::getFileExtension());
} }
@ -63,6 +71,7 @@ class YamlSymfonyTest extends YamlTestBase {
* @covers ::decode * @covers ::decode
*/ */
public function testError() { public function testError() {
$this->expectDeprecation("Calling Drupal\Component\Serialization\YamlSymfony::decode() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use \Drupal\Component\Serialization\Yaml::decode() instead. See https://www.drupal.org/node/3415489");
$this->expectException(InvalidDataTypeException::class); $this->expectException(InvalidDataTypeException::class);
YamlSymfony::decode('foo: [ads'); YamlSymfony::decode('foo: [ads');
} }
@ -72,7 +81,8 @@ class YamlSymfonyTest extends YamlTestBase {
* *
* @covers ::encode * @covers ::encode
*/ */
public function testObjectSupportDisabled() { public function testEncodeObjectSupportDisabled() {
$this->expectDeprecation("Calling Drupal\Component\Serialization\YamlSymfony::encode() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use \Drupal\Component\Serialization\Yaml::encode() instead. See https://www.drupal.org/node/3415489");
$this->expectException(InvalidDataTypeException::class); $this->expectException(InvalidDataTypeException::class);
$this->expectExceptionMessage('Object support when dumping a YAML file has been disabled.'); $this->expectExceptionMessage('Object support when dumping a YAML file has been disabled.');
$object = new \stdClass(); $object = new \stdClass();
@ -80,4 +90,20 @@ class YamlSymfonyTest extends YamlTestBase {
YamlSymfony::encode([$object]); YamlSymfony::encode([$object]);
} }
/**
* Ensures that decoding PHP objects does not work in Symfony.
*
* @covers ::decode
*/
public function testDecodeObjectSupportDisabled(): void {
$this->expectDeprecation("Calling Drupal\Component\Serialization\YamlSymfony::decode() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use \Drupal\Component\Serialization\Yaml::decode() instead. See https://www.drupal.org/node/3415489");
$this->expectException(InvalidDataTypeException::class);
$this->expectExceptionMessageMatches('/^Object support when parsing a YAML file has been disabled/');
$yaml = <<<YAML
obj: !php/object "O:8:\"stdClass\":1:{s:3:\"foo\";s:3:\"bar\";}"
YAML;
YamlSymfony::decode($yaml);
}
} }

View File

@ -5,175 +5,94 @@ declare(strict_types=1);
namespace Drupal\Tests\Component\Serialization; namespace Drupal\Tests\Component\Serialization;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException; use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Serialization\SerializationInterface;
use Drupal\Component\Serialization\Yaml; use Drupal\Component\Serialization\Yaml;
use Drupal\Component\Serialization\YamlPecl;
use Drupal\Component\Serialization\YamlSymfony;
use PHPUnit\Framework\TestCase;
/** /**
* @coversDefaultClass \Drupal\Component\Serialization\Yaml * Tests the Yaml serialization implementation.
*
* @group Drupal
* @group Serialization * @group Serialization
* @coversDefaultClass \Drupal\Component\Serialization\Yaml
*/ */
class YamlTest extends TestCase { class YamlTest extends YamlTestBase {
/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
protected $mockParser;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->mockParser = $this->getMockBuilder('\stdClass')
->addMethods(['encode', 'decode', 'getFileExtension'])
->getMock();
YamlParserProxy::setMock($this->mockParser);
}
/**
* {@inheritdoc}
*/
protected function tearDown(): void {
YamlParserProxy::setMock(NULL);
parent::tearDown();
}
/** /**
* Tests encoding and decoding basic data structures.
*
* @covers ::encode
* @covers ::decode * @covers ::decode
* @dataProvider providerEncodeDecodeTests
*/ */
public function testDecode() { public function testEncodeDecode($data) {
$this->mockParser $this->assertSame($data, Yaml::decode(Yaml::encode($data)));
->expects($this->once()) }
->method('decode');
YamlStub::decode('test'); /**
* Tests decoding YAML node anchors.
*
* @covers ::decode
* @dataProvider providerDecodeTests
*/
public function testDecode($string, $data) {
$this->assertSame($data, Yaml::decode($string));
}
/**
* Tests our encode settings.
*
* @covers ::encode
*/
public function testEncode() {
// cSpell:disable
$this->assertSame('foo:
bar: \'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis\'
', Yaml::encode(['foo' => ['bar' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis']]));
// cSpell:enable
} }
/** /**
* @covers ::getFileExtension * @covers ::getFileExtension
*/ */
public function testGetFileExtension() { public function testGetFileExtension() {
$this->mockParser $this->assertSame('yml', Yaml::getFileExtension());
->expects($this->never())
->method('getFileExtension');
$this->assertEquals('yml', YamlStub::getFileExtension());
} }
/** /**
* Tests all YAML files are decoded in the same way with Symfony and PECL. * Tests that invalid YAML throws an exception.
* *
* This test is a little bit slow but it tests that we do not have any bugs in * @covers ::decode
* our YAML that might not be decoded correctly in any of our implementations.
*
* @todo This should exist as an integration test not part of our unit tests.
* https://www.drupal.org/node/2597730
*
* @requires extension yaml
* @dataProvider providerYamlFilesInCore
*/ */
public function testYamlFiles($file) { public function testError() {
$data = file_get_contents($file); $this->expectException(InvalidDataTypeException::class);
try { Yaml::decode('foo: [ads');
$this->assertEquals(YamlSymfony::decode($data), YamlPecl::decode($data), $file);
}
catch (InvalidDataTypeException $e) {
// Provide file context to the failure so the exception message is useful.
$this->fail("Exception thrown parsing $file:\n" . $e->getMessage());
}
} }
/** /**
* Ensures that decoding php objects does not work in PECL. * Ensures that php object support is disabled.
* *
* @requires extension yaml * @covers ::encode
*
* @see \Drupal\Tests\Component\Serialization\YamlTest::testObjectSupportDisabledSymfony()
*/ */
public function testObjectSupportDisabledPecl() { public function testEncodeObjectSupportDisabled() {
$this->expectException(InvalidDataTypeException::class);
$this->expectExceptionMessage('Object support when dumping a YAML file has been disabled.');
$object = new \stdClass(); $object = new \stdClass();
$object->foo = 'bar'; $object->foo = 'bar';
// In core all Yaml encoding is done via Symfony and it does not support Yaml::encode([$object]);
// objects so in order to encode an object we have to use the PECL
// extension.
// @see \Drupal\Component\Serialization\Yaml::encode()
$yaml = YamlPecl::encode([$object]);
$this->assertEquals(['O:8:"stdClass":1:{s:3:"foo";s:3:"bar";}'], YamlPecl::decode($yaml));
} }
/** /**
* Ensures that decoding php objects does not work in Symfony. * Ensures that decoding PHP objects does not work in Symfony.
* *
* @requires extension yaml * @covers ::decode
*
* @see \Drupal\Tests\Component\Serialization\YamlTest::testObjectSupportDisabledPecl()
*/ */
public function testObjectSupportDisabledSymfony() { public function testDecodeObjectSupportDisabled() {
$this->expectException(InvalidDataTypeException::class); $this->expectException(InvalidDataTypeException::class);
$this->expectExceptionMessageMatches('/^Object support when parsing a YAML file has been disabled/'); $this->expectExceptionMessageMatches('/^Object support when parsing a YAML file has been disabled/');
$object = new \stdClass(); $yaml = <<<YAML
$object->foo = 'bar'; obj: !php/object "O:8:\"stdClass\":1:{s:3:\"foo\";s:3:\"bar\";}"
// In core all Yaml encoding is done via Symfony and it does not support YAML;
// objects so in order to encode an object we have to use the PECL
// extension.
// @see \Drupal\Component\Serialization\Yaml::encode()
$yaml = YamlPecl::encode([$object]);
YamlSymfony::decode($yaml);
}
/** Yaml::decode($yaml);
* Data provider that lists all YAML files in core.
*/
public static function providerYamlFilesInCore() {
$files = [];
$dirs = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . '/../../../../../', \RecursiveDirectoryIterator::FOLLOW_SYMLINKS));
foreach ($dirs as $dir) {
$pathname = $dir->getPathname();
// Exclude core/node_modules.
if ($dir->getExtension() == 'yml' && !str_contains($pathname, '/../../../../../node_modules')) {
if (str_contains($dir->getRealPath(), 'invalid_file')) {
// There are some intentionally invalid files provided for testing
// library API behaviors, ignore them.
continue;
}
$files[] = [$dir->getRealPath()];
}
}
return $files;
}
}
class YamlStub extends Yaml {
public static function getSerializer() {
return '\Drupal\Tests\Component\Serialization\YamlParserProxy';
}
}
class YamlParserProxy implements SerializationInterface {
/**
* @var \Drupal\Component\Serialization\SerializationInterface
*/
protected static $mock;
public static function setMock($mock) {
static::$mock = $mock;
}
public static function encode($data) {
return static::$mock->encode($data);
}
public static function decode($raw) {
return static::$mock->decode($raw);
}
public static function getFileExtension() {
return static::$mock->getFileExtension();
} }
} }

View File

@ -1,4 +1 @@
# Test version of default.services.yml from drupal/core. # Test version of default.services.yml from drupal/core.
# Add a dummy key until YamlPecl can validate an empty YAML file:
# https://www.drupal.org/project/drupal/issues/3003300
foo: bar

View File

@ -1,4 +1 @@
# default.services.yml fixture scaffolded from "file-mappings" in drupal-project composer.json fixture. # default.services.yml fixture scaffolded from "file-mappings" in drupal-project composer.json fixture.
# Add a dummy key until YamlPecl can validate an empty YAML file:
# https://www.drupal.org/project/drupal/issues/3003300
foo: bar

View File

@ -1,78 +0,0 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\Core\Serialization;
use Drupal\Component\Serialization\SerializationInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Site\Settings;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Core\Serialization\Yaml
* @group Serialization
*/
class YamlTest extends UnitTestCase {
/**
* Tests that the overridden serializer is called.
*
* @covers ::getSerializer
* @runInSeparateProcess
*/
public function testGetSerialization() {
new Settings(['yaml_parser_class' => YamlParserProxy::class]);
$this->assertEquals(YamlParserProxy::class, Settings::get('yaml_parser_class'));
$mock = $this->getMockBuilder(YamlTestMockInterface::class)
->onlyMethods(['decode'])
->getMock();
$mock
->expects($this->once())
->method('decode');
YamlParserProxy::setMock($mock);
Yaml::decode('---');
new Settings([]);
}
}
class YamlParserProxy implements SerializationInterface {
/**
* @var \Drupal\Component\Serialization\SerializationInterface
*/
protected static $mock;
public static function setMock($mock) {
static::$mock = $mock;
}
public static function encode($data) {
return static::$mock->encode($data);
}
public static function decode($raw) {
return static::$mock->decode($raw);
}
public static function getFileExtension() {
return static::$mock->getFileExtension();
}
}
/**
* Interface used in the mocking process of this test.
*/
interface YamlTestMockInterface {
/**
* Function used in the mocking process of this test.
*/
public function decode();
}