123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- <?php
- namespace Drupal\Tests\Core;
- use Drupal\Core\DependencyInjection\ClassResolverInterface;
- use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
- use Drupal\Core\Entity\EntityStorageInterface;
- use Drupal\Core\Entity\EntityTypeManagerInterface;
- use Drupal\Core\Entity\Query\QueryAggregateInterface;
- use Drupal\Core\Entity\Query\QueryInterface;
- use Drupal\Tests\UnitTestCase;
- use Drupal\Core\Url;
- use Symfony\Component\HttpFoundation\RequestStack;
- /**
- * Tests the Drupal class.
- *
- * @coversDefaultClass \Drupal
- * @group DrupalTest
- */
- 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(['get'])
- ->getMock();
- }
- /**
- * Tests the get/setContainer() method.
- *
- * @covers ::getContainer
- */
- public function testSetContainer() {
- \Drupal::setContainer($this->container);
- $this->assertSame($this->container, \Drupal::getContainer());
- }
- /**
- * @covers ::getContainer
- */
- public function testGetContainerException() {
- $this->expectException(ContainerNotInitializedException::class);
- $this->expectExceptionMessage('\Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.');
- \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
- * @group legacy
- * @expectedDeprecation \Drupal::entityManager() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager() instead in most cases. If the needed method is not on \Drupal\Core\Entity\EntityTypeManagerInterface, see the deprecated \Drupal\Core\Entity\EntityManager to find the correct interface or service. See https://www.drupal.org/node/2549139
- */
- 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 cache() method.
- *
- * @covers ::cache
- */
- public function testCache() {
- $this->setMockContainerService('cache.test');
- $this->assertNotNull(\Drupal::cache('test'));
- }
- /**
- * Tests the classResolver method.
- *
- * @covers ::classResolver
- */
- public function testClassResolver() {
- $class_resolver = $this->prophesize(ClassResolverInterface::class);
- $this->setMockContainerService('class_resolver', $class_resolver->reveal());
- $this->assertInstanceOf(ClassResolverInterface::class, \Drupal::classResolver());
- }
- /**
- * Tests the classResolver method when called with a class.
- *
- * @covers ::classResolver
- */
- public function testClassResolverWithClass() {
- $class_resolver = $this->prophesize(ClassResolverInterface::class);
- $class_resolver->getInstanceFromDefinition(static::class)->willReturn($this);
- $this->setMockContainerService('class_resolver', $class_resolver->reveal());
- $this->assertSame($this, \Drupal::classResolver(static::class));
- }
- /**
- * 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->createMock('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->createMock(QueryInterface::class);
- $storage = $this->createMock(EntityStorageInterface::class);
- $storage
- ->expects($this->once())
- ->method('getQuery')
- ->with('OR')
- ->willReturn($query);
- $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
- $entity_type_manager
- ->expects($this->once())
- ->method('getStorage')
- ->with('test_entity')
- ->willReturn($storage);
- $this->setMockContainerService('entity_type.manager', $entity_type_manager);
- $this->assertInstanceOf(QueryInterface::class, \Drupal::entityQuery('test_entity', 'OR'));
- }
- /**
- * Tests the entityQueryAggregate() method.
- *
- * @covers ::entityQueryAggregate
- */
- public function testEntityQueryAggregate() {
- $query = $this->createMock(QueryAggregateInterface::class);
- $storage = $this->createMock(EntityStorageInterface::class);
- $storage
- ->expects($this->once())
- ->method('getAggregateQuery')
- ->with('OR')
- ->willReturn($query);
- $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
- $entity_type_manager
- ->expects($this->once())
- ->method('getStorage')
- ->with('test_entity')
- ->willReturn($storage);
- $this->setMockContainerService('entity_type.manager', $entity_type_manager);
- $this->assertInstanceOf(QueryAggregateInterface::class, \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()
- *
- * @group legacy
- * @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()
- */
- public function testUrl() {
- $route_parameters = ['test_parameter' => 'test'];
- $options = ['test_option' => 'test'];
- $generator = $this->createMock('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
- *
- * @group legacy
- *
- * @expectedDeprecation \Drupal::l() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Link::fromTextAndUrl() instead. See https://www.drupal.org/node/2614344
- *
- * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate()
- */
- public function testL() {
- $route_parameters = ['test_parameter' => 'test'];
- $options = ['test_option' => 'test'];
- $generator = $this->createMock('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);
- }
- }
|