View source
<?php
namespace Drupal\Tests\core_context\Functional;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionComponent;
use Drupal\Tests\BrowserTestBase;
class LayoutBuilderIntegrationTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'core_context',
'core_context_test',
'layout_builder',
'node',
];
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
]);
$this
->drupalPlaceBlock('local_tasks_block');
$storage = FieldStorageConfig::create([
'entity_type' => 'node',
'field_name' => 'context',
'type' => 'context',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
]);
$storage
->save();
FieldConfig::create([
'field_storage' => $storage,
'bundle' => 'page',
])
->save();
}
public function provider() {
return [
'context values in third-party entity display settings' => [
'block_configuration' => [
'id' => 'context_block',
'context_mapping' => [
'value' => '@core_context:value',
'letter' => '@core_context:letter',
],
],
'layout_overridable' => FALSE,
'third_party_contexts' => [
'value' => [
'type' => 'integer',
'label' => 'Holy computer number',
'description' => 'A convenient power of two',
'value' => 512,
],
'letter' => [
'type' => 'string',
'label' => 'Sponsoring letter',
'description' => 'A letter of the NATO phonetic alphabet',
'value' => 'Charlie',
],
],
'entity_values' => [],
],
'context values in third-party entity display settings, without mapping' => [
'block_configuration' => [
'id' => 'context_block_optional',
],
'layout_overridable' => FALSE,
'third_party_contexts' => [
'value' => [
'type' => 'integer',
'label' => 'Holy computer number',
'description' => 'A convenient power of two',
'value' => 512,
],
'letter' => [
'type' => 'string',
'label' => 'Sponsoring letter',
'description' => 'A letter of the NATO phonetic alphabet',
'value' => 'Charlie',
],
],
'entity_values' => [],
],
'context values in entity field' => [
'block_configuration' => [
'id' => 'context_block',
'context_mapping' => [
'value' => '@core_context:value',
'letter' => '@core_context:letter',
],
],
'layout_overridable' => TRUE,
'third_party_contexts' => [],
'entity_values' => [
'context' => [
[
'id' => 'value',
'type' => 'integer',
'label' => 'Holy computer number',
'description' => 'A convenient power of two',
'value' => 512,
],
[
'id' => 'letter',
'type' => 'string',
'label' => 'Sponsoring letter',
'description' => 'A letter of the NATO phonetic alphabet',
'value' => 'Charlie',
],
],
],
],
'context values in entity field, without mapping' => [
'block_configuration' => [
'id' => 'context_block_optional',
],
'layout_overridable' => TRUE,
'third_party_contexts' => [],
'entity_values' => [
'context' => [
[
'id' => 'value',
'type' => 'integer',
'label' => 'Holy computer number',
'description' => 'A convenient power of two',
'value' => 512,
],
[
'id' => 'letter',
'type' => 'string',
'label' => 'Sponsoring letter',
'description' => 'A letter of the NATO phonetic alphabet',
'value' => 'Charlie',
],
],
],
],
'context values in entity field and third-party entity display settings, without mapping' => [
'block_configuration' => [
'id' => 'context_block_optional',
],
'layout_overridable' => TRUE,
'third_party_contexts' => [
'value' => [
'type' => 'integer',
'label' => 'Holy computer number',
'description' => 'A convenient power of two',
'value' => 512,
],
],
'entity_values' => [
'context' => [
[
'id' => 'letter',
'type' => 'string',
'label' => 'Sponsoring letter',
'description' => 'A letter of the NATO phonetic alphabet',
'value' => 'Charlie',
],
],
],
],
'context values in block plugin configuration' => [
'block_configuration' => [
'id' => 'context_block',
'context' => [
'value' => 512,
'letter' => 'Charlie',
],
],
'layout_overridable' => FALSE,
'third_party_contexts' => [],
'entity_values' => [],
],
];
}
public function test(array $block_configuration, $layout_overridable = FALSE, $third_party_contexts = [], array $entity_values = []) {
$page = $this
->getSession()
->getPage();
$component = SectionComponent::fromArray([
'uuid' => $this->container
->get('uuid')
->generate(),
'region' => 'content',
'configuration' => $block_configuration,
'additional' => [],
'weight' => 0,
]);
$section = new Section('layout_onecol');
$section
->appendComponent($component);
$display = $this->container
->get('entity_display.repository')
->getViewDisplay('node', 'page', 'full');
$display
->enableLayoutBuilder()
->setOverridable($layout_overridable)
->appendSection($section)
->setThirdPartySetting('core_context', 'contexts', $third_party_contexts)
->save();
$account = $this
->drupalCreateUser([
'administer node display',
'configure any layout',
'edit own page content',
]);
$this
->drupalLogin($account);
$entity_values += [
'type' => 'page',
];
$node = $this
->drupalCreateNode($entity_values);
if ($layout_overridable) {
$section_list = $node
->get(OverridesSectionStorage::FIELD_NAME);
$section_list
->appendSection($section);
$node
->save();
}
$this
->drupalGet($node
->toUrl());
$assert_session = $this
->assertSession();
$assert_session
->statusCodeEquals(200);
$assert_session
->pageTextContains('The context value is 512, brought to you by the letter Charlie.');
if ($layout_overridable) {
$page
->clickLink('Layout');
$assert_session
->statusCodeEquals(200);
}
if ($third_party_contexts || $block_configuration['id'] === 'context_block_optional') {
$this
->drupalGet('/admin/structure/types/manage/page/display/full');
$page
->clickLink('Manage layout');
$assert_session
->statusCodeEquals(200);
}
}
public function testNonBundleableEntityType() {
$this->container
->get('entity_display.repository')
->getViewDisplay('user', 'user')
->enableLayoutBuilder()
->save();
$account = $this
->drupalCreateUser([
'administer user display',
'configure any layout',
]);
$this
->drupalLogin($account);
$this
->drupalGet('/admin/config/people/accounts/display/default/layout');
$this
->assertSession()
->statusCodeEquals(200);
}
}