comparison core/tests/Drupal/Tests/Component/Serialization/YamlTest.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
75 $this->fail("Exception thrown parsing $file:\n" . $e->getMessage()); 75 $this->fail("Exception thrown parsing $file:\n" . $e->getMessage());
76 } 76 }
77 } 77 }
78 78
79 /** 79 /**
80 * Ensures that decoding php objects is similar for PECL and Symfony. 80 * Ensures that decoding php objects does not work in PECL.
81 * 81 *
82 * @requires extension yaml 82 * @requires extension yaml
83 *
84 * @see \Drupal\Tests\Component\Serialization\YamlTest::testObjectSupportDisabledSymfony()
83 */ 85 */
84 public function testObjectSupportDisabled() { 86 public function testObjectSupportDisabledPecl() {
85 $object = new \stdClass(); 87 $object = new \stdClass();
86 $object->foo = 'bar'; 88 $object->foo = 'bar';
87 // In core all Yaml encoding is done via Symfony and it does not support 89 // In core all Yaml encoding is done via Symfony and it does not support
88 // objects so in order to encode an object we hace to use the PECL 90 // objects so in order to encode an object we have to use the PECL
89 // extension. 91 // extension.
90 // @see \Drupal\Component\Serialization\Yaml::encode() 92 // @see \Drupal\Component\Serialization\Yaml::encode()
91 $yaml = YamlPecl::encode([$object]); 93 $yaml = YamlPecl::encode([$object]);
92 $this->assertEquals(['O:8:"stdClass":1:{s:3:"foo";s:3:"bar";}'], YamlPecl::decode($yaml)); 94 $this->assertEquals(['O:8:"stdClass":1:{s:3:"foo";s:3:"bar";}'], YamlPecl::decode($yaml));
93 $this->assertEquals(['!php/object "O:8:\"stdClass\":1:{s:3:\"foo\";s:3:\"bar\";}"'], YamlSymfony::decode($yaml)); 95 }
96
97 /**
98 * Ensures that decoding php objects does not work in Symfony.
99 *
100 * @requires extension yaml
101 *
102 * @see \Drupal\Tests\Component\Serialization\YamlTest::testObjectSupportDisabledPecl()
103 */
104 public function testObjectSupportDisabledSymfony() {
105 if (method_exists($this, 'setExpectedExceptionRegExp')) {
106 $this->setExpectedExceptionRegExp(InvalidDataTypeException::class, '/^Object support when parsing a YAML file has been disabled/');
107 }
108 else {
109 $this->expectException(InvalidDataTypeException::class);
110 $this->expectExceptionMessageRegExp('/^Object support when parsing a YAML file has been disabled/');
111 }
112 $object = new \stdClass();
113 $object->foo = 'bar';
114 // In core all Yaml encoding is done via Symfony and it does not support
115 // objects so in order to encode an object we have to use the PECL
116 // extension.
117 // @see \Drupal\Component\Serialization\Yaml::encode()
118 $yaml = YamlPecl::encode([$object]);
119 YamlSymfony::decode($yaml);
94 } 120 }
95 121
96 /** 122 /**
97 * Data provider that lists all YAML files in core. 123 * Data provider that lists all YAML files in core.
98 */ 124 */
99 public function providerYamlFilesInCore() { 125 public function providerYamlFilesInCore() {
100 $files = []; 126 $files = [];
101 $dirs = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . '/../../../../../', \RecursiveDirectoryIterator::FOLLOW_SYMLINKS)); 127 $dirs = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . '/../../../../../', \RecursiveDirectoryIterator::FOLLOW_SYMLINKS));
102 foreach ($dirs as $dir) { 128 foreach ($dirs as $dir) {
103 $pathname = $dir->getPathname(); 129 $pathname = $dir->getPathname();
104 // Exclude vendor. 130 // Exclude core/node_modules.
105 if ($dir->getExtension() == 'yml' && strpos($pathname, '/../../../../../vendor') === FALSE) { 131 if ($dir->getExtension() == 'yml' && strpos($pathname, '/../../../../../node_modules') === FALSE) {
106 if (strpos($dir->getRealPath(), 'invalid_file') !== FALSE) { 132 if (strpos($dir->getRealPath(), 'invalid_file') !== FALSE) {
107 // There are some intentionally invalid files provided for testing 133 // There are some intentionally invalid files provided for testing
108 // library API behaviours, ignore them. 134 // library API behaviours, ignore them.
109 continue; 135 continue;
110 } 136 }