function InlineBlockTest::testNoLayoutSave

Same name in other branches
  1. 9 core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php \Drupal\Tests\layout_builder\FunctionalJavascript\InlineBlockTest::testNoLayoutSave()
  2. 8.9.x core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php \Drupal\Tests\layout_builder\FunctionalJavascript\InlineBlockTest::testNoLayoutSave()
  3. 10 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 118

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) : void {
    $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->drupalGet(static::FIELD_UI_PREFIX . '/display/default');
    $this->submitForm([
        '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('The block body', $block->get('body')
            ->getValue()[0]['value']);
    }
    else {
        // The block should not be visible.
        // Blocks are currently only deleted when the parent entity is deleted.
        $assert_session->pageTextNotContains('The block body');
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.