3 namespace Drupal\Tests\content_moderation\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\node\Entity\Node;
7 use Drupal\node\Entity\NodeType;
8 use Drupal\workflows\Entity\Workflow;
11 * @coversDefaultClass \Drupal\content_moderation\EntityOperations
13 * @group content_moderation
15 class EntityOperationsTest extends KernelTestBase {
20 public static $modules = [
31 protected function 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');
39 $this->createNodeType();
43 * Creates a page node type to test with, ensuring that it's moderated.
45 protected function createNodeType() {
46 $node_type = NodeType::create([
51 $workflow = Workflow::load('editorial');
52 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
57 * Verifies that the process of saving pending revisions works as expected.
59 public function testPendingRevisions() {
60 // Create a new node in draft.
61 $page = Node::create([
65 $page->moderation_state->value = 'draft';
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());
78 // Moderate the entity to published.
80 $page->moderation_state->value = 'published';
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());
89 // Make a new pending revision in Draft.
91 $page->moderation_state->value = 'draft';
94 // Verify normal loads return the still-default previous version.
95 $page = Node::load($id);
96 $this->assertEquals('B', $page->getTitle());
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);
105 $latest = end($revision_ids);
106 $page = $storage->loadRevision($latest);
107 $this->assertEquals('C', $page->getTitle());
109 $page->setTitle('D');
110 $page->moderation_state->value = 'published';
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());
119 // Now check that we can immediately add a new published revision over it.
120 $page->setTitle('E');
121 $page->moderation_state->value = 'published';
124 $page = Node::load($id);
125 $this->assertEquals('E', $page->getTitle());
126 $this->assertTrue($page->isDefaultRevision());
127 $this->assertTrue($page->isPublished());
131 * Verifies that a newly-created node can go straight to published.
133 public function testPublishedCreation() {
134 // Create a new node in draft.
135 $page = Node::create([
139 $page->moderation_state->value = 'published';
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());
153 * Verifies that an unpublished state may be made the default revision.
155 public function testArchive() {
156 $page = Node::create([
158 'title' => $this->randomString(),
161 $page->moderation_state->value = 'published';
166 // The newly-created page should already be published.
167 $page = Node::load($id);
168 $this->assertTrue($page->isPublished());
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';
174 $new_revision_id = $page->getRevisionId();
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());