You are here

class DrupalTest in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/DrupalTest.php \Drupal\Tests\Core\DrupalTest

Tests the Drupal class.

@coversDefaultClass \Drupal @group DrupalTest

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase

Expanded class hierarchy of DrupalTest

File

core/tests/Drupal/Tests/Core/DrupalTest.php, line 20
Contains \Drupal\Tests\Core\DrupalTest.

Namespace

Drupal\Tests\Core
View source
class DrupalTest extends UnitTestCase {

  /**
   * The mock container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $container;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->container = $this
      ->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerBuilder')
      ->setMethods(array(
      'get',
    ))
      ->getMock();
  }

  /**
   * Tests the get/setContainer() method.
   *
   * @covers ::getContainer
   */
  public function testSetContainer() {
    \Drupal::setContainer($this->container);
    $this
      ->assertSame($this->container, \Drupal::getContainer());
  }

  /**
   * @covers ::getContainer
   *
   * @expectedException \Drupal\Core\DependencyInjection\ContainerNotInitializedException
   * @expectedExceptionMessage \Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.
   */
  public function testGetContainerException() {
    \Drupal::getContainer();
  }

  /**
   * Tests the service() method.
   *
   * @covers ::service
   */
  public function testService() {
    $this
      ->setMockContainerService('test_service');
    $this
      ->assertNotNull(\Drupal::service('test_service'));
  }

  /**
   * Tests the currentUser() method.
   *
   * @covers ::currentUser
   */
  public function testCurrentUser() {
    $this
      ->setMockContainerService('current_user');
    $this
      ->assertNotNull(\Drupal::currentUser());
  }

  /**
   * Tests the entityManager() method.
   *
   * @covers ::entityManager
   */
  public function testEntityManager() {
    $this
      ->setMockContainerService('entity.manager');
    $this
      ->assertNotNull(\Drupal::entityManager());
  }

  /**
   * Tests the entityTypeManager() method.
   *
   * @covers ::entityTypeManager
   */
  public function testEntityTypeManager() {
    $this
      ->setMockContainerService('entity_type.manager');
    $this
      ->assertNotNull(\Drupal::entityTypeManager());
  }

  /**
   * Tests the database() method.
   *
   * @covers ::database
   */
  public function testDatabase() {
    $this
      ->setMockContainerService('database');
    $this
      ->assertNotNull(\Drupal::database());
  }

  /**
   * Tests the service() method.
   *
   * @covers ::cache
   */
  public function testCache() {
    $this
      ->setMockContainerService('cache.test');
    $this
      ->assertNotNull(\Drupal::cache('test'));
  }

  /**
   * Tests the keyValueExpirable() method.
   *
   * @covers ::keyValueExpirable
   */
  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.
   *
   * @covers ::lock
   */
  public function testLock() {
    $this
      ->setMockContainerService('lock');
    $this
      ->assertNotNull(\Drupal::lock());
  }

  /**
   * Tests the config() method.
   *
   * @covers ::config
   */
  public function testConfig() {
    $config = $this
      ->getMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
    $config
      ->expects($this
      ->once())
      ->method('get')
      ->with('test_config')
      ->will($this
      ->returnValue(TRUE));
    $this
      ->setMockContainerService('config.factory', $config);

    // Test \Drupal::config(), not $this->config().
    $this
      ->assertNotNull(\Drupal::config('test_config'));
  }

  /**
   * Tests the queue() method.
   *
   * @covers ::queue
   */
  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 testRequestStack() method.
   *
   * @covers ::requestStack
   */
  public function testRequestStack() {
    $request_stack = new RequestStack();
    $this
      ->setMockContainerService('request_stack', $request_stack);
    $this
      ->assertSame($request_stack, \Drupal::requestStack());
  }

  /**
   * Tests the keyValue() method.
   *
   * @covers ::keyValue
   */
  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.
   *
   * @covers ::state
   */
  public function testState() {
    $this
      ->setMockContainerService('state');
    $this
      ->assertNotNull(\Drupal::state());
  }

  /**
   * Tests the httpClient() method.
   *
   * @covers ::httpClient
   */
  public function testHttpClient() {
    $this
      ->setMockContainerService('http_client');
    $this
      ->assertNotNull(\Drupal::httpClient());
  }

  /**
   * Tests the entityQuery() method.
   *
   * @covers ::entityQuery
   */
  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.
   *
   * @covers ::entityQueryAggregate
   */
  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.
   *
   * @covers ::flood
   */
  public function testFlood() {
    $this
      ->setMockContainerService('flood');
    $this
      ->assertNotNull(\Drupal::flood());
  }

  /**
   * Tests the moduleHandler() method.
   *
   * @covers ::moduleHandler
   */
  public function testModuleHandler() {
    $this
      ->setMockContainerService('module_handler');
    $this
      ->assertNotNull(\Drupal::moduleHandler());
  }

  /**
   * Tests the typedDataManager() method.
   *
   * @covers ::typedDataManager
   */
  public function testTypedDataManager() {
    $this
      ->setMockContainerService('typed_data_manager');
    $this
      ->assertNotNull(\Drupal::typedDataManager());
  }

  /**
   * Tests the token() method.
   *
   * @covers ::token
   */
  public function testToken() {
    $this
      ->setMockContainerService('token');
    $this
      ->assertNotNull(\Drupal::token());
  }

  /**
   * Tests the urlGenerator() method.
   *
   * @covers ::urlGenerator
   */
  public function testUrlGenerator() {
    $this
      ->setMockContainerService('url_generator');
    $this
      ->assertNotNull(\Drupal::urlGenerator());
  }

  /**
   * Tests the url() method.
   *
   * @covers ::url
   * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
   */
  public function testUrl() {
    $route_parameters = array(
      'test_parameter' => 'test',
    );
    $options = array(
      'test_option' => 'test',
    );
    $generator = $this
      ->getMock('Drupal\\Core\\Routing\\UrlGeneratorInterface');
    $generator
      ->expects($this
      ->once())
      ->method('generateFromRoute')
      ->with('test_route', $route_parameters, $options)
      ->will($this
      ->returnValue('path_string'));
    $this
      ->setMockContainerService('url_generator', $generator);
    $this
      ->assertInternalType('string', \Drupal::url('test_route', $route_parameters, $options));
  }

  /**
   * Tests the linkGenerator() method.
   *
   * @covers ::linkGenerator
   */
  public function testLinkGenerator() {
    $this
      ->setMockContainerService('link_generator');
    $this
      ->assertNotNull(\Drupal::linkGenerator());
  }

  /**
   * Tests the l() method.
   *
   * @covers ::l
   * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate()
   */
  public function testL() {
    $route_parameters = array(
      'test_parameter' => 'test',
    );
    $options = array(
      'test_option' => 'test',
    );
    $generator = $this
      ->getMock('Drupal\\Core\\Utility\\LinkGeneratorInterface');
    $url = new Url('test_route', $route_parameters, $options);
    $generator
      ->expects($this
      ->once())
      ->method('generate')
      ->with('Test title', $url)
      ->will($this
      ->returnValue('link_html_string'));
    $this
      ->setMockContainerService('link_generator', $generator);
    $this
      ->assertInternalType('string', \Drupal::l('Test title', $url));
  }

  /**
   * Tests the translation() method.
   *
   * @covers ::translation
   */
  public function testTranslation() {
    $this
      ->setMockContainerService('string_translation');
    $this
      ->assertNotNull(\Drupal::translation());
  }

  /**
   * Tests the languageManager() method.
   *
   * @covers ::languageManager
   */
  public function testLanguageManager() {
    $this
      ->setMockContainerService('language_manager');
    $this
      ->assertNotNull(\Drupal::languageManager());
  }

  /**
   * Tests the csrfToken() method.
   *
   * @covers ::csrfToken
   */
  public function testCsrfToken() {
    $this
      ->setMockContainerService('csrf_token');
    $this
      ->assertNotNull(\Drupal::csrfToken());
  }

  /**
   * Tests the transliteration() method.
   *
   * @covers ::transliteration
   */
  public function testTransliteration() {
    $this
      ->setMockContainerService('transliteration');
    $this
      ->assertNotNull(\Drupal::transliteration());
  }

  /**
   * Tests the formBuilder() method.
   *
   * @covers ::formBuilder
   */
  public function testFormBuilder() {
    $this
      ->setMockContainerService('form_builder');
    $this
      ->assertNotNull(\Drupal::formBuilder());
  }

  /**
   * Tests the menuTree() method.
   *
   * @covers ::menuTree
   */
  public function testMenuTree() {
    $this
      ->setMockContainerService('menu.link_tree');
    $this
      ->assertNotNull(\Drupal::menuTree());
  }

  /**
   * Tests the pathValidator() method.
   *
   * @covers ::pathValidator
   */
  public function testPathValidator() {
    $this
      ->setMockContainerService('path.validator');
    $this
      ->assertNotNull(\Drupal::pathValidator());
  }

  /**
   * Tests the accessManager() method.
   *
   * @covers ::accessManager
   */
  public function testAccessManager() {
    $this
      ->setMockContainerService('access_manager');
    $this
      ->assertNotNull(\Drupal::accessManager());
  }

  /**
   * 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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalTest::$container protected property The mock container.
DrupalTest::setMockContainerService protected function Sets up a mock expectation for the container get() method.
DrupalTest::setUp protected function Overrides UnitTestCase::setUp
DrupalTest::testAccessManager public function Tests the accessManager() method.
DrupalTest::testCache public function Tests the service() method.
DrupalTest::testConfig public function Tests the config() method.
DrupalTest::testCsrfToken public function Tests the csrfToken() method.
DrupalTest::testCurrentUser public function Tests the currentUser() method.
DrupalTest::testDatabase public function Tests the database() method.
DrupalTest::testEntityManager public function Tests the entityManager() method.
DrupalTest::testEntityQuery public function Tests the entityQuery() method.
DrupalTest::testEntityQueryAggregate public function Tests the entityQueryAggregate() method.
DrupalTest::testEntityTypeManager public function Tests the entityTypeManager() method.
DrupalTest::testFlood public function Tests the flood() method.
DrupalTest::testFormBuilder public function Tests the formBuilder() method.
DrupalTest::testGetContainerException public function @covers ::getContainer
DrupalTest::testHttpClient public function Tests the httpClient() method.
DrupalTest::testKeyValue public function Tests the keyValue() method.
DrupalTest::testKeyValueExpirable public function Tests the keyValueExpirable() method.
DrupalTest::testL public function Tests the l() method.
DrupalTest::testLanguageManager public function Tests the languageManager() method.
DrupalTest::testLinkGenerator public function Tests the linkGenerator() method.
DrupalTest::testLock public function Tests the lock() method.
DrupalTest::testMenuTree public function Tests the menuTree() method.
DrupalTest::testModuleHandler public function Tests the moduleHandler() method.
DrupalTest::testPathValidator public function Tests the pathValidator() method.
DrupalTest::testQueue public function Tests the queue() method.
DrupalTest::testRequestStack public function Tests the testRequestStack() method.
DrupalTest::testService public function Tests the service() method.
DrupalTest::testSetContainer public function Tests the get/setContainer() method.
DrupalTest::testState public function Tests the state() method.
DrupalTest::testToken public function Tests the token() method.
DrupalTest::testTranslation public function Tests the translation() method.
DrupalTest::testTransliteration public function Tests the transliteration() method.
DrupalTest::testTypedDataManager public function Tests the typedDataManager() method.
DrupalTest::testUrl public function Tests the url() method.
DrupalTest::testUrlGenerator public function Tests the urlGenerator() method.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.