comparison core/tests/Drupal/Tests/Core/DrupalTest.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\Tests\Core;
4
5 use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Entity\Query\QueryAggregateInterface;
9 use Drupal\Core\Entity\Query\QueryInterface;
10 use Drupal\Tests\UnitTestCase;
11 use Drupal\Core\Url;
12 use Symfony\Component\HttpFoundation\RequestStack;
13
14 /**
15 * Tests the Drupal class.
16 *
17 * @coversDefaultClass \Drupal
18 * @group DrupalTest
19 */
20 class DrupalTest extends UnitTestCase {
21
22 /**
23 * The mock container.
24 *
25 * @var \Symfony\Component\DependencyInjection\ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject
26 */
27 protected $container;
28
29 /**
30 * {@inheritdoc}
31 */
32 protected function setUp() {
33 parent::setUp();
34 $this->container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
35 ->setMethods(['get'])
36 ->getMock();
37 }
38
39 /**
40 * Tests the get/setContainer() method.
41 *
42 * @covers ::getContainer
43 */
44 public function testSetContainer() {
45 \Drupal::setContainer($this->container);
46 $this->assertSame($this->container, \Drupal::getContainer());
47 }
48
49 /**
50 * @covers ::getContainer
51 */
52 public function testGetContainerException() {
53 $this->setExpectedException(ContainerNotInitializedException::class, '\Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.');
54 \Drupal::getContainer();
55 }
56
57 /**
58 * Tests the service() method.
59 *
60 * @covers ::service
61 */
62 public function testService() {
63 $this->setMockContainerService('test_service');
64 $this->assertNotNull(\Drupal::service('test_service'));
65 }
66
67 /**
68 * Tests the currentUser() method.
69 *
70 * @covers ::currentUser
71 */
72 public function testCurrentUser() {
73 $this->setMockContainerService('current_user');
74 $this->assertNotNull(\Drupal::currentUser());
75 }
76
77 /**
78 * Tests the entityManager() method.
79 *
80 * @covers ::entityManager
81 */
82 public function testEntityManager() {
83 $this->setMockContainerService('entity.manager');
84 $this->assertNotNull(\Drupal::entityManager());
85 }
86
87 /**
88 * Tests the entityTypeManager() method.
89 *
90 * @covers ::entityTypeManager
91 */
92 public function testEntityTypeManager() {
93 $this->setMockContainerService('entity_type.manager');
94 $this->assertNotNull(\Drupal::entityTypeManager());
95 }
96
97 /**
98 * Tests the database() method.
99 *
100 * @covers ::database
101 */
102 public function testDatabase() {
103 $this->setMockContainerService('database');
104 $this->assertNotNull(\Drupal::database());
105 }
106
107 /**
108 * Tests the cache() method.
109 *
110 * @covers ::cache
111 */
112 public function testCache() {
113 $this->setMockContainerService('cache.test');
114 $this->assertNotNull(\Drupal::cache('test'));
115 }
116
117 /**
118 * Tests the classResolver method.
119 *
120 * @covers ::classResolver
121 */
122 public function testClassResolver() {
123 $this->setMockContainerService('class_resolver');
124 $this->assertNotNull(\Drupal::classResolver());
125 }
126
127 /**
128 * Tests the keyValueExpirable() method.
129 *
130 * @covers ::keyValueExpirable
131 */
132 public function testKeyValueExpirable() {
133 $keyvalue = $this->getMockBuilder('Drupal\Core\KeyValueStore\KeyValueExpirableFactory')
134 ->disableOriginalConstructor()
135 ->getMock();
136 $keyvalue->expects($this->once())
137 ->method('get')
138 ->with('test_collection')
139 ->will($this->returnValue(TRUE));
140 $this->setMockContainerService('keyvalue.expirable', $keyvalue);
141
142 $this->assertNotNull(\Drupal::keyValueExpirable('test_collection'));
143 }
144
145 /**
146 * Tests the lock() method.
147 *
148 * @covers ::lock
149 */
150 public function testLock() {
151 $this->setMockContainerService('lock');
152 $this->assertNotNull(\Drupal::lock());
153 }
154
155 /**
156 * Tests the config() method.
157 *
158 * @covers ::config
159 */
160 public function testConfig() {
161 $config = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
162 $config->expects($this->once())
163 ->method('get')
164 ->with('test_config')
165 ->will($this->returnValue(TRUE));
166 $this->setMockContainerService('config.factory', $config);
167
168 // Test \Drupal::config(), not $this->config().
169 $this->assertNotNull(\Drupal::config('test_config'));
170 }
171
172 /**
173 * Tests the queue() method.
174 *
175 * @covers ::queue
176 */
177 public function testQueue() {
178 $queue = $this->getMockBuilder('Drupal\Core\Queue\QueueFactory')
179 ->disableOriginalConstructor()
180 ->getMock();
181 $queue->expects($this->once())
182 ->method('get')
183 ->with('test_queue', TRUE)
184 ->will($this->returnValue(TRUE));
185 $this->setMockContainerService('queue', $queue);
186
187 $this->assertNotNull(\Drupal::queue('test_queue', TRUE));
188 }
189
190 /**
191 * Tests the testRequestStack() method.
192 *
193 * @covers ::requestStack
194 */
195 public function testRequestStack() {
196 $request_stack = new RequestStack();
197 $this->setMockContainerService('request_stack', $request_stack);
198
199 $this->assertSame($request_stack, \Drupal::requestStack());
200 }
201
202 /**
203 * Tests the keyValue() method.
204 *
205 * @covers ::keyValue
206 */
207 public function testKeyValue() {
208 $keyvalue = $this->getMockBuilder('Drupal\Core\KeyValueStore\KeyValueFactory')
209 ->disableOriginalConstructor()
210 ->getMock();
211 $keyvalue->expects($this->once())
212 ->method('get')
213 ->with('test_collection')
214 ->will($this->returnValue(TRUE));
215 $this->setMockContainerService('keyvalue', $keyvalue);
216
217 $this->assertNotNull(\Drupal::keyValue('test_collection'));
218 }
219
220 /**
221 * Tests the state() method.
222 *
223 * @covers ::state
224 */
225 public function testState() {
226 $this->setMockContainerService('state');
227 $this->assertNotNull(\Drupal::state());
228 }
229
230 /**
231 * Tests the httpClient() method.
232 *
233 * @covers ::httpClient
234 */
235 public function testHttpClient() {
236 $this->setMockContainerService('http_client');
237 $this->assertNotNull(\Drupal::httpClient());
238 }
239
240 /**
241 * Tests the entityQuery() method.
242 *
243 * @covers ::entityQuery
244 */
245 public function testEntityQuery() {
246 $query = $this->getMock(QueryInterface::class);
247 $storage = $this->getMock(EntityStorageInterface::class);
248 $storage
249 ->expects($this->once())
250 ->method('getQuery')
251 ->with('OR')
252 ->willReturn($query);
253
254 $entity_type_manager = $this->getMock(EntityTypeManagerInterface::class);
255 $entity_type_manager
256 ->expects($this->once())
257 ->method('getStorage')
258 ->with('test_entity')
259 ->willReturn($storage);
260
261 $this->setMockContainerService('entity_type.manager', $entity_type_manager);
262
263 $this->assertInstanceOf(QueryInterface::class, \Drupal::entityQuery('test_entity', 'OR'));
264 }
265
266 /**
267 * Tests the entityQueryAggregate() method.
268 *
269 * @covers ::entityQueryAggregate
270 */
271 public function testEntityQueryAggregate() {
272 $query = $this->getMock(QueryAggregateInterface::class);
273 $storage = $this->getMock(EntityStorageInterface::class);
274 $storage
275 ->expects($this->once())
276 ->method('getAggregateQuery')
277 ->with('OR')
278 ->willReturn($query);
279
280 $entity_type_manager = $this->getMock(EntityTypeManagerInterface::class);
281 $entity_type_manager
282 ->expects($this->once())
283 ->method('getStorage')
284 ->with('test_entity')
285 ->willReturn($storage);
286
287 $this->setMockContainerService('entity_type.manager', $entity_type_manager);
288
289 $this->assertInstanceOf(QueryAggregateInterface::class, \Drupal::entityQueryAggregate('test_entity', 'OR'));
290 }
291
292 /**
293 * Tests the flood() method.
294 *
295 * @covers ::flood
296 */
297 public function testFlood() {
298 $this->setMockContainerService('flood');
299 $this->assertNotNull(\Drupal::flood());
300 }
301
302 /**
303 * Tests the moduleHandler() method.
304 *
305 * @covers ::moduleHandler
306 */
307 public function testModuleHandler() {
308 $this->setMockContainerService('module_handler');
309 $this->assertNotNull(\Drupal::moduleHandler());
310 }
311
312 /**
313 * Tests the typedDataManager() method.
314 *
315 * @covers ::typedDataManager
316 */
317 public function testTypedDataManager() {
318 $this->setMockContainerService('typed_data_manager');
319 $this->assertNotNull(\Drupal::typedDataManager());
320 }
321
322 /**
323 * Tests the token() method.
324 *
325 * @covers ::token
326 */
327 public function testToken() {
328 $this->setMockContainerService('token');
329 $this->assertNotNull(\Drupal::token());
330 }
331
332 /**
333 * Tests the urlGenerator() method.
334 *
335 * @covers ::urlGenerator
336 */
337 public function testUrlGenerator() {
338 $this->setMockContainerService('url_generator');
339 $this->assertNotNull(\Drupal::urlGenerator());
340 }
341
342 /**
343 * Tests the url() method.
344 *
345 * @covers ::url
346 * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
347 */
348 public function testUrl() {
349 $route_parameters = ['test_parameter' => 'test'];
350 $options = ['test_option' => 'test'];
351 $generator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface');
352 $generator->expects($this->once())
353 ->method('generateFromRoute')
354 ->with('test_route', $route_parameters, $options)
355 ->will($this->returnValue('path_string'));
356 $this->setMockContainerService('url_generator', $generator);
357
358 $this->assertInternalType('string', \Drupal::url('test_route', $route_parameters, $options));
359 }
360
361 /**
362 * Tests the linkGenerator() method.
363 *
364 * @covers ::linkGenerator
365 */
366 public function testLinkGenerator() {
367 $this->setMockContainerService('link_generator');
368 $this->assertNotNull(\Drupal::linkGenerator());
369 }
370
371 /**
372 * Tests the l() method.
373 *
374 * @covers ::l
375 * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate()
376 */
377 public function testL() {
378 $route_parameters = ['test_parameter' => 'test'];
379 $options = ['test_option' => 'test'];
380 $generator = $this->getMock('Drupal\Core\Utility\LinkGeneratorInterface');
381 $url = new Url('test_route', $route_parameters, $options);
382 $generator->expects($this->once())
383 ->method('generate')
384 ->with('Test title', $url)
385 ->will($this->returnValue('link_html_string'));
386 $this->setMockContainerService('link_generator', $generator);
387
388 $this->assertInternalType('string', \Drupal::l('Test title', $url));
389 }
390
391 /**
392 * Tests the translation() method.
393 *
394 * @covers ::translation
395 */
396 public function testTranslation() {
397 $this->setMockContainerService('string_translation');
398 $this->assertNotNull(\Drupal::translation());
399 }
400
401 /**
402 * Tests the languageManager() method.
403 *
404 * @covers ::languageManager
405 */
406 public function testLanguageManager() {
407 $this->setMockContainerService('language_manager');
408 $this->assertNotNull(\Drupal::languageManager());
409 }
410
411 /**
412 * Tests the csrfToken() method.
413 *
414 * @covers ::csrfToken
415 */
416 public function testCsrfToken() {
417 $this->setMockContainerService('csrf_token');
418 $this->assertNotNull(\Drupal::csrfToken());
419 }
420
421 /**
422 * Tests the transliteration() method.
423 *
424 * @covers ::transliteration
425 */
426 public function testTransliteration() {
427 $this->setMockContainerService('transliteration');
428 $this->assertNotNull(\Drupal::transliteration());
429 }
430
431 /**
432 * Tests the formBuilder() method.
433 *
434 * @covers ::formBuilder
435 */
436 public function testFormBuilder() {
437 $this->setMockContainerService('form_builder');
438 $this->assertNotNull(\Drupal::formBuilder());
439 }
440
441 /**
442 * Tests the menuTree() method.
443 *
444 * @covers ::menuTree
445 */
446 public function testMenuTree() {
447 $this->setMockContainerService('menu.link_tree');
448 $this->assertNotNull(\Drupal::menuTree());
449 }
450
451 /**
452 * Tests the pathValidator() method.
453 *
454 * @covers ::pathValidator
455 */
456 public function testPathValidator() {
457 $this->setMockContainerService('path.validator');
458 $this->assertNotNull(\Drupal::pathValidator());
459 }
460
461 /**
462 * Tests the accessManager() method.
463 *
464 * @covers ::accessManager
465 */
466 public function testAccessManager() {
467 $this->setMockContainerService('access_manager');
468 $this->assertNotNull(\Drupal::accessManager());
469 }
470
471 /**
472 * Sets up a mock expectation for the container get() method.
473 *
474 * @param string $service_name
475 * The service name to expect for the get() method.
476 * @param mixed $return
477 * The value to return from the mocked container get() method.
478 */
479 protected function setMockContainerService($service_name, $return = NULL) {
480 $expects = $this->container->expects($this->once())
481 ->method('get')
482 ->with($service_name);
483
484 if (isset($return)) {
485 $expects->will($this->returnValue($return));
486 }
487 else {
488 $expects->will($this->returnValue(TRUE));
489 }
490
491 \Drupal::setContainer($this->container);
492 }
493
494 }