FixedBlockAdminTest.php in Fixed Block Content 8
File
tests/src/Functional/FixedBlockAdminTest.php
View source
<?php
namespace Drupal\Tests\fixed_block_content\Functional;
class FixedBlockAdminTest extends FunctionalFixedBlockTestBase {
protected $adminUser;
protected $randomContent;
public function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer blocks',
'access administration pages',
'view the administration theme',
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalPlaceBlock('local_tasks_block', []);
$this
->drupalPlaceBlock('page_title_block', []);
$this->randomContent = $this
->randomMachineName(128);
}
public function testAdmin() {
$this
->doTestExportConfirmFormNoDefaultContent();
$this
->doTestImportConfirmForm();
$this
->doTestExportConfirmFormWithDefaultContent();
$this
->doTestExportConfirmFormWithDefaultContentOverExisting();
$this
->doTestFixedBlockDeleteForm();
}
public function doTestExportConfirmFormNoDefaultContent() {
$this
->drupalGet('admin/structure/block');
$this
->clickLink('Custom block library');
$this
->clickLink('Fixed blocks');
$this
->clickLink('Restore default content');
$this
->assertText('Are you sure you want to restore the Basic fixed to its default content?');
$this
->drupalPostForm(NULL, [], 'Confirm');
$block_content = $this->fixedBlock
->getBlockContent(FALSE);
$this
->assertNotNull($block_content);
$this
->assertEmpty($block_content
->get('body')
->getString());
}
public function doTestImportConfirmForm() {
$block_content = $this->fixedBlock
->getBlockContent();
$block_content
->get('body')
->setValue($this->randomContent);
$block_content
->save();
$this
->drupalGet('admin/structure/block');
$this
->clickLink('Custom block library');
$this
->clickLink('Fixed blocks');
$this
->clickLink('Set contents as default');
$this
->assertText('Are you sure you want to set the Basic fixed current content as the default?');
$this
->drupalPostForm(NULL, [], 'Confirm');
$this->fixedBlock = $this->container
->get('entity_type.manager')
->getStorage('fixed_block_content')
->load('basic_fixed');
if (method_exists($this, 'assertStringContainsString')) {
$this
->assertStringContainsString($this->randomContent, $this->fixedBlock
->get('default_content'));
}
else {
$this
->assertContains($this->randomContent, $this->fixedBlock
->get('default_content'));
}
}
public function doTestExportConfirmFormWithDefaultContent() {
$block_content = $this->fixedBlock
->getBlockContent(FALSE);
$block_content
->delete();
$this
->drupalGet('admin/structure/block/block-content/fixed-block-content/manage/basic_fixed/export');
$this
->assertText('Are you sure you want to restore the Basic fixed to its default content?');
$this
->drupalPostForm(NULL, [], 'Confirm');
$block_content = $this->fixedBlock
->getBlockContent(FALSE);
$this
->assertNotNull($block_content);
$this
->assertEquals($block_content
->get('body')
->getString(), $this->randomContent);
}
public function doTestExportConfirmFormWithDefaultContentOverExisting() {
$block_content = $this->fixedBlock
->getBlockContent(FALSE);
$block_content
->get('body')
->setValue($this
->randomString(128));
$block_content
->save();
$this
->drupalGet('admin/structure/block/block-content/fixed-block-content/manage/basic_fixed/export');
$this
->assertText('Update the existing block content');
$this
->drupalPostForm(NULL, [
'update_existing' => TRUE,
], 'Confirm');
$new_block_content = $this->fixedBlock
->getBlockContent(FALSE);
$this
->assertEquals($block_content
->id(), $new_block_content
->id());
$this
->assertEquals($block_content
->uuid(), $new_block_content
->uuid());
$this
->assertEquals($new_block_content
->get('body')
->getString(), $this->randomContent);
}
public function doTestFixedBlockDeleteForm() {
$block_content_id = $this->fixedBlock
->getBlockContent(FALSE)
->id();
$this
->drupalGet('admin/structure/block/block-content/fixed-block-content');
$this
->clickLink('Delete');
$this
->assertText('Delete the linked custom block as well');
$edit = [
'delete_linked_block' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, 'Delete');
$this
->assertText('The fixed block content Basic fixed has been deleted.');
$this->fixedBlock = $this->container
->get('entity_type.manager')
->getStorage('fixed_block_content')
->load('basic_fixed');
$this
->assertNull($this->fixedBlock);
$block_content = $this->container
->get('entity_type.manager')
->getStorage('block_content')
->loadUnchanged($block_content_id);
$this
->assertNull($block_content);
}
}