Same name in this branch
  1. 10 core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php \Drupal\entity_test\Entity\EntityTest
  2. 10 core/modules/views/tests/src/Unit/Plugin/area/EntityTest.php \Drupal\Tests\views\Unit\Plugin\area\EntityTest
  3. 10 core/modules/views/tests/src/Unit/Plugin/argument_validator/EntityTest.php \Drupal\Tests\views\Unit\Plugin\argument_validator\EntityTest
Same name and namespace in other branches
  1. 8.9.x core/modules/views/tests/src/Unit/Plugin/area/EntityTest.php \Drupal\Tests\views\Unit\Plugin\area\EntityTest
  2. 9 core/modules/views/tests/src/Unit/Plugin/area/EntityTest.php \Drupal\Tests\views\Unit\Plugin\area\EntityTest

@coversDefaultClass \Drupal\views\Plugin\views\area\Entity @group Entity

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, RandomGeneratorTrait, PhpUnitWarnings
    • class \Drupal\Tests\views\Unit\Plugin\area\EntityTest

Expanded class hierarchy of EntityTest

File

core/modules/views/tests/src/Unit/Plugin/area/EntityTest.php, line 18

Namespace

Drupal\Tests\views\Unit\Plugin\area
View source
class EntityTest extends UnitTestCase {

  /**
   * The tested entity area handler.
   *
   * @var \Drupal\views\Plugin\views\area\Entity
   */
  protected $entityHandler;

  /**
   * The mocked entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityTypeManager;

  /**
   * The entity repository.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityRepository;

  /**
   * The entity display repository.
   *
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityDisplayRepository;

  /**
   * The mocked entity storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityStorage;

  /**
   * The mocked entity view builder.
   *
   * @var \Drupal\Core\Entity\EntityViewBuilderInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityViewBuilder;

  /**
   * The mocked view executable.
   *
   * @var \Drupal\views\ViewExecutable|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $executable;

  /**
   * The mocked display.
   *
   * @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $display;

  /**
   * The mocked style plugin.
   *
   * @var \Drupal\views\Plugin\views\style\StylePluginBase|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $stylePlugin;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $this->entityRepository = $this
      ->createMock(EntityRepositoryInterface::class);
    $this->entityDisplayRepository = $this
      ->createMock(EntityDisplayRepositoryInterface::class);
    $this->entityStorage = $this
      ->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
    $this->entityViewBuilder = $this
      ->createMock('Drupal\\Core\\Entity\\EntityViewBuilderInterface');
    $this->executable = $this
      ->getMockBuilder('Drupal\\views\\ViewExecutable')
      ->disableOriginalConstructor()
      ->getMock();
    $this->display = $this
      ->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\DisplayPluginBase')
      ->disableOriginalConstructor()
      ->getMock();
    $this->stylePlugin = $this
      ->getMockBuilder('Drupal\\views\\Plugin\\views\\style\\StylePluginBase')
      ->disableOriginalConstructor()
      ->getMock();
    $this->executable->style_plugin = $this->stylePlugin;
    $this->entityHandler = new Entity([], 'entity', [
      'entity_type' => 'entity_test',
    ], $this->entityTypeManager, $this->entityRepository, $this->entityDisplayRepository);
    $this->display
      ->expects($this
      ->any())
      ->method('getPlugin')
      ->with('style')
      ->willReturn($this->stylePlugin);
    $this->executable
      ->expects($this
      ->any())
      ->method('getStyle')
      ->willReturn($this->stylePlugin);
    $token = $this
      ->getMockBuilder('Drupal\\Core\\Utility\\Token')
      ->disableOriginalConstructor()
      ->getMock();
    $token
      ->expects($this
      ->any())
      ->method('replace')
      ->willReturnArgument(0);
    $container = new ContainerBuilder();
    $container
      ->set('token', $token);
    \Drupal::setContainer($container);
  }

  /**
   * Ensures that the entity type manager returns an entity storage.
   */
  protected function setupEntityTypeManager() {
    $this->entityTypeManager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->with('entity_test')
      ->willReturn($this->entityStorage);
    $this->entityTypeManager
      ->expects($this
      ->any())
      ->method('getViewBuilder')
      ->with('entity_test')
      ->willReturn($this->entityViewBuilder);
  }

  /**
   * Data provider for testing different types of tokens.
   *
   * @return array
   */
  public static function providerTestTokens() {
    return [
      [
        '{{ raw_arguments.test1 }}',
        5,
      ],
      [
        '{{ arguments.test2 }}',
        6,
      ],
      [
        '{{ test_render_token }}',
        7,
      ],
      [
        '{{ test:global_token }}',
        8,
      ],
    ];
  }

  /**
   * @covers ::render
   * @covers ::defineOptions
   * @covers ::init
   */
  public function testRenderWithId() {
    $this
      ->setupEntityTypeManager();
    $options = [
      'target' => 1,
      'tokenize' => FALSE,
    ];

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $this
      ->createMock('Drupal\\Core\\Entity\\EntityInterface');
    $entity
      ->expects($this
      ->once())
      ->method('access')
      ->willReturn(TRUE);
    $this->entityStorage
      ->expects($this
      ->never())
      ->method('loadByProperties');
    $this->entityRepository
      ->expects($this
      ->any())
      ->method('loadEntityByConfigTarget')
      ->willReturn($entity);
    $this->entityViewBuilder
      ->expects($this
      ->once())
      ->method('view')
      ->with($entity, 'default')
      ->willReturn([
      '#markup' => 'hallo',
    ]);
    $this->entityHandler
      ->init($this->executable, $this->display, $options);
    $result = $this->entityHandler
      ->render();
    $this
      ->assertEquals([
      '#markup' => 'hallo',
    ], $result);
  }

  /**
   * @covers ::render
   * @covers ::defineOptions
   * @covers ::init
   *
   * @dataProvider providerTestTokens
   */
  public function testRenderWithIdAndToken($token, $id) {
    $this
      ->setupEntityTypeManager();
    $options = [
      'target' => $token,
      'tokenize' => TRUE,
    ];
    $entity = $this
      ->createMock('Drupal\\Core\\Entity\\EntityInterface');
    $entity
      ->expects($this
      ->once())
      ->method('access')
      ->willReturn(TRUE);
    $this->stylePlugin
      ->expects($this
      ->once())
      ->method('tokenizeValue')
      ->with($token, 0)
      ->willReturn($id);
    $this->entityStorage
      ->expects($this
      ->never())
      ->method('loadByProperties');
    $this->entityStorage
      ->expects($this
      ->once())
      ->method('load')
      ->with($id)
      ->willReturn($entity);
    $this->entityViewBuilder
      ->expects($this
      ->once())
      ->method('view')
      ->with($entity, 'default')
      ->willReturn([
      '#markup' => 'hallo',
    ]);
    $this->entityHandler
      ->init($this->executable, $this->display, $options);
    $result = $this->entityHandler
      ->render();
    $this
      ->assertEquals([
      '#markup' => 'hallo',
    ], $result);
  }

  /**
   * @covers ::render
   * @covers ::defineOptions
   * @covers ::init
   */
  public function testRenderWithUuid() {
    $this
      ->setupEntityTypeManager();
    $uuid = '1d52762e-b9d8-4177-908f-572d1a5845a4';
    $options = [
      'target' => $uuid,
      'tokenize' => FALSE,
    ];
    $entity = $this
      ->createMock('Drupal\\Core\\Entity\\EntityInterface');
    $entity
      ->expects($this
      ->once())
      ->method('access')
      ->willReturn(TRUE);
    $this->entityStorage
      ->expects($this
      ->never())
      ->method('load');
    $this->entityRepository
      ->expects($this
      ->once())
      ->method('loadEntityByConfigTarget')
      ->willReturn($entity);
    $this->entityViewBuilder
      ->expects($this
      ->once())
      ->method('view')
      ->with($entity, 'default')
      ->willReturn([
      '#markup' => 'hallo',
    ]);
    $this->entityHandler
      ->init($this->executable, $this->display, $options);
    $result = $this->entityHandler
      ->render();
    $this
      ->assertEquals([
      '#markup' => 'hallo',
    ], $result);
  }

  /**
   * @covers ::calculateDependencies
   *
   * @dataProvider providerTestTokens
   */
  public function testCalculateDependenciesWithPlaceholder($token, $id) {
    $this
      ->setupEntityTypeManager();
    $options = [
      'target' => $token,
    ];
    $this->entityHandler
      ->init($this->executable, $this->display, $options);
    $this
      ->assertEquals([], $this->entityHandler
      ->calculateDependencies());
  }

  /**
   * @covers ::calculateDependencies
   */
  public function testCalculateDependenciesWithUuid() {
    $this
      ->setupEntityTypeManager();
    $uuid = '1d52762e-b9d8-4177-908f-572d1a5845a4';
    $entity = $this
      ->createMock('Drupal\\Core\\Entity\\EntityInterface');
    $entity_type = $this
      ->createMock('Drupal\\Core\\Entity\\EntityTypeInterface');
    $entity
      ->expects($this
      ->once())
      ->method('getConfigDependencyName')
      ->willReturn('entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4');
    $this->entityStorage
      ->expects($this
      ->never())
      ->method('load');
    $this->entityRepository
      ->expects($this
      ->once())
      ->method('loadEntityByConfigTarget')
      ->willReturn($entity);
    $entity_type
      ->expects($this
      ->once())
      ->method('getConfigDependencyKey')
      ->willReturn('content');
    $this->entityTypeManager
      ->expects($this
      ->once())
      ->method('getDefinition')
      ->willReturn($entity_type);
    $options = [
      'target' => $uuid,
    ];
    $this->entityHandler
      ->init($this->executable, $this->display, $options);
    $this
      ->assertEquals([
      'content' => [
        'entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4',
      ],
    ], $this->entityHandler
      ->calculateDependencies());
  }

  /**
   * @covers ::calculateDependencies
   */
  public function testCalculateDependenciesWithEntityId() {
    $this
      ->setupEntityTypeManager();
    $entity = $this
      ->createMock('Drupal\\Core\\Entity\\EntityInterface');
    $entity_type = $this
      ->createMock('Drupal\\Core\\Entity\\EntityTypeInterface');
    $entity
      ->expects($this
      ->once())
      ->method('getConfigDependencyName')
      ->willReturn('entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4');
    $this->entityRepository
      ->expects($this
      ->once())
      ->method('loadEntityByConfigTarget')
      ->willReturn($entity);
    $this->entityStorage
      ->expects($this
      ->never())
      ->method('loadByProperties');
    $entity_type
      ->expects($this
      ->once())
      ->method('getConfigDependencyKey')
      ->willReturn('content');
    $this->entityTypeManager
      ->expects($this
      ->once())
      ->method('getDefinition')
      ->willReturn($entity_type);
    $options = [
      'target' => 1,
    ];
    $this->entityHandler
      ->init($this->executable, $this->display, $options);
    $this
      ->assertEquals([
      'content' => [
        'entity_test:test-bundle:1d52762e-b9d8-4177-908f-572d1a5845a4',
      ],
    ], $this->entityHandler
      ->calculateDependencies());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityTest::$display protected property The mocked display.
EntityTest::$entityDisplayRepository protected property The entity display repository.
EntityTest::$entityHandler protected property The tested entity area handler.
EntityTest::$entityRepository protected property The entity repository.
EntityTest::$entityStorage protected property The mocked entity storage.
EntityTest::$entityTypeManager protected property The mocked entity type manager.
EntityTest::$entityViewBuilder protected property The mocked entity view builder.
EntityTest::$executable protected property The mocked view executable.
EntityTest::$stylePlugin protected property The mocked style plugin.
EntityTest::providerTestTokens public static function Data provider for testing different types of tokens.
EntityTest::setUp protected function Overrides UnitTestCase::setUp
EntityTest::setupEntityTypeManager protected function Ensures that the entity type manager returns an entity storage.
EntityTest::testCalculateDependenciesWithEntityId public function @covers ::calculateDependencies
EntityTest::testCalculateDependenciesWithPlaceholder public function @covers ::calculateDependencies
EntityTest::testCalculateDependenciesWithUuid public function @covers ::calculateDependencies
EntityTest::testRenderWithId public function @covers ::render @covers ::defineOptions @covers ::init
EntityTest::testRenderWithIdAndToken public function @covers ::render @covers ::defineOptions @covers ::init
EntityTest::testRenderWithUuid public function @covers ::render @covers ::defineOptions @covers ::init
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function