Backup of db before drupal security update
[yaffs-website] / web / core / modules / config_translation / tests / src / Unit / ConfigEntityMapperTest.php
1 <?php
2
3 namespace Drupal\Tests\config_translation\Unit;
4
5 use Drupal\config_translation\ConfigEntityMapper;
6 use Drupal\Core\Url;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\Routing\Route;
9
10 /**
11  * Tests the functionality provided by the configuration entity mapper.
12  *
13  * @group config_translation
14  */
15 class ConfigEntityMapperTest extends UnitTestCase {
16
17   /**
18    * The configuration entity mapper to test.
19    *
20    * @var \Drupal\config_translation\ConfigEntityMapper
21    */
22   protected $configEntityMapper;
23
24   /**
25    * The entity manager used for testing.
26    *
27    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
28    */
29   protected $entityManager;
30
31   /**
32    * The entity instance used for testing.
33    *
34    * @var \Drupal\Core\Entity\EntityInterface|\PHPUnit_Framework_MockObject_MockObject
35    */
36   protected $entity;
37
38   /**
39    * The route provider used for testing.
40    *
41    * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
42    */
43   protected $routeProvider;
44
45   /**
46    * The mocked language manager.
47    *
48    * @var \Drupal\Core\Language\LanguageManagerInterface $language_manager|\PHPUnit_Framework_MockObject_MockObject
49    */
50   protected $languageManager;
51
52   protected function setUp() {
53     $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
54
55     $this->entity = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityInterface');
56
57     $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
58
59     $this->routeProvider
60       ->expects($this->any())
61       ->method('getRouteByName')
62       ->with('entity.configurable_language.edit_form')
63       ->will($this->returnValue(new Route('/admin/config/regional/language/edit/{configurable_language}')));
64
65     $definition = [
66       'class' => '\Drupal\config_translation\ConfigEntityMapper',
67       'base_route_name' => 'entity.configurable_language.edit_form',
68       'title' => '@label language',
69       'names' => [],
70       'entity_type' => 'configurable_language',
71       'route_name' => 'config_translation.item.overview.entity.configurable_language.edit_form',
72     ];
73
74     $typed_config_manager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
75
76     $locale_config_manager = $this->getMockBuilder('Drupal\locale\LocaleConfigManager')
77       ->disableOriginalConstructor()
78       ->getMock();
79
80     $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
81
82     $this->configEntityMapper = new ConfigEntityMapper(
83       'configurable_language',
84       $definition,
85       $this->getConfigFactoryStub(),
86       $typed_config_manager,
87       $locale_config_manager,
88       $this->getMock('Drupal\config_translation\ConfigMapperManagerInterface'),
89       $this->routeProvider,
90       $this->getStringTranslationStub(),
91       $this->entityManager,
92       $this->languageManager
93     );
94   }
95
96   /**
97    * Tests ConfigEntityMapper::setEntity() and ConfigEntityMapper::getEntity().
98    */
99   public function testEntityGetterAndSetter() {
100     $this->entity
101       ->expects($this->once())
102       ->method('id')
103       ->with()
104       ->will($this->returnValue('entity_id'));
105
106     $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
107     $entity_type
108       ->expects($this->any())
109       ->method('getConfigPrefix')
110       ->will($this->returnValue('config_prefix'));
111     $this->entityManager
112       ->expects($this->once())
113       ->method('getDefinition')
114       ->with('configurable_language')
115       ->will($this->returnValue($entity_type));
116
117     // No entity is set.
118     $this->assertNull($this->configEntityMapper->getEntity());
119
120     $result = $this->configEntityMapper->setEntity($this->entity);
121     $this->assertTrue($result);
122
123     // Ensure that the getter provides the entity.
124     $this->assertEquals($this->entity, $this->configEntityMapper->getEntity());
125
126     // Ensure that the configuration name was added to the mapper.
127     $plugin_definition = $this->configEntityMapper->getPluginDefinition();
128     $this->assertTrue(in_array('config_prefix.entity_id', $plugin_definition['names']));
129
130     // Make sure setEntity() returns FALSE when called a second time.
131     $result = $this->configEntityMapper->setEntity($this->entity);
132     $this->assertFalse($result);
133   }
134
135   /**
136    * Tests ConfigEntityMapper::getOverviewRouteParameters().
137    */
138   public function testGetOverviewRouteParameters() {
139     $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
140     $this->entityManager
141       ->expects($this->once())
142       ->method('getDefinition')
143       ->with('configurable_language')
144       ->will($this->returnValue($entity_type));
145     $this->configEntityMapper->setEntity($this->entity);
146
147     $this->entity
148       ->expects($this->once())
149       ->method('id')
150       ->with()
151       ->will($this->returnValue('entity_id'));
152
153     $result = $this->configEntityMapper->getOverviewRouteParameters();
154
155     $this->assertSame(['configurable_language' => 'entity_id'], $result);
156   }
157
158   /**
159    * Tests ConfigEntityMapper::getType().
160    */
161   public function testGetType() {
162     $result = $this->configEntityMapper->getType();
163     $this->assertSame('configurable_language', $result);
164   }
165
166   /**
167    * Tests ConfigEntityMapper::getTypeName().
168    */
169   public function testGetTypeName() {
170     $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
171     $entity_type->expects($this->once())
172       ->method('getLabel')
173       ->will($this->returnValue('test'));
174     $this->entityManager
175       ->expects($this->once())
176       ->method('getDefinition')
177       ->with('configurable_language')
178       ->will($this->returnValue($entity_type));
179
180     $result = $this->configEntityMapper->getTypeName();
181     $this->assertSame('test', $result);
182   }
183
184   /**
185    * Tests ConfigEntityMapper::getTypeLabel().
186    */
187   public function testGetTypeLabel() {
188     $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
189     $entity_type->expects($this->once())
190       ->method('getLabel')
191       ->will($this->returnValue('test'));
192     $this->entityManager
193       ->expects($this->once())
194       ->method('getDefinition')
195       ->with('configurable_language')
196       ->will($this->returnValue($entity_type));
197
198     $result = $this->configEntityMapper->getTypeLabel();
199     $this->assertSame('test', $result);
200   }
201
202   /**
203    * Tests ConfigEntityMapper::getOperations().
204    */
205   public function testGetOperations() {
206     $result = $this->configEntityMapper->getOperations();
207
208     $expected = [
209       'list' => [
210         'title' => 'List',
211         'url' => Url::fromRoute('config_translation.entity_list', ['mapper_id' => 'configurable_language']),
212       ],
213     ];
214
215     $this->assertEquals($expected, $result);
216   }
217
218 }