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\Tests\content_moderation\Traits\ContentModerationTestTrait;
11 * Tests the view access control handler for moderation state entities.
13 * @group content_moderation
15 class ModerationStateAccessTest extends BrowserTestBase {
17 use ContentModerationTestTrait;
22 public static $modules = [
23 'content_moderation_test_views',
28 * Test the view operation access handler with the view permission.
30 public function testViewShowsCorrectStates() {
31 $node_type_id = 'test';
32 $this->createNodeType('Test', $node_type_id);
38 $editor1 = $this->drupalCreateUser($permissions);
39 $this->drupalLogin($editor1);
41 $node_1 = Node::create([
42 'type' => $node_type_id,
43 'title' => 'Draft node',
44 'uid' => $editor1->id(),
46 $node_1->moderation_state->value = 'draft';
49 $node_2 = Node::create([
50 'type' => $node_type_id,
51 'title' => 'Published node',
52 'uid' => $editor1->id(),
54 $node_2->moderation_state->value = 'published';
57 // Resave the node with a new state.
58 $node_2->setTitle('Archived node');
59 $node_2->moderation_state->value = 'archived';
62 // Now show the View, and confirm that the state labels are showing.
63 $this->drupalGet('/latest');
64 $page = $this->getSession()->getPage();
65 $this->assertTrue($page->hasContent('Draft'));
66 $this->assertTrue($page->hasContent('Archived'));
67 $this->assertFalse($page->hasContent('Published'));
69 // Now log in as an admin and test the same thing.
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.
93 * @return \Drupal\node\Entity\NodeType
94 * The node type just created.
96 protected function createNodeType($label, $machine_name) {
97 /** @var \Drupal\node\Entity\NodeType $node_type */
98 $node_type = NodeType::create([
99 'type' => $machine_name,
104 $workflow = $this->createEditorialWorkflow();
105 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', $machine_name);