NodeAccessTest.php

Same filename in this branch
  1. 8.9.x core/modules/content_moderation/tests/src/Functional/NodeAccessTest.php
  2. 8.9.x core/modules/node/tests/src/Kernel/NodeAccessTest.php
Same filename in other branches
  1. 9 core/modules/content_moderation/tests/src/Kernel/NodeAccessTest.php
  2. 9 core/modules/content_moderation/tests/src/Functional/NodeAccessTest.php
  3. 9 core/modules/node/tests/src/Kernel/NodeAccessTest.php
  4. 10 core/modules/content_moderation/tests/src/Kernel/NodeAccessTest.php
  5. 10 core/modules/content_moderation/tests/src/Functional/NodeAccessTest.php
  6. 10 core/modules/node/tests/src/Kernel/NodeAccessTest.php
  7. 11.x core/modules/content_moderation/tests/src/Kernel/NodeAccessTest.php
  8. 11.x core/modules/content_moderation/tests/src/Functional/NodeAccessTest.php
  9. 11.x core/modules/node/tests/src/Kernel/NodeAccessTest.php

Namespace

Drupal\Tests\content_moderation\Kernel

File

core/modules/content_moderation/tests/src/Kernel/NodeAccessTest.php

View source
<?php

namespace Drupal\Tests\content_moderation\Kernel;

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;

/**
 * Tests with node access enabled.
 *
 * @group content_moderation
 */
class NodeAccessTest extends KernelTestBase {
    use NodeCreationTrait;
    use UserCreationTrait;
    use ContentModerationTestTrait;
    
    /**
     * The moderation information service.
     *
     * @var \Drupal\content_moderation\ModerationInformationInterface
     */
    protected $moderationInformation;
    
    /**
     * {@inheritdoc}
     */
    public static $modules = [
        'content_moderation',
        'filter',
        'node',
        'node_access_test',
        'system',
        'user',
        'workflows',
    ];
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() {
        parent::setUp();
        $this->installEntitySchema('content_moderation_state');
        $this->installEntitySchema('node');
        $this->installEntitySchema('user');
        $this->installEntitySchema('workflow');
        $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();
        $this->moderationInformation = \Drupal::service('content_moderation.moderation_information');
    }
    
    /**
     * @covers \Drupal\content_moderation\ModerationInformation::getDefaultRevisionId
     */
    public function testGetDefaultRevisionId() {
        // Create an admin user.
        $user = $this->createUser([], NULL, TRUE);
        \Drupal::currentUser()->setAccount($user);
        // Create a node.
        $node = $this->createNode([
            'type' => 'page',
        ]);
        $this->assertEquals($node->getRevisionId(), $this->moderationInformation
            ->getDefaultRevisionId('node', $node->id()));
        // Create a non-admin user.
        $user = $this->createUser();
        \Drupal::currentUser()->setAccount($user);
        $this->assertEquals($node->getRevisionId(), $this->moderationInformation
            ->getDefaultRevisionId('node', $node->id()));
    }
    
    /**
     * @covers \Drupal\content_moderation\ModerationInformation::getLatestRevisionId
     * @group legacy
     * @expectedDeprecation Drupal\content_moderation\ModerationInformation::getLatestRevisionId is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use RevisionableStorageInterface::getLatestRevisionId() instead. See https://www.drupal.org/node/3087295
     */
    public function testGetLatestRevisionId() {
        // Create an admin user.
        $user = $this->createUser([], NULL, TRUE);
        \Drupal::currentUser()->setAccount($user);
        // Create a node.
        $node = $this->createNode([
            'type' => 'page',
        ]);
        $this->assertEquals($node->getRevisionId(), $this->moderationInformation
            ->getLatestRevisionId('node', $node->id()));
        // Create a non-admin user.
        $user = $this->createUser();
        \Drupal::currentUser()->setAccount($user);
        $this->assertEquals($node->getRevisionId(), $this->moderationInformation
            ->getLatestRevisionId('node', $node->id()));
    }

}

Classes

Title Deprecated Summary
NodeAccessTest Tests with node access enabled.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.