Same filename and directory in other branches
- 8.9.x core/tests/Drupal/KernelTests/Core/Config/ConfigImporterMissingContentTest.php
- 9 core/tests/Drupal/KernelTests/Core/Config/ConfigImporterMissingContentTest.php
Namespace
Drupal\KernelTests\Core\Config
File
core/tests/Drupal/KernelTests/Core/Config/ConfigImporterMissingContentTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\KernelTests\Core\Config;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Block\Plugin\Block\Broken;
use Drupal\Core\Config\ConfigImporter;
use Drupal\Core\Config\StorageComparer;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Logger\RfcLoggerTrait;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\block\Traits\BlockCreationTrait;
use Psr\Log\LoggerInterface;
class ConfigImporterMissingContentTest extends KernelTestBase implements LoggerInterface {
use BlockCreationTrait;
use RfcLoggerTrait;
protected $logMessages = [];
protected $configImporter;
protected static $modules = [
'system',
'user',
'entity_test',
'config_test',
'config_import_test',
];
public function register(ContainerBuilder $container) {
parent::register($container);
$container
->register('logger.ConfigImporterMissingContentTest', __CLASS__)
->addTag('logger');
$container
->set('logger.ConfigImporterMissingContentTest', $this);
}
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('user');
$this
->installConfig([
'system',
'config_test',
]);
unset($GLOBALS['hook_config_test']);
$this
->copyConfig($this->container
->get('config.storage'), $this->container
->get('config.storage.sync'));
$storage_comparer = new StorageComparer($this->container
->get('config.storage.sync'), $this->container
->get('config.storage'));
$this->configImporter = new ConfigImporter($storage_comparer
->createChangelist(), $this->container
->get('event_dispatcher'), $this->container
->get('config.manager'), $this->container
->get('lock'), $this->container
->get('config.typed'), $this->container
->get('module_handler'), $this->container
->get('module_installer'), $this->container
->get('theme_handler'), $this->container
->get('string_translation'), $this->container
->get('extension.list.module'), $this->container
->get('extension.list.theme'));
}
public function testMissingContent() {
\Drupal::state()
->set('config_import_test.config_import_missing_content', TRUE);
$storage = $this->container
->get('config.storage');
$sync = $this->container
->get('config.storage.sync');
$entity_one = EntityTest::create([
'name' => 'one',
]);
$entity_two = EntityTest::create([
'name' => 'two',
]);
$entity_three = EntityTest::create([
'name' => 'three',
]);
$dynamic_name = 'config_test.dynamic.dotted.default';
$original_dynamic_data = $storage
->read($dynamic_name);
$original_dynamic_data['dependencies']['content'][] = $entity_one
->getConfigDependencyName();
$original_dynamic_data['dependencies']['content'][] = $entity_two
->getConfigDependencyName();
$original_dynamic_data['dependencies']['content'][] = $entity_three
->getConfigDependencyName();
$sync
->write($dynamic_name, $original_dynamic_data);
$this->configImporter
->reset()
->import();
$this
->assertEquals([], $this->configImporter
->getErrors(), 'There were no errors during the import.');
$this
->assertEquals($entity_one
->uuid(), \Drupal::state()
->get('config_import_test.config_import_missing_content_one'), 'The missing content event is fired during configuration import.');
$this
->assertEquals($entity_two
->uuid(), \Drupal::state()
->get('config_import_test.config_import_missing_content_two'), 'The missing content event is fired during configuration import.');
$original_dynamic_data = $storage
->read($dynamic_name);
$this
->assertEquals([
$entity_one
->getConfigDependencyName(),
$entity_two
->getConfigDependencyName(),
$entity_three
->getConfigDependencyName(),
], $original_dynamic_data['dependencies']['content'], 'The imported configuration entity has the missing content entity dependency.');
}
public function testMissingBlockContent() {
$this
->enableModules([
'block',
'block_content',
]);
$this->container
->get('theme_installer')
->install([
'stark',
]);
$this
->installEntitySchema('block_content');
$block_content_type = BlockContentType::create([
'id' => 'test',
'label' => 'Test block content',
'description' => "Provides a block type",
]);
$block_content_type
->save();
$block_content = BlockContent::create([
'info' => 'Prototype',
'type' => 'test',
'uuid' => '6376f337-fcbf-4b28-b30e-ed5b6932e692',
]);
$block_content
->save();
$plugin_id = 'block_content' . PluginBase::DERIVATIVE_SEPARATOR . $block_content
->uuid();
$block = $this
->placeBlock($plugin_id);
$storage = $this->container
->get('config.storage');
$sync = $this->container
->get('config.storage.sync');
$this
->copyConfig($storage, $sync);
$block
->delete();
$block_content
->delete();
$block_content_type
->delete();
$this->logMessages = [];
$config_importer = $this
->configImporter();
$config_importer
->import();
$this
->assertNotContains('The "block_content:6376f337-fcbf-4b28-b30e-ed5b6932e692" was not found', $this->logMessages);
$instance = $this->container
->get('plugin.manager.block')
->createInstance($plugin_id);
$this
->assertContains('The "block_content:6376f337-fcbf-4b28-b30e-ed5b6932e692" was not found', $this->logMessages);
$this
->assertInstanceOf(Broken::class, $instance);
}
public function log($level, $message, array $context = []) : void {
$this->logMessages[] = PlainTextOutput::renderFromHtml(new FormattableMarkup($message, $context));
}
}
Classes