BlockConfigEntityUnitTest.php 3.2 KB

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