View source
<?php
namespace Drupal\Tests\acquia_contenthub\Unit\Normalizer;
use Drupal\acquia_contenthub\Normalizer\ContentEntityViewModesExtractor;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\HtmlResponse;
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
require_once __DIR__ . '/../Polyfill/Drupal.php';
class ContentEntityViewModesExtractorTest extends UnitTestCase {
protected $currentUser;
protected $entityDisplayRepository;
protected $entityTypeManager;
protected $entityTypeRepository;
protected $entityConfigStorage;
protected $entityTypeConfig;
protected $renderer;
protected $kernel;
protected $accountSwitcher;
protected $contentEntity;
protected $contentHubSubscription;
protected $configFactory;
protected $blockManager;
protected $settings;
protected $contentEntityViewModesExtractor;
protected $requestStack;
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();
}
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);
}
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');
$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');
$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;
});
$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);
}
}