3 namespace Drupal\Tests\block\Unit;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
8 use Drupal\Tests\UnitTestCase;
11 * @coversDefaultClass \Drupal\block\Entity\Block
14 class BlockConfigEntityUnitTest extends UnitTestCase {
17 * The entity type used for testing.
19 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
21 protected $entityType;
24 * The entity type manager used for testing.
26 * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
28 protected $entityTypeManager;
31 * The ID of the type of the entity under test.
35 protected $entityTypeId;
38 * The UUID generator used for testing.
40 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
47 protected function setUp() {
48 $this->entityTypeId = $this->randomMachineName();
50 $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
51 $this->entityType->expects($this->any())
52 ->method('getProvider')
53 ->will($this->returnValue('block'));
55 $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
56 $this->entityTypeManager->expects($this->any())
57 ->method('getDefinition')
58 ->with($this->entityTypeId)
59 ->will($this->returnValue($this->entityType));
61 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
63 $container = new ContainerBuilder();
64 $container->set('entity_type.manager', $this->entityTypeManager);
65 $container->set('uuid', $this->uuid);
66 \Drupal::setContainer($container);
70 * @covers ::calculateDependencies
72 public function testCalculateDependencies() {
73 $values = ['theme' => 'stark'];
74 // Mock the entity under test so that we can mock getPluginCollections().
75 $entity = $this->getMockBuilder('\Drupal\block\Entity\Block')
76 ->setConstructorArgs([$values, $this->entityTypeId])
77 ->setMethods(['getPluginCollections'])
79 // Create a configurable plugin that would add a dependency.
80 $instance_id = $this->randomMachineName();
81 $instance = new TestConfigurablePlugin([], $instance_id, ['provider' => 'test']);
83 // Create a plugin collection to contain the instance.
84 $plugin_collection = $this->getMockBuilder('\Drupal\Core\Plugin\DefaultLazyPluginCollection')
85 ->disableOriginalConstructor()
88 $plugin_collection->expects($this->atLeastOnce())
91 ->will($this->returnValue($instance));
92 $plugin_collection->addInstanceId($instance_id);
94 // Return the mocked plugin collection.
95 $entity->expects($this->once())
96 ->method('getPluginCollections')
97 ->will($this->returnValue([$plugin_collection]));
99 $dependencies = $entity->calculateDependencies()->getDependencies();
100 $this->assertContains('test', $dependencies['module']);
101 $this->assertContains('stark', $dependencies['theme']);