class ModeratedNodeResourceTestBase

Same name and namespace in other branches
  1. 11.x core/modules/rest/tests/src/Functional/EntityResource/ModeratedNode/ModeratedNodeResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\ModeratedNode\ModeratedNodeResourceTestBase
  2. 10 core/modules/rest/tests/src/Functional/EntityResource/ModeratedNode/ModeratedNodeResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\ModeratedNode\ModeratedNodeResourceTestBase
  3. 8.9.x core/modules/rest/tests/src/Functional/EntityResource/ModeratedNode/ModeratedNodeResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\ModeratedNode\ModeratedNodeResourceTestBase

Extend the Node resource test base and apply moderation to the entity.

Hierarchy

Expanded class hierarchy of ModeratedNodeResourceTestBase

File

core/modules/rest/tests/src/Functional/EntityResource/ModeratedNode/ModeratedNodeResourceTestBase.php, line 11

Namespace

Drupal\Tests\rest\Functional\EntityResource\ModeratedNode
View source
abstract class ModeratedNodeResourceTestBase extends NodeResourceTestBase {
  use ContentModerationTestTrait;
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'content_moderation',
  ];
  
  /**
   * The test editorial workflow.
   *
   * @var \Drupal\workflows\WorkflowInterface
   */
  protected $workflow;
  
  /**
   * {@inheritdoc}
   */
  protected function setUpAuthorization($method) {
    parent::setUpAuthorization($method);
    switch ($method) {
      case 'POST':
      case 'PATCH':
      case 'DELETE':
        $this->grantPermissionsToTestedRole([
          'use editorial transition publish',
          'use editorial transition create_new_draft',
        ]);
        break;

    }
  }
  
  /**
   * {@inheritdoc}
   */
  protected function createEntity() {
    $entity = parent::createEntity();
    if (!$this->workflow) {
      $this->workflow = $this->createEditorialWorkflow();
    }
    $this->workflow
      ->getTypePlugin()
      ->addEntityTypeAndBundle($entity->getEntityTypeId(), $entity->bundle());
    $this->workflow
      ->save();
    return $entity;
  }
  
  /**
   * {@inheritdoc}
   */
  protected function getExpectedNormalizedEntity() {
    return array_merge(parent::getExpectedNormalizedEntity(), [
      'moderation_state' => [
        [
          'value' => 'published',
        ],
      ],
      'vid' => [
        [
          'value' => (int) $this->entity
            ->getRevisionId(),
        ],
      ],
    ]);
  }

}

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