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\Tests\content_moderation\Traits\ContentModerationTestTrait;
11 * @coversDefaultClass \Drupal\content_moderation\EntityOperations
13 * @group content_moderation
15 class EntityOperationsTest extends KernelTestBase {
17 use ContentModerationTestTrait;
22 public static $modules = [
33 protected function 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');
41 $this->createNodeType();
45 * Creates a page node type to test with, ensuring that it's moderated.
47 protected function createNodeType() {
48 $node_type = NodeType::create([
53 $workflow = $this->createEditorialWorkflow();
54 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
59 * Verifies that the process of saving pending revisions works as expected.
61 public function testPendingRevisions() {
62 // Create a new node in draft.
63 $page = Node::create([
67 $page->moderation_state->value = 'draft';
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());
80 // Moderate the entity to published.
82 $page->moderation_state->value = 'published';
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());
91 // Make a new pending revision in Draft.
93 $page->moderation_state->value = 'draft';
96 // Verify normal loads return the still-default previous version.
97 $page = Node::load($id);
98 $this->assertEquals('B', $page->getTitle());
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);
107 $latest = end($revision_ids);
108 $page = $storage->loadRevision($latest);
109 $this->assertEquals('C', $page->getTitle());
111 $page->setTitle('D');
112 $page->moderation_state->value = 'published';
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());
121 // Now check that we can immediately add a new published revision over it.
122 $page->setTitle('E');
123 $page->moderation_state->value = 'published';
126 $page = Node::load($id);
127 $this->assertEquals('E', $page->getTitle());
128 $this->assertTrue($page->isDefaultRevision());
129 $this->assertTrue($page->isPublished());
133 * Verifies that a newly-created node can go straight to published.
135 public function testPublishedCreation() {
136 // Create a new node in draft.
137 $page = Node::create([
141 $page->moderation_state->value = 'published';
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());
155 * Verifies that an unpublished state may be made the default revision.
157 public function testArchive() {
158 $page = Node::create([
160 'title' => $this->randomString(),
163 $page->moderation_state->value = 'published';
168 // The newly-created page should already be published.
169 $page = Node::load($id);
170 $this->assertTrue($page->isPublished());
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';
176 $new_revision_id = $page->getRevisionId();
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());