function PageContextTest::testBuildContentModerationWithPendingActive

Tests the content moderation build when is active with a pending entity.

File

core/modules/navigation/tests/src/Unit/PageContextTest.php, line 212

Class

PageContextTest
Tests the PageContext Top Bar item build output.

Namespace

Drupal\Tests\navigation\Unit

Code

public function testBuildContentModerationWithPendingActive() : void {
  // Entity current state is 'draft', active is 'published'.
  $entity = $this->prophesize(ContentEntityInterface::class);
  $entity->get('moderation_state')
    ->willReturn((object) [
    'value' => 'draft',
  ]);
  $entity->isDefaultRevision()
    ->willReturn(TRUE);
  $entity->getEntityTypeId()
    ->willReturn('node');
  $entity->id()
    ->willReturn('1');
  $entity->label()
    ->willReturn('Example Title');
  $active = $this->prophesize(ContentEntityInterface::class);
  $active->get('moderation_state')
    ->willReturn((object) [
    'value' => 'published',
  ]);
  // State objects and labels.
  $draft_state = $this->prophesize(StateInterface::class);
  $draft_state->label()
    ->willReturn('Draft');
  $published_state = $this->prophesize(StateInterface::class);
  $published_state->label()
    ->willReturn('Published');
  $type = $this->prophesize(WorkflowTypeInterface::class);
  $type->getState('draft')
    ->willReturn($draft_state->reveal());
  $type->getState('published')
    ->willReturn($published_state->reveal());
  $workflow = $this->prophesize(WorkflowInterface::class);
  $workflow->getTypePlugin()
    ->willReturn($type->reveal());
  $moderation = $this->prophesize(ModerationInformationInterface::class);
  $moderation->isModeratedEntity($entity->reveal())
    ->willReturn(TRUE);
  $moderation->getWorkflowForEntity($entity->reveal())
    ->willReturn($workflow->reveal());
  $moderation->getWorkflowForEntity($active->reveal())
    ->willReturn($workflow->reveal());
  $moderation->hasPendingRevision($entity->reveal())
    ->willReturn(TRUE);
  $entity_repository = $this->prophesize(EntityRepositoryInterface::class);
  $entity_repository->getActive('node', '1')
    ->willReturn($active->reveal());
  $route_helper = $this->prophesize(EntityRouteHelper::class);
  $route_helper->getContentEntityFromRoute()
    ->willReturn($entity->reveal());
  $plugin = new PageContext([], 'page_context', [], $route_helper->reveal(), $entity_repository->reveal(), $moderation->reveal());
  $build = $plugin->build();
  $this->assertSame('Example Title', $build[0]['#slots']['content']);
  $this->assertSame('navigation:badge', $build[1]['#component']);
  $this->assertSame('Draft (Published available)', $build[1]['#slots']['label']);
  // Status still defaults to info as entity might not be EntityPublishedInterface here.
  $this->assertSame('info', $build[1]['#props']['status']);
}

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