ContentModerationAccessTest.php 2.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php

namespace Drupal\Tests\content_moderation\Kernel;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Session\UserSession;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Entity\Role;

/**
 * Tests content moderation access.
 *
 * @group content_moderation
 */
class ContentModerationAccessTest extends KernelTestBase {

  use NodeCreationTrait;
  use UserCreationTrait;
  use ContentModerationTestTrait;

  /**
   * {@inheritdoc}
   */
28
  protected static $modules = [
29
30
31
32
33
34
35
36
37
38
39
    'content_moderation',
    'filter',
    'node',
    'system',
    'user',
    'workflows',
  ];

  /**
   * {@inheritdoc}
   */
40
  protected function setUp(): void {
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    parent::setUp();

    $this->installEntitySchema('content_moderation_state');
    $this->installEntitySchema('node');
    $this->installEntitySchema('user');
    $this->installConfig(['content_moderation', 'filter']);
    $this->installSchema('system', ['sequences']);
    $this->installSchema('node', ['node_access']);

    // Add a moderated node type.
    $node_type = NodeType::create([
      'type' => 'page',
      'label' => 'Page',
    ]);
    $node_type->save();
    $workflow = $this->createEditorialWorkflow();
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
    $workflow->save();
  }

  /**
   * Tests access cacheability.
   */
64
  public function testAccessCacheability() {
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    $node = $this->createNode(['type' => 'page']);

    /** @var \Drupal\user\RoleInterface $authenticated */
    $authenticated = Role::create([
      'id' => 'authenticated',
    ]);
    $authenticated->grantPermission('access content');
    $authenticated->grantPermission('edit any page content');
    $authenticated->save();

    $account = new UserSession([
      'uid' => 2,
      'roles' => ['authenticated'],
    ]);

    $result = $node->access('update', $account, TRUE);
    $this->assertFalse($result->isAllowed());
    $this->assertEquals(['user.permissions'], $result->getCacheContexts());
    $this->assertEquals(['config:workflows.workflow.editorial', 'node:' . $node->id()], $result->getCacheTags());
    $this->assertEquals(CacheBackendInterface::CACHE_PERMANENT, $result->getCacheMaxAge());

    $authenticated->grantPermission('use editorial transition create_new_draft');
    $authenticated->save();

    \Drupal::entityTypeManager()->getAccessControlHandler('node')->resetCache();
    $result = $node->access('update', $account, TRUE);
    $this->assertTrue($result->isAllowed());
    $this->assertEquals(['user.permissions'], $result->getCacheContexts());
    $this->assertEquals(['config:workflows.workflow.editorial', 'node:' . $node->id()], $result->getCacheTags());
    $this->assertEquals(CacheBackendInterface::CACHE_PERMANENT, $result->getCacheMaxAge());
  }

}