Update to Drupal 8.2.0. For more information, see https://www.drupal.org/project/drupal/releases/8.2.0
This commit is contained in:
parent
2f563ab520
commit
f1c8716f57
1732 changed files with 52334 additions and 11780 deletions
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\Component\Serialization;
|
||||
|
||||
use Drupal\Component\Serialization\YamlPecl;
|
||||
|
||||
/**
|
||||
* Tests the YamlPecl serialization implementation.
|
||||
*
|
||||
* @group Drupal
|
||||
* @group Serialization
|
||||
* @coversDefaultClass \Drupal\Component\Serialization\YamlPecl
|
||||
* @requires extension yaml
|
||||
*/
|
||||
class YamlPeclTest extends YamlTestBase {
|
||||
|
||||
/**
|
||||
* Tests encoding and decoding basic data structures.
|
||||
*
|
||||
* @covers ::encode
|
||||
* @covers ::decode
|
||||
* @dataProvider providerEncodeDecodeTests
|
||||
*/
|
||||
public function testEncodeDecode($data) {
|
||||
$this->assertEquals($data, YamlPecl::decode(YamlPecl::encode($data)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests decoding YAML node anchors.
|
||||
*
|
||||
* @covers ::decode
|
||||
* @dataProvider providerDecodeTests
|
||||
*/
|
||||
public function testDecode($string, $data) {
|
||||
$this->assertEquals($data, YamlPecl::decode($string));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests our encode settings.
|
||||
*
|
||||
* @covers ::encode
|
||||
*/
|
||||
public function testEncode() {
|
||||
$this->assertEquals('---
|
||||
foo:
|
||||
bar: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis
|
||||
...
|
||||
', YamlPecl::encode(['foo' => ['bar' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis']]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests YAML boolean callback.
|
||||
*
|
||||
* @param string $string
|
||||
* String value for the YAML boolean.
|
||||
* @param string|bool $expected
|
||||
* The expected return value.
|
||||
*
|
||||
* @covers ::applyBooleanCallbacks
|
||||
* @dataProvider providerBoolTest
|
||||
*/
|
||||
public function testApplyBooleanCallbacks($string, $expected) {
|
||||
$this->assertEquals($expected, YamlPecl::applyBooleanCallbacks($string, 'bool', NULL));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getFileExtension
|
||||
*/
|
||||
public function testGetFileExtension() {
|
||||
$this->assertEquals('yml', YamlPecl::getFileExtension());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that invalid YAML throws an exception.
|
||||
*
|
||||
* @covers ::errorHandler
|
||||
* @expectedException \Drupal\Component\Serialization\Exception\InvalidDataTypeException
|
||||
*/
|
||||
public function testError() {
|
||||
YamlPecl::decode('foo: [ads');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\Component\Serialization;
|
||||
|
||||
use Drupal\Component\Serialization\YamlSymfony;
|
||||
|
||||
/**
|
||||
* Tests the YamlSymfony serialization implementation.
|
||||
*
|
||||
* @group Drupal
|
||||
* @group Serialization
|
||||
* @coversDefaultClass \Drupal\Component\Serialization\YamlSymfony
|
||||
*/
|
||||
class YamlSymfonyTest extends YamlTestBase {
|
||||
|
||||
/**
|
||||
* Tests encoding and decoding basic data structures.
|
||||
*
|
||||
* @covers ::encode
|
||||
* @covers ::decode
|
||||
* @dataProvider providerEncodeDecodeTests
|
||||
*/
|
||||
public function testEncodeDecode($data) {
|
||||
$this->assertEquals($data, YamlSymfony::decode(YamlSymfony::encode($data)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests decoding YAML node anchors.
|
||||
*
|
||||
* @covers ::decode
|
||||
* @dataProvider providerDecodeTests
|
||||
*/
|
||||
public function testDecode($string, $data) {
|
||||
$this->assertEquals($data, YamlSymfony::decode($string));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests our encode settings.
|
||||
*
|
||||
* @covers ::encode
|
||||
*/
|
||||
public function testEncode() {
|
||||
$this->assertEquals('foo:
|
||||
bar: \'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis\'
|
||||
', YamlSymfony::encode(['foo' => ['bar' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sapien ex, venenatis vitae nisi eu, posuere luctus dolor. Nullam convallis']]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getFileExtension
|
||||
*/
|
||||
public function testGetFileExtension() {
|
||||
$this->assertEquals('yml', YamlSymfony::getFileExtension());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that invalid YAML throws an exception.
|
||||
*
|
||||
* @covers ::decode
|
||||
* @expectedException \Drupal\Component\Serialization\Exception\InvalidDataTypeException
|
||||
*/
|
||||
public function testError() {
|
||||
YamlSymfony::decode('foo: [ads');
|
||||
}
|
||||
|
||||
}
|
|
@ -2,7 +2,11 @@
|
|||
|
||||
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 Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
|
@ -12,47 +16,118 @@ use Drupal\Tests\UnitTestCase;
|
|||
class YamlTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* @covers ::decode
|
||||
* @var \PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
public function testDecode() {
|
||||
// Test that files without line break endings are properly interpreted.
|
||||
$yaml = 'foo: bar';
|
||||
$expected = array(
|
||||
'foo' => 'bar',
|
||||
);
|
||||
$this->assertSame($expected, Yaml::decode($yaml));
|
||||
$yaml .= "\n";
|
||||
$this->assertSame($expected, Yaml::decode($yaml));
|
||||
$yaml .= "\n";
|
||||
$this->assertSame($expected, Yaml::decode($yaml));
|
||||
protected $mockParser;
|
||||
|
||||
$yaml = "{}\n";
|
||||
$expected = array();
|
||||
$this->assertSame($expected, Yaml::decode($yaml));
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->mockParser = $this->getMockBuilder('\stdClass')
|
||||
->setMethods(['encode', 'decode', 'getFileExtension'])
|
||||
->getMock();
|
||||
YamlParserProxy::setMock($this->mockParser);
|
||||
}
|
||||
|
||||
$yaml = '';
|
||||
$this->assertNULL(Yaml::decode($yaml));
|
||||
$yaml .= "\n";
|
||||
$this->assertNULL(Yaml::decode($yaml));
|
||||
$yaml .= "\n";
|
||||
$this->assertNULL(Yaml::decode($yaml));
|
||||
public function tearDown() {
|
||||
YamlParserProxy::setMock(NULL);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::encode
|
||||
* @covers ::decode
|
||||
*/
|
||||
public function testEncode() {
|
||||
$decoded = array(
|
||||
'foo' => 'bar',
|
||||
);
|
||||
$this->assertSame('foo: bar' . "\n", Yaml::encode($decoded));
|
||||
public function testDecode() {
|
||||
$this->mockParser
|
||||
->expects($this->once())
|
||||
->method('decode');
|
||||
YamlStub::decode('test');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getFileExtension
|
||||
*/
|
||||
public function testGetFileExtension() {
|
||||
$this->assertEquals('yml', Yaml::getFileExtension());
|
||||
$this->mockParser
|
||||
->expects($this->never())
|
||||
->method('getFileExtension');
|
||||
$this->assertEquals('yml', YamlStub::getFileExtension());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests all YAML files are decoded in the same way with Symfony and PECL.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider that lists all YAML files in core.
|
||||
*/
|
||||
public function providerYamlFilesInCore() {
|
||||
$files = [];
|
||||
$dirs = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . '/../../../../../', \RecursiveDirectoryIterator::FOLLOW_SYMLINKS));
|
||||
foreach ($dirs as $dir) {
|
||||
$pathname = $dir->getPathname();
|
||||
// Exclude vendor.
|
||||
if ($dir->getExtension() == 'yml' && strpos($pathname, '/../../../../../vendor') === FALSE) {
|
||||
if (strpos($dir->getRealPath(), 'invalid_file') !== FALSE) {
|
||||
// There are some intentionally invalid files provided for testing
|
||||
// library API behaviours, 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\Component\Serialization;
|
||||
|
||||
/**
|
||||
* Provides standard data to validate different YAML implementations.
|
||||
*/
|
||||
abstract class YamlTestBase extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
/**
|
||||
* Some data that should be able to be serialized.
|
||||
*/
|
||||
public function providerEncodeDecodeTests() {
|
||||
return [
|
||||
[
|
||||
'foo' => 'bar',
|
||||
'id' => 'schnitzel',
|
||||
'ponies' => ['nope', 'thanks'],
|
||||
'how' => [
|
||||
'about' => 'if',
|
||||
'i' => 'ask',
|
||||
'nicely',
|
||||
],
|
||||
'the' => [
|
||||
'answer' => [
|
||||
'still' => 'would',
|
||||
'be' => 'Y',
|
||||
],
|
||||
],
|
||||
'how_many_times' => 123,
|
||||
'should_i_ask' => FALSE,
|
||||
1,
|
||||
FALSE,
|
||||
[1, FALSE],
|
||||
[10],
|
||||
[0 => '123456'],
|
||||
],
|
||||
[NULL]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Some data that should be able to be de-serialized.
|
||||
*/
|
||||
public function providerDecodeTests() {
|
||||
$data = [
|
||||
// NULL files.
|
||||
['', NULL],
|
||||
["\n", NULL],
|
||||
["---\n...\n", NULL],
|
||||
|
||||
// Node anchors.
|
||||
[
|
||||
"
|
||||
jquery.ui:
|
||||
version: &jquery_ui 1.10.2
|
||||
|
||||
jquery.ui.accordion:
|
||||
version: *jquery_ui
|
||||
",
|
||||
[
|
||||
'jquery.ui' => [
|
||||
'version' => '1.10.2',
|
||||
],
|
||||
'jquery.ui.accordion' => [
|
||||
'version' => '1.10.2',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
// 1.2 Bool values.
|
||||
foreach ($this->providerBoolTest() as $test) {
|
||||
$data[] = ['bool: ' . $test[0], ['bool' => $test[1]]];
|
||||
}
|
||||
$data = array_merge($data, $this->providerBoolTest());
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests different boolean serialization and de-serialization.
|
||||
*/
|
||||
public function providerBoolTest() {
|
||||
return [
|
||||
['true', TRUE],
|
||||
['TRUE', TRUE],
|
||||
['True', TRUE],
|
||||
['y', 'y'],
|
||||
['Y', 'Y'],
|
||||
['false', FALSE],
|
||||
['FALSE', FALSE],
|
||||
['False', FALSE],
|
||||
['n', 'n'],
|
||||
['N', 'N'],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue