3 namespace Drupal\Tests\Core;
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;
12 use Symfony\Component\HttpFoundation\RequestStack;
15 * Tests the Drupal class.
17 * @coversDefaultClass \Drupal
20 class DrupalTest extends UnitTestCase {
25 * @var \Symfony\Component\DependencyInjection\ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject
32 protected function setUp() {
34 $this->container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
40 * Tests the get/setContainer() method.
42 * @covers ::getContainer
44 public function testSetContainer() {
45 \Drupal::setContainer($this->container);
46 $this->assertSame($this->container, \Drupal::getContainer());
50 * @covers ::getContainer
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();
58 * Tests the service() method.
62 public function testService() {
63 $this->setMockContainerService('test_service');
64 $this->assertNotNull(\Drupal::service('test_service'));
68 * Tests the currentUser() method.
70 * @covers ::currentUser
72 public function testCurrentUser() {
73 $this->setMockContainerService('current_user');
74 $this->assertNotNull(\Drupal::currentUser());
78 * Tests the entityManager() method.
80 * @covers ::entityManager
82 public function testEntityManager() {
83 $this->setMockContainerService('entity.manager');
84 $this->assertNotNull(\Drupal::entityManager());
88 * Tests the entityTypeManager() method.
90 * @covers ::entityTypeManager
92 public function testEntityTypeManager() {
93 $this->setMockContainerService('entity_type.manager');
94 $this->assertNotNull(\Drupal::entityTypeManager());
98 * Tests the database() method.
102 public function testDatabase() {
103 $this->setMockContainerService('database');
104 $this->assertNotNull(\Drupal::database());
108 * Tests the cache() method.
112 public function testCache() {
113 $this->setMockContainerService('cache.test');
114 $this->assertNotNull(\Drupal::cache('test'));
118 * Tests the classResolver method.
120 * @covers ::classResolver
122 public function testClassResolver() {
123 $this->setMockContainerService('class_resolver');
124 $this->assertNotNull(\Drupal::classResolver());
128 * Tests the keyValueExpirable() method.
130 * @covers ::keyValueExpirable
132 public function testKeyValueExpirable() {
133 $keyvalue = $this->getMockBuilder('Drupal\Core\KeyValueStore\KeyValueExpirableFactory')
134 ->disableOriginalConstructor()
136 $keyvalue->expects($this->once())
138 ->with('test_collection')
139 ->will($this->returnValue(TRUE));
140 $this->setMockContainerService('keyvalue.expirable', $keyvalue);
142 $this->assertNotNull(\Drupal::keyValueExpirable('test_collection'));
146 * Tests the lock() method.
150 public function testLock() {
151 $this->setMockContainerService('lock');
152 $this->assertNotNull(\Drupal::lock());
156 * Tests the config() method.
160 public function testConfig() {
161 $config = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
162 $config->expects($this->once())
164 ->with('test_config')
165 ->will($this->returnValue(TRUE));
166 $this->setMockContainerService('config.factory', $config);
168 // Test \Drupal::config(), not $this->config().
169 $this->assertNotNull(\Drupal::config('test_config'));
173 * Tests the queue() method.
177 public function testQueue() {
178 $queue = $this->getMockBuilder('Drupal\Core\Queue\QueueFactory')
179 ->disableOriginalConstructor()
181 $queue->expects($this->once())
183 ->with('test_queue', TRUE)
184 ->will($this->returnValue(TRUE));
185 $this->setMockContainerService('queue', $queue);
187 $this->assertNotNull(\Drupal::queue('test_queue', TRUE));
191 * Tests the testRequestStack() method.
193 * @covers ::requestStack
195 public function testRequestStack() {
196 $request_stack = new RequestStack();
197 $this->setMockContainerService('request_stack', $request_stack);
199 $this->assertSame($request_stack, \Drupal::requestStack());
203 * Tests the keyValue() method.
207 public function testKeyValue() {
208 $keyvalue = $this->getMockBuilder('Drupal\Core\KeyValueStore\KeyValueFactory')
209 ->disableOriginalConstructor()
211 $keyvalue->expects($this->once())
213 ->with('test_collection')
214 ->will($this->returnValue(TRUE));
215 $this->setMockContainerService('keyvalue', $keyvalue);
217 $this->assertNotNull(\Drupal::keyValue('test_collection'));
221 * Tests the state() method.
225 public function testState() {
226 $this->setMockContainerService('state');
227 $this->assertNotNull(\Drupal::state());
231 * Tests the httpClient() method.
233 * @covers ::httpClient
235 public function testHttpClient() {
236 $this->setMockContainerService('http_client');
237 $this->assertNotNull(\Drupal::httpClient());
241 * Tests the entityQuery() method.
243 * @covers ::entityQuery
245 public function testEntityQuery() {
246 $query = $this->getMock(QueryInterface::class);
247 $storage = $this->getMock(EntityStorageInterface::class);
249 ->expects($this->once())
252 ->willReturn($query);
254 $entity_type_manager = $this->getMock(EntityTypeManagerInterface::class);
256 ->expects($this->once())
257 ->method('getStorage')
258 ->with('test_entity')
259 ->willReturn($storage);
261 $this->setMockContainerService('entity_type.manager', $entity_type_manager);
263 $this->assertInstanceOf(QueryInterface::class, \Drupal::entityQuery('test_entity', 'OR'));
267 * Tests the entityQueryAggregate() method.
269 * @covers ::entityQueryAggregate
271 public function testEntityQueryAggregate() {
272 $query = $this->getMock(QueryAggregateInterface::class);
273 $storage = $this->getMock(EntityStorageInterface::class);
275 ->expects($this->once())
276 ->method('getAggregateQuery')
278 ->willReturn($query);
280 $entity_type_manager = $this->getMock(EntityTypeManagerInterface::class);
282 ->expects($this->once())
283 ->method('getStorage')
284 ->with('test_entity')
285 ->willReturn($storage);
287 $this->setMockContainerService('entity_type.manager', $entity_type_manager);
289 $this->assertInstanceOf(QueryAggregateInterface::class, \Drupal::entityQueryAggregate('test_entity', 'OR'));
293 * Tests the flood() method.
297 public function testFlood() {
298 $this->setMockContainerService('flood');
299 $this->assertNotNull(\Drupal::flood());
303 * Tests the moduleHandler() method.
305 * @covers ::moduleHandler
307 public function testModuleHandler() {
308 $this->setMockContainerService('module_handler');
309 $this->assertNotNull(\Drupal::moduleHandler());
313 * Tests the typedDataManager() method.
315 * @covers ::typedDataManager
317 public function testTypedDataManager() {
318 $this->setMockContainerService('typed_data_manager');
319 $this->assertNotNull(\Drupal::typedDataManager());
323 * Tests the token() method.
327 public function testToken() {
328 $this->setMockContainerService('token');
329 $this->assertNotNull(\Drupal::token());
333 * Tests the urlGenerator() method.
335 * @covers ::urlGenerator
337 public function testUrlGenerator() {
338 $this->setMockContainerService('url_generator');
339 $this->assertNotNull(\Drupal::urlGenerator());
343 * Tests the url() method.
346 * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
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);
358 $this->assertInternalType('string', \Drupal::url('test_route', $route_parameters, $options));
362 * Tests the linkGenerator() method.
364 * @covers ::linkGenerator
366 public function testLinkGenerator() {
367 $this->setMockContainerService('link_generator');
368 $this->assertNotNull(\Drupal::linkGenerator());
372 * Tests the l() method.
375 * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate()
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())
384 ->with('Test title', $url)
385 ->will($this->returnValue('link_html_string'));
386 $this->setMockContainerService('link_generator', $generator);
388 $this->assertInternalType('string', \Drupal::l('Test title', $url));
392 * Tests the translation() method.
394 * @covers ::translation
396 public function testTranslation() {
397 $this->setMockContainerService('string_translation');
398 $this->assertNotNull(\Drupal::translation());
402 * Tests the languageManager() method.
404 * @covers ::languageManager
406 public function testLanguageManager() {
407 $this->setMockContainerService('language_manager');
408 $this->assertNotNull(\Drupal::languageManager());
412 * Tests the csrfToken() method.
414 * @covers ::csrfToken
416 public function testCsrfToken() {
417 $this->setMockContainerService('csrf_token');
418 $this->assertNotNull(\Drupal::csrfToken());
422 * Tests the transliteration() method.
424 * @covers ::transliteration
426 public function testTransliteration() {
427 $this->setMockContainerService('transliteration');
428 $this->assertNotNull(\Drupal::transliteration());
432 * Tests the formBuilder() method.
434 * @covers ::formBuilder
436 public function testFormBuilder() {
437 $this->setMockContainerService('form_builder');
438 $this->assertNotNull(\Drupal::formBuilder());
442 * Tests the menuTree() method.
446 public function testMenuTree() {
447 $this->setMockContainerService('menu.link_tree');
448 $this->assertNotNull(\Drupal::menuTree());
452 * Tests the pathValidator() method.
454 * @covers ::pathValidator
456 public function testPathValidator() {
457 $this->setMockContainerService('path.validator');
458 $this->assertNotNull(\Drupal::pathValidator());
462 * Tests the accessManager() method.
464 * @covers ::accessManager
466 public function testAccessManager() {
467 $this->setMockContainerService('access_manager');
468 $this->assertNotNull(\Drupal::accessManager());
472 * Sets up a mock expectation for the container get() method.
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.
479 protected function setMockContainerService($service_name, $return = NULL) {
480 $expects = $this->container->expects($this->once())
482 ->with($service_name);
484 if (isset($return)) {
485 $expects->will($this->returnValue($return));
488 $expects->will($this->returnValue(TRUE));
491 \Drupal::setContainer($this->container);