Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663
This commit is contained in:
parent
eb34d130a8
commit
f32e58e4b1
8476 changed files with 211648 additions and 170042 deletions
92
core/tests/Drupal/KernelTests/AssertConfigTrait.php
Normal file
92
core/tests/Drupal/KernelTests/AssertConfigTrait.php
Normal file
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\KernelTests\AssertConfigTrait.
|
||||
*/
|
||||
|
||||
namespace Drupal\KernelTests;
|
||||
|
||||
use Drupal\Component\Diff\Diff;
|
||||
|
||||
/**
|
||||
* Trait to help with diffing config.
|
||||
*/
|
||||
trait AssertConfigTrait {
|
||||
|
||||
/**
|
||||
* Ensures that a specific config diff does not contain unwanted changes.
|
||||
*
|
||||
* @param \Drupal\Component\Diff\Diff $result
|
||||
* The diff result for the passed in config name.
|
||||
* @param string $config_name
|
||||
* The config name to check.
|
||||
* @param array $skipped_config
|
||||
* An array of skipped config, keyed by string. If the value is TRUE, the
|
||||
* entire file will be ignored, otherwise it's an array of strings which are
|
||||
* ignored.
|
||||
*
|
||||
* @throws \Exception
|
||||
* Thrown when a configuration is different.
|
||||
*/
|
||||
protected function assertConfigDiff(Diff $result, $config_name, array $skipped_config) {
|
||||
foreach ($result->getEdits() as $op) {
|
||||
switch (get_class($op)) {
|
||||
case 'Drupal\Component\Diff\Engine\DiffOpCopy':
|
||||
// Nothing to do, a copy is what we expect.
|
||||
break;
|
||||
case 'Drupal\Component\Diff\Engine\DiffOpDelete':
|
||||
case 'Drupal\Component\Diff\Engine\DiffOpChange':
|
||||
// It is not part of the skipped config, so we can directly throw the
|
||||
// exception.
|
||||
if (!in_array($config_name, array_keys($skipped_config))) {
|
||||
throw new \Exception($config_name . ': ' . var_export($op, TRUE));
|
||||
}
|
||||
|
||||
// Allow to skip entire config files.
|
||||
if ($skipped_config[$config_name] === TRUE) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Allow to skip some specific lines of imported config files.
|
||||
// Ensure that the only changed lines are the ones we marked as
|
||||
// skipped.
|
||||
$all_skipped = TRUE;
|
||||
|
||||
$changes = get_class($op) == 'Drupal\Component\Diff\Engine\DiffOpDelete' ? $op->orig : $op->closing;
|
||||
foreach ($changes as $closing) {
|
||||
// Skip some of the changes, as they are caused by module install
|
||||
// code.
|
||||
$found = FALSE;
|
||||
if (!empty($skipped_config[$config_name])) {
|
||||
foreach ($skipped_config[$config_name] as $line) {
|
||||
if (strpos($closing, $line) !== FALSE) {
|
||||
$found = TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
$all_skipped = $all_skipped && $found;
|
||||
}
|
||||
|
||||
if (!$all_skipped) {
|
||||
throw new \Exception($config_name . ': ' . var_export($op, TRUE));
|
||||
}
|
||||
break;
|
||||
case 'Drupal\Component\Diff\Engine\DiffOpAdd':
|
||||
foreach ($op->closing as $closing) {
|
||||
// The UUIDs don't exist in the default config.
|
||||
if (strpos($closing, 'uuid: ') === 0) {
|
||||
continue;
|
||||
}
|
||||
throw new \Exception($config_name . ': ' . var_export($op, TRUE));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new \Exception($config_name . ': ' . var_export($op, TRUE));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\KernelTests\Component\Utility\SafeMarkupKernelTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\KernelTests\Component\Utility;
|
||||
|
||||
use Drupal\Component\FileCache\FileCacheFactory;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\Site\Settings;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Provides a test covering integration of SafeMarkup with other systems.
|
||||
*
|
||||
* @group Utility
|
||||
*/
|
||||
class SafeMarkupKernelTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['system'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installSchema('system', 'router');
|
||||
$this->container->get('router.builder')->rebuild();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets arguments for SafeMarkup::format() based on Url::fromUri() parameters.
|
||||
*
|
||||
* @param string $uri
|
||||
* The URI of the resource.
|
||||
*
|
||||
* @param array $options
|
||||
* The options to pass to Url::fromUri().
|
||||
*
|
||||
* @return array
|
||||
* Array containing:
|
||||
* - ':url': A URL string.
|
||||
*/
|
||||
protected static function getSafeMarkupUriArgs($uri, $options = []) {
|
||||
$args[':url'] = Url::fromUri($uri, $options)->toString();
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests URL ":placeholders" in SafeMarkup::format().
|
||||
*
|
||||
* @dataProvider providerTestSafeMarkupUri
|
||||
*/
|
||||
public function testSafeMarkupUri($string, $uri, $options, $expected) {
|
||||
$args = self::getSafeMarkupUriArgs($uri, $options);
|
||||
$this->assertEquals($expected, SafeMarkup::format($string, $args));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function providerTestSafeMarkupUri() {
|
||||
$data = [];
|
||||
$data['routed-url'] = [
|
||||
'Hey giraffe <a href=":url">MUUUH</a>',
|
||||
'route:system.admin',
|
||||
[],
|
||||
'Hey giraffe <a href="/admin">MUUUH</a>',
|
||||
];
|
||||
$data['routed-with-query'] = [
|
||||
'Hey giraffe <a href=":url">MUUUH</a>',
|
||||
'route:system.admin',
|
||||
['query' => ['bar' => 'baz#']],
|
||||
'Hey giraffe <a href="/admin?bar=baz%23">MUUUH</a>',
|
||||
];
|
||||
$data['routed-with-fragment'] = [
|
||||
'Hey giraffe <a href=":url">MUUUH</a>',
|
||||
'route:system.admin',
|
||||
['fragment' => 'bar<'],
|
||||
'Hey giraffe <a href="/admin#bar&lt;">MUUUH</a>',
|
||||
];
|
||||
$data['unrouted-url'] = [
|
||||
'Hey giraffe <a href=":url">MUUUH</a>',
|
||||
'base://foo',
|
||||
[],
|
||||
'Hey giraffe <a href="/foo">MUUUH</a>',
|
||||
];
|
||||
$data['unrouted-with-query'] = [
|
||||
'Hey giraffe <a href=":url">MUUUH</a>',
|
||||
'base://foo',
|
||||
['query' => ['bar' => 'baz#']],
|
||||
'Hey giraffe <a href="/foo?bar=baz%23">MUUUH</a>',
|
||||
];
|
||||
$data['unrouted-with-fragment'] = [
|
||||
'Hey giraffe <a href=":url">MUUUH</a>',
|
||||
'base://foo',
|
||||
['fragment' => 'bar<'],
|
||||
'Hey giraffe <a href="/foo#bar&lt;">MUUUH</a>',
|
||||
];
|
||||
$data['mailto-protocol'] = [
|
||||
'Hey giraffe <a href=":url">MUUUH</a>',
|
||||
'mailto:test@example.com',
|
||||
[],
|
||||
'Hey giraffe <a href="mailto:test@example.com">MUUUH</a>',
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerTestSafeMarkupUriWithException
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testSafeMarkupUriWithExceptionUri($string, $uri) {
|
||||
// Should throw an \InvalidArgumentException, due to Uri::toString().
|
||||
$args = self::getSafeMarkupUriArgs($uri);
|
||||
|
||||
SafeMarkup::format($string, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function providerTestSafeMarkupUriWithException() {
|
||||
$data = [];
|
||||
$data['js-protocol'] = [
|
||||
'Hey giraffe <a href=":url">MUUUH</a>',
|
||||
"javascript:alert('xss')",
|
||||
];
|
||||
$data['js-with-fromCharCode'] = [
|
||||
'Hey giraffe <a href=":url">MUUUH</a>',
|
||||
"javascript:alert(String.fromCharCode(88,83,83))",
|
||||
];
|
||||
$data['non-url-with-colon'] = [
|
||||
'Hey giraffe <a href=":url">MUUUH</a>',
|
||||
"llamas: they are not URLs",
|
||||
];
|
||||
$data['non-url-with-html'] = [
|
||||
'Hey giraffe <a href=":url">MUUUH</a>',
|
||||
'<span>not a url</span>',
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
119
core/tests/Drupal/KernelTests/Config/DefaultConfigTest.php
Normal file
119
core/tests/Drupal/KernelTests/Config/DefaultConfigTest.php
Normal file
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\KernelTests\Config\DefaultConfigTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\KernelTests\Config;
|
||||
|
||||
use Drupal\Component\FileCache\FileCacheFactory;
|
||||
use Drupal\Core\Config\FileStorage;
|
||||
use Drupal\Core\Config\InstallStorage;
|
||||
use Drupal\Core\Config\StorageInterface;
|
||||
use Drupal\Core\Site\Settings;
|
||||
use Drupal\KernelTests\AssertConfigTrait;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests that the installed config matches the default config.
|
||||
*
|
||||
* @group Config
|
||||
*/
|
||||
class DefaultConfigTest extends KernelTestBase {
|
||||
|
||||
use AssertConfigTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $timeLimit = 500;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['system', 'user'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// @todo ModuleInstaller calls system_rebuild_module_data which is part of
|
||||
// system.module, see https://www.drupal.org/node/2208429.
|
||||
include_once $this->root . '/core/modules/system/system.module';
|
||||
|
||||
// Set up the state values so we know where to find the files when running
|
||||
// drupal_get_filename().
|
||||
// @todo Remove as part of https://www.drupal.org/node/2186491
|
||||
system_rebuild_module_data();
|
||||
|
||||
$this->installSchema('system', 'router');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if installed config is equal to the exported config.
|
||||
*
|
||||
* @dataProvider providerTestModuleConfig
|
||||
*/
|
||||
public function testModuleConfig($module) {
|
||||
/** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
|
||||
$module_installer = $this->container->get('module_installer');
|
||||
/** @var \Drupal\Core\Config\StorageInterface $active_config_storage */
|
||||
$active_config_storage = $this->container->get('config.storage');
|
||||
/** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
|
||||
$config_manager = $this->container->get('config.manager');
|
||||
|
||||
$module_installer->install([$module]);
|
||||
|
||||
// System and user are required in order to be able to install some of the
|
||||
// other modules. Therefore they are put into static::$modules, which though
|
||||
// doesn't install config files, so import those config files explicitly.
|
||||
switch ($module) {
|
||||
case 'system':
|
||||
case 'user':
|
||||
$this->installConfig([$module]);
|
||||
break;
|
||||
}
|
||||
|
||||
$default_install_path = drupal_get_path('module', $module) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
|
||||
$module_config_storage = new FileStorage($default_install_path, StorageInterface::DEFAULT_COLLECTION);
|
||||
|
||||
// The following config entries are changed on module install, so compare
|
||||
// them doesn't make sense.
|
||||
$skipped_config = [];
|
||||
$skipped_config['locale.settings'][] = 'path: ';
|
||||
$skipped_config['syslog.settings'][] = 'facility: ';
|
||||
// @todo Figure out why simpletest.settings is not installed.
|
||||
$skipped_config['simpletest.settings'] = TRUE;
|
||||
|
||||
// Compare the installed config with the one in the module directory.
|
||||
foreach ($module_config_storage->listAll() as $config_name) {
|
||||
$result = $config_manager->diff($module_config_storage, $active_config_storage, $config_name);
|
||||
$this->assertConfigDiff($result, $config_name, $skipped_config);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test data provider for ::testModuleConfig().
|
||||
*
|
||||
* @return array
|
||||
* An array of module names to test.
|
||||
*/
|
||||
public function providerTestModuleConfig() {
|
||||
$module_dirs = array_keys(iterator_to_array(new \FilesystemIterator(__DIR__ . '/../../../../modules/')));
|
||||
$module_names = array_map(function($path) {
|
||||
return str_replace(__DIR__ . '/../../../../modules/', '', $path);
|
||||
}, $module_dirs);
|
||||
$modules_keyed = array_combine($module_names, $module_names);
|
||||
|
||||
$data = array_map(function ($module) {
|
||||
return [$module];
|
||||
}, $modules_keyed);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\KernelTests\Core\Common\DrupalSetMessageTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\KernelTests\Core\Common;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* @covers ::drupal_set_message
|
||||
* @group PHPUnit
|
||||
*/
|
||||
class DrupalSetMessageTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* The basic functionality of drupal_set_message().
|
||||
*/
|
||||
public function testDrupalSetMessage() {
|
||||
drupal_set_message(t('A message: @foo', ['@foo' => 'bar']));
|
||||
$messages = drupal_get_messages();
|
||||
$this->assertInstanceOf('Drupal\Core\Render\Markup', $messages['status'][0]);
|
||||
$this->assertEquals('A message: bar', (string) $messages['status'][0]);
|
||||
}
|
||||
|
||||
public function tearDown() {
|
||||
// Clear session to prevent global leakage.
|
||||
unset($_SESSION['messages']);
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\KernelTests\Core\StringTranslation\TranslationStringTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\KernelTests\Core\StringTranslation;
|
||||
|
||||
use Drupal\Core\Site\Settings;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
|
||||
/**
|
||||
* Tests the TranslatableMarkup class.
|
||||
*
|
||||
* @group StringTranslation
|
||||
*/
|
||||
class TranslationStringTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = [
|
||||
'language'
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
ConfigurableLanguage::createFromLangcode('de')->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that TranslatableMarkup objects can be compared.
|
||||
*/
|
||||
public function testComparison() {
|
||||
$this->rebootAndPrepareSettings();
|
||||
$a = \Drupal::service('string_translation')->translate('Example @number', ['@number' => 42], ['langcode' => 'de']);
|
||||
|
||||
$this->rebootAndPrepareSettings();
|
||||
$b = \Drupal::service('string_translation')->translate('Example @number', ['@number' => 42], ['langcode' => 'de']);
|
||||
$c = \Drupal::service('string_translation')->translate('Example @number', ['@number' => 43], ['langcode' => 'de']);
|
||||
$d = \Drupal::service('string_translation')->translate('Example @number', ['@number' => 42], ['langcode' => 'en']);
|
||||
|
||||
// The two objects have the same settings so == comparison will work.
|
||||
$this->assertEquals($a, $b);
|
||||
// The two objects are not the same object.
|
||||
$this->assertNotSame($a, $b);
|
||||
// TranslationWrappers which have different settings are not equal.
|
||||
$this->assertNotEquals($a, $c);
|
||||
$this->assertNotEquals($a, $d);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reboots the kernel to set custom translations in Settings.
|
||||
*/
|
||||
protected function rebootAndPrepareSettings() {
|
||||
// Reboot the container so that different services are injected and the new
|
||||
// settings are picked.
|
||||
$kernel = $this->container->get('kernel');
|
||||
$kernel->shutdown();
|
||||
$kernel->boot();
|
||||
$settings = Settings::getAll();
|
||||
$settings['locale_custom_strings_de'] = ['' => ['Example @number' => 'Example @number translated']];
|
||||
// Recreate the settings static.
|
||||
new Settings($settings);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\KernelTests\Core\Theme\ThemeRenderAndAutoescapeTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\KernelTests\Core\Theme;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Core\Link;
|
||||
use Drupal\Core\Render\RenderContext;
|
||||
use Drupal\Core\Render\Markup;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests the theme_render_and_autoescape() function.
|
||||
*
|
||||
* @group Theme
|
||||
*/
|
||||
class ThemeRenderAndAutoescapeTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['system'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installSchema('system', 'router');
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerTestThemeRenderAndAutoescape
|
||||
*/
|
||||
public function testThemeRenderAndAutoescape($arg, $expected) {
|
||||
if (is_array($arg) && isset($arg['#type']) && $arg['#type'] === 'link') {
|
||||
$arg = Link::createFromRoute($arg['#title'], $arg['#url']);
|
||||
}
|
||||
|
||||
$context = new RenderContext();
|
||||
// Use a closure here since we need to render with a render context.
|
||||
$theme_render_and_autoescape = function () use ($arg) {
|
||||
return theme_render_and_autoescape($arg);
|
||||
};
|
||||
/** @var \Drupal\Core\Render\RendererInterface $renderer */
|
||||
$renderer = \Drupal::service('renderer');
|
||||
$output = $renderer->executeInRenderContext($context, $theme_render_and_autoescape);
|
||||
$this->assertEquals($expected, $output);
|
||||
$this-> assertInternalType('string', $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide test examples.
|
||||
*/
|
||||
public function providerTestThemeRenderAndAutoescape() {
|
||||
return [
|
||||
'empty string unchanged' => ['', ''],
|
||||
'simple string unchanged' => ['ab', 'ab'],
|
||||
'int (scalar) cast to string' => [111, '111'],
|
||||
'float (scalar) cast to string' => [2.10, '2.10'],
|
||||
'> is escaped' => ['>', '>'],
|
||||
'Markup EM tag is unchanged' => [Markup::create('<em>hi</em>'), '<em>hi</em>'],
|
||||
'Markup SCRIPT tag is unchanged' => [Markup::create('<script>alert("hi");</script>'), '<script>alert("hi");</script>'],
|
||||
'EM tag in string is escaped' => ['<em>hi</em>', Html::escape('<em>hi</em>')],
|
||||
'type link render array is rendered' => [['#type' => 'link', '#title' => 'Text', '#url' => '<none>'], '<a href="">Text</a>'],
|
||||
'type markup with EM tags is rendered' => [['#markup' => '<em>hi</em>'], '<em>hi</em>'],
|
||||
'SCRIPT tag in string is escaped' => [
|
||||
'<script>alert(123)</script>',
|
||||
Html::escape('<script>alert(123)</script>')
|
||||
],
|
||||
'type plain_text render array EM tag is escaped' => [['#plain_text' => '<em>hi</em>'], Html::escape('<em>hi</em>')],
|
||||
'type hidden render array is rendered' => [['#type' => 'hidden', '#name' => 'foo', '#value' => 'bar'], "<input type=\"hidden\" name=\"foo\" value=\"bar\" />\n"],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures invalid content is handled correctly.
|
||||
*
|
||||
* @expectedException \Exception
|
||||
*/
|
||||
public function testThemeEscapeAndRenderNotPrintable() {
|
||||
theme_render_and_autoescape(new NonPrintable());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class NonPrintable { }
|
|
@ -7,6 +7,9 @@
|
|||
|
||||
namespace Drupal\KernelTests;
|
||||
|
||||
use Drupal\Component\FileCache\ApcuFileCacheBackend;
|
||||
use Drupal\Component\FileCache\FileCache;
|
||||
use Drupal\Component\FileCache\FileCacheFactory;
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\Config\ConfigImporter;
|
||||
|
@ -20,6 +23,7 @@ use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
|
|||
use Drupal\Core\Extension\ExtensionDiscovery;
|
||||
use Drupal\Core\Site\Settings;
|
||||
use Drupal\simpletest\AssertContentTrait;
|
||||
use Drupal\simpletest\AssertHelperTrait;
|
||||
use Drupal\simpletest\RandomGeneratorTrait;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
@ -52,6 +56,7 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
|
|||
|
||||
use AssertLegacyTrait;
|
||||
use AssertContentTrait;
|
||||
use AssertHelperTrait;
|
||||
use RandomGeneratorTrait;
|
||||
|
||||
/**
|
||||
|
@ -212,6 +217,7 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
|
|||
parent::setUp();
|
||||
|
||||
$this->root = static::getDrupalRoot();
|
||||
$this->initFileCache();
|
||||
$this->bootEnvironment();
|
||||
$this->bootKernel();
|
||||
}
|
||||
|
@ -254,8 +260,7 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
|
|||
$this->siteDirectory = vfsStream::url('root/sites/simpletest/' . $suffix);
|
||||
|
||||
mkdir($this->siteDirectory . '/files', 0775);
|
||||
mkdir($this->siteDirectory . '/files/config/' . CONFIG_ACTIVE_DIRECTORY, 0775, TRUE);
|
||||
mkdir($this->siteDirectory . '/files/config/' . CONFIG_STAGING_DIRECTORY, 0775, TRUE);
|
||||
mkdir($this->siteDirectory . '/files/config/' . CONFIG_SYNC_DIRECTORY, 0775, TRUE);
|
||||
|
||||
// Ensure that all code that relies on drupal_valid_test_ua() can still be
|
||||
// safely executed. This primarily affects the (test) site directory
|
||||
|
@ -273,8 +278,7 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
|
|||
new Settings($settings);
|
||||
|
||||
$GLOBALS['config_directories'] = array(
|
||||
CONFIG_ACTIVE_DIRECTORY => $this->siteDirectory . '/files/config/active',
|
||||
CONFIG_STAGING_DIRECTORY => $this->siteDirectory . '/files/config/staging',
|
||||
CONFIG_SYNC_DIRECTORY => $this->siteDirectory . '/files/config/sync',
|
||||
);
|
||||
|
||||
foreach (Database::getAllConnectionInfo() as $key => $targets) {
|
||||
|
@ -343,6 +347,14 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
|
|||
// register() is only called if a new container was built/compiled.
|
||||
$this->container = $kernel->getContainer();
|
||||
|
||||
// Ensure database tasks have been run.
|
||||
require_once __DIR__ . '/../../../includes/install.inc';
|
||||
$connection = Database::getConnection();
|
||||
$errors = db_installer_object($connection->driver())->runTasks();
|
||||
if (!empty($errors)) {
|
||||
$this->fail('Failed to run installer database tasks: ' . implode(', ', $errors));
|
||||
}
|
||||
|
||||
if ($modules) {
|
||||
$this->container->get('module_handler')->loadAll();
|
||||
}
|
||||
|
@ -394,17 +406,17 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
|
|||
protected function getDatabaseConnectionInfo() {
|
||||
// If the test is run with argument dburl then use it.
|
||||
$db_url = getenv('SIMPLETEST_DB');
|
||||
if (!empty($db_url)) {
|
||||
if (empty($db_url)) {
|
||||
$this->markTestSkipped('There is no database connection so no tests can be run. You must provide a SIMPLETEST_DB environment variable to run PHPUnit based functional tests outside of run-tests.sh. See https://www.drupal.org/node/2116263#skipped-tests for more information.');
|
||||
}
|
||||
else {
|
||||
$database = Database::convertDbUrlToConnectionInfo($db_url, $this->root);
|
||||
Database::addConnectionInfo('default', 'default', $database);
|
||||
}
|
||||
|
||||
// Clone the current connection and replace the current prefix.
|
||||
$connection_info = Database::getConnectionInfo('default');
|
||||
if (is_null($connection_info)) {
|
||||
throw new \InvalidArgumentException('There is no database connection so no tests can be run. You must provide a SIMPLETEST_DB environment variable, like "sqlite://localhost//tmp/test.sqlite", to run PHPUnit based functional tests outside of run-tests.sh.');
|
||||
}
|
||||
else {
|
||||
if (!empty($connection_info)) {
|
||||
Database::renameConnection('default', 'simpletest_original_default');
|
||||
foreach ($connection_info as $target => $value) {
|
||||
// Replace the full table prefix definition to ensure that no table
|
||||
|
@ -479,6 +491,32 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
|
|||
return $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the FileCache component.
|
||||
*
|
||||
* We can not use the Settings object in a component, that's why we have to do
|
||||
* it here instead of \Drupal\Component\FileCache\FileCacheFactory.
|
||||
*/
|
||||
protected function initFileCache() {
|
||||
$configuration = Settings::get('file_cache');
|
||||
|
||||
// Provide a default configuration, if not set.
|
||||
if (!isset($configuration['default'])) {
|
||||
$configuration['default'] = [
|
||||
'class' => FileCache::class,
|
||||
'cache_backend_class' => NULL,
|
||||
'cache_backend_configuration' => [],
|
||||
];
|
||||
// @todo Use extension_loaded('apcu') for non-testbot
|
||||
// https://www.drupal.org/node/2447753.
|
||||
if (function_exists('apc_fetch')) {
|
||||
$configuration['default']['cache_backend_class'] = ApcuFileCacheBackend::class;
|
||||
}
|
||||
}
|
||||
FileCacheFactory::setConfiguration($configuration);
|
||||
FileCacheFactory::setPrefix(Settings::getApcuPrefix('file_cache', $this->root));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Extension objects for $modules to enable.
|
||||
*
|
||||
|
@ -625,6 +663,9 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
|
|||
}
|
||||
}
|
||||
|
||||
// Clean FileCache cache.
|
||||
FileCache::reset();
|
||||
|
||||
// Clean up statics, container, and settings.
|
||||
if (function_exists('drupal_static_reset')) {
|
||||
drupal_static_reset();
|
||||
|
@ -863,8 +904,14 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
|
|||
* The rendered string output (typically HTML).
|
||||
*/
|
||||
protected function render(array &$elements) {
|
||||
$content = $this->container->get('renderer')->render($elements);
|
||||
drupal_process_attached($elements);
|
||||
// Use the bare HTML page renderer to render our links.
|
||||
$renderer = $this->container->get('bare_html_page_renderer');
|
||||
$response = $renderer->renderBarePage(
|
||||
$elements, '', $this->container->get('theme.manager')->getActiveTheme()->getName()
|
||||
);
|
||||
|
||||
// Glean the content from the response object.
|
||||
$content = $response->getContent();
|
||||
$this->setRawContent($content);
|
||||
$this->verbose('<pre style="white-space: pre-wrap">' . Html::escape($content));
|
||||
return $content;
|
||||
|
@ -896,7 +943,7 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
|
|||
if (!$this->configImporter) {
|
||||
// Set up the ConfigImporter object for testing.
|
||||
$storage_comparer = new StorageComparer(
|
||||
$this->container->get('config.storage.staging'),
|
||||
$this->container->get('config.storage.sync'),
|
||||
$this->container->get('config.storage'),
|
||||
$this->container->get('config.manager')
|
||||
);
|
||||
|
@ -1025,7 +1072,7 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
|
|||
trigger_error(sprintf("KernelTestBase::\$%s no longer exists. Use the regular API method to retrieve it instead (e.g., Settings).", $name), E_USER_DEPRECATED);
|
||||
switch ($name) {
|
||||
case 'public_files_directory':
|
||||
return Settings::get('file_public_path', conf_path() . '/files');
|
||||
return Settings::get('file_public_path', \Drupal::service('site.path') . '/files');
|
||||
|
||||
case 'private_files_directory':
|
||||
return $this->container->get('config.factory')->get('system.file')->get('path.private');
|
||||
|
@ -1034,15 +1081,14 @@ abstract class KernelTestBase extends \PHPUnit_Framework_TestCase implements Ser
|
|||
return file_directory_temp();
|
||||
|
||||
case 'translation_files_directory':
|
||||
return Settings::get('file_public_path', conf_path() . '/translations');
|
||||
return Settings::get('file_public_path', \Drupal::service('site.path') . '/translations');
|
||||
}
|
||||
}
|
||||
|
||||
if ($name === 'configDirectories') {
|
||||
trigger_error(sprintf("KernelTestBase::\$%s no longer exists. Use config_get_config_directory() directly instead.", $name), E_USER_DEPRECATED);
|
||||
return array(
|
||||
CONFIG_ACTIVE_DIRECTORY => config_get_config_directory(CONFIG_ACTIVE_DIRECTORY),
|
||||
CONFIG_STAGING_DIRECTORY => config_get_config_directory(CONFIG_STAGING_DIRECTORY),
|
||||
CONFIG_SYNC_DIRECTORY => config_get_config_directory(CONFIG_SYNC_DIRECTORY),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
namespace Drupal\KernelTests;
|
||||
|
||||
use Drupal\Component\FileCache\FileCacheFactory;
|
||||
use Drupal\Core\Database\Database;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;
|
||||
|
@ -38,8 +39,7 @@ class KernelTestBaseTest extends KernelTestBase {
|
|||
substr($this->databasePrefix, 10) => array(
|
||||
'files' => array(
|
||||
'config' => array(
|
||||
'active' => array(),
|
||||
'staging' => array(),
|
||||
'sync' => array(),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -74,8 +74,8 @@ class KernelTestBaseTest extends KernelTestBase {
|
|||
$GLOBALS['destroy-me'] = TRUE;
|
||||
$this->assertArrayHasKey('destroy-me', $GLOBALS);
|
||||
|
||||
$schema = $this->container->get('database')->schema();
|
||||
$schema->createTable('foo', array(
|
||||
$database = $this->container->get('database');
|
||||
$database->schema()->createTable('foo', array(
|
||||
'fields' => array(
|
||||
'number' => array(
|
||||
'type' => 'int',
|
||||
|
@ -84,7 +84,16 @@ class KernelTestBaseTest extends KernelTestBase {
|
|||
),
|
||||
),
|
||||
));
|
||||
$this->assertTrue($schema->tableExists('foo'));
|
||||
$this->assertTrue($database->schema()->tableExists('foo'));
|
||||
|
||||
// Ensure that the database tasks have been run during set up. Neither MySQL
|
||||
// nor SQLite make changes that are testable.
|
||||
if ($database->driver() == 'pgsql') {
|
||||
$this->assertEquals('on', $database->query("SHOW standard_conforming_strings")->fetchField());
|
||||
$this->assertEquals('escape', $database->query("SHOW bytea_output")->fetchField());
|
||||
}
|
||||
|
||||
$this->assertNotNull(FileCacheFactory::getPrefix());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Reference in a new issue