Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / block_content / tests / src / Functional / PageEditTest.php
1 <?php
2
3 namespace Drupal\Tests\block_content\Functional;
4
5 use Drupal\block_content\Entity\BlockContent;
6
7 /**
8  * Create a block and test block edit functionality.
9  *
10  * @group block_content
11  */
12 class PageEditTest extends BlockContentTestBase {
13
14   protected function setUp() {
15     parent::setUp();
16
17     $this->drupalPlaceBlock('page_title_block');
18   }
19
20   /**
21    * Checks block edit functionality.
22    */
23   public function testPageEdit() {
24     $this->drupalLogin($this->adminUser);
25
26     $title_key = 'info[0][value]';
27     $body_key = 'body[0][value]';
28     // Create block to edit.
29     $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'));
33
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.');
38
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.');
43
44     // Edit the content of the block.
45     $edit = [];
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'));
50
51     // Edit the same block, creating a new revision.
52     $this->drupalGet("block/" . $block->id());
53     $edit = [];
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'));
58
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.');
63
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()]));
68   }
69
70 }