View source
<?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 Symfony\Component\HttpFoundation\RequestStack;
class DrupalTest extends UnitTestCase {
protected $container;
protected function setUp() : void {
parent::setUp();
$this->container = $this
->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerBuilder')
->onlyMethods([
'get',
])
->getMock();
}
public function testSetContainer() {
\Drupal::setContainer($this->container);
$this
->assertSame($this->container, \Drupal::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();
}
public function testService() {
$this
->setMockContainerService('test_service');
$this
->assertNotNull(\Drupal::service('test_service'));
}
public function testCurrentUser() {
$this
->setMockContainerService('current_user');
$this
->assertNotNull(\Drupal::currentUser());
}
public function testEntityTypeManager() {
$this
->setMockContainerService('entity_type.manager');
$this
->assertNotNull(\Drupal::entityTypeManager());
}
public function testDatabase() {
$this
->setMockContainerService('database');
$this
->assertNotNull(\Drupal::database());
}
public function testCache() {
$this
->setMockContainerService('cache.test');
$this
->assertNotNull(\Drupal::cache('test'));
}
public function testClassResolver() {
$class_resolver = $this
->prophesize(ClassResolverInterface::class);
$this
->setMockContainerService('class_resolver', $class_resolver
->reveal());
$this
->assertInstanceOf(ClassResolverInterface::class, \Drupal::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));
}
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'));
}
public function testLock() {
$this
->setMockContainerService('lock');
$this
->assertNotNull(\Drupal::lock());
}
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);
$this
->assertNotNull(\Drupal::config('test_config'));
}
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));
}
public function testRequestStack() {
$request_stack = new RequestStack();
$this
->setMockContainerService('request_stack', $request_stack);
$this
->assertSame($request_stack, \Drupal::requestStack());
}
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'));
}
public function testState() {
$this
->setMockContainerService('state');
$this
->assertNotNull(\Drupal::state());
}
public function testHttpClient() {
$this
->setMockContainerService('http_client');
$this
->assertNotNull(\Drupal::httpClient());
}
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'));
}
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'));
}
public function testFlood() {
$this
->setMockContainerService('flood');
$this
->assertNotNull(\Drupal::flood());
}
public function testModuleHandler() {
$this
->setMockContainerService('module_handler');
$this
->assertNotNull(\Drupal::moduleHandler());
}
public function testTypedDataManager() {
$this
->setMockContainerService('typed_data_manager');
$this
->assertNotNull(\Drupal::typedDataManager());
}
public function testToken() {
$this
->setMockContainerService('token');
$this
->assertNotNull(\Drupal::token());
}
public function testUrlGenerator() {
$this
->setMockContainerService('url_generator');
$this
->assertNotNull(\Drupal::urlGenerator());
}
public function testLinkGenerator() {
$this
->setMockContainerService('link_generator');
$this
->assertNotNull(\Drupal::linkGenerator());
}
public function testTranslation() {
$this
->setMockContainerService('string_translation');
$this
->assertNotNull(\Drupal::translation());
}
public function testLanguageManager() {
$this
->setMockContainerService('language_manager');
$this
->assertNotNull(\Drupal::languageManager());
}
public function testCsrfToken() {
$this
->setMockContainerService('csrf_token');
$this
->assertNotNull(\Drupal::csrfToken());
}
public function testTransliteration() {
$this
->setMockContainerService('transliteration');
$this
->assertNotNull(\Drupal::transliteration());
}
public function testFormBuilder() {
$this
->setMockContainerService('form_builder');
$this
->assertNotNull(\Drupal::formBuilder());
}
public function testMenuTree() {
$this
->setMockContainerService('menu.link_tree');
$this
->assertNotNull(\Drupal::menuTree());
}
public function testPathValidator() {
$this
->setMockContainerService('path.validator');
$this
->assertNotNull(\Drupal::pathValidator());
}
public function testAccessManager() {
$this
->setMockContainerService('access_manager');
$this
->assertNotNull(\Drupal::accessManager());
}
public function testPhpConstants() {
$normalizer = function (string $version) : string {
preg_match('{^(\\d{1,5})(\\.\\d++)?(\\.\\d++)?$}i', $version, $matches);
return $matches[1] . (!empty($matches[2]) ? $matches[2] : '.9999999') . (!empty($matches[3]) ? $matches[3] : '.9999999');
};
$recommended_php = $normalizer(\Drupal::RECOMMENDED_PHP);
$this
->assertTrue(version_compare($recommended_php, \Drupal::MINIMUM_PHP, '>='), "\\Drupal::RECOMMENDED_PHP should be greater or equal to \\Drupal::MINIMUM_PHP");
$this
->assertSame('10.9999999.9999999', $normalizer('10'));
$this
->assertSame('10.1.9999999', $normalizer('10.1'));
$this
->assertSame('10.1.2', $normalizer('10.1.2'));
}
protected function setMockContainerService($service_name, $return = NULL) {
$expects = $this->container
->expects($this
->once())
->method('get')
->with($service_name)
->willReturn($return ?? new \stdClass());
\Drupal::setContainer($this->container);
}
}