From f254657406ed3cb3963ed5767bbfb248aa2886e2 Mon Sep 17 00:00:00 2001 From: Mark Carver Date: Sat, 20 Jul 2019 17:56:44 -0500 Subject: [PATCH] Issue #3068483 by markcarver: Automatically map !translate YAML tag to TranslatableMarkup Signed-off-by: Mark Carver --- .../Core/Serialization/YamlTest.php | 40 +++++++++++++++ .../Component/Serialization/YamlPeclTest.php | 13 +++++ .../Serialization/YamlSymfonyTest.php | 13 +++++ .../Component/Serialization/YamlTestBase.php | 50 +++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 core/tests/Drupal/KernelTests/Core/Serialization/YamlTest.php diff --git a/core/tests/Drupal/KernelTests/Core/Serialization/YamlTest.php b/core/tests/Drupal/KernelTests/Core/Serialization/YamlTest.php new file mode 100644 index 0000000000..d2a62f2798 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Serialization/YamlTest.php @@ -0,0 +1,40 @@ +assertInstanceOf(TranslatableMarkup::class, $label); + + // Ensure that the argument was set properly. + $this->assertEquals(['@arg' => 'value'], $label->getArguments()); + + // Ensure that the context option was set properly. + $this->assertEquals('Something', $label->getOption('context')); + } + +} diff --git a/core/tests/Drupal/Tests/Component/Serialization/YamlPeclTest.php b/core/tests/Drupal/Tests/Component/Serialization/YamlPeclTest.php index 910a6356b8..cdd978f077 100644 --- a/core/tests/Drupal/Tests/Component/Serialization/YamlPeclTest.php +++ b/core/tests/Drupal/Tests/Component/Serialization/YamlPeclTest.php @@ -96,4 +96,17 @@ public function testError() { YamlPecl::decode('foo: [ads'); } + /** + * Tests YAML 1.2 tag support (!tag). + * + * @covers ::addTagCallback + * @covers ::getDefaultTagCallbacks + * @covers ::getTagCallbacks + * @covers ::removeTagCallback + * @covers ::setTagCallbacks + */ + public function testTagSupport() { + $this->assertYamlTags(YamlPecl::class); + } + } diff --git a/core/tests/Drupal/Tests/Component/Serialization/YamlSymfonyTest.php b/core/tests/Drupal/Tests/Component/Serialization/YamlSymfonyTest.php index eeedf538d6..9b99d84599 100644 --- a/core/tests/Drupal/Tests/Component/Serialization/YamlSymfonyTest.php +++ b/core/tests/Drupal/Tests/Component/Serialization/YamlSymfonyTest.php @@ -87,4 +87,17 @@ public function testObjectSupportDisabled() { YamlSymfony::encode([$object]); } + /** + * Tests YAML 1.2 tag support (!tag). + * + * @covers ::addTagCallback + * @covers ::getDefaultTagCallbacks + * @covers ::getTagCallbacks + * @covers ::removeTagCallback + * @covers ::setTagCallbacks + */ + public function testTagSupport() { + $this->assertYamlTags(YamlSymfony::class); + } + } diff --git a/core/tests/Drupal/Tests/Component/Serialization/YamlTestBase.php b/core/tests/Drupal/Tests/Component/Serialization/YamlTestBase.php index 82226d0d34..1c9e25848e 100644 --- a/core/tests/Drupal/Tests/Component/Serialization/YamlTestBase.php +++ b/core/tests/Drupal/Tests/Component/Serialization/YamlTestBase.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\Component\Serialization; +use Drupal\Component\Serialization\TaggedSerializationInterface; use PHPUnit\Framework\TestCase; /** @@ -9,6 +10,55 @@ */ abstract class YamlTestBase extends TestCase { + /** + * Asserts that a serializer can support YAML 1.2 tags. + * + * @param string $serializer + * The class name of the serializer to test. + */ + protected function assertYamlTags($serializer) { + /* @var \Drupal\Component\Serialization\TaggedSerializationInterface $serializer */ + + // Ensure the serializer supports the tagged interface. + $this->assertTrue(is_subclass_of($serializer, TaggedSerializationInterface::class)); + + // Ensure the component's default callbacks. + $this->assertEquals($serializer::getDefaultTagCallbacks(), $serializer::getTagCallbacks()); + + $yaml = 'value: !sum [1, 2, 3]'; + + // Ensure that without a callback, the tag is ignored. + $data = $serializer::decode($yaml); + $this->assertEquals($data['value'], [1, 2, 3]); + + // Now, add the custom tag callback. + $sum = function ($value, $tag) { + return array_sum($value); + }; + $serializer::addTagCallback('!sum', $sum); + + // Ensure that with a tag callback, the value is converted. + $data = $serializer::decode($yaml); + $this->assertEquals($data['value'], 6); + + // Remove the custom tag callback and ensure it returns original callback. + $callback = $serializer::removeTagCallback('!sum'); + $this->assertEquals($sum, $callback); + + // Ensure that without a callback, the tag is ignored. + $data = $serializer::decode($yaml); + $this->assertEquals($data['value'], [1, 2, 3]); + + // Reset tag callbacks. + $serializer::setTagCallbacks(); + $callbacks = new \ReflectionProperty($serializer, 'tagCallbacks'); + $callbacks->setAccessible(TRUE); + $this->assertEquals(NULL, $callbacks->getValue($serializer)); + + // Ensure the component's default callbacks are restored. + $this->assertEquals($serializer::getDefaultTagCallbacks(), $serializer::getTagCallbacks()); + } + /** * Some data that should be able to be serialized. */ -- 2.22.0