Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / block_content / tests / src / Functional / BlockContentRevisionsTest.php
1 <?php
2
3 namespace Drupal\Tests\block_content\Functional;
4
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\user\Entity\User;
7 use Drupal\user\UserInterface;
8
9 /**
10  * Create a block with revisions.
11  *
12  * @group block_content
13  */
14 class BlockContentRevisionsTest extends BlockContentTestBase {
15
16   /**
17    * Stores blocks created during the test.
18    * @var array
19    */
20   protected $blocks;
21
22   /**
23    * Stores log messages used during the test.
24    * @var array
25    */
26   protected $revisionLogs;
27
28   /**
29    * Sets the test up.
30    */
31   protected function setUp() {
32     parent::setUp();
33
34     /** @var UserInterface $user */
35     $user = User::load(1);
36
37     // Create initial block.
38     $block = $this->createBlockContent('initial');
39
40     $blocks = [];
41     $logs = [];
42
43     // Get original block.
44     $blocks[] = $block->getRevisionId();
45     $logs[] = '';
46
47     // Create three revisions.
48     $revision_count = 3;
49     for ($i = 0; $i < $revision_count; $i++) {
50       $block->setNewRevision(TRUE);
51       $block->setRevisionLogMessage($this->randomMachineName(32));
52       $block->setRevisionUser($this->adminUser);
53       $block->setRevisionCreationTime(REQUEST_TIME);
54       $logs[] = $block->getRevisionLogMessage();
55       $block->save();
56       $blocks[] = $block->getRevisionId();
57     }
58
59     $this->blocks = $blocks;
60     $this->revisionLogs = $logs;
61   }
62
63   /**
64    * Checks block revision related operations.
65    */
66   public function testRevisions() {
67     $blocks = $this->blocks;
68     $logs = $this->revisionLogs;
69
70     foreach ($blocks as $delta => $revision_id) {
71       // Confirm the correct revision text appears.
72       /** @var \Drupal\block_content\BlockContentInterface  $loaded */
73       $loaded = $this->container->get('entity_type.manager')
74         ->getStorage('block_content')
75         ->loadRevision($revision_id);
76       // Verify revision log is the same.
77       $this->assertEqual($loaded->getRevisionLogMessage(), $logs[$delta], format_string('Correct log message found for revision @revision', [
78         '@revision' => $loaded->getRevisionId(),
79       ]));
80       if ($delta > 0) {
81         $this->assertTrue($loaded->getRevisionUser() instanceof UserInterface, 'Revision User found.');
82         $this->assertTrue(is_numeric($loaded->getRevisionUserId()), 'Revision User ID found.');
83         $this->assertTrue(is_numeric($loaded->getRevisionCreationTime()), 'Revision time found.');
84       }
85     }
86
87     // Confirm that this is the default revision.
88     $this->assertTrue($loaded->isDefaultRevision(), 'Third block revision is the default one.');
89
90     // Make a new revision and set it to not be default.
91     // This will create a new revision that is not "front facing".
92     // Save this as a non-default revision.
93     $loaded->setNewRevision();
94     $loaded->isDefaultRevision(FALSE);
95     $loaded->body = $this->randomMachineName(8);
96     $loaded->save();
97
98     $this->drupalGet('block/' . $loaded->id());
99     $this->assertNoText($loaded->body->value, 'Revision body text is not present on default version of block.');
100
101     // Verify that the non-default revision id is greater than the default
102     // revision id.
103     $default_revision = BlockContent::load($loaded->id());
104     $this->assertTrue($loaded->getRevisionId() > $default_revision->getRevisionId(), 'Revision id is greater than default revision id.');
105   }
106
107 }