3 namespace Drupal\Tests\content_moderation\Functional;
5 use Drupal\node\Entity\Node;
6 use Drupal\node\Entity\NodeType;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\workflows\Entity\Workflow;
11 * Tests the view access control handler for moderation state entities.
13 * @group content_moderation
15 class ModerationStateAccessTest extends BrowserTestBase {
20 public static $modules = [
21 'content_moderation_test_views',
26 * Test the view operation access handler with the view permission.
28 public function testViewShowsCorrectStates() {
29 $node_type_id = 'test';
30 $this->createNodeType('Test', $node_type_id);
35 'view content moderation',
37 $editor1 = $this->drupalCreateUser($permissions);
38 $this->drupalLogin($editor1);
40 $node_1 = Node::create([
41 'type' => $node_type_id,
42 'title' => 'Draft node',
43 'uid' => $editor1->id(),
45 $node_1->moderation_state->value = 'draft';
48 $node_2 = Node::create([
49 'type' => $node_type_id,
50 'title' => 'Published node',
51 'uid' => $editor1->id(),
53 $node_2->moderation_state->value = 'published';
56 // Resave the node with a new state.
57 $node_2->setTitle('Archived node');
58 $node_2->moderation_state->value = 'archived';
61 // Now show the View, and confirm that the state labels are showing.
62 $this->drupalGet('/latest');
63 $page = $this->getSession()->getPage();
64 $this->assertTrue($page->hasContent('Draft'));
65 $this->assertTrue($page->hasContent('Archived'));
66 $this->assertFalse($page->hasContent('Published'));
68 // Now log in as an admin and test the same thing.
72 'administer content moderation',
74 $admin1 = $this->drupalCreateUser($permissions);
75 $this->drupalLogin($admin1);
77 $this->drupalGet('/latest');
78 $page = $this->getSession()->getPage();
79 $this->assertEquals(200, $this->getSession()->getStatusCode());
80 $this->assertTrue($page->hasContent('Draft'));
81 $this->assertTrue($page->hasContent('Archived'));
82 $this->assertFalse($page->hasContent('Published'));
86 * Creates a new node type.
88 * @param string $label
89 * The human-readable label of the type to create.
90 * @param string $machine_name
91 * The machine name of the type to create.
94 * The node type just created.
96 protected function createNodeType($label, $machine_name) {
97 /** @var NodeType $node_type */
98 $node_type = NodeType::create([
99 'type' => $machine_name,
104 $workflow = Workflow::load('editorial');
105 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', $machine_name);