3 namespace Drupal\Tests\block_content\Functional;
5 use Drupal\block_content\Entity\BlockContent;
8 * Create a block and test block edit functionality.
10 * @group block_content
12 class PageEditTest extends BlockContentTestBase {
14 protected function setUp() {
17 $this->drupalPlaceBlock('page_title_block');
21 * Checks block edit functionality.
23 public function testPageEdit() {
24 $this->drupalLogin($this->adminUser);
26 $title_key = 'info[0][value]';
27 $body_key = 'body[0][value]';
28 // Create block to edit.
30 $edit['info[0][value]'] = mb_strtolower($this->randomMachineName(8));
31 $edit[$body_key] = $this->randomMachineName(16);
32 $this->drupalPostForm('block/add/basic', $edit, t('Save'));
34 // Check that the block exists in the database.
35 $blocks = \Drupal::entityQuery('block_content')->condition('info', $edit['info[0][value]'])->execute();
36 $block = BlockContent::load(reset($blocks));
37 $this->assertTrue($block, 'Custom block found in database.');
39 // Load the edit page.
40 $this->drupalGet('block/' . $block->id());
41 $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
42 $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
44 // Edit the content of the block.
46 $edit[$title_key] = $this->randomMachineName(8);
47 $edit[$body_key] = $this->randomMachineName(16);
48 // Stay on the current page, without reloading.
49 $this->drupalPostForm(NULL, $edit, t('Save'));
51 // Edit the same block, creating a new revision.
52 $this->drupalGet("block/" . $block->id());
54 $edit['info[0][value]'] = $this->randomMachineName(8);
55 $edit[$body_key] = $this->randomMachineName(16);
56 $edit['revision'] = TRUE;
57 $this->drupalPostForm(NULL, $edit, t('Save'));
59 // Ensure that the block revision has been created.
60 \Drupal::entityManager()->getStorage('block_content')->resetCache([$block->id()]);
61 $revised_block = BlockContent::load($block->id());
62 $this->assertNotIdentical($block->getRevisionId(), $revised_block->getRevisionId(), 'A new revision has been created.');
64 // Test deleting the block.
65 $this->drupalGet("block/" . $revised_block->id());
66 $this->clickLink(t('Delete'));
67 $this->assertText(format_string('Are you sure you want to delete the custom block @label?', ['@label' => $revised_block->label()]));