View source
<?php
namespace Drupal\Tests\workbench_moderation\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\workbench_moderation\Entity\ModerationState;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
class EntityOperationsTest extends KernelTestBase {
public static $modules = [
'workbench_moderation',
'node',
'views',
'options',
'user',
'system',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installSchema('node', 'node_access');
$this
->installEntitySchema('user');
$this
->installConfig('workbench_moderation');
$this
->createNodeType();
}
protected function createNodeType() {
$node_type = NodeType::create([
'type' => 'page',
'label' => 'Page',
]);
$node_type
->setThirdPartySetting('workbench_moderation', 'enabled', TRUE);
$node_type
->save();
}
public function testForwardRevisions() {
$page = Node::create([
'type' => 'page',
'title' => 'A',
]);
$page->moderation_state->target_id = '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->target_id = '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->target_id = '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->target_id = '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->target_id = '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->target_id = '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() {
$published_id = $this
->randomMachineName();
$published_state = ModerationState::create([
'id' => $published_id,
'label' => $this
->randomString(),
'published' => TRUE,
'default_revision' => TRUE,
]);
$published_state
->save();
$archived_id = $this
->randomMachineName();
$archived_state = ModerationState::create([
'id' => $archived_id,
'label' => $this
->randomString(),
'published' => FALSE,
'default_revision' => TRUE,
]);
$archived_state
->save();
$page = Node::create([
'type' => 'page',
'title' => $this
->randomString(),
]);
$page->moderation_state->target_id = $published_id;
$page
->save();
$id = $page
->id();
$page = Node::load($id);
$this
->assertTrue($page
->isPublished());
$page->moderation_state->target_id = $archived_id;
$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());
}
}