You are here

public function InlineBlockTest::testNoLayoutSave in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php \Drupal\Tests\layout_builder\FunctionalJavascript\InlineBlockTest::testNoLayoutSave()

Tests adding a new entity block and then not saving the layout.

@dataProvider layoutNoSaveProvider

File

core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php, line 110

Class

InlineBlockTest
Tests that the inline block feature works correctly.

Namespace

Drupal\Tests\layout_builder\FunctionalJavascript

Code

public function testNoLayoutSave($operation, $no_save_button_text, $confirm_button_text) {
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'access contextual links',
    'configure any layout',
    'administer node display',
    'create and edit custom blocks',
  ]));
  $assert_session = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();
  $this
    ->assertEmpty($this->blockStorage
    ->loadMultiple(), 'No entity blocks exist');

  // Enable layout builder and overrides.
  $this
    ->drupalPostForm(static::FIELD_UI_PREFIX . '/display/default', [
    'layout[enabled]' => TRUE,
    'layout[allow_custom]' => TRUE,
  ], 'Save');
  $this
    ->drupalGet('node/1/layout');
  $this
    ->addInlineBlockToLayout('Block title', 'The block body');
  $page
    ->pressButton($no_save_button_text);
  if ($confirm_button_text) {
    $page
      ->pressButton($confirm_button_text);
  }
  $this
    ->drupalGet('node/1');
  $this
    ->assertEmpty($this->blockStorage
    ->loadMultiple(), 'No entity blocks were created when layout changes are discarded.');
  $assert_session
    ->pageTextNotContains('The block body');
  $this
    ->drupalGet('node/1/layout');
  $this
    ->addInlineBlockToLayout('Block title', 'The block body');
  $this
    ->assertSaveLayout();
  $this
    ->drupalGet('node/1');
  $assert_session
    ->pageTextContains('The block body');
  $blocks = $this->blockStorage
    ->loadMultiple();
  $this
    ->assertCount(1, $blocks);

  /* @var \Drupal\Core\Entity\ContentEntityBase $block */
  $block = array_pop($blocks);
  $revision_id = $block
    ->getRevisionId();

  // Confirm the block can be edited.
  $this
    ->drupalGet('node/1/layout');
  $this
    ->configureInlineBlock('The block body', 'The block updated body');
  $page
    ->pressButton($no_save_button_text);
  if ($confirm_button_text) {
    $page
      ->pressButton($confirm_button_text);
  }
  $this
    ->drupalGet('node/1');
  $blocks = $this->blockStorage
    ->loadMultiple();

  // When reverting or discarding the update block should not be on the page.
  $assert_session
    ->pageTextNotContains('The block updated body');
  if ($operation === 'discard_changes') {

    // When discarding the original block body should appear.
    $assert_session
      ->pageTextContains('The block body');
    $this
      ->assertCount(1, $blocks);
    $block = array_pop($blocks);
    $this
      ->assertEquals($block
      ->getRevisionId(), $revision_id);
    $this
      ->assertEquals($block
      ->get('body')
      ->getValue()[0]['value'], 'The block body');
  }
  else {

    // The block should not be visible.
    // Blocks are currently only deleted when the parent entity is deleted.
    $assert_session
      ->pageTextNotContains('The block body');
  }
}