Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / editor / tests / src / Unit / EditorConfigEntityUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\editor\Unit;
4
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;
10
11 /**
12  * @coversDefaultClass \Drupal\editor\Entity\Editor
13  * @group editor
14  */
15 class EditorConfigEntityUnitTest extends UnitTestCase {
16
17   /**
18    * The entity type used for testing.
19    *
20    * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
21    */
22   protected $entityType;
23
24   /**
25    * The entity manager used for testing.
26    *
27    * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
28    */
29   protected $entityTypeManager;
30
31   /**
32    * The ID of the type of the entity under test.
33    *
34    * @var string
35    */
36   protected $entityTypeId;
37
38   /**
39    * The UUID generator used for testing.
40    *
41    * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
42    */
43   protected $uuid;
44
45   /**
46    * The editor plugin manager used for testing.
47    *
48    * @var \Drupal\editor\Plugin\EditorManager|\PHPUnit_Framework_MockObject_MockObject
49    */
50   protected $editorPluginManager;
51
52   /**
53    * Editor plugin ID.
54    *
55    * @var string
56    */
57   protected $editorId;
58
59   /**
60    * {@inheritdoc}
61    */
62   protected function setUp() {
63     $this->editorId = $this->randomMachineName();
64     $this->entityTypeId = $this->randomMachineName();
65
66     $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
67     $this->entityType->expects($this->any())
68       ->method('getProvider')
69       ->will($this->returnValue('editor'));
70
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));
76
77     $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
78
79     $this->editorPluginManager = $this->getMockBuilder('Drupal\editor\Plugin\EditorManager')
80       ->disableOriginalConstructor()
81       ->getMock();
82
83     $entity_manager = new EntityManager();
84
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);
94   }
95
96   /**
97    * @covers ::calculateDependencies
98    */
99   public function testCalculateDependencies() {
100     $format_id = 'filter.format.test';
101     $values = ['editor' => $this->editorId, 'format' => $format_id];
102
103     $plugin = $this->getMockBuilder('Drupal\editor\Plugin\EditorPluginInterface')
104       ->disableOriginalConstructor()
105       ->getMock();
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([]));
112
113     $this->editorPluginManager->expects($this->any())
114       ->method('createInstance')
115       ->with($this->editorId)
116       ->will($this->returnValue($plugin));
117
118     $entity = new Editor($values, $this->entityTypeId);
119
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'));
124
125     $storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
126     $storage->expects($this->once())
127       ->method('load')
128       ->with($format_id)
129       ->will($this->returnValue($filter_format));
130
131     $this->entityTypeManager->expects($this->once())
132       ->method('getStorage')
133       ->with('filter_format')
134       ->will($this->returnValue($storage));
135
136     $dependencies = $entity->calculateDependencies()->getDependencies();
137     $this->assertContains('test_module', $dependencies['module']);
138     $this->assertContains('filter.format.test', $dependencies['config']);
139   }
140
141 }