3 namespace Drupal\Tests\content_moderation\Kernel;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\workflows\Entity\Workflow;
12 * @coversDefaultClass \Drupal\content_moderation\EntityOperations
14 * @group content_moderation
16 class EntityOperationsTest extends KernelTestBase {
21 public static $modules = [
32 protected function 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');
40 $this->createNodeType();
44 * Creates a page node type to test with, ensuring that it's moderated.
46 protected function createNodeType() {
47 $node_type = NodeType::create([
52 $workflow = Workflow::load('editorial');
53 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
58 * Verifies that the process of saving forward-revisions works as expected.
60 public function testForwardRevisions() {
61 // Create a new node in draft.
62 $page = Node::create([
66 $page->moderation_state->value = 'draft';
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());
79 // Moderate the entity to published.
81 $page->moderation_state->value = 'published';
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());
90 // Make a new forward-revision in Draft.
92 $page->moderation_state->value = 'draft';
95 // Verify normal loads return the still-default previous version.
96 $page = Node::load($id);
97 $this->assertEquals('B', $page->getTitle());
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);
106 $latest = end($revision_ids);
107 $page = $storage->loadRevision($latest);
108 $this->assertEquals('C', $page->getTitle());
110 $page->setTitle('D');
111 $page->moderation_state->value = 'published';
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());
120 // Now check that we can immediately add a new published revision over it.
121 $page->setTitle('E');
122 $page->moderation_state->value = 'published';
125 $page = Node::load($id);
126 $this->assertEquals('E', $page->getTitle());
127 $this->assertTrue($page->isDefaultRevision());
128 $this->assertTrue($page->isPublished());
132 * Verifies that a newly-created node can go straight to published.
134 public function testPublishedCreation() {
135 // Create a new node in draft.
136 $page = Node::create([
140 $page->moderation_state->value = 'published';
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());
154 * Verifies that an unpublished state may be made the default revision.
156 public function testArchive() {
157 $page = Node::create([
159 'title' => $this->randomString(),
162 $page->moderation_state->value = 'published';
167 // The newly-created page should already be published.
168 $page = Node::load($id);
169 $this->assertTrue($page->isPublished());
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';
175 $new_revision_id = $page->getRevisionId();
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());