Pull merge.
[yaffs-website] / web / core / modules / block / tests / src / Unit / BlockConfigEntityUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\block\Unit;
4
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;
9
10 /**
11  * @coversDefaultClass \Drupal\block\Entity\Block
12  * @group block
13  */
14 class BlockConfigEntityUnitTest extends UnitTestCase {
15
16   /**
17    * The entity type used for testing.
18    *
19    * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
20    */
21   protected $entityType;
22
23   /**
24    * The entity type manager used for testing.
25    *
26    * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
27    */
28   protected $entityTypeManager;
29
30   /**
31    * The ID of the type of the entity under test.
32    *
33    * @var string
34    */
35   protected $entityTypeId;
36
37   /**
38    * The UUID generator used for testing.
39    *
40    * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
41    */
42   protected $uuid;
43
44   /**
45    * {@inheritdoc}
46    */
47   protected function setUp() {
48     $this->entityTypeId = $this->randomMachineName();
49
50     $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
51     $this->entityType->expects($this->any())
52       ->method('getProvider')
53       ->will($this->returnValue('block'));
54
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));
60
61     $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
62
63     $container = new ContainerBuilder();
64     $container->set('entity_type.manager', $this->entityTypeManager);
65     $container->set('uuid', $this->uuid);
66     \Drupal::setContainer($container);
67   }
68
69   /**
70    * @covers ::calculateDependencies
71    */
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'])
78       ->getMock();
79     // Create a configurable plugin that would add a dependency.
80     $instance_id = $this->randomMachineName();
81     $instance = new TestConfigurablePlugin([], $instance_id, ['provider' => 'test']);
82
83     // Create a plugin collection to contain the instance.
84     $plugin_collection = $this->getMockBuilder('\Drupal\Core\Plugin\DefaultLazyPluginCollection')
85       ->disableOriginalConstructor()
86       ->setMethods(['get'])
87       ->getMock();
88     $plugin_collection->expects($this->atLeastOnce())
89       ->method('get')
90       ->with($instance_id)
91       ->will($this->returnValue($instance));
92     $plugin_collection->addInstanceId($instance_id);
93
94     // Return the mocked plugin collection.
95     $entity->expects($this->once())
96       ->method('getPluginCollections')
97       ->will($this->returnValue([$plugin_collection]));
98
99     $dependencies = $entity->calculateDependencies()->getDependencies();
100     $this->assertContains('test', $dependencies['module']);
101     $this->assertContains('stark', $dependencies['theme']);
102   }
103
104 }