Version 1
[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\editor\Entity\Editor;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * @coversDefaultClass \Drupal\editor\Entity\Editor
11  * @group editor
12  */
13 class EditorConfigEntityUnitTest extends UnitTestCase {
14
15   /**
16    * The entity type used for testing.
17    *
18    * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
19    */
20   protected $entityType;
21
22   /**
23    * The entity manager used for testing.
24    *
25    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
26    */
27   protected $entityManager;
28
29   /**
30    * The ID of the type of the entity under test.
31    *
32    * @var string
33    */
34   protected $entityTypeId;
35
36   /**
37    * The UUID generator used for testing.
38    *
39    * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
40    */
41   protected $uuid;
42
43   /**
44    * The editor plugin manager used for testing.
45    *
46    * @var \Drupal\editor\Plugin\EditorManager|\PHPUnit_Framework_MockObject_MockObject
47    */
48   protected $editorPluginManager;
49
50   /**
51    * Editor plugin ID.
52    *
53    * @var string
54    */
55   protected $editorId;
56
57   /**
58    * {@inheritdoc}
59    */
60   protected function setUp() {
61     $this->editorId = $this->randomMachineName();
62     $this->entityTypeId = $this->randomMachineName();
63
64     $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
65     $this->entityType->expects($this->any())
66       ->method('getProvider')
67       ->will($this->returnValue('editor'));
68
69     $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
70     $this->entityManager->expects($this->any())
71       ->method('getDefinition')
72       ->with($this->entityTypeId)
73       ->will($this->returnValue($this->entityType));
74
75     $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
76
77     $this->editorPluginManager = $this->getMockBuilder('Drupal\editor\Plugin\EditorManager')
78       ->disableOriginalConstructor()
79       ->getMock();
80
81     $container = new ContainerBuilder();
82     $container->set('entity.manager', $this->entityManager);
83     $container->set('uuid', $this->uuid);
84     $container->set('plugin.manager.editor', $this->editorPluginManager);
85     \Drupal::setContainer($container);
86   }
87
88   /**
89    * @covers ::calculateDependencies
90    */
91   public function testCalculateDependencies() {
92     $format_id = 'filter.format.test';
93     $values = ['editor' => $this->editorId, 'format' => $format_id];
94
95     $plugin = $this->getMockBuilder('Drupal\editor\Plugin\EditorPluginInterface')
96       ->disableOriginalConstructor()
97       ->getMock();
98     $plugin->expects($this->once())
99       ->method('getPluginDefinition')
100       ->will($this->returnValue(['provider' => 'test_module']));
101     $plugin->expects($this->once())
102       ->method('getDefaultSettings')
103       ->will($this->returnValue([]));
104
105     $this->editorPluginManager->expects($this->any())
106       ->method('createInstance')
107       ->with($this->editorId)
108       ->will($this->returnValue($plugin));
109
110     $entity = new Editor($values, $this->entityTypeId);
111
112     $filter_format = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityInterface');
113     $filter_format->expects($this->once())
114       ->method('getConfigDependencyName')
115       ->will($this->returnValue('filter.format.test'));
116
117     $storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
118     $storage->expects($this->once())
119       ->method('load')
120       ->with($format_id)
121       ->will($this->returnValue($filter_format));
122
123     $this->entityManager->expects($this->once())
124       ->method('getStorage')
125       ->with('filter_format')
126       ->will($this->returnValue($storage));
127
128     $dependencies = $entity->calculateDependencies()->getDependencies();
129     $this->assertContains('test_module', $dependencies['module']);
130     $this->assertContains('filter.format.test', $dependencies['config']);
131   }
132
133 }