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:
Pantheon Automation 2016-10-06 15:16:20 -07:00 committed by Greg Anderson
parent 2f563ab520
commit f1c8716f57
1732 changed files with 52334 additions and 11780 deletions

View file

@ -1,4 +1,5 @@
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Assertion\InspectorTest.

View file

@ -0,0 +1,39 @@
<?php
namespace Drupal\Tests\Component\ClassFinder;
use Composer\Autoload\ClassLoader;
use Drupal\Component\ClassFinder\ClassFinder;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Component\ClassFinder\ClassFinder
* @group ClassFinder
*/
class ClassFinderTest extends UnitTestCase {
/**
* @covers ::findFile
*/
public function testFindFile() {
$finder = new ClassFinder();
// The full path is returned therefore only tests with
// assertStringEndsWith() so the test is portable.
$this->assertStringEndsWith('core/tests/Drupal/Tests/UnitTestCase.php', $finder->findFile(UnitTestCase::class));
$class = 'Not\\A\\Class';
$this->assertNull($finder->findFile($class));
// Register an autoloader that can find this class.
$loader = new ClassLoader();
$loader->addClassMap([$class => __FILE__]);
$loader->register();
$this->assertEquals(__FILE__, $finder->findFile($class));
// This shouldn't prevent us from finding the original file.
$this->assertStringEndsWith('core/tests/Drupal/Tests/UnitTestCase.php', $finder->findFile(UnitTestCase::class));
// Clean up the additional autoloader after the test.
$loader->unregister();
}
}

View file

@ -172,10 +172,24 @@ class ContainerAwareEventDispatcherTest extends SymfonyContainerAwareEventDispat
$this->assertTrue($otherService->preFooInvoked);
}
public function testGetListenerPriority()
public function testGetListenerPriorityWithServices()
{
// Override the parent test as our implementation doesn't define
// getListenerPriority().
$container = new ContainerBuilder();
$container->register('listener_service', TestEventListener::class);
$listeners = array(
'test_event' => array(
5 => array(
array('service' => array('listener_service', 'preFoo')),
),
),
);
$dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
$listenerService = $container->get('listener_service');
$actualPriority = $dispatcher->getListenerPriority('test_event', [$listenerService, 'preFoo']);
$this->assertSame(5, $actualPriority);
}
}

View file

@ -2,6 +2,8 @@
namespace Drupal\Tests\Component\FileCache;
use Drupal\Component\FileCache\FileCache;
use Drupal\Component\FileCache\NullFileCache;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Tests\UnitTestCase;
@ -17,18 +19,15 @@ class FileCacheFactoryTest extends UnitTestCase {
protected function setUp() {
parent::setUp();
$settings = [
'collection' => 'test-23',
'cache_backend_class' => '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend',
'cache_backend_configuration' => [
'bin' => 'dog',
$configuration = [
'test_foo_settings' => [
'collection' => 'test-23',
'cache_backend_class' => '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend',
'cache_backend_configuration' => [
'bin' => 'dog',
],
],
];
$configuration = FileCacheFactory::getConfiguration();
if (!$configuration) {
$configuration = [];
}
$configuration += [ 'test_foo_settings' => $settings ];
FileCacheFactory::setConfiguration($configuration);
FileCacheFactory::setPrefix('prefix');
}
@ -65,6 +64,97 @@ class FileCacheFactoryTest extends UnitTestCase {
FileCacheFactory::get('test_foo_settings', []);
}
/**
* @covers ::get
*/
public function testGetDisabledFileCache() {
// Ensure the returned FileCache is an instance of FileCache::class.
$file_cache = FileCacheFactory::get('test_foo_settings', []);
$this->assertInstanceOf(FileCache::class, $file_cache);
$configuration = FileCacheFactory::getConfiguration();
$configuration[FileCacheFactory::DISABLE_CACHE] = TRUE;
FileCacheFactory::setConfiguration($configuration);
// Ensure the returned FileCache is now an instance of NullFileCache::class.
$file_cache = FileCacheFactory::get('test_foo_settings', []);
$this->assertInstanceOf(NullFileCache::class, $file_cache);
}
/**
* @covers ::get
*
* @dataProvider configurationDataProvider
*/
public function testGetConfigurationOverrides($configuration, $arguments, $class) {
FileCacheFactory::setConfiguration($configuration);
$file_cache = FileCacheFactory::get('test_foo_settings', $arguments);
$this->assertInstanceOf($class, $file_cache);
}
/**
* Data provider for testGetConfigurationOverrides().
*/
public function configurationDataProvider() {
$data = [];
// Get a unique FileCache class.
$file_cache = $this->getMockBuilder(FileCache::class)
->disableOriginalConstructor()
->getMock();
$class = get_class($file_cache);
// Test fallback configuration.
$data['fallback-configuration'] = [[
], [], FileCache::class];
// Test default configuration.
$data['default-configuration'] = [[
'default' => [
'class' => $class,
],
], [], $class];
// Test specific per collection setting.
$data['collection-setting'] = [[
'test_foo_settings' => [
'class' => $class,
],
], [], $class];
// Test default configuration plus specific per collection setting.
$data['default-plus-collection-setting'] = [[
'default' => [
'class' => '\stdClass',
],
'test_foo_settings' => [
'class' => $class,
],
], [], $class];
// Test default configuration plus class specific override.
$data['default-plus-class-override'] = [[
'default' => [
'class' => '\stdClass',
],
], [ 'class' => $class ], $class];
// Test default configuration plus class specific override plus specific
// per collection setting.
$data['default-plus-class-plus-collection-setting'] = [[
'default' => [
'class' => '\stdClass',
],
'test_foo_settings' => [
'class' => $class,
],
], [ 'class' => '\stdClass'], $class];
return $data;
}
/**
* @covers ::getConfiguration
* @covers ::setConfiguration

View file

@ -230,7 +230,7 @@ public function testMethod($parameter)
EOS;
$this->assertEquals($this->buildExpectedClass($class, $method_body), $result);
$this->assertEquals($this->buildExpectedClass($class, $method_body), $result);
}
/**

View file

@ -13,6 +13,20 @@ use Drupal\Tests\UnitTestCase;
*/
class FormattableMarkupTest extends UnitTestCase {
/**
* The error message of the last error in the error handler.
*
* @var string
*/
protected $lastErrorMessage;
/**
* The error number of the last error in the error handler.
*
* @var int
*/
protected $lastErrorNumber;
/**
* @covers ::__toString
* @covers ::jsonSerialize
@ -35,4 +49,54 @@ class FormattableMarkupTest extends UnitTestCase {
$this->assertEquals(strlen($string), $formattable_string->count());
}
/**
* Custom error handler that saves the last error.
*
* We need this custom error handler because we cannot rely on the error to
* exception conversion as __toString is never allowed to leak any kind of
* exception.
*
* @param int $error_number
* The error number.
* @param string $error_message
* The error message.
*/
public function errorHandler($error_number, $error_message) {
$this->lastErrorNumber = $error_number;
$this->lastErrorMessage = $error_message;
}
/**
* @covers ::__toString
* @dataProvider providerTestUnexpectedPlaceholder
*/
public function testUnexpectedPlaceholder($string, $arguments, $error_number, $error_message) {
// We set a custom error handler because of https://github.com/sebastianbergmann/phpunit/issues/487
set_error_handler([$this, 'errorHandler']);
// We want this to trigger an error.
$markup = new FormattableMarkup($string, $arguments);
// Cast it to a string which will generate the errors.
$output = (string) $markup;
restore_error_handler();
// The string should not change.
$this->assertEquals($string, $output);
$this->assertEquals($error_number, $this->lastErrorNumber);
$this->assertEquals($error_message, $this->lastErrorMessage);
}
/**
* Data provider for FormattableMarkupTest::testUnexpectedPlaceholder().
*
* @return array
*/
public function providerTestUnexpectedPlaceholder() {
return [
['Non alpha starting character: ~placeholder', ['~placeholder' => 'replaced'], E_USER_ERROR, 'Invalid placeholder (~placeholder) in string: Non alpha starting character: ~placeholder'],
['Alpha starting character: placeholder', ['placeholder' => 'replaced'], E_USER_DEPRECATED, 'Invalid placeholder (placeholder) in string: Alpha starting character: placeholder'],
// Ensure that where the placeholder is located in the the string is
// irrelevant.
['placeholder', ['placeholder' => 'replaced'], E_USER_DEPRECATED, 'Invalid placeholder (placeholder) in string: placeholder'],
];
}
}

View file

@ -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');
}
}

View file

@ -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');
}
}

View file

@ -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();
}
}

View file

@ -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'],
];
}
}

View file

@ -5,7 +5,7 @@
* Contains \Drupal\Tests\Component\Utility\ArgumentsResolverTest.
*/
namespace Drupal\Tests\Component\Utility {
namespace Drupal\Tests\Component\Utility;
use Drupal\Component\Utility\ArgumentsResolver;
use Drupal\Tests\UnitTestCase;
@ -187,7 +187,7 @@ class ArgumentsResolverTest extends UnitTestCase {
$data = [];
$data[] = [function($foo) {}];
$data[] = [[new TestClass(), 'access']];
$data[] = ['test_access_arguments_resolver_access'];
$data[] = ['Drupal\Tests\Component\Utility\test_access_arguments_resolver_access'];
return $data;
}
@ -214,9 +214,5 @@ interface TestInterface1 {
interface TestInterface2 {
}
}
namespace {
function test_access_arguments_resolver_access($foo) {
}
function test_access_arguments_resolver_access($foo) {
}

View file

@ -22,20 +22,6 @@ use Drupal\Tests\UnitTestCase;
*/
class SafeMarkupTest extends UnitTestCase {
/**
* The error message of the last error in the error handler.
*
* @var string
*/
protected $lastErrorMessage;
/**
* The error number of the last error in the error handler.
*
* @var int
*/
protected $lastErrorNumber;
/**
* {@inheritdoc}
*/
@ -137,7 +123,7 @@ class SafeMarkupTest extends UnitTestCase {
UrlHelper::setAllowedProtocols(['http', 'https', 'mailto']);
$result = SafeMarkup::format($string, $args);
$this->assertEquals($expected, $result, $message);
$this->assertEquals($expected, (string) $result, $message);
$this->assertEquals($expected_is_safe, $result instanceof MarkupInterface, 'SafeMarkup::format correctly sets the result as safe or not safe.');
foreach ($args as $arg) {
@ -171,42 +157,10 @@ class SafeMarkupTest extends UnitTestCase {
$tests['non-url-with-colon'] = ['Hey giraffe <a href=":url">MUUUH</a>', [':url' => "llamas: they are not URLs"], 'Hey giraffe <a href=" they are not URLs">MUUUH</a>', '', TRUE];
$tests['non-url-with-html'] = ['Hey giraffe <a href=":url">MUUUH</a>', [':url' => "<span>not a url</span>"], 'Hey giraffe <a href="&lt;span&gt;not a url&lt;/span&gt;">MUUUH</a>', '', TRUE];
// Tests non-standard placeholders that will not replace.
$tests['non-standard-placeholder'] = ['Hey hey', ['risky' => "<script>alert('foo');</script>"], 'Hey hey', '', TRUE];
return $tests;
}
/**
* Custom error handler that saves the last error.
*
* We need this custom error handler because we cannot rely on the error to
* exception conversion as __toString is never allowed to leak any kind of
* exception.
*
* @param int $error_number
* The error number.
* @param string $error_message
* The error message.
*/
public function errorHandler($error_number, $error_message) {
$this->lastErrorNumber = $error_number;
$this->lastErrorMessage = $error_message;
}
/**
* String formatting with SafeMarkup::format() and an unsupported placeholder.
*
* When you call SafeMarkup::format() with an unsupported placeholder, an
* InvalidArgumentException should be thrown.
*/
public function testUnexpectedFormat() {
// We set a custom error handler because of https://github.com/sebastianbergmann/phpunit/issues/487
set_error_handler([$this, 'errorHandler']);
// We want this to trigger an error.
$error = SafeMarkup::format('Broken placeholder: ~placeholder', ['~placeholder' => 'broken'])->__toString();
restore_error_handler();
$this->assertEquals(E_USER_ERROR, $this->lastErrorNumber);
$this->assertEquals('Invalid placeholder (~placeholder) in string: Broken placeholder: ~placeholder', $this->lastErrorMessage);
}
}

View file

@ -505,6 +505,18 @@ class XssTest extends UnitTestCase {
'Image tag with alt and title attribute',
array('img')
),
array(
'<a href="https://www.drupal.org/" rel="dc:publisher">Drupal</a>',
'<a href="https://www.drupal.org/" rel="dc:publisher">Drupal</a>',
'Link tag with rel attribute',
array('a')
),
array(
'<span property="dc:subject">Drupal 8: The best release ever.</span>',
'<span property="dc:subject">Drupal 8: The best release ever.</span>',
'Span tag with property attribute',
array('span')
),
array(
'<img src="http://example.com/foo.jpg" data-caption="Drupal 8: The best release ever.">',
'<img src="http://example.com/foo.jpg" data-caption="Drupal 8: The best release ever.">',