annotate core/modules/block/tests/src/Unit/BlockConfigEntityUnitTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\block\Unit;
Chris@0 4
Chris@0 5 use Drupal\Core\DependencyInjection\ContainerBuilder;
Chris@0 6 use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
Chris@0 7 use Drupal\Tests\UnitTestCase;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * @coversDefaultClass \Drupal\block\Entity\Block
Chris@0 11 * @group block
Chris@0 12 */
Chris@0 13 class BlockConfigEntityUnitTest extends UnitTestCase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * The entity type used for testing.
Chris@0 17 *
Chris@0 18 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
Chris@0 19 */
Chris@0 20 protected $entityType;
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The entity manager used for testing.
Chris@0 24 *
Chris@0 25 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
Chris@0 26 */
Chris@0 27 protected $entityManager;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * The ID of the type of the entity under test.
Chris@0 31 *
Chris@0 32 * @var string
Chris@0 33 */
Chris@0 34 protected $entityTypeId;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * The UUID generator used for testing.
Chris@0 38 *
Chris@0 39 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
Chris@0 40 */
Chris@0 41 protected $uuid;
Chris@0 42
Chris@0 43 /**
Chris@0 44 * {@inheritdoc}
Chris@0 45 */
Chris@0 46 protected function setUp() {
Chris@0 47 $this->entityTypeId = $this->randomMachineName();
Chris@0 48
Chris@0 49 $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
Chris@0 50 $this->entityType->expects($this->any())
Chris@0 51 ->method('getProvider')
Chris@0 52 ->will($this->returnValue('block'));
Chris@0 53
Chris@0 54 $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
Chris@0 55 $this->entityManager->expects($this->any())
Chris@0 56 ->method('getDefinition')
Chris@0 57 ->with($this->entityTypeId)
Chris@0 58 ->will($this->returnValue($this->entityType));
Chris@0 59
Chris@0 60 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
Chris@0 61
Chris@0 62 $container = new ContainerBuilder();
Chris@0 63 $container->set('entity.manager', $this->entityManager);
Chris@0 64 $container->set('uuid', $this->uuid);
Chris@0 65 \Drupal::setContainer($container);
Chris@0 66 }
Chris@0 67
Chris@0 68 /**
Chris@0 69 * @covers ::calculateDependencies
Chris@0 70 */
Chris@0 71 public function testCalculateDependencies() {
Chris@0 72 $values = ['theme' => 'stark'];
Chris@0 73 // Mock the entity under test so that we can mock getPluginCollections().
Chris@0 74 $entity = $this->getMockBuilder('\Drupal\block\Entity\Block')
Chris@0 75 ->setConstructorArgs([$values, $this->entityTypeId])
Chris@0 76 ->setMethods(['getPluginCollections'])
Chris@0 77 ->getMock();
Chris@0 78 // Create a configurable plugin that would add a dependency.
Chris@0 79 $instance_id = $this->randomMachineName();
Chris@0 80 $instance = new TestConfigurablePlugin([], $instance_id, ['provider' => 'test']);
Chris@0 81
Chris@0 82 // Create a plugin collection to contain the instance.
Chris@0 83 $plugin_collection = $this->getMockBuilder('\Drupal\Core\Plugin\DefaultLazyPluginCollection')
Chris@0 84 ->disableOriginalConstructor()
Chris@0 85 ->setMethods(['get'])
Chris@0 86 ->getMock();
Chris@0 87 $plugin_collection->expects($this->atLeastOnce())
Chris@0 88 ->method('get')
Chris@0 89 ->with($instance_id)
Chris@0 90 ->will($this->returnValue($instance));
Chris@0 91 $plugin_collection->addInstanceId($instance_id);
Chris@0 92
Chris@0 93 // Return the mocked plugin collection.
Chris@0 94 $entity->expects($this->once())
Chris@0 95 ->method('getPluginCollections')
Chris@0 96 ->will($this->returnValue([$plugin_collection]));
Chris@0 97
Chris@0 98 $dependencies = $entity->calculateDependencies()->getDependencies();
Chris@0 99 $this->assertContains('test', $dependencies['module']);
Chris@0 100 $this->assertContains('stark', $dependencies['theme']);
Chris@0 101 }
Chris@0 102
Chris@0 103 }