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