View source
<?php
namespace Drupal\Tests\entity_usage\FunctionalJavascript;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
use Drupal\Tests\entity_usage\Traits\EntityUsageLastEntityQueryTrait;
use Drupal\user\Entity\Role;
class EntityUsageLayoutBuilderEntityBrowserBlockTest extends EntityUsageJavascriptTestBase {
use ContextualLinkClickTrait;
use EntityUsageLastEntityQueryTrait;
protected $defaultTheme = 'classy';
protected static $modules = [
'block',
'text',
'user',
'layout_builder',
'layout_discovery',
];
public function setUp() {
parent::setUp();
$node_type = NodeType::create([
'type' => 'article',
'name' => 'article',
]);
$node_type
->save();
LayoutBuilderEntityViewDisplay::create([
'targetEntityType' => 'node',
'bundle' => $node_type
->id(),
'mode' => 'default',
'status' => TRUE,
])
->enableLayoutBuilder()
->setOverridable()
->save();
$this
->config('entity_usage.settings')
->set('local_task_enabled_entity_types', [
'node',
])
->set('track_enabled_source_entity_types', [
'node',
])
->set('track_enabled_target_entity_types', [
'node',
])
->set('track_enabled_plugins', [
'layout_builder',
'entity_reference',
])
->save();
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('page_title_block');
$router_builder = \Drupal::service('router.builder');
$router_builder
->rebuild();
}
public function testLayoutBuilderEntityBrowserBlockUsage() {
$session = $this
->getSession();
$page = $session
->getPage();
$assert_session = $this
->assertSession();
\Drupal::service('module_installer')
->install([
'entity_browser_block',
'entity_browser_entity_form',
], TRUE);
$widget_uuid = \Drupal::service('uuid')
->generate();
$browser = \Drupal::entityTypeManager()
->getStorage('entity_browser')
->create([
'name' => 'eu_test_browser',
'label' => 'Entity Usage - Test Entity Browser',
'display' => 'modal',
'display_configuration' => [
'width' => '',
'height' => '',
'link_text' => 'Select new TEB',
'auto_open' => FALSE,
],
'selection_display' => 'no_display',
'selection_display_configuration' => [],
'widget_selector' => 'tabs',
'widget_selector_configuration' => [],
'widgets' => [
$widget_uuid => [
'id' => 'entity_form',
'label' => 'Create new',
'weight' => 0,
'uuid' => $widget_uuid,
'settings' => [
'entity_type' => 'node',
'bundle' => 'article',
'form_mode' => 'default',
'submit_text' => 'Save node',
],
],
],
]);
$browser
->save();
$router_builder = \Drupal::service('router.builder');
$router_builder
->rebuild();
$host_node = Node::create([
'title' => 'Host node 1',
'type' => 'article',
]);
$host_node
->save();
$role = Role::load('authenticated');
$this
->grantPermissions($role, [
'configure all article node layout overrides',
'access eu_test_browser entity browser pages',
'access entity usage statistics',
'access contextual links',
]);
$this
->drupalGet("/node/{$host_node->id()}/layout");
$page
->clickLink('Add block in Section 1, Content region');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '#drupal-off-canvas'));
$assert_session
->assertWaitOnAjaxRequest();
$this
->saveHtmlOutput();
$ebb_link = $assert_session
->elementExists('css', '#drupal-off-canvas a:contains("Entity Usage - Test Entity Browser")');
$ebb_link
->click();
$ebb_button = $assert_session
->waitForElementVisible('css', '#drupal-off-canvas input[value="Select new TEB"]');
$ebb_button
->press();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', 'iframe[name="entity_browser_iframe_eu_test_browser"]'));
$session
->wait(5000);
$this
->getSession()
->switchToIFrame('entity_browser_iframe_eu_test_browser');
$page
->fillField('Title', 'First target node');
$page
->pressButton('Save node');
$assert_session
->assertWaitOnAjaxRequest();
$this
->saveHtmlOutput();
$assert_session
->elementTextContains('css', '#drupal-off-canvas table', 'First target node');
$add_block_button = $assert_session
->elementExists('css', '#drupal-off-canvas input[value="Add block"]');
$add_block_button
->press();
$assert_session
->assertNoElementAfterWait('css', '#drupal-off-canvas', 5000);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('You have unsaved changes');
$block_selector = '.layout-builder__layout .layout-builder__region .layout-builder-block.block-entity-browser-blockeu-test-browser';
$block = $assert_session
->elementExists('css', $block_selector);
$rendered_node = $assert_session
->elementExists('css', 'article.node', $block);
$this
->assertStringContainsString('First target node', $rendered_node
->getText());
$page
->pressButton('Save layout');
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('The layout override has been saved');
$block_selector = '.node__content .layout__region--content .block-entity-browser-blockeu-test-browser';
$block = $assert_session
->elementExists('css', $block_selector);
$rendered_node = $assert_session
->elementExists('css', 'article.node', $block);
$this
->assertStringContainsStringIgnoringCase('First target node', $rendered_node
->getText());
$first_target_node = $this
->getLastEntityOfType('node', TRUE);
$page
->clickLink('First target node');
$this
->saveHtmlOutput();
$page
->clickLink('Usage');
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('Entity usage information for First target node');
$first_row_title_link = $assert_session
->elementExists('xpath', '//table/tbody/tr[1]/td[1]/a');
$this
->assertEquals($host_node
->getTitle(), $first_row_title_link
->getText());
$this
->assertStringContainsString($host_node
->toUrl()
->toString(), $first_row_title_link
->getAttribute('href'));
$first_row_field_label = $this
->xpath('//table/tbody/tr[1]/td[4]')[0];
$this
->assertEquals('Layout', $first_row_field_label
->getText());
$assert_session
->pageTextNotContains('Translations or previous revisions');
$page
->clickLink($host_node
->getTitle());
$page
->clickLink('Layout');
$this
->saveHtmlOutput();
$page
->clickLink('Add block in Section 1, Content region');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '#drupal-off-canvas'));
$assert_session
->assertWaitOnAjaxRequest();
$this
->saveHtmlOutput();
$ebb_link = $assert_session
->elementExists('css', '#drupal-off-canvas a:contains("Entity Usage - Test Entity Browser")');
$ebb_link
->click();
$ebb_button = $assert_session
->waitForElementVisible('css', '#drupal-off-canvas input[value="Select new TEB"]');
$ebb_button
->press();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', 'iframe[name="entity_browser_iframe_eu_test_browser"]'));
$session
->wait(5000);
$this
->getSession()
->switchToIFrame('entity_browser_iframe_eu_test_browser');
$page
->fillField('Title', 'Second target node');
$page
->pressButton('Save node');
$assert_session
->assertWaitOnAjaxRequest();
$this
->saveHtmlOutput();
$assert_session
->elementTextContains('css', '#drupal-off-canvas table', 'Second target node');
$add_block_button = $assert_session
->elementExists('css', '#drupal-off-canvas input[value="Add block"]');
$add_block_button
->press();
$assert_session
->assertNoElementAfterWait('css', '#drupal-off-canvas', 5000);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('You have unsaved changes');
$blocks = $page
->findAll('css', '.layout-builder__layout .layout-builder__region .layout-builder-block.block-entity-browser-blockeu-test-browser');
$rendered_node = $assert_session
->elementExists('css', 'article.node', $blocks[1]);
$this
->assertStringContainsString('Second target node', $rendered_node
->getText());
$page
->pressButton('Save layout');
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('The layout override has been saved');
$blocks = $page
->findAll('css', '.node__content .layout__region--content .block-entity-browser-blockeu-test-browser');
$rendered_node = $assert_session
->elementExists('css', 'article.node', $blocks[1]);
$this
->assertStringContainsString('Second target node', $rendered_node
->getText());
$page
->clickLink('Second target node');
$page
->clickLink('Usage');
$assert_session
->pageTextContains('Entity usage information for Second target node');
$first_row_title_link = $assert_session
->elementExists('xpath', '//table/tbody/tr[1]/td[1]/a');
$this
->assertEquals($host_node
->getTitle(), $first_row_title_link
->getText());
$this
->assertStringContainsString($host_node
->toUrl()
->toString(), $first_row_title_link
->getAttribute('href'));
$first_row_field_label = $this
->xpath('//table/tbody/tr[1]/td[4]')[0];
$this
->assertEquals('Layout', $first_row_field_label
->getText());
$page
->clickLink($host_node
->getTitle());
$page
->clickLink('First target node');
$page
->clickLink('Usage');
$assert_session
->pageTextContains('Entity usage information for First target node');
$first_row_title_link = $assert_session
->elementExists('xpath', '//table/tbody/tr[1]/td[1]/a');
$this
->assertEquals($host_node
->getTitle(), $first_row_title_link
->getText());
$this
->assertStringContainsString($host_node
->toUrl()
->toString(), $first_row_title_link
->getAttribute('href'));
$first_row_field_label = $this
->xpath('//table/tbody/tr[1]/td[4]')[0];
$this
->assertEquals('Layout', $first_row_field_label
->getText());
\Drupal::service('module_installer')
->install([
'contextual',
], TRUE);
$this
->drupalGet("/node/{$host_node->id()}/layout");
$blocks = $page
->findAll('css', '.layout-builder__layout .layout-builder__region .layout-builder-block.block-entity-browser-blockeu-test-browser');
$first_block = $blocks[0];
$contextual_wrapper = $assert_session
->elementExists('css', '.contextual', $first_block);
$contextual_id = $contextual_wrapper
->getAttribute('data-contextual-id');
$this
->clickContextualLink('div[data-contextual-id="' . $contextual_id . '"]', 'Remove block');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '#drupal-off-canvas'));
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('Are you sure you want to remove');
$remove_button = $assert_session
->elementExists('css', '#drupal-off-canvas input[value="Remove"]');
$remove_button
->press();
$assert_session
->assertNoElementAfterWait('css', '#drupal-off-canvas', 5000);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('You have unsaved changes');
$page
->pressButton('Save layout');
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('The layout override has been saved');
$this
->drupalGet("/node/{$first_target_node->id()}/usage");
$assert_session
->pageTextContains('Entity usage information for First target node');
$first_row_title_link = $assert_session
->elementExists('xpath', '//table/tbody/tr[1]/td[1]/a');
$this
->assertEquals($host_node
->getTitle(), $first_row_title_link
->getText());
$this
->assertStringContainsString($host_node
->toUrl()
->toString(), $first_row_title_link
->getAttribute('href'));
$first_row_field_label = $this
->xpath('//table/tbody/tr[1]/td[4]')[0];
$this
->assertEquals('Layout', $first_row_field_label
->getText());
$first_row_used_in = $this
->xpath('//table/tbody/tr[1]/td[6]')[0];
$this
->assertEquals('Translations or previous revisions', $first_row_used_in
->getText());
}
}