3 namespace Drupal\Tests\block_content\Functional;
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\Component\Utility\Unicode;
9 * Create a block and test block edit functionality.
11 * @group block_content
13 class PageEditTest extends BlockContentTestBase {
15 protected function setUp() {
18 $this->drupalPlaceBlock('page_title_block');
22 * Checks block edit functionality.
24 public function testPageEdit() {
25 $this->drupalLogin($this->adminUser);
27 $title_key = 'info[0][value]';
28 $body_key = 'body[0][value]';
29 // Create block to edit.
31 $edit['info[0][value]'] = Unicode::strtolower($this->randomMachineName(8));
32 $edit[$body_key] = $this->randomMachineName(16);
33 $this->drupalPostForm('block/add/basic', $edit, t('Save'));
35 // Check that the block exists in the database.
36 $blocks = \Drupal::entityQuery('block_content')->condition('info', $edit['info[0][value]'])->execute();
37 $block = BlockContent::load(reset($blocks));
38 $this->assertTrue($block, 'Custom block found in database.');
40 // Load the edit page.
41 $this->drupalGet('block/' . $block->id());
42 $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
43 $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
45 // Edit the content of the block.
47 $edit[$title_key] = $this->randomMachineName(8);
48 $edit[$body_key] = $this->randomMachineName(16);
49 // Stay on the current page, without reloading.
50 $this->drupalPostForm(NULL, $edit, t('Save'));
52 // Edit the same block, creating a new revision.
53 $this->drupalGet("block/" . $block->id());
55 $edit['info[0][value]'] = $this->randomMachineName(8);
56 $edit[$body_key] = $this->randomMachineName(16);
57 $edit['revision'] = TRUE;
58 $this->drupalPostForm(NULL, $edit, t('Save'));
60 // Ensure that the block revision has been created.
61 \Drupal::entityManager()->getStorage('block_content')->resetCache([$block->id()]);
62 $revised_block = BlockContent::load($block->id());
63 $this->assertNotIdentical($block->getRevisionId(), $revised_block->getRevisionId(), 'A new revision has been created.');
65 // Test deleting the block.
66 $this->drupalGet("block/" . $revised_block->id());
67 $this->clickLink(t('Delete'));
68 $this->assertText(format_string('Are you sure you want to delete the custom block @label?', ['@label' => $revised_block->label()]));