RdfMappingConfigEntityUnitTest.php in Drupal 8
File
core/modules/rdf/tests/src/Unit/RdfMappingConfigEntityUnitTest.php
View source
<?php
namespace Drupal\Tests\rdf\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\rdf\Entity\RdfMapping;
class RdfMappingConfigEntityUnitTest extends UnitTestCase {
protected $entityType;
protected $entityTypeManager;
protected $entityTypeId;
protected $uuid;
protected function setUp() {
$this->entityTypeId = $this
->randomMachineName();
$this->entityType = $this
->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$this->entityType
->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,
];
$target_entity_type
->expects($this
->any())
->method('getBundleEntityType')
->will($this
->returnValue(NULL));
$target_entity_type
->expects($this
->any())
->method('getBundleConfigDependency')
->will($this
->returnValue([
'type' => 'module',
'name' => 'test_module',
]));
$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->entityTypeId)
->will($this
->returnValue($this->entityType));
$this->entityTypeManager
->expects($this
->at(2))
->method('getDefinition')
->with($this->entityTypeId)
->will($this
->returnValue($this->entityType));
$entity = new RdfMapping($values, $this->entityTypeId);
$dependencies = $entity
->calculateDependencies()
->getDependencies();
$this
->assertArrayNotHasKey('config', $dependencies);
$this
->assertContains('test_module', $dependencies['module']);
}
public function testCalculateDependenciesWithEntityBundle() {
$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'));
$bundle_id = $this
->randomMachineName(10);
$values = [
'targetEntityType' => $target_entity_type_id,
'bundle' => $bundle_id,
];
$target_entity_type
->expects($this
->any())
->method('getBundleConfigDependency')
->will($this
->returnValue([
'type' => 'config',
'name' => 'test_module.type.' . $bundle_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->entityTypeId)
->will($this
->returnValue($this->entityType));
$entity = new RdfMapping($values, $this->entityTypeId);
$dependencies = $entity
->calculateDependencies()
->getDependencies();
$this
->assertContains('test_module.type.' . $bundle_id, $dependencies['config']);
$this
->assertContains('test_module', $dependencies['module']);
}
}