You are here

class ContentEntityViewModesExtractorTest in Acquia Content Hub 8

PHPUnit test for the ContentEntityViewModesExtractor class.

@coversDefaultClass \Drupal\acquia_contenthub\Normalizer\ContentEntityViewModesExtractor

@group acquia_contenthub

Hierarchy

Expanded class hierarchy of ContentEntityViewModesExtractorTest

File

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

Namespace

Drupal\Tests\acquia_contenthub\Unit\Normalizer
View source
class ContentEntityViewModesExtractorTest extends UnitTestCase {

  /**
   * The current session user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $currentUser;

  /**
   * Entity display repository.
   *
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityDisplayRepository;

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityTypeManager;

  /**
   * Entity type repository.
   *
   * @var \Drupal\Core\Entity\EntityTypeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityTypeRepository;

  /**
   * Entity Config Storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityConfigStorage;

  /**
   * The entity type config.
   *
   * @var \Drupal\acquia_contenthub\ContentHubEntityTypeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityTypeConfig;

  /**
   * Renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $renderer;

  /**
   * The Kernel.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $kernel;

  /**
   * Account Switcher Service.
   *
   * @var \Drupal\Core\Session\AccountSwitcherInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $accountSwitcher;

  /**
   * Content Entity.
   *
   * @var \Drupal\Core\Entity\ContentEntityInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $contentEntity;

  /**
   * Content Hub Subscription.
   *
   * @var \Drupal\acquia_contenthub\ContentHubSubscription|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $contentHubSubscription;

  /**
   * Config Factory.
   *
   * @var \Drupal\Core\Config\ConfigFactory|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configFactory;

  /**
   * The Block Manager.
   *
   * @var \Drupal\Core\Block\BlockManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $blockManager;

  /**
   * Settings.
   *
   * @var \Drupal\Core\Config\Config|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $settings;

  /**
   * Content Entity View Modes Extractor.
   *
   * @var \Drupal\acquia_contenthub\Normalizer\ContentEntityViewModesExtractor
   */
  protected $contentEntityViewModesExtractor;

  /**
   * The Request Stack Service.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    parent::setUp();
    $this->currentUser = $this
      ->createMock('Drupal\\Core\\Session\\AccountProxyInterface');
    $this->entityDisplayRepository = $this
      ->createMock('Drupal\\Core\\Entity\\EntityDisplayRepositoryInterface');
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->entityTypeRepository = $this
      ->createMock('Drupal\\Core\\Entity\\EntityTypeRepositoryInterface');
    $this->entityConfigStorage = $this
      ->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
    $this->entityTypeManager
      ->getStorage('acquia_contenthub_entity_config')
      ->willReturn($this->entityConfigStorage);
    $this->entityTypeConfig = $this
      ->createMock('Drupal\\acquia_contenthub\\ContentHubEntityTypeConfigInterface');
    $this->renderer = $this
      ->createMock('Drupal\\Core\\Render\\RendererInterface');
    $this->kernel = $this
      ->createMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
    $this->accountSwitcher = $this
      ->createMock('Drupal\\Core\\Session\\AccountSwitcherInterface');
    $this->contentEntity = $this
      ->createMock('Drupal\\Core\\Entity\\ContentEntityInterface');
    $this->contentHubSubscription = $this
      ->getMockBuilder('\\Drupal\\acquia_contenthub\\ContentHubSubscription')
      ->disableOriginalConstructor()
      ->getMock();
    $this->configFactory = $this
      ->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
    $this->blockManager = $this
      ->createMock('Drupal\\Core\\Block\\BlockManagerInterface');
    $config = $this
      ->getMockBuilder('Drupal\\Core\\Config\\Config')
      ->disableOriginalConstructor()
      ->getMock();
    $config
      ->expects($this
      ->once())
      ->method('get')
      ->with('user_role')
      ->willReturn(AccountInterface::ANONYMOUS_ROLE);
    $this->configFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with('acquia_contenthub.entity_config')
      ->willReturn($config);
    $this->requestStack = $this
      ->getMockBuilder('Symfony\\Component\\HttpFoundation\\RequestStack')
      ->disableOriginalConstructor()
      ->getMock();
  }

  /**
   * Test the getRenderedViewModes method, configured not to be rendered.
   *
   * @covers ::getRenderedViewModes
   */
  public function testGetRenderedViewModesConfiguredNotToBeRendered() {
    $this->contentEntity
      ->expects($this
      ->once())
      ->method('getEntityTypeId')
      ->willReturn('entity_type_1');
    $this->contentEntity
      ->expects($this
      ->once())
      ->method('bundle')
      ->willReturn('bundle_1');
    $this->contentEntity
      ->expects($this
      ->any())
      ->method('isNew')
      ->willReturn(FALSE);
    $this->entityConfigStorage
      ->expects($this
      ->once())
      ->method('loadMultiple')
      ->with([
      'entity_type_1',
    ])
      ->willReturn([]);
    $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);
    $this
      ->assertNull($rendered_view_modes);
  }

  /**
   * Test the getRenderedViewModes method, has view mode.
   *
   * @covers ::getRenderedViewModes
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentEntityViewModesExtractorTest::$accountSwitcher protected property Account Switcher Service.
ContentEntityViewModesExtractorTest::$blockManager protected property The Block Manager.
ContentEntityViewModesExtractorTest::$configFactory protected property Config Factory.
ContentEntityViewModesExtractorTest::$contentEntity protected property Content Entity.
ContentEntityViewModesExtractorTest::$contentEntityViewModesExtractor protected property Content Entity View Modes Extractor.
ContentEntityViewModesExtractorTest::$contentHubSubscription protected property Content Hub Subscription.
ContentEntityViewModesExtractorTest::$currentUser protected property The current session user.
ContentEntityViewModesExtractorTest::$entityConfigStorage protected property Entity Config Storage.
ContentEntityViewModesExtractorTest::$entityDisplayRepository protected property Entity display repository.
ContentEntityViewModesExtractorTest::$entityTypeConfig protected property The entity type config.
ContentEntityViewModesExtractorTest::$entityTypeManager protected property Entity type manager.
ContentEntityViewModesExtractorTest::$entityTypeRepository protected property Entity type repository.
ContentEntityViewModesExtractorTest::$kernel protected property The Kernel.
ContentEntityViewModesExtractorTest::$renderer protected property Renderer.
ContentEntityViewModesExtractorTest::$requestStack protected property The Request Stack Service.
ContentEntityViewModesExtractorTest::$settings protected property Settings.
ContentEntityViewModesExtractorTest::setUp public function Overrides UnitTestCase::setUp
ContentEntityViewModesExtractorTest::testGetRenderedViewModesConfiguredNotToBeRendered public function Test the getRenderedViewModes method, configured not to be rendered.
ContentEntityViewModesExtractorTest::testGetRenderedViewModesHasViewMode public function Test the getRenderedViewModes method, has view mode.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.