Chris@0: container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder') Chris@0: ->setMethods(['get']) Chris@0: ->getMock(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the get/setContainer() method. Chris@0: * Chris@0: * @covers ::getContainer Chris@0: */ Chris@0: public function testSetContainer() { Chris@0: \Drupal::setContainer($this->container); Chris@0: $this->assertSame($this->container, \Drupal::getContainer()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @covers ::getContainer Chris@0: */ Chris@0: public function testGetContainerException() { Chris@0: $this->setExpectedException(ContainerNotInitializedException::class, '\Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.'); Chris@0: \Drupal::getContainer(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the service() method. Chris@0: * Chris@0: * @covers ::service Chris@0: */ Chris@0: public function testService() { Chris@0: $this->setMockContainerService('test_service'); Chris@0: $this->assertNotNull(\Drupal::service('test_service')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the currentUser() method. Chris@0: * Chris@0: * @covers ::currentUser Chris@0: */ Chris@0: public function testCurrentUser() { Chris@0: $this->setMockContainerService('current_user'); Chris@0: $this->assertNotNull(\Drupal::currentUser()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the entityManager() method. Chris@0: * Chris@0: * @covers ::entityManager Chris@0: */ Chris@0: public function testEntityManager() { Chris@0: $this->setMockContainerService('entity.manager'); Chris@0: $this->assertNotNull(\Drupal::entityManager()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the entityTypeManager() method. Chris@0: * Chris@0: * @covers ::entityTypeManager Chris@0: */ Chris@0: public function testEntityTypeManager() { Chris@0: $this->setMockContainerService('entity_type.manager'); Chris@0: $this->assertNotNull(\Drupal::entityTypeManager()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the database() method. Chris@0: * Chris@0: * @covers ::database Chris@0: */ Chris@0: public function testDatabase() { Chris@0: $this->setMockContainerService('database'); Chris@0: $this->assertNotNull(\Drupal::database()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the cache() method. Chris@0: * Chris@0: * @covers ::cache Chris@0: */ Chris@0: public function testCache() { Chris@0: $this->setMockContainerService('cache.test'); Chris@0: $this->assertNotNull(\Drupal::cache('test')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the classResolver method. Chris@0: * Chris@0: * @covers ::classResolver Chris@0: */ Chris@0: public function testClassResolver() { Chris@17: $class_resolver = $this->prophesize(ClassResolverInterface::class); Chris@17: $this->setMockContainerService('class_resolver', $class_resolver->reveal()); Chris@17: $this->assertInstanceOf(ClassResolverInterface::class, \Drupal::classResolver()); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests the classResolver method when called with a class. Chris@17: * Chris@17: * @covers ::classResolver Chris@17: */ Chris@17: public function testClassResolverWithClass() { Chris@17: $class_resolver = $this->prophesize(ClassResolverInterface::class); Chris@17: $class_resolver->getInstanceFromDefinition(static::class)->willReturn($this); Chris@17: $this->setMockContainerService('class_resolver', $class_resolver->reveal()); Chris@17: $this->assertSame($this, \Drupal::classResolver(static::class)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the keyValueExpirable() method. Chris@0: * Chris@0: * @covers ::keyValueExpirable Chris@0: */ Chris@0: public function testKeyValueExpirable() { Chris@0: $keyvalue = $this->getMockBuilder('Drupal\Core\KeyValueStore\KeyValueExpirableFactory') Chris@0: ->disableOriginalConstructor() Chris@0: ->getMock(); Chris@0: $keyvalue->expects($this->once()) Chris@0: ->method('get') Chris@0: ->with('test_collection') Chris@0: ->will($this->returnValue(TRUE)); Chris@0: $this->setMockContainerService('keyvalue.expirable', $keyvalue); Chris@0: Chris@0: $this->assertNotNull(\Drupal::keyValueExpirable('test_collection')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the lock() method. Chris@0: * Chris@0: * @covers ::lock Chris@0: */ Chris@0: public function testLock() { Chris@0: $this->setMockContainerService('lock'); Chris@0: $this->assertNotNull(\Drupal::lock()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the config() method. Chris@0: * Chris@0: * @covers ::config Chris@0: */ Chris@0: public function testConfig() { Chris@0: $config = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface'); Chris@0: $config->expects($this->once()) Chris@0: ->method('get') Chris@0: ->with('test_config') Chris@0: ->will($this->returnValue(TRUE)); Chris@0: $this->setMockContainerService('config.factory', $config); Chris@0: Chris@0: // Test \Drupal::config(), not $this->config(). Chris@0: $this->assertNotNull(\Drupal::config('test_config')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the queue() method. Chris@0: * Chris@0: * @covers ::queue Chris@0: */ Chris@0: public function testQueue() { Chris@0: $queue = $this->getMockBuilder('Drupal\Core\Queue\QueueFactory') Chris@0: ->disableOriginalConstructor() Chris@0: ->getMock(); Chris@0: $queue->expects($this->once()) Chris@0: ->method('get') Chris@0: ->with('test_queue', TRUE) Chris@0: ->will($this->returnValue(TRUE)); Chris@0: $this->setMockContainerService('queue', $queue); Chris@0: Chris@0: $this->assertNotNull(\Drupal::queue('test_queue', TRUE)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the testRequestStack() method. Chris@0: * Chris@0: * @covers ::requestStack Chris@0: */ Chris@0: public function testRequestStack() { Chris@0: $request_stack = new RequestStack(); Chris@0: $this->setMockContainerService('request_stack', $request_stack); Chris@0: Chris@0: $this->assertSame($request_stack, \Drupal::requestStack()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the keyValue() method. Chris@0: * Chris@0: * @covers ::keyValue Chris@0: */ Chris@0: public function testKeyValue() { Chris@0: $keyvalue = $this->getMockBuilder('Drupal\Core\KeyValueStore\KeyValueFactory') Chris@0: ->disableOriginalConstructor() Chris@0: ->getMock(); Chris@0: $keyvalue->expects($this->once()) Chris@0: ->method('get') Chris@0: ->with('test_collection') Chris@0: ->will($this->returnValue(TRUE)); Chris@0: $this->setMockContainerService('keyvalue', $keyvalue); Chris@0: Chris@0: $this->assertNotNull(\Drupal::keyValue('test_collection')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the state() method. Chris@0: * Chris@0: * @covers ::state Chris@0: */ Chris@0: public function testState() { Chris@0: $this->setMockContainerService('state'); Chris@0: $this->assertNotNull(\Drupal::state()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the httpClient() method. Chris@0: * Chris@0: * @covers ::httpClient Chris@0: */ Chris@0: public function testHttpClient() { Chris@0: $this->setMockContainerService('http_client'); Chris@0: $this->assertNotNull(\Drupal::httpClient()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the entityQuery() method. Chris@0: * Chris@0: * @covers ::entityQuery Chris@0: */ Chris@0: public function testEntityQuery() { Chris@0: $query = $this->getMock(QueryInterface::class); Chris@0: $storage = $this->getMock(EntityStorageInterface::class); Chris@0: $storage Chris@0: ->expects($this->once()) Chris@0: ->method('getQuery') Chris@0: ->with('OR') Chris@0: ->willReturn($query); Chris@0: Chris@0: $entity_type_manager = $this->getMock(EntityTypeManagerInterface::class); Chris@0: $entity_type_manager Chris@0: ->expects($this->once()) Chris@0: ->method('getStorage') Chris@0: ->with('test_entity') Chris@0: ->willReturn($storage); Chris@0: Chris@0: $this->setMockContainerService('entity_type.manager', $entity_type_manager); Chris@0: Chris@0: $this->assertInstanceOf(QueryInterface::class, \Drupal::entityQuery('test_entity', 'OR')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the entityQueryAggregate() method. Chris@0: * Chris@0: * @covers ::entityQueryAggregate Chris@0: */ Chris@0: public function testEntityQueryAggregate() { Chris@0: $query = $this->getMock(QueryAggregateInterface::class); Chris@0: $storage = $this->getMock(EntityStorageInterface::class); Chris@0: $storage Chris@0: ->expects($this->once()) Chris@0: ->method('getAggregateQuery') Chris@0: ->with('OR') Chris@0: ->willReturn($query); Chris@0: Chris@0: $entity_type_manager = $this->getMock(EntityTypeManagerInterface::class); Chris@0: $entity_type_manager Chris@0: ->expects($this->once()) Chris@0: ->method('getStorage') Chris@0: ->with('test_entity') Chris@0: ->willReturn($storage); Chris@0: Chris@0: $this->setMockContainerService('entity_type.manager', $entity_type_manager); Chris@0: Chris@0: $this->assertInstanceOf(QueryAggregateInterface::class, \Drupal::entityQueryAggregate('test_entity', 'OR')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the flood() method. Chris@0: * Chris@0: * @covers ::flood Chris@0: */ Chris@0: public function testFlood() { Chris@0: $this->setMockContainerService('flood'); Chris@0: $this->assertNotNull(\Drupal::flood()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the moduleHandler() method. Chris@0: * Chris@0: * @covers ::moduleHandler Chris@0: */ Chris@0: public function testModuleHandler() { Chris@0: $this->setMockContainerService('module_handler'); Chris@0: $this->assertNotNull(\Drupal::moduleHandler()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the typedDataManager() method. Chris@0: * Chris@0: * @covers ::typedDataManager Chris@0: */ Chris@0: public function testTypedDataManager() { Chris@0: $this->setMockContainerService('typed_data_manager'); Chris@0: $this->assertNotNull(\Drupal::typedDataManager()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the token() method. Chris@0: * Chris@0: * @covers ::token Chris@0: */ Chris@0: public function testToken() { Chris@0: $this->setMockContainerService('token'); Chris@0: $this->assertNotNull(\Drupal::token()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the urlGenerator() method. Chris@0: * Chris@0: * @covers ::urlGenerator Chris@0: */ Chris@0: public function testUrlGenerator() { Chris@0: $this->setMockContainerService('url_generator'); Chris@0: $this->assertNotNull(\Drupal::urlGenerator()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the url() method. Chris@0: * Chris@0: * @covers ::url Chris@0: * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() Chris@18: * Chris@18: * @group legacy Chris@18: * @expectedDeprecation Drupal::url() is deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Instead create a \Drupal\Core\Url object directly, for example using Url::fromRoute() Chris@0: */ Chris@0: public function testUrl() { Chris@0: $route_parameters = ['test_parameter' => 'test']; Chris@0: $options = ['test_option' => 'test']; Chris@0: $generator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface'); Chris@0: $generator->expects($this->once()) Chris@0: ->method('generateFromRoute') Chris@0: ->with('test_route', $route_parameters, $options) Chris@0: ->will($this->returnValue('path_string')); Chris@0: $this->setMockContainerService('url_generator', $generator); Chris@0: Chris@0: $this->assertInternalType('string', \Drupal::url('test_route', $route_parameters, $options)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the linkGenerator() method. Chris@0: * Chris@0: * @covers ::linkGenerator Chris@0: */ Chris@0: public function testLinkGenerator() { Chris@0: $this->setMockContainerService('link_generator'); Chris@0: $this->assertNotNull(\Drupal::linkGenerator()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the l() method. Chris@0: * Chris@0: * @covers ::l Chris@0: * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate() Chris@0: */ Chris@0: public function testL() { Chris@0: $route_parameters = ['test_parameter' => 'test']; Chris@0: $options = ['test_option' => 'test']; Chris@0: $generator = $this->getMock('Drupal\Core\Utility\LinkGeneratorInterface'); Chris@0: $url = new Url('test_route', $route_parameters, $options); Chris@0: $generator->expects($this->once()) Chris@0: ->method('generate') Chris@0: ->with('Test title', $url) Chris@0: ->will($this->returnValue('link_html_string')); Chris@0: $this->setMockContainerService('link_generator', $generator); Chris@0: Chris@0: $this->assertInternalType('string', \Drupal::l('Test title', $url)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the translation() method. Chris@0: * Chris@0: * @covers ::translation Chris@0: */ Chris@0: public function testTranslation() { Chris@0: $this->setMockContainerService('string_translation'); Chris@0: $this->assertNotNull(\Drupal::translation()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the languageManager() method. Chris@0: * Chris@0: * @covers ::languageManager Chris@0: */ Chris@0: public function testLanguageManager() { Chris@0: $this->setMockContainerService('language_manager'); Chris@0: $this->assertNotNull(\Drupal::languageManager()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the csrfToken() method. Chris@0: * Chris@0: * @covers ::csrfToken Chris@0: */ Chris@0: public function testCsrfToken() { Chris@0: $this->setMockContainerService('csrf_token'); Chris@0: $this->assertNotNull(\Drupal::csrfToken()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the transliteration() method. Chris@0: * Chris@0: * @covers ::transliteration Chris@0: */ Chris@0: public function testTransliteration() { Chris@0: $this->setMockContainerService('transliteration'); Chris@0: $this->assertNotNull(\Drupal::transliteration()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the formBuilder() method. Chris@0: * Chris@0: * @covers ::formBuilder Chris@0: */ Chris@0: public function testFormBuilder() { Chris@0: $this->setMockContainerService('form_builder'); Chris@0: $this->assertNotNull(\Drupal::formBuilder()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the menuTree() method. Chris@0: * Chris@0: * @covers ::menuTree Chris@0: */ Chris@0: public function testMenuTree() { Chris@0: $this->setMockContainerService('menu.link_tree'); Chris@0: $this->assertNotNull(\Drupal::menuTree()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the pathValidator() method. Chris@0: * Chris@0: * @covers ::pathValidator Chris@0: */ Chris@0: public function testPathValidator() { Chris@0: $this->setMockContainerService('path.validator'); Chris@0: $this->assertNotNull(\Drupal::pathValidator()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the accessManager() method. Chris@0: * Chris@0: * @covers ::accessManager Chris@0: */ Chris@0: public function testAccessManager() { Chris@0: $this->setMockContainerService('access_manager'); Chris@0: $this->assertNotNull(\Drupal::accessManager()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets up a mock expectation for the container get() method. Chris@0: * Chris@0: * @param string $service_name Chris@0: * The service name to expect for the get() method. Chris@0: * @param mixed $return Chris@0: * The value to return from the mocked container get() method. Chris@0: */ Chris@0: protected function setMockContainerService($service_name, $return = NULL) { Chris@0: $expects = $this->container->expects($this->once()) Chris@0: ->method('get') Chris@0: ->with($service_name); Chris@0: Chris@0: if (isset($return)) { Chris@0: $expects->will($this->returnValue($return)); Chris@0: } Chris@0: else { Chris@0: $expects->will($this->returnValue(TRUE)); Chris@0: } Chris@0: Chris@0: \Drupal::setContainer($this->container); Chris@0: } Chris@0: Chris@0: }