You are here

public function ContentEntityViewModesExtractorTest::testGetRenderedViewModesHasViewMode in Acquia Content Hub 8

Test the getRenderedViewModes method, has view mode.

@covers ::getRenderedViewModes

File

tests/src/Unit/Normalizer/ContentEntityViewModesExtractorTest.php, line 209

Class

ContentEntityViewModesExtractorTest
PHPUnit test for the ContentEntityViewModesExtractor class.

Namespace

Drupal\Tests\acquia_contenthub\Unit\Normalizer

Code

public function testGetRenderedViewModesHasViewMode() {
  $this->contentEntity
    ->expects($this
    ->any())
    ->method('getEntityTypeId')
    ->willReturn('entity_type_1');
  $this->contentEntity
    ->expects($this
    ->any())
    ->method('bundle')
    ->willReturn('bundle_1');
  $this->contentEntity
    ->expects($this
    ->any())
    ->method('isNew')
    ->willReturn(FALSE);
  $this->entityConfigStorage
    ->expects($this
    ->any(0))
    ->method('loadMultiple')
    ->with([
    'entity_type_1',
  ])
    ->willReturn([
    'entity_type_1' => $this->entityTypeConfig,
  ]);
  $this->entityConfigStorage
    ->expects($this
    ->any(1))
    ->method('loadMultiple')
    ->with([
    'entity_type_1',
  ])
    ->willReturn([
    'entity_type_1' => $this->entityTypeConfig,
  ]);
  $this->entityTypeConfig
    ->expects($this
    ->once())
    ->method('getRenderingViewModes')
    ->with('bundle_1')
    ->willReturn([
    'view_mode_2',
  ]);
  $this->entityDisplayRepository
    ->expects($this
    ->once())
    ->method('getViewModeOptionsByBundle')
    ->with('entity_type_1', 'bundle_1')
    ->willReturn([
    'view_mode_1' => 'view_mode_1 label',
    'view_mode_2' => 'view_mode_2 label',
  ]);
  $this->entityTypeConfig
    ->expects($this
    ->once())
    ->method('getPreviewImageField')
    ->with('bundle_1')
    ->willReturn('field_media->field_image');
  $this->entityTypeConfig
    ->expects($this
    ->once())
    ->method('getPreviewImageStyle')
    ->with('bundle_1')
    ->willReturn('medium');
  $field_media = $this
    ->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityInterface')
    ->disableOriginalConstructor()
    ->getMock();
  $field_image = $this
    ->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityInterface')
    ->disableOriginalConstructor()
    ->getMock();
  $media_entity = $this
    ->createMock('Drupal\\Core\\Entity\\EntityInterface');
  $image_entity = $this
    ->createMock('Drupal\\file\\FileInterface');
  $image_entity
    ->expects($this
    ->once())
    ->method('bundle')
    ->willReturn('file');
  $image_entity
    ->expects($this
    ->once())
    ->method('getFileUri')
    ->willReturn('a_file_uri');
  $this->contentEntity->field_media = $field_media;
  $this->contentEntity->field_media->entity = $media_entity;
  $this->contentEntity->field_media->entity->field_image = $field_image;
  $this->contentEntity->field_media->entity->field_image->entity = $image_entity;
  $entity_storage = $this
    ->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
  $this->entityTypeRepository = $this
    ->createMock('Drupal\\Core\\Entity\\EntityTypeRepositoryInterface');

  // $container = $this->getMock('Drupal\Core\DependencyInjection\Container');
  $image_style = $this
    ->getMockBuilder('Drupal\\image\\Entity\\ImageStyle')
    ->disableOriginalConstructor()
    ->getMock();
  $url_generator = $this
    ->getMockBuilder('Drupal\\Core\\Routing\\UrlGenerator')
    ->disableOriginalConstructor()
    ->getMock();
  $url_generator
    ->expects($this
    ->once())
    ->method('getPathFromRoute')
    ->willReturn('a_generated_url');

  // Setting up the Container.
  $container = new ContainerBuilder();
  $container
    ->set('entity_type.manager', $this->entityTypeManager);
  $container
    ->set('entity_type.repository', $this->entityTypeRepository);
  $container
    ->set('url_generator', $url_generator);
  \Drupal::setContainer($container);
  $image_entity
    ->expects($this
    ->once())
    ->method('bundle')
    ->willReturn('file');
  $image_entity
    ->expects($this
    ->once())
    ->method('getFileUri')
    ->willReturn('a_file_uri');
  $this->entityTypeManager
    ->getStorage('image_style')
    ->willReturn($entity_storage);
  $entity_storage
    ->expects($this
    ->once())
    ->method('load')
    ->with('medium')
    ->willReturn($image_style);
  $image_style
    ->expects($this
    ->once())
    ->method('buildUrl')
    ->with('a_file_uri')
    ->willReturn('a_style_decorated_file_uri');
  $this->contentHubSubscription
    ->expects($this
    ->once())
    ->method('setHmacAuthorization')
    ->willReturnCallback(function (Request $request, $use_shared_secret) {
    $request->headers
      ->set('Authorization', 'Acquia ContentHub:testSignature');
    return $request;
  });

  // Creating response obtained from the internal sub-request. Check that the
  // cookies and server variables are passed to the sub-request.
  $this->kernel
    ->expects($this
    ->once())
    ->method('handle')
    ->willReturnCallback(function (Request $request, $type) {
    $authorization_header = $request->headers
      ->get('Authorization');
    $output = "<html>{$type}|{$authorization_header}</html>";
    $response = new HtmlResponse();
    $response
      ->setContent($output);
    return $response;
  });
  $contentEntityViewModesExtractor = new ContentEntityViewModesExtractor($this->currentUser, $this->entityDisplayRepository, $this->entityTypeManager
    ->reveal(), $this->renderer, $this->kernel, $this->accountSwitcher, $this->contentHubSubscription, $this->configFactory, $this->blockManager, $this->requestStack);
  $rendered_view_modes = $contentEntityViewModesExtractor
    ->getRenderedViewModes($this->contentEntity);
  $expected_rendered_view_modes = [
    'view_mode_2' => [
      'id' => 'view_mode_2',
      'preview_image' => 'file_create_url:a_style_decorated_file_uri',
      'label' => 'view_mode_2 label',
      'url' => '/a_generated_url',
      'html' => '<html>' . HttpKernelInterface::SUB_REQUEST . '|Acquia ContentHub:testSignature</html>',
    ],
  ];
  $this
    ->assertEquals($expected_rendered_view_modes, $rendered_view_modes);
}