ModerationStateAccessTest.php 2.99 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace Drupal\Tests\content_moderation\Functional;

use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
8
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
9 10 11 12 13 14 15 16

/**
 * Tests the view access control handler for moderation state entities.
 *
 * @group content_moderation
 */
class ModerationStateAccessTest extends BrowserTestBase {

17 18
  use ContentModerationTestTrait;

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'content_moderation_test_views',
    'content_moderation',
  ];

  /**
   * Test the view operation access handler with the view permission.
   */
  public function testViewShowsCorrectStates() {
    $node_type_id = 'test';
    $this->createNodeType('Test', $node_type_id);

    $permissions = [
      'access content',
      'view all revisions',
    ];
    $editor1 = $this->drupalCreateUser($permissions);
    $this->drupalLogin($editor1);

    $node_1 = Node::create([
      'type' => $node_type_id,
      'title' => 'Draft node',
      'uid' => $editor1->id(),
    ]);
46
    $node_1->moderation_state->value = 'draft';
47 48 49 50 51 52 53
    $node_1->save();

    $node_2 = Node::create([
      'type' => $node_type_id,
      'title' => 'Published node',
      'uid' => $editor1->id(),
    ]);
54
    $node_2->moderation_state->value = 'published';
55 56 57 58
    $node_2->save();

    // Resave the node with a new state.
    $node_2->setTitle('Archived node');
59
    $node_2->moderation_state->value = 'archived';
60 61 62 63 64
    $node_2->save();

    // Now show the View, and confirm that the state labels are showing.
    $this->drupalGet('/latest');
    $page = $this->getSession()->getPage();
65 66 67
    $this->assertTrue($page->hasContent('Draft'));
    $this->assertTrue($page->hasContent('Archived'));
    $this->assertFalse($page->hasContent('Published'));
68 69 70 71 72 73 74 75 76 77 78 79

    // Now log in as an admin and test the same thing.
    $permissions = [
      'access content',
      'view all revisions',
    ];
    $admin1 = $this->drupalCreateUser($permissions);
    $this->drupalLogin($admin1);

    $this->drupalGet('/latest');
    $page = $this->getSession()->getPage();
    $this->assertEquals(200, $this->getSession()->getStatusCode());
80 81 82
    $this->assertTrue($page->hasContent('Draft'));
    $this->assertTrue($page->hasContent('Archived'));
    $this->assertFalse($page->hasContent('Published'));
83 84 85 86 87 88 89 90 91 92
  }

  /**
   * Creates a new node type.
   *
   * @param string $label
   *   The human-readable label of the type to create.
   * @param string $machine_name
   *   The machine name of the type to create.
   *
93
   * @return \Drupal\node\Entity\NodeType
94 95 96
   *   The node type just created.
   */
  protected function createNodeType($label, $machine_name) {
97
    /** @var \Drupal\node\Entity\NodeType $node_type */
98 99 100 101 102 103
    $node_type = NodeType::create([
      'type' => $machine_name,
      'label' => $label,
    ]);
    $node_type->save();

104
    $workflow = $this->createEditorialWorkflow();
105 106
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', $machine_name);
    $workflow->save();
107 108 109 110
    return $node_type;
  }

}