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
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/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
scripts/**/*

View File

@ -2,37 +2,43 @@
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.
*
* Proxy implementation that will choose the best library based on availability.
* Provides a YAML serialization implementation using symfony/yaml.
*/
class Yaml implements SerializationInterface {
/**
* The YAML implementation to use.
*
* @var \Drupal\Component\Serialization\SerializationInterface
*/
protected static $serializer;
/**
* {@inheritdoc}
*/
public static function encode($data) {
// Instead of using \Drupal\Component\Serialization\Yaml::getSerializer(),
// always using Symfony for writing the data, to reduce the risk of having
// differences if different environments (like production and development)
// do not match in terms of what YAML implementation is available.
return YamlSymfony::encode($data);
try {
// Set the indentation to 2 to match Drupal's coding standards.
$yaml = new Dumper(2);
return $yaml->dump($data, PHP_INT_MAX, 0, SymfonyYaml::DUMP_EXCEPTION_ON_INVALID_TYPE | SymfonyYaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
}
catch (\Exception $e) {
throw new InvalidDataTypeException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* {@inheritdoc}
*/
public static function decode($raw) {
$serializer = static::getSerializer();
return $serializer::decode($raw);
try {
$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.
*
* @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() {
if (!isset(static::$serializer)) {
// 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;
@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);
return YamlSymfony::class;
}
}

View File

@ -2,6 +2,8 @@
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 Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Dumper;
@ -9,6 +11,11 @@ use Symfony\Component\Yaml\Yaml as SymfonyYaml;
/**
* 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 {
@ -16,6 +23,7 @@ class YamlSymfony implements SerializationInterface {
* {@inheritdoc}
*/
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 {
// Set the indentation to 2 to match Drupal's coding standards.
$yaml = new Dumper(2);
@ -30,6 +38,7 @@ class YamlSymfony implements SerializationInterface {
* {@inheritdoc}
*/
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 {
$yaml = new Parser();
// Make sure we have a single trailing newline. A very simple config like
@ -45,6 +54,7 @@ class YamlSymfony implements SerializationInterface {
* {@inheritdoc}
*/
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';
}

View File

@ -1,28 +1,7 @@
<?php
namespace Drupal\Core\Serialization;
use Drupal\Core\Site\Settings;
use Drupal\Component\Serialization\Yaml as ComponentYaml;
// phpcs:ignoreFile
/**
* Provides a YAML serialization implementation.
*
* Allow settings to override the YAML implementation resolution.
* Provides a BC layer for Drupal\Core\Serialization\Yaml.
*/
class Yaml extends ComponentYaml {
/**
* {@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();
}
}
class_alias('\Drupal\Component\Serialization\Yaml', '\Drupal\Core\Serialization\Yaml');

View File

@ -42,6 +42,10 @@ final class Settings {
'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.',
],
'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,21 +192,9 @@ EOD;
];
$this->drupalGet('admin/config/development/configuration/single/import');
$this->submitForm($edit, 'Import');
if (extension_loaded('yaml')) {
// 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('Object support when parsing a YAML file has been disabled');
}
// @see \Drupal\Tests\Component\Serialization\YamlSymfonyTest:: testDecodeObjectSupportDisabled()
$this->assertSession()->responseContains('The import failed with the following message:');
$this->assertSession()->responseContains('Object support when parsing a YAML file has been disabled');
}
/**

View File

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

View File

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

View File

@ -6,15 +6,18 @@ namespace Drupal\Tests\Component\Serialization;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Serialization\YamlSymfony;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
/**
* Tests the YamlSymfony serialization implementation.
*
* @group Drupal
* @group Serialization
* @group legacy
* @coversDefaultClass \Drupal\Component\Serialization\YamlSymfony
*/
class YamlSymfonyTest extends YamlTestBase {
use ExpectDeprecationTrait;
/**
* Tests encoding and decoding basic data structures.
@ -24,6 +27,8 @@ class YamlSymfonyTest extends YamlTestBase {
* @dataProvider providerEncodeDecodeTests
*/
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)));
}
@ -34,6 +39,7 @@ class YamlSymfonyTest extends YamlTestBase {
* @dataProvider providerDecodeTests
*/
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));
}
@ -43,6 +49,7 @@ class YamlSymfonyTest extends YamlTestBase {
* @covers ::encode
*/
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
$this->assertEquals('foo:
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
*/
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());
}
@ -63,6 +71,7 @@ class YamlSymfonyTest extends YamlTestBase {
* @covers ::decode
*/
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);
YamlSymfony::decode('foo: [ads');
}
@ -72,7 +81,8 @@ class YamlSymfonyTest extends YamlTestBase {
*
* @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->expectExceptionMessage('Object support when dumping a YAML file has been disabled.');
$object = new \stdClass();
@ -80,4 +90,20 @@ class YamlSymfonyTest extends YamlTestBase {
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;
use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
use Drupal\Component\Serialization\SerializationInterface;
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
* @coversDefaultClass \Drupal\Component\Serialization\Yaml
*/
class YamlTest extends TestCase {
/**
* @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();
}
class YamlTest extends YamlTestBase {
/**
* Tests encoding and decoding basic data structures.
*
* @covers ::encode
* @covers ::decode
* @dataProvider providerEncodeDecodeTests
*/
public function testDecode() {
$this->mockParser
->expects($this->once())
->method('decode');
YamlStub::decode('test');
public function testEncodeDecode($data) {
$this->assertSame($data, Yaml::decode(Yaml::encode($data)));
}
/**
* 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
*/
public function testGetFileExtension() {
$this->mockParser
->expects($this->never())
->method('getFileExtension');
$this->assertEquals('yml', YamlStub::getFileExtension());
$this->assertSame('yml', Yaml::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
* 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
* @covers ::decode
*/
public function testYamlFiles($file) {
$data = file_get_contents($file);
try {
$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());
}
public function testError() {
$this->expectException(InvalidDataTypeException::class);
Yaml::decode('foo: [ads');
}
/**
* Ensures that decoding php objects does not work in PECL.
* Ensures that php object support is disabled.
*
* @requires extension yaml
*
* @see \Drupal\Tests\Component\Serialization\YamlTest::testObjectSupportDisabledSymfony()
* @covers ::encode
*/
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->foo = 'bar';
// In core all Yaml encoding is done via Symfony and it does not support
// 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));
Yaml::encode([$object]);
}
/**
* Ensures that decoding php objects does not work in Symfony.
* Ensures that decoding PHP objects does not work in Symfony.
*
* @requires extension yaml
*
* @see \Drupal\Tests\Component\Serialization\YamlTest::testObjectSupportDisabledPecl()
* @covers ::decode
*/
public function testObjectSupportDisabledSymfony() {
public function testDecodeObjectSupportDisabled() {
$this->expectException(InvalidDataTypeException::class);
$this->expectExceptionMessageMatches('/^Object support when parsing a YAML file has been disabled/');
$object = new \stdClass();
$object->foo = 'bar';
// In core all Yaml encoding is done via Symfony and it does not support
// 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 = <<<YAML
obj: !php/object "O:8:\"stdClass\":1:{s:3:\"foo\";s:3:\"bar\";}"
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();
Yaml::decode($yaml);
}
}

View File

@ -1,4 +1 @@
# 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.
# 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();
}