annotate core/modules/block/tests/src/Unit/BlockConfigEntityUnitTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
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@14 6 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@18 7 use Drupal\Core\Extension\ModuleHandlerInterface;
Chris@18 8 use Drupal\Core\Extension\ThemeHandlerInterface;
Chris@0 9 use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
Chris@0 10 use Drupal\Tests\UnitTestCase;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * @coversDefaultClass \Drupal\block\Entity\Block
Chris@0 14 * @group block
Chris@0 15 */
Chris@0 16 class BlockConfigEntityUnitTest extends UnitTestCase {
Chris@0 17
Chris@0 18 /**
Chris@0 19 * The entity type used for testing.
Chris@0 20 *
Chris@0 21 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
Chris@0 22 */
Chris@0 23 protected $entityType;
Chris@0 24
Chris@0 25 /**
Chris@14 26 * The entity type manager used for testing.
Chris@0 27 *
Chris@14 28 * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
Chris@0 29 */
Chris@14 30 protected $entityTypeManager;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * The ID of the type of the entity under test.
Chris@0 34 *
Chris@0 35 * @var string
Chris@0 36 */
Chris@0 37 protected $entityTypeId;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * The UUID generator used for testing.
Chris@0 41 *
Chris@0 42 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
Chris@0 43 */
Chris@0 44 protected $uuid;
Chris@0 45
Chris@0 46 /**
Chris@18 47 * The module handler.
Chris@18 48 *
Chris@18 49 * @var \Drupal\Core\Extension\ModuleHandlerInterface|\Prophecy\Prophecy\ProphecyInterface
Chris@18 50 */
Chris@18 51 protected $moduleHandler;
Chris@18 52
Chris@18 53 /**
Chris@18 54 * The theme handler.
Chris@18 55 *
Chris@18 56 * @var \Drupal\Core\Extension\ThemeHandlerInterface|\Prophecy\Prophecy\ProphecyInterface
Chris@18 57 */
Chris@18 58 protected $themeHandler;
Chris@18 59
Chris@18 60 /**
Chris@0 61 * {@inheritdoc}
Chris@0 62 */
Chris@0 63 protected function setUp() {
Chris@0 64 $this->entityTypeId = $this->randomMachineName();
Chris@0 65
Chris@0 66 $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
Chris@0 67 $this->entityType->expects($this->any())
Chris@0 68 ->method('getProvider')
Chris@0 69 ->will($this->returnValue('block'));
Chris@0 70
Chris@14 71 $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
Chris@14 72 $this->entityTypeManager->expects($this->any())
Chris@0 73 ->method('getDefinition')
Chris@0 74 ->with($this->entityTypeId)
Chris@0 75 ->will($this->returnValue($this->entityType));
Chris@0 76
Chris@0 77 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
Chris@0 78
Chris@18 79 $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
Chris@18 80 $this->themeHandler = $this->prophesize(ThemeHandlerInterface::class);
Chris@18 81
Chris@0 82 $container = new ContainerBuilder();
Chris@14 83 $container->set('entity_type.manager', $this->entityTypeManager);
Chris@18 84 $container->set('module_handler', $this->moduleHandler->reveal());
Chris@18 85 $container->set('theme_handler', $this->themeHandler->reveal());
Chris@0 86 $container->set('uuid', $this->uuid);
Chris@0 87 \Drupal::setContainer($container);
Chris@0 88 }
Chris@0 89
Chris@0 90 /**
Chris@0 91 * @covers ::calculateDependencies
Chris@0 92 */
Chris@0 93 public function testCalculateDependencies() {
Chris@18 94 $this->themeHandler->themeExists('stark')->willReturn(TRUE);
Chris@0 95 $values = ['theme' => 'stark'];
Chris@0 96 // Mock the entity under test so that we can mock getPluginCollections().
Chris@0 97 $entity = $this->getMockBuilder('\Drupal\block\Entity\Block')
Chris@0 98 ->setConstructorArgs([$values, $this->entityTypeId])
Chris@0 99 ->setMethods(['getPluginCollections'])
Chris@0 100 ->getMock();
Chris@0 101 // Create a configurable plugin that would add a dependency.
Chris@0 102 $instance_id = $this->randomMachineName();
Chris@18 103 $this->moduleHandler->moduleExists('test')->willReturn(TRUE);
Chris@0 104 $instance = new TestConfigurablePlugin([], $instance_id, ['provider' => 'test']);
Chris@0 105
Chris@0 106 // Create a plugin collection to contain the instance.
Chris@0 107 $plugin_collection = $this->getMockBuilder('\Drupal\Core\Plugin\DefaultLazyPluginCollection')
Chris@0 108 ->disableOriginalConstructor()
Chris@0 109 ->setMethods(['get'])
Chris@0 110 ->getMock();
Chris@0 111 $plugin_collection->expects($this->atLeastOnce())
Chris@0 112 ->method('get')
Chris@0 113 ->with($instance_id)
Chris@0 114 ->will($this->returnValue($instance));
Chris@0 115 $plugin_collection->addInstanceId($instance_id);
Chris@0 116
Chris@0 117 // Return the mocked plugin collection.
Chris@0 118 $entity->expects($this->once())
Chris@0 119 ->method('getPluginCollections')
Chris@0 120 ->will($this->returnValue([$plugin_collection]));
Chris@0 121
Chris@0 122 $dependencies = $entity->calculateDependencies()->getDependencies();
Chris@0 123 $this->assertContains('test', $dependencies['module']);
Chris@0 124 $this->assertContains('stark', $dependencies['theme']);
Chris@0 125 }
Chris@0 126
Chris@0 127 }