3 namespace Drupal\Tests\rdf\Unit;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\rdf\Entity\RdfMapping;
10 * @coversDefaultClass \Drupal\rdf\Entity\RdfMapping
13 class RdfMappingConfigEntityUnitTest extends UnitTestCase {
16 * The entity type used for testing.
18 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
20 protected $entityType;
23 * The entity manager used for testing.
25 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
27 protected $entityManager;
30 * The ID of the type of the entity under test.
34 protected $entityTypeId;
37 * The UUID generator used for testing.
39 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
46 protected function setUp() {
47 $this->entityTypeId = $this->randomMachineName();
49 $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
50 $this->entityType->expects($this->any())
51 ->method('getProvider')
52 ->will($this->returnValue('entity'));
54 $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
56 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
58 $container = new ContainerBuilder();
59 $container->set('entity.manager', $this->entityManager);
60 $container->set('uuid', $this->uuid);
61 \Drupal::setContainer($container);
66 * @covers ::calculateDependencies
68 public function testCalculateDependencies() {
69 $target_entity_type_id = $this->randomMachineName(16);
71 $target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
72 $target_entity_type->expects($this->any())
73 ->method('getProvider')
74 ->will($this->returnValue('test_module'));
75 $values = ['targetEntityType' => $target_entity_type_id];
76 $target_entity_type->expects($this->any())
77 ->method('getBundleEntityType')
78 ->will($this->returnValue(NULL));
80 $this->entityManager->expects($this->at(0))
81 ->method('getDefinition')
82 ->with($target_entity_type_id)
83 ->will($this->returnValue($target_entity_type));
84 $this->entityManager->expects($this->at(1))
85 ->method('getDefinition')
86 ->with($this->entityTypeId)
87 ->will($this->returnValue($this->entityType));
89 $entity = new RdfMapping($values, $this->entityTypeId);
90 $dependencies = $entity->calculateDependencies()->getDependencies();
91 $this->assertArrayNotHasKey('config', $dependencies);
92 $this->assertContains('test_module', $dependencies['module']);
96 * @covers ::calculateDependencies
98 public function testCalculateDependenciesWithEntityBundle() {
99 $target_entity_type_id = $this->randomMachineName(16);
100 $target_entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
101 $target_entity_type->expects($this->any())
102 ->method('getProvider')
103 ->will($this->returnValue('test_module'));
104 $bundle_id = $this->randomMachineName(10);
105 $values = ['targetEntityType' => $target_entity_type_id , 'bundle' => $bundle_id];
107 $target_entity_type->expects($this->any())
108 ->method('getBundleConfigDependency')
109 ->will($this->returnValue(['type' => 'config', 'name' => 'test_module.type.' . $bundle_id]));
111 $this->entityManager->expects($this->at(0))
112 ->method('getDefinition')
113 ->with($target_entity_type_id)
114 ->will($this->returnValue($target_entity_type));
115 $this->entityManager->expects($this->at(1))
116 ->method('getDefinition')
117 ->with($this->entityTypeId)
118 ->will($this->returnValue($this->entityType));
120 $entity = new RdfMapping($values, $this->entityTypeId);
121 $dependencies = $entity->calculateDependencies()->getDependencies();
122 $this->assertContains('test_module.type.' . $bundle_id, $dependencies['config']);
123 $this->assertContains('test_module', $dependencies['module']);