EntityOperationsTest.php in Drupal 9
File
core/modules/content_moderation/tests/src/Kernel/EntityOperationsTest.php
View source
<?php
namespace Drupal\Tests\content_moderation\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
class EntityOperationsTest extends KernelTestBase {
use ContentModerationTestTrait;
protected static $modules = [
'content_moderation',
'node',
'user',
'system',
'workflows',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installSchema('node', 'node_access');
$this
->installEntitySchema('user');
$this
->installEntitySchema('content_moderation_state');
$this
->installConfig('content_moderation');
$this
->createNodeType();
}
protected function createNodeType() {
$node_type = NodeType::create([
'type' => 'page',
'label' => 'Page',
]);
$node_type
->save();
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'page');
$workflow
->save();
}
public function testPendingRevisions() {
$page = Node::create([
'type' => 'page',
'title' => 'A',
]);
$page->moderation_state->value = 'draft';
$page
->save();
$id = $page
->id();
$page = Node::load($id);
$this
->assertEquals('A', $page
->getTitle());
$this
->assertTrue($page
->isDefaultRevision());
$this
->assertFalse($page
->isPublished());
$page
->setTitle('B');
$page->moderation_state->value = 'published';
$page
->save();
$page = Node::load($id);
$this
->assertEquals('B', $page
->getTitle());
$this
->assertTrue($page
->isDefaultRevision());
$this
->assertTrue($page
->isPublished());
$page
->setTitle('C');
$page->moderation_state->value = 'draft';
$page
->save();
$page = Node::load($id);
$this
->assertEquals('B', $page
->getTitle());
$storage = \Drupal::entityTypeManager()
->getStorage('node');
$revision_ids = $storage
->revisionIds($page);
sort($revision_ids);
$latest = end($revision_ids);
$page = $storage
->loadRevision($latest);
$this
->assertEquals('C', $page
->getTitle());
$page
->setTitle('D');
$page->moderation_state->value = 'published';
$page
->save();
$page = Node::load($id);
$this
->assertEquals('D', $page
->getTitle());
$this
->assertTrue($page
->isDefaultRevision());
$this
->assertTrue($page
->isPublished());
$page
->setTitle('E');
$page->moderation_state->value = 'published';
$page
->save();
$page = Node::load($id);
$this
->assertEquals('E', $page
->getTitle());
$this
->assertTrue($page
->isDefaultRevision());
$this
->assertTrue($page
->isPublished());
}
public function testPublishedCreation() {
$page = Node::create([
'type' => 'page',
'title' => 'A',
]);
$page->moderation_state->value = 'published';
$page
->save();
$id = $page
->id();
$page = Node::load($id);
$this
->assertEquals('A', $page
->getTitle());
$this
->assertTrue($page
->isDefaultRevision());
$this
->assertTrue($page
->isPublished());
}
public function testArchive() {
$page = Node::create([
'type' => 'page',
'title' => $this
->randomString(),
]);
$page->moderation_state->value = 'published';
$page
->save();
$id = $page
->id();
$page = Node::load($id);
$this
->assertTrue($page
->isPublished());
$page->moderation_state->value = 'archived';
$page
->save();
$new_revision_id = $page
->getRevisionId();
$storage = \Drupal::entityTypeManager()
->getStorage('node');
$new_revision = $storage
->loadRevision($new_revision_id);
$this
->assertFalse($new_revision
->isPublished());
$this
->assertTrue($new_revision
->isDefaultRevision());
}
}