Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / EntityOperationsTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\node\Entity\Node;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\workflows\Entity\Workflow;
9
10 /**
11  * @coversDefaultClass \Drupal\content_moderation\EntityOperations
12  *
13  * @group content_moderation
14  */
15 class EntityOperationsTest extends KernelTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = [
21     'content_moderation',
22     'node',
23     'user',
24     'system',
25     'workflows',
26   ];
27
28   /**
29    * {@inheritdoc}
30    */
31   protected function setUp() {
32     parent::setUp();
33     $this->installEntitySchema('node');
34     $this->installSchema('node', 'node_access');
35     $this->installEntitySchema('user');
36     $this->installEntitySchema('content_moderation_state');
37     $this->installConfig('content_moderation');
38
39     $this->createNodeType();
40   }
41
42   /**
43    * Creates a page node type to test with, ensuring that it's moderated.
44    */
45   protected function createNodeType() {
46     $node_type = NodeType::create([
47       'type' => 'page',
48       'label' => 'Page',
49     ]);
50     $node_type->save();
51     $workflow = Workflow::load('editorial');
52     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
53     $workflow->save();
54   }
55
56   /**
57    * Verifies that the process of saving pending revisions works as expected.
58    */
59   public function testPendingRevisions() {
60     // Create a new node in draft.
61     $page = Node::create([
62       'type' => 'page',
63       'title' => 'A',
64     ]);
65     $page->moderation_state->value = 'draft';
66     $page->save();
67
68     $id = $page->id();
69
70     // Verify the entity saved correctly, and that the presence of pending
71     // revisions doesn't affect the default node load.
72     /** @var \Drupal\node\Entity\Node $page */
73     $page = Node::load($id);
74     $this->assertEquals('A', $page->getTitle());
75     $this->assertTrue($page->isDefaultRevision());
76     $this->assertFalse($page->isPublished());
77
78     // Moderate the entity to published.
79     $page->setTitle('B');
80     $page->moderation_state->value = 'published';
81     $page->save();
82
83     // Verify the entity is now published and public.
84     $page = Node::load($id);
85     $this->assertEquals('B', $page->getTitle());
86     $this->assertTrue($page->isDefaultRevision());
87     $this->assertTrue($page->isPublished());
88
89     // Make a new pending revision in Draft.
90     $page->setTitle('C');
91     $page->moderation_state->value = 'draft';
92     $page->save();
93
94     // Verify normal loads return the still-default previous version.
95     $page = Node::load($id);
96     $this->assertEquals('B', $page->getTitle());
97
98     // Verify we can load the pending revision, even if the mechanism is kind
99     // of gross. Note: revisionIds() is only available on NodeStorageInterface,
100     // so this won't work for non-nodes. We'd need to use entity queries. This
101     // is a core bug that should get fixed.
102     $storage = \Drupal::entityTypeManager()->getStorage('node');
103     $revision_ids = $storage->revisionIds($page);
104     sort($revision_ids);
105     $latest = end($revision_ids);
106     $page = $storage->loadRevision($latest);
107     $this->assertEquals('C', $page->getTitle());
108
109     $page->setTitle('D');
110     $page->moderation_state->value = 'published';
111     $page->save();
112
113     // Verify normal loads return the still-default previous version.
114     $page = Node::load($id);
115     $this->assertEquals('D', $page->getTitle());
116     $this->assertTrue($page->isDefaultRevision());
117     $this->assertTrue($page->isPublished());
118
119     // Now check that we can immediately add a new published revision over it.
120     $page->setTitle('E');
121     $page->moderation_state->value = 'published';
122     $page->save();
123
124     $page = Node::load($id);
125     $this->assertEquals('E', $page->getTitle());
126     $this->assertTrue($page->isDefaultRevision());
127     $this->assertTrue($page->isPublished());
128   }
129
130   /**
131    * Verifies that a newly-created node can go straight to published.
132    */
133   public function testPublishedCreation() {
134     // Create a new node in draft.
135     $page = Node::create([
136       'type' => 'page',
137       'title' => 'A',
138     ]);
139     $page->moderation_state->value = 'published';
140     $page->save();
141
142     $id = $page->id();
143
144     // Verify the entity saved correctly.
145     /** @var \Drupal\node\Entity\Node $page */
146     $page = Node::load($id);
147     $this->assertEquals('A', $page->getTitle());
148     $this->assertTrue($page->isDefaultRevision());
149     $this->assertTrue($page->isPublished());
150   }
151
152   /**
153    * Verifies that an unpublished state may be made the default revision.
154    */
155   public function testArchive() {
156     $page = Node::create([
157       'type' => 'page',
158       'title' => $this->randomString(),
159     ]);
160
161     $page->moderation_state->value = 'published';
162     $page->save();
163
164     $id = $page->id();
165
166     // The newly-created page should already be published.
167     $page = Node::load($id);
168     $this->assertTrue($page->isPublished());
169
170     // When the page is moderated to the archived state, then the latest
171     // revision should be the default revision, and it should be unpublished.
172     $page->moderation_state->value = 'archived';
173     $page->save();
174     $new_revision_id = $page->getRevisionId();
175
176     $storage = \Drupal::entityTypeManager()->getStorage('node');
177     $new_revision = $storage->loadRevision($new_revision_id);
178     $this->assertFalse($new_revision->isPublished());
179     $this->assertTrue($new_revision->isDefaultRevision());
180   }
181
182 }