Skip to content
......@@ -8,6 +8,8 @@
/**
* Tests for configuration dependencies.
*
* @coversDefaultClass \Drupal\Core\Config\ConfigManager
*
* @group config
*/
class ConfigDependencyTest extends EntityKernelTestBase {
......@@ -346,6 +348,123 @@ public function testConfigEntityUninstallComplex(array $entity_id_suffixes) {
$this->assertFalse($storage->load($entity_4->id()), 'Entity 4 deleted');
}
/**
* @covers ::uninstall
* @covers ::getConfigEntitiesToChangeOnDependencyRemoval
*/
public function testConfigEntityUninstallThirdParty() {
/** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
$config_manager = \Drupal::service('config.manager');
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $storage */
$storage = $this->container->get('entity_type.manager')
->getStorage('config_test');
// Entity 1 will be fixed because it only has a dependency via third-party
// settings, which are fixable.
$entity_1 = $storage->create([
'id' => 'entity_1',
'dependencies' => [
'enforced' => [
'module' => ['config_test'],
],
],
'third_party_settings' => [
'node' => [
'foo' => 'bar',
],
],
]);
$entity_1->save();
// Entity 2 has a dependency on entity 1.
$entity_2 = $storage->create([
'id' => 'entity_2',
'dependencies' => [
'enforced' => [
'config' => [$entity_1->getConfigDependencyName()],
],
],
'third_party_settings' => [
'node' => [
'foo' => 'bar',
],
],
]);
$entity_2->save();
// Entity 3 will be unchanged because it is dependent on entity 2 which can
// be fixed. The ConfigEntityInterface::onDependencyRemoval() method will
// not be called for this entity.
$entity_3 = $storage->create([
'id' => 'entity_3',
'dependencies' => [
'enforced' => [
'config' => [$entity_2->getConfigDependencyName()],
],
],
]);
$entity_3->save();
// Entity 4's config dependency will be fixed but it will still be deleted
// because it also depends on the node module.
$entity_4 = $storage->create([
'id' => 'entity_4',
'dependencies' => [
'enforced' => [
'config' => [$entity_1->getConfigDependencyName()],
'module' => ['node', 'config_test'],
],
],
]);
$entity_4->save();
\Drupal::state()->set('config_test.fix_dependencies', []);
\Drupal::state()->set('config_test.on_dependency_removal_called', []);
// Do a dry run using
// \Drupal\Core\Config\ConfigManager::getConfigEntitiesToChangeOnDependencyRemoval().
$config_entities = $config_manager->getConfigEntitiesToChangeOnDependencyRemoval('module', ['node']);
$config_entity_ids = [
'update' => [],
'delete' => [],
'unchanged' => [],
];
foreach ($config_entities as $type => $config_entities_by_type) {
foreach ($config_entities_by_type as $config_entity) {
$config_entity_ids[$type][] = $config_entity->id();
}
}
$expected = [
'update' => [$entity_1->id(), $entity_2->id()],
'delete' => [$entity_4->id()],
'unchanged' => [$entity_3->id()],
];
$this->assertSame($expected, $config_entity_ids);
$called = \Drupal::state()->get('config_test.on_dependency_removal_called', []);
$this->assertFalse(in_array($entity_3->id(), $called), 'ConfigEntityInterface::onDependencyRemoval() is not called for entity 3.');
$this->assertSame([$entity_1->id(), $entity_4->id(), $entity_2->id()], $called, 'The most dependent entities have ConfigEntityInterface::onDependencyRemoval() called first.');
// Perform a module rebuild so we can know where the node module is located
// and uninstall it.
// @todo Remove as part of https://www.drupal.org/node/2186491
system_rebuild_module_data();
// Perform the uninstall.
$config_manager->uninstall('module', 'node');
// Test that expected actions have been performed.
$entity_1 = $storage->load($entity_1->id());
$this->assertTrue($entity_1, 'Entity 1 not deleted');
$this->assertSame($entity_1->getThirdPartySettings('node'), [], 'Entity 1 third party settings updated.');
$entity_2 = $storage->load($entity_2->id());
$this->assertTrue($entity_2, 'Entity 2 not deleted');
$this->assertSame($entity_2->getThirdPartySettings('node'), [], 'Entity 2 third party settings updated.');
$this->assertSame($entity_2->calculateDependencies()->getDependencies()['config'], [$entity_1->getConfigDependencyName()], 'Entity 2 still depends on entity 1.');
$entity_3 = $storage->load($entity_3->id());
$this->assertTrue($entity_3, 'Entity 3 not deleted');
$this->assertSame($entity_3->calculateDependencies()->getDependencies()['config'], [$entity_2->getConfigDependencyName()], 'Entity 3 still depends on entity 2.');
$this->assertFalse($storage->load($entity_4->id()), 'Entity 4 deleted');
}
/**
* Tests deleting a configuration entity and dependency management.
*/
......
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\comment\Entity\CommentType;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
/**
* @coversDefaultClass \Drupal\Core\Entity\EntityDisplayBase
*
* @group Entity
*/
class EntityDisplayBaseTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['entity_test', 'entity_test_third_party', 'field', 'system', 'comment'];
/**
* @covers ::onDependencyRemoval
*/
public function testOnDependencyRemoval() {
// Create a comment field for entity_test.
$comment_bundle = CommentType::create([
'id' => 'entity_test',
'label' => 'entity_test',
'description' => '',
'target_entity_type_id' => 'entity_test',
]);
$comment_bundle->save();
$comment_display = EntityViewDisplay::create([
'targetEntityType' => 'comment',
'bundle' => 'entity_test',
'mode' => 'default',
'status' => TRUE,
'third_party_settings' => [
'entity_test_third_party' => [
'key' => 'value',
],
],
]);
$comment_display->save();
$field_storage = FieldStorageConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'test_field',
'type' => 'comment',
'settings' => [
'comment_type' => 'entity_test',
],
]);
$field_storage->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'label' => $this->randomMachineName(),
'bundle' => 'entity_test',
]);
$field->save();
// Create an entity view display for entity_test.
$entity_display = EntityViewDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
'status' => TRUE,
'content' => [
'test_field' => ['type' => 'comment_default', 'settings' => ['view_mode' => 'default'], 'label' => 'hidden', 'third_party_settings' => []],
],
'third_party_settings' => [
'entity_test_third_party' => [
'key' => 'value',
],
],
]);
$entity_display->save();
$expected_component = [
'type' => 'comment_default',
'settings' => ['view_mode' => 'default'],
'label' => 'hidden',
'third_party_settings' => [],
];
$entity_display->getComponent('test_field');
$this->assertEquals($expected_component, $entity_display->getComponent('test_field'));
$expected_dependencies = [
'config' => [
'core.entity_view_display.comment.entity_test.default',
'field.field.entity_test.entity_test.test_field',
],
'module' => [
'comment',
'entity_test',
'entity_test_third_party',
],
];
$this->assertSame($expected_dependencies, $entity_display->getDependencies());
// Uninstall the third-party settings provider and reload the display.
$this->container->get('module_installer')->uninstall(['entity_test_third_party']);
$entity_display = EntityViewDisplay::load('entity_test.entity_test.default');
// The component should remain unchanged.
$this->assertEquals($expected_component, $entity_display->getComponent('test_field'));
// The dependencies should no longer contain 'entity_test_third_party'.
$expected_dependencies['module'] = [
'comment',
'entity_test',
];
$this->assertSame($expected_dependencies, $entity_display->getDependencies());
}
}
......@@ -119,7 +119,11 @@ abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase {
/**
* The temp file directory for the test environment.
*
* This is set in BrowserTestBase::prepareEnvironment().
* This is set in BrowserTestBase::prepareEnvironment(). This value has to
* match the temporary directory created in install_base_system() for test
* installs.
*
* @see install_base_system()
*
* @var string
*/
......@@ -351,7 +355,11 @@ protected function initMink() {
$driver = $this->getDefaultDriverInstance();
if ($driver instanceof GoutteDriver) {
$driver->getClient()->setClient(\Drupal::httpClient());
// Turn off curl timeout. Having a timeout is not a problem in a normal
// test running, but it is a problem when debugging.
/** @var \GuzzleHttp\Client $client */
$client = $this->container->get('http_client_factory')->fromOptions(['timeout' => NULL]);
$driver->getClient()->setClient($client);
}
$session = new Session($driver);
......@@ -1015,9 +1023,13 @@ public function installDrupal() {
$directory = DRUPAL_ROOT . '/' . $this->siteDirectory;
copy(DRUPAL_ROOT . '/sites/default/default.settings.php', $directory . '/settings.php');
// All file system paths are created by System module during installation.
// The public file system path is created during installation. Additionally,
// during tests:
// - The temporary directory is set and created by install_base_system().
// - The private file directory is created post install by this method.
// @see system_requirements()
// @see TestBase::prepareEnvironment()
// @see install_base_system()
$settings['settings']['file_public_path'] = (object) array(
'value' => $this->publicFilesDirectory,
'required' => TRUE,
......@@ -1092,16 +1104,8 @@ public function installDrupal() {
$config = $container->get('config.factory');
// Manually create and configure private and temporary files directories.
// Manually create the private directory.
file_prepare_directory($this->privateFilesDirectory, FILE_CREATE_DIRECTORY);
file_prepare_directory($this->tempFilesDirectory, FILE_CREATE_DIRECTORY);
// While the temporary files path could be preset/enforced in settings.php
// like the public files directory above, some tests expect it to be
// configurable in the UI. If declared in settings.php, it would no longer
// be configurable.
$config->getEditable('system.file')
->set('path.temporary', $this->tempFilesDirectory)
->save();
// Manually configure the test mail collector implementation to prevent
// tests from sending out emails and collect them in state instead.
......
......@@ -74,19 +74,18 @@ protected function getPaths() {
public function testComposerJson() {
foreach ($this->getPaths() as $path) {
$json = file_get_contents($path . '/composer.json');
$result = json_decode($json);
$this->assertNotNull($result, $this->getErrorMessages()[json_last_error()]);
}
}
/**
* Tests composer.lock hash.
* Tests composer.lock content-hash.
*/
public function testComposerLockHash() {
$json = file_get_contents($this->root . '/composer.json');
$content_hash = self::getContentHash(file_get_contents($this->root . '/composer.json'));
$lock = json_decode(file_get_contents($this->root . '/composer.lock'), TRUE);
$this->assertSame(md5($json), $lock['hash']);
$this->assertSame($content_hash, $lock['content-hash']);
}
/**
......@@ -126,4 +125,50 @@ public function testAllModulesReplaced() {
}
}
// @codingStandardsIgnoreStart
/**
* The following method is copied from \Composer\Package\Locker.
*
* @see https://github.com/composer/composer
*/
/**
* Returns the md5 hash of the sorted content of the composer file.
*
* @param string $composerFileContents The contents of the composer file.
*
* @return string
*/
protected static function getContentHash($composerFileContents)
{
$content = json_decode($composerFileContents, true);
$relevantKeys = array(
'name',
'version',
'require',
'require-dev',
'conflict',
'replace',
'provide',
'minimum-stability',
'prefer-stable',
'repositories',
'extra',
);
$relevantContent = array();
foreach (array_intersect($relevantKeys, array_keys($content)) as $key) {
$relevantContent[$key] = $content[$key];
}
if (isset($content['config']['platform'])) {
$relevantContent['config']['platform'] = $content['config']['platform'];
}
ksort($relevantContent);
return md5(json_encode($relevantContent));
}
// @codingStandardsIgnoreEnd
}
......@@ -5,6 +5,7 @@
use Drupal\Core\DrupalKernel;
use Drupal\Tests\UnitTestCase;
use org\bovigo\vfs\vfsStream;
use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\HttpFoundation\Request;
/**
......@@ -47,6 +48,63 @@ public function testTrustedHosts($host, $server_name, $message, $expected = FALS
Request::setFactory(NULL);
}
/**
* Tests the reregistration of autoloaders if APCu available.
*
* This test runs in a separate process since it registers class loaders and
* results in statics being set.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
* @requires function apcu_fetch
* @covers ::initializeSettings
*/
public function testInitializeSettings() {
$request = new Request();
$classloader = new fakeAutoloader();
// Create a kernel suitable for testing.
$kernel = $this->getMockBuilder(DrupalKernel::class)
->disableOriginalConstructor()
->setMethods(['do_not_mock_any_methods'])
->getMock();
$classloader_property = new \ReflectionProperty($kernel, 'classLoader');
$classloader_property->setAccessible(TRUE);
$classloader_property->setValue($kernel, $classloader);
$method = new \ReflectionMethod($kernel, 'initializeSettings');
$method->setAccessible(TRUE);
// Prepend another autoloader to simulate Drush's autoloader.
$fake_drush_autoloader = function () {
return NULL;
};
spl_autoload_register($fake_drush_autoloader, TRUE, TRUE);
// Before calling DrupalKernel::initializeSettings() the first autoloader
// is the fake Drush autoloader.
$this->assertSame($fake_drush_autoloader, spl_autoload_functions()[0]);
// Call DrupalKernel::initializeSettings() to simulate part of a Drupal
// bootstrap. During the include of autoload.php Composer would prepend
// Drupal's autoloader and then this method should not result in Drush's
// autoloader becoming the first autoloader even if it swaps out
// Composer's autoloader for an optimised one.
$method->invoke($kernel, $request);
$autoloaders = spl_autoload_functions();
// The first autoloader should be the APCu based autoloader.
$this->assertInstanceOf(ApcClassLoader::class, $autoloaders[0][0]);
// The second autoloader should be the original autoloader the kernel was
// constructed with.
$this->assertSame($classloader, $autoloaders[1][0]);
// The third autoloader should be Drush's autoloader.
$this->assertSame($fake_drush_autoloader, $autoloaders[2]);
// Reset the request factory because it is statically stored on the
// request.
Request::setFactory(NULL);
}
/**
* Provides test data for testTrustedHosts().
*/
......@@ -136,6 +194,49 @@ public function testFindSitePath() {
}
/**
* A fake autoloader for testing
*/
class fakeAutoloader {
/**
* Registers this instance as an autoloader.
*
* @param bool $prepend
* Whether to prepend the autoloader or not
*/
public function register($prepend = FALSE) {
spl_autoload_register(array($this, 'loadClass'), TRUE, $prepend);
}
/**
* Unregisters this instance as an autoloader.
*/
public function unregister() {
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @return null
* This class never loads.
*/
public function loadClass() {
return NULL;
}
/**
* Finds a file by class name while caching lookups to APC.
*
* @return null
* This class never finds.
*/
public function findFile() {
return NULL;
}
}
}
......
<?php
namespace Drupal\Tests\Core\StringTranslation;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Tests\UnitTestCase;
/**
* Tests the TranslatableMarkup class.
*
* @coversDefaultClass \Drupal\Core\StringTranslation\PluralTranslatableMarkup
* @group StringTranslation
*/
class PluralTranslatableMarkupTest extends UnitTestCase {
/**
* Tests serialization of PluralTranslatableMarkup().
*
* @dataProvider providerPluralTranslatableMarkupSerialization
*/
public function testPluralTranslatableMarkupSerialization($count, $expected_text) {
// Add a mock string translation service to the container.
$container = new ContainerBuilder();
$container->set('string_translation', $this->getStringTranslationStub());
\Drupal::setContainer($container);
// Create an object to serialize and unserialize.
$markup = new PluralTranslatableMarkup($count, 'singular @count', 'plural @count');
$serialized_markup = unserialize(serialize($markup));
$this->assertEquals($expected_text, $serialized_markup->render());
}
/**
* Data provider for ::testPluralTranslatableMarkupSerialization().
*/
public function providerPluralTranslatableMarkupSerialization() {
return [
[1, 'singular 1'],
[2, 'plural 2'],
];
}
}