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);
36 $editor1 = $this->drupalCreateUser($permissions);
37 $this->drupalLogin($editor1);
39 $node_1 = Node::create([
40 'type' => $node_type_id,
41 'title' => 'Draft node',
42 'uid' => $editor1->id(),
44 $node_1->moderation_state->value = 'draft';
47 $node_2 = Node::create([
48 'type' => $node_type_id,
49 'title' => 'Published node',
50 'uid' => $editor1->id(),
52 $node_2->moderation_state->value = 'published';
55 // Resave the node with a new state.
56 $node_2->setTitle('Archived node');
57 $node_2->moderation_state->value = 'archived';
60 // Now show the View, and confirm that the state labels are showing.
61 $this->drupalGet('/latest');
62 $page = $this->getSession()->getPage();
63 $this->assertTrue($page->hasContent('Draft'));
64 $this->assertTrue($page->hasContent('Archived'));
65 $this->assertFalse($page->hasContent('Published'));
67 // Now log in as an admin and test the same thing.
72 $admin1 = $this->drupalCreateUser($permissions);
73 $this->drupalLogin($admin1);
75 $this->drupalGet('/latest');
76 $page = $this->getSession()->getPage();
77 $this->assertEquals(200, $this->getSession()->getStatusCode());
78 $this->assertTrue($page->hasContent('Draft'));
79 $this->assertTrue($page->hasContent('Archived'));
80 $this->assertFalse($page->hasContent('Published'));
84 * Creates a new node type.
86 * @param string $label
87 * The human-readable label of the type to create.
88 * @param string $machine_name
89 * The machine name of the type to create.
91 * @return \Drupal\node\Entity\NodeType
92 * The node type just created.
94 protected function createNodeType($label, $machine_name) {
95 /** @var \Drupal\node\Entity\NodeType $node_type */
96 $node_type = NodeType::create([
97 'type' => $machine_name,
102 $workflow = Workflow::load('editorial');
103 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', $machine_name);