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