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