EntityDisplayModeBaseUnitTest.php in Drupal 8
File
core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php
View source
<?php
namespace Drupal\Tests\Core\Config\Entity;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
class EntityDisplayModeBaseUnitTest extends UnitTestCase {
protected $entity;
protected $entityInfo;
protected $entityTypeManager;
protected $entityType;
protected $uuid;
protected function setUp() {
$this->entityType = $this
->randomMachineName();
$this->entityInfo = $this
->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$this->entityInfo
->expects($this
->any())
->method('getProvider')
->will($this
->returnValue('entity'));
$this->entityTypeManager = $this
->createMock(EntityTypeManagerInterface::class);
$this->uuid = $this
->createMock('\\Drupal\\Component\\Uuid\\UuidInterface');
$container = new ContainerBuilder();
$container
->set('entity_type.manager', $this->entityTypeManager);
$container
->set('uuid', $this->uuid);
\Drupal::setContainer($container);
}
public function testCalculateDependencies() {
$target_entity_type_id = $this
->randomMachineName(16);
$target_entity_type = $this
->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$target_entity_type
->expects($this
->any())
->method('getProvider')
->will($this
->returnValue('test_module'));
$values = [
'targetEntityType' => $target_entity_type_id,
];
$this->entityTypeManager
->expects($this
->at(0))
->method('getDefinition')
->with($target_entity_type_id)
->will($this
->returnValue($target_entity_type));
$this->entityTypeManager
->expects($this
->at(1))
->method('getDefinition')
->with($this->entityType)
->will($this
->returnValue($this->entityInfo));
$this->entity = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityDisplayModeBase')
->setConstructorArgs([
$values,
$this->entityType,
])
->setMethods([
'getFilterFormat',
])
->getMock();
$dependencies = $this->entity
->calculateDependencies()
->getDependencies();
$this
->assertContains('test_module', $dependencies['module']);
}
public function testSetTargetType() {
$mock = $this
->getMockBuilder('Drupal\\Core\\Entity\\EntityDisplayModeBase')
->setMethods(NULL)
->setConstructorArgs([
[
'something' => 'nothing',
],
'test_type',
])
->getMock();
$bad_target = 'uninitialized';
$target = 'test_target_type';
$property = new \ReflectionProperty($mock, 'targetEntityType');
$property
->setAccessible(TRUE);
$property
->setValue($mock, $bad_target);
$mock
->setTargetType($target);
$this
->assertNotEquals($bad_target, $property
->getValue($mock));
$this
->assertEquals($target, $property
->getValue($mock));
}
public function testGetTargetType() {
$mock = $this
->getMockBuilder('Drupal\\Core\\Entity\\EntityDisplayModeBase')
->setMethods(NULL)
->setConstructorArgs([
[
'something' => 'nothing',
],
'test_type',
])
->getMock();
$target = 'test_target_type';
$property = new \ReflectionProperty($mock, 'targetEntityType');
$property
->setAccessible(TRUE);
$property
->setValue($mock, $target);
$value = $mock
->getTargetType($target);
$this
->assertEquals($value, $property
->getValue($mock));
}
}