diff --git a/core/tests/Drupal/Tests/Core/DrupalTest.php b/core/tests/Drupal/Tests/Core/DrupalTest.php new file mode 100644 index 0000000..d506b4b --- /dev/null +++ b/core/tests/Drupal/Tests/Core/DrupalTest.php @@ -0,0 +1,357 @@ + 'Drupal test', + 'description' => 'Tests the Drupal class.', + 'group' => 'System' + ); + } + + public function setUp() { + $this->container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder') + ->setMethods(array('get')) + ->getMock(); + } + + /** + * Tests the get/setContainer() method. + */ + public function testSetContainer() { + \Drupal::setContainer($this->container); + $this->assertSame($this->container, \Drupal::getContainer()); + } + + /** + * Tests the service() method. + */ + public function testService() { + $this->setMockContainerService('test_service'); + $this->assertNotNull(\Drupal::service('test_service')); + } + + /** + * Tests the service() method. + */ + public function testRequest() { + $this->setMockContainerService('request'); + $this->assertNotNull(\Drupal::request()); + } + + /** + * Tests the currentUser() method. + */ + public function testCurrentUser() { + $this->setMockContainerService('current_user'); + $this->assertNotNull(\Drupal::currentUser()); + } + + /** + * Tests the entityManager() method. + */ + public function testEntityManager() { + $this->setMockContainerService('entity.manager'); + $this->assertNotNull(\Drupal::entityManager()); + } + + /** + * Tests the database() method. + */ + public function testDatabase() { + $this->setMockContainerService('database'); + $this->assertNotNull(\Drupal::database()); + } + + /** + * Tests the service() method. + */ + public function testCache() { + $this->setMockContainerService('cache.test'); + $this->assertNotNull(\Drupal::cache('test')); + } + + /** + * Tests the keyValueExpirable() method. + */ + public function testKeyValueExpirable() { + $keyvalue = $this->getMockBuilder('Drupal\Core\KeyValueStore\KeyValueExpirableFactory') + ->disableOriginalConstructor() + ->getMock(); + $keyvalue->expects($this->once()) + ->method('get') + ->with('test_collection') + ->will($this->returnValue(TRUE)); + $this->setMockContainerService('keyvalue.expirable', $keyvalue); + + $this->assertNotNull(\Drupal::keyValueExpirable('test_collection')); + } + + /** + * Tests the lock() method. + */ + public function testLock() { + $this->setMockContainerService('lock'); + $this->assertNotNull(\Drupal::lock()); + } + + /** + * Tests the config() method. + */ + public function testConfig() { + $config = $this->getMockBuilder('Drupal\Core\Config\ConfigFactory') + ->disableOriginalConstructor() + ->getMock(); + $config->expects($this->once()) + ->method('get') + ->with('test_config') + ->will($this->returnValue(TRUE)); + $this->setMockContainerService('config.factory', $config); + + $this->assertNotNull(\Drupal::config('test_config')); + } + + /** + * Tests the queue() method. + */ + public function testQueue() { + $queue = $this->getMockBuilder('Drupal\Core\Queue\QueueFactory') + ->disableOriginalConstructor() + ->getMock(); + $queue->expects($this->once()) + ->method('get') + ->with('test_queue', TRUE) + ->will($this->returnValue(TRUE)); + $this->setMockContainerService('queue', $queue); + + $this->assertNotNull(\Drupal::queue('test_queue', TRUE)); + } + + /** + * Tests the keyValue() method. + */ + public function testKeyValue() { + $keyvalue = $this->getMockBuilder('Drupal\Core\KeyValueStore\KeyValueFactory') + ->disableOriginalConstructor() + ->getMock(); + $keyvalue->expects($this->once()) + ->method('get') + ->with('test_collection') + ->will($this->returnValue(TRUE)); + $this->setMockContainerService('keyvalue', $keyvalue); + + $this->assertNotNull(\Drupal::keyValue('test_collection')); + } + + /** + * Tests the state() method. + */ + public function testState() { + $this->setMockContainerService('state'); + $this->assertNotNull(\Drupal::state()); + } + + /** + * Tests the httpClient() method. + */ + public function testHttpClient() { + $this->setMockContainerService('http_default_client'); + $this->assertNotNull(\Drupal::httpClient()); + } + + /** + * Tests the entityQuery() method. + */ + public function testEntityQuery() { + $query = $this->getMockBuilder('Drupal\Core\Entity\Query\QueryFactory') + ->disableOriginalConstructor() + ->getMock(); + $query->expects($this->once()) + ->method('get') + ->with('test_entity', 'OR') + ->will($this->returnValue(TRUE)); + $this->setMockContainerService('entity.query', $query); + + $this->assertNotNull(\Drupal::entityQuery('test_entity', 'OR')); + } + + /** + * Tests the entityQueryAggregate() method. + */ + public function testEntityQueryAggregate() { + $query = $this->getMockBuilder('Drupal\Core\Entity\Query\QueryFactory') + ->disableOriginalConstructor() + ->getMock(); + $query->expects($this->once()) + ->method('getAggregate') + ->with('test_entity', 'OR') + ->will($this->returnValue(TRUE)); + $this->setMockContainerService('entity.query', $query); + + $this->assertNotNull(\Drupal::entityQueryAggregate('test_entity', 'OR')); + } + + /** + * Tests the flood() method. + */ + public function testFlood() { + $this->setMockContainerService('flood'); + $this->assertNotNull(\Drupal::flood()); + } + + /** + * Tests the moduleHandler() method. + */ + public function testModuleHandler() { + $this->setMockContainerService('module_handler'); + $this->assertNotNull(\Drupal::moduleHandler()); + } + + /** + * Tests the typedData() method. + */ + public function testTypedData() { + $this->setMockContainerService('typed_data'); + $this->assertNotNull(\Drupal::typedData()); + } + + /** + * Tests the token() method. + */ + public function testToken() { + $this->setMockContainerService('token'); + $this->assertNotNull(\Drupal::token()); + } + + /** + * Tests the urlGenerator() method. + */ + public function testUrlGenerator() { + $this->setMockContainerService('url_generator'); + $this->assertNotNull(\Drupal::urlGenerator()); + } + + /** + * Tests the url() method. + */ + public function testUrl() { + $route_parameters = array('test_parameter' => 'test'); + $options = array('test_option' => 'test'); + $generator = $this->getMockBuilder('Drupal\Core\Routing\UrlGeneratorInterface') + ->disableOriginalConstructor() + ->getMock(); + $generator->expects($this->once()) + ->method('generateFromRoute') + ->with('test_route', $route_parameters, $options) + ->will($this->returnValue(TRUE)); + $this->setMockContainerService('url_generator', $generator); + + $this->assertNotNull(\Drupal::url('test_route', $route_parameters, $options)); + } + + /** + * Tests the linkGenerator() method. + */ + public function testLinkGenerator() { + $this->setMockContainerService('link_generator'); + $this->assertNotNull(\Drupal::linkGenerator()); + } + + /** + * Tests the l() method. + */ + public function testL() { + $route_parameters = array('test_parameter' => 'test'); + $options = array('test_option' => 'test'); + $generator = $this->getMockBuilder('Drupal\Core\Utility\LinkGeneratorInterface') + ->disableOriginalConstructor() + ->getMock(); + $generator->expects($this->once()) + ->method('generate') + ->with('Test title', 'test_route', $route_parameters, $options) + ->will($this->returnValue(TRUE)); + $this->setMockContainerService('link_generator', $generator); + + $this->assertNotNull(\Drupal::l('Test title', 'test_route', $route_parameters, $options)); + } + + /** + * Tests the translation() method. + */ + public function testTranslation() { + $this->setMockContainerService('string_translation'); + $this->assertNotNull(\Drupal::translation()); + } + + /** + * Tests the languageManager() method. + */ + public function testLanguageManager() { + $this->setMockContainerService('language_manager'); + $this->assertNotNull(\Drupal::languageManager()); + } + + /** + * Tests the csrfToken() method. + */ + public function testCsrfToken() { + $this->setMockContainerService('csrf_token'); + $this->assertNotNull(\Drupal::csrfToken()); + } + + /** + * Tests the transliteration() method. + */ + public function testTransliteration() { + $this->setMockContainerService('transliteration'); + $this->assertNotNull(\Drupal::transliteration()); + } + + /** + * Sets up a mock expectation for the container get() method. + * + * @param string $service_name + * The service name to expect for the get() method. + * @param mixed $return + * The value to return from the mocked container get() method. + */ + protected function setMockContainerService($service_name, $return = NULL) { + $expects = $this->container->expects($this->once()) + ->method('get') + ->with($service_name); + + if (isset($return)) { + $expects->will($this->returnValue($return)); + } + else { + $expects->will($this->returnValue(TRUE)); + } + + \Drupal::setContainer($this->container); + } + +} diff --git a/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerUnitTest.php b/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerUnitTest.php index e865f2e..908b07c 100644 --- a/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Extension/ModuleHandlerUnitTest.php @@ -37,16 +37,12 @@ function setUp() { /** * Tests loading of an include from a nonexistent module. + * + * Attepmting to load a file from a non-existent module should + * return FALSE. */ public function testLoadInclude() { - $notice_thrown = FALSE; - try { - $this->moduleHandler->loadInclude('foo', 'inc'); - } - catch (PHPUnit_Framework_Error_Notice $e) { - $notice_thrown = TRUE; - } - $this->assertFalse($notice_thrown, 'A notice was not thrown for a nonexistent module.'); + $this->assertFalse($this->moduleHandler->loadInclude('foo', 'inc')); } }