3 namespace Drupal\Tests\editor\Unit;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Entity\EntityManager;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\editor\Entity\Editor;
9 use Drupal\Tests\UnitTestCase;
12 * @coversDefaultClass \Drupal\editor\Entity\Editor
15 class EditorConfigEntityUnitTest extends UnitTestCase {
18 * The entity type used for testing.
20 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
22 protected $entityType;
25 * The entity manager used for testing.
27 * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
29 protected $entityTypeManager;
32 * The ID of the type of the entity under test.
36 protected $entityTypeId;
39 * The UUID generator used for testing.
41 * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
46 * The editor plugin manager used for testing.
48 * @var \Drupal\editor\Plugin\EditorManager|\PHPUnit_Framework_MockObject_MockObject
50 protected $editorPluginManager;
62 protected function setUp() {
63 $this->editorId = $this->randomMachineName();
64 $this->entityTypeId = $this->randomMachineName();
66 $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
67 $this->entityType->expects($this->any())
68 ->method('getProvider')
69 ->will($this->returnValue('editor'));
71 $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
72 $this->entityTypeManager->expects($this->any())
73 ->method('getDefinition')
74 ->with($this->entityTypeId)
75 ->will($this->returnValue($this->entityType));
77 $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
79 $this->editorPluginManager = $this->getMockBuilder('Drupal\editor\Plugin\EditorManager')
80 ->disableOriginalConstructor()
83 $entity_manager = new EntityManager();
85 $container = new ContainerBuilder();
86 $container->set('entity.manager', $entity_manager);
87 $container->set('entity_type.manager', $this->entityTypeManager);
88 $container->set('uuid', $this->uuid);
89 $container->set('plugin.manager.editor', $this->editorPluginManager);
90 // Inject the container into entity.manager so it can defer to
91 // entity_type.manager.
92 $entity_manager->setContainer($container);
93 \Drupal::setContainer($container);
97 * @covers ::calculateDependencies
99 public function testCalculateDependencies() {
100 $format_id = 'filter.format.test';
101 $values = ['editor' => $this->editorId, 'format' => $format_id];
103 $plugin = $this->getMockBuilder('Drupal\editor\Plugin\EditorPluginInterface')
104 ->disableOriginalConstructor()
106 $plugin->expects($this->once())
107 ->method('getPluginDefinition')
108 ->will($this->returnValue(['provider' => 'test_module']));
109 $plugin->expects($this->once())
110 ->method('getDefaultSettings')
111 ->will($this->returnValue([]));
113 $this->editorPluginManager->expects($this->any())
114 ->method('createInstance')
115 ->with($this->editorId)
116 ->will($this->returnValue($plugin));
118 $entity = new Editor($values, $this->entityTypeId);
120 $filter_format = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityInterface');
121 $filter_format->expects($this->once())
122 ->method('getConfigDependencyName')
123 ->will($this->returnValue('filter.format.test'));
125 $storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
126 $storage->expects($this->once())
129 ->will($this->returnValue($filter_format));
131 $this->entityTypeManager->expects($this->once())
132 ->method('getStorage')
133 ->with('filter_format')
134 ->will($this->returnValue($storage));
136 $dependencies = $entity->calculateDependencies()->getDependencies();
137 $this->assertContains('test_module', $dependencies['module']);
138 $this->assertContains('filter.format.test', $dependencies['config']);