annotate core/tests/Drupal/Tests/Core/DrupalTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\Core;
Chris@0 4
Chris@17 5 use Drupal\Core\DependencyInjection\ClassResolverInterface;
Chris@0 6 use Drupal\Core\DependencyInjection\ContainerNotInitializedException;
Chris@0 7 use Drupal\Core\Entity\EntityStorageInterface;
Chris@0 8 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@0 9 use Drupal\Core\Entity\Query\QueryAggregateInterface;
Chris@0 10 use Drupal\Core\Entity\Query\QueryInterface;
Chris@0 11 use Drupal\Tests\UnitTestCase;
Chris@0 12 use Drupal\Core\Url;
Chris@0 13 use Symfony\Component\HttpFoundation\RequestStack;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Tests the Drupal class.
Chris@0 17 *
Chris@0 18 * @coversDefaultClass \Drupal
Chris@0 19 * @group DrupalTest
Chris@0 20 */
Chris@0 21 class DrupalTest extends UnitTestCase {
Chris@0 22
Chris@0 23 /**
Chris@0 24 * The mock container.
Chris@0 25 *
Chris@0 26 * @var \Symfony\Component\DependencyInjection\ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject
Chris@0 27 */
Chris@0 28 protected $container;
Chris@0 29
Chris@0 30 /**
Chris@0 31 * {@inheritdoc}
Chris@0 32 */
Chris@0 33 protected function setUp() {
Chris@0 34 parent::setUp();
Chris@0 35 $this->container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
Chris@0 36 ->setMethods(['get'])
Chris@0 37 ->getMock();
Chris@0 38 }
Chris@0 39
Chris@0 40 /**
Chris@0 41 * Tests the get/setContainer() method.
Chris@0 42 *
Chris@0 43 * @covers ::getContainer
Chris@0 44 */
Chris@0 45 public function testSetContainer() {
Chris@0 46 \Drupal::setContainer($this->container);
Chris@0 47 $this->assertSame($this->container, \Drupal::getContainer());
Chris@0 48 }
Chris@0 49
Chris@0 50 /**
Chris@0 51 * @covers ::getContainer
Chris@0 52 */
Chris@0 53 public function testGetContainerException() {
Chris@0 54 $this->setExpectedException(ContainerNotInitializedException::class, '\Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container.');
Chris@0 55 \Drupal::getContainer();
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * Tests the service() method.
Chris@0 60 *
Chris@0 61 * @covers ::service
Chris@0 62 */
Chris@0 63 public function testService() {
Chris@0 64 $this->setMockContainerService('test_service');
Chris@0 65 $this->assertNotNull(\Drupal::service('test_service'));
Chris@0 66 }
Chris@0 67
Chris@0 68 /**
Chris@0 69 * Tests the currentUser() method.
Chris@0 70 *
Chris@0 71 * @covers ::currentUser
Chris@0 72 */
Chris@0 73 public function testCurrentUser() {
Chris@0 74 $this->setMockContainerService('current_user');
Chris@0 75 $this->assertNotNull(\Drupal::currentUser());
Chris@0 76 }
Chris@0 77
Chris@0 78 /**
Chris@0 79 * Tests the entityManager() method.
Chris@0 80 *
Chris@0 81 * @covers ::entityManager
Chris@0 82 */
Chris@0 83 public function testEntityManager() {
Chris@0 84 $this->setMockContainerService('entity.manager');
Chris@0 85 $this->assertNotNull(\Drupal::entityManager());
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * Tests the entityTypeManager() method.
Chris@0 90 *
Chris@0 91 * @covers ::entityTypeManager
Chris@0 92 */
Chris@0 93 public function testEntityTypeManager() {
Chris@0 94 $this->setMockContainerService('entity_type.manager');
Chris@0 95 $this->assertNotNull(\Drupal::entityTypeManager());
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * Tests the database() method.
Chris@0 100 *
Chris@0 101 * @covers ::database
Chris@0 102 */
Chris@0 103 public function testDatabase() {
Chris@0 104 $this->setMockContainerService('database');
Chris@0 105 $this->assertNotNull(\Drupal::database());
Chris@0 106 }
Chris@0 107
Chris@0 108 /**
Chris@0 109 * Tests the cache() method.
Chris@0 110 *
Chris@0 111 * @covers ::cache
Chris@0 112 */
Chris@0 113 public function testCache() {
Chris@0 114 $this->setMockContainerService('cache.test');
Chris@0 115 $this->assertNotNull(\Drupal::cache('test'));
Chris@0 116 }
Chris@0 117
Chris@0 118 /**
Chris@0 119 * Tests the classResolver method.
Chris@0 120 *
Chris@0 121 * @covers ::classResolver
Chris@0 122 */
Chris@0 123 public function testClassResolver() {
Chris@17 124 $class_resolver = $this->prophesize(ClassResolverInterface::class);
Chris@17 125 $this->setMockContainerService('class_resolver', $class_resolver->reveal());
Chris@17 126 $this->assertInstanceOf(ClassResolverInterface::class, \Drupal::classResolver());
Chris@17 127 }
Chris@17 128
Chris@17 129 /**
Chris@17 130 * Tests the classResolver method when called with a class.
Chris@17 131 *
Chris@17 132 * @covers ::classResolver
Chris@17 133 */
Chris@17 134 public function testClassResolverWithClass() {
Chris@17 135 $class_resolver = $this->prophesize(ClassResolverInterface::class);
Chris@17 136 $class_resolver->getInstanceFromDefinition(static::class)->willReturn($this);
Chris@17 137 $this->setMockContainerService('class_resolver', $class_resolver->reveal());
Chris@17 138 $this->assertSame($this, \Drupal::classResolver(static::class));
Chris@0 139 }
Chris@0 140
Chris@0 141 /**
Chris@0 142 * Tests the keyValueExpirable() method.
Chris@0 143 *
Chris@0 144 * @covers ::keyValueExpirable
Chris@0 145 */
Chris@0 146 public function testKeyValueExpirable() {
Chris@0 147 $keyvalue = $this->getMockBuilder('Drupal\Core\KeyValueStore\KeyValueExpirableFactory')
Chris@0 148 ->disableOriginalConstructor()
Chris@0 149 ->getMock();
Chris@0 150 $keyvalue->expects($this->once())
Chris@0 151 ->method('get')
Chris@0 152 ->with('test_collection')
Chris@0 153 ->will($this->returnValue(TRUE));
Chris@0 154 $this->setMockContainerService('keyvalue.expirable', $keyvalue);
Chris@0 155
Chris@0 156 $this->assertNotNull(\Drupal::keyValueExpirable('test_collection'));
Chris@0 157 }
Chris@0 158
Chris@0 159 /**
Chris@0 160 * Tests the lock() method.
Chris@0 161 *
Chris@0 162 * @covers ::lock
Chris@0 163 */
Chris@0 164 public function testLock() {
Chris@0 165 $this->setMockContainerService('lock');
Chris@0 166 $this->assertNotNull(\Drupal::lock());
Chris@0 167 }
Chris@0 168
Chris@0 169 /**
Chris@0 170 * Tests the config() method.
Chris@0 171 *
Chris@0 172 * @covers ::config
Chris@0 173 */
Chris@0 174 public function testConfig() {
Chris@0 175 $config = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
Chris@0 176 $config->expects($this->once())
Chris@0 177 ->method('get')
Chris@0 178 ->with('test_config')
Chris@0 179 ->will($this->returnValue(TRUE));
Chris@0 180 $this->setMockContainerService('config.factory', $config);
Chris@0 181
Chris@0 182 // Test \Drupal::config(), not $this->config().
Chris@0 183 $this->assertNotNull(\Drupal::config('test_config'));
Chris@0 184 }
Chris@0 185
Chris@0 186 /**
Chris@0 187 * Tests the queue() method.
Chris@0 188 *
Chris@0 189 * @covers ::queue
Chris@0 190 */
Chris@0 191 public function testQueue() {
Chris@0 192 $queue = $this->getMockBuilder('Drupal\Core\Queue\QueueFactory')
Chris@0 193 ->disableOriginalConstructor()
Chris@0 194 ->getMock();
Chris@0 195 $queue->expects($this->once())
Chris@0 196 ->method('get')
Chris@0 197 ->with('test_queue', TRUE)
Chris@0 198 ->will($this->returnValue(TRUE));
Chris@0 199 $this->setMockContainerService('queue', $queue);
Chris@0 200
Chris@0 201 $this->assertNotNull(\Drupal::queue('test_queue', TRUE));
Chris@0 202 }
Chris@0 203
Chris@0 204 /**
Chris@0 205 * Tests the testRequestStack() method.
Chris@0 206 *
Chris@0 207 * @covers ::requestStack
Chris@0 208 */
Chris@0 209 public function testRequestStack() {
Chris@0 210 $request_stack = new RequestStack();
Chris@0 211 $this->setMockContainerService('request_stack', $request_stack);
Chris@0 212
Chris@0 213 $this->assertSame($request_stack, \Drupal::requestStack());
Chris@0 214 }
Chris@0 215
Chris@0 216 /**
Chris@0 217 * Tests the keyValue() method.
Chris@0 218 *
Chris@0 219 * @covers ::keyValue
Chris@0 220 */
Chris@0 221 public function testKeyValue() {
Chris@0 222 $keyvalue = $this->getMockBuilder('Drupal\Core\KeyValueStore\KeyValueFactory')
Chris@0 223 ->disableOriginalConstructor()
Chris@0 224 ->getMock();
Chris@0 225 $keyvalue->expects($this->once())
Chris@0 226 ->method('get')
Chris@0 227 ->with('test_collection')
Chris@0 228 ->will($this->returnValue(TRUE));
Chris@0 229 $this->setMockContainerService('keyvalue', $keyvalue);
Chris@0 230
Chris@0 231 $this->assertNotNull(\Drupal::keyValue('test_collection'));
Chris@0 232 }
Chris@0 233
Chris@0 234 /**
Chris@0 235 * Tests the state() method.
Chris@0 236 *
Chris@0 237 * @covers ::state
Chris@0 238 */
Chris@0 239 public function testState() {
Chris@0 240 $this->setMockContainerService('state');
Chris@0 241 $this->assertNotNull(\Drupal::state());
Chris@0 242 }
Chris@0 243
Chris@0 244 /**
Chris@0 245 * Tests the httpClient() method.
Chris@0 246 *
Chris@0 247 * @covers ::httpClient
Chris@0 248 */
Chris@0 249 public function testHttpClient() {
Chris@0 250 $this->setMockContainerService('http_client');
Chris@0 251 $this->assertNotNull(\Drupal::httpClient());
Chris@0 252 }
Chris@0 253
Chris@0 254 /**
Chris@0 255 * Tests the entityQuery() method.
Chris@0 256 *
Chris@0 257 * @covers ::entityQuery
Chris@0 258 */
Chris@0 259 public function testEntityQuery() {
Chris@0 260 $query = $this->getMock(QueryInterface::class);
Chris@0 261 $storage = $this->getMock(EntityStorageInterface::class);
Chris@0 262 $storage
Chris@0 263 ->expects($this->once())
Chris@0 264 ->method('getQuery')
Chris@0 265 ->with('OR')
Chris@0 266 ->willReturn($query);
Chris@0 267
Chris@0 268 $entity_type_manager = $this->getMock(EntityTypeManagerInterface::class);
Chris@0 269 $entity_type_manager
Chris@0 270 ->expects($this->once())
Chris@0 271 ->method('getStorage')
Chris@0 272 ->with('test_entity')
Chris@0 273 ->willReturn($storage);
Chris@0 274
Chris@0 275 $this->setMockContainerService('entity_type.manager', $entity_type_manager);
Chris@0 276
Chris@0 277 $this->assertInstanceOf(QueryInterface::class, \Drupal::entityQuery('test_entity', 'OR'));
Chris@0 278 }
Chris@0 279
Chris@0 280 /**
Chris@0 281 * Tests the entityQueryAggregate() method.
Chris@0 282 *
Chris@0 283 * @covers ::entityQueryAggregate
Chris@0 284 */
Chris@0 285 public function testEntityQueryAggregate() {
Chris@0 286 $query = $this->getMock(QueryAggregateInterface::class);
Chris@0 287 $storage = $this->getMock(EntityStorageInterface::class);
Chris@0 288 $storage
Chris@0 289 ->expects($this->once())
Chris@0 290 ->method('getAggregateQuery')
Chris@0 291 ->with('OR')
Chris@0 292 ->willReturn($query);
Chris@0 293
Chris@0 294 $entity_type_manager = $this->getMock(EntityTypeManagerInterface::class);
Chris@0 295 $entity_type_manager
Chris@0 296 ->expects($this->once())
Chris@0 297 ->method('getStorage')
Chris@0 298 ->with('test_entity')
Chris@0 299 ->willReturn($storage);
Chris@0 300
Chris@0 301 $this->setMockContainerService('entity_type.manager', $entity_type_manager);
Chris@0 302
Chris@0 303 $this->assertInstanceOf(QueryAggregateInterface::class, \Drupal::entityQueryAggregate('test_entity', 'OR'));
Chris@0 304 }
Chris@0 305
Chris@0 306 /**
Chris@0 307 * Tests the flood() method.
Chris@0 308 *
Chris@0 309 * @covers ::flood
Chris@0 310 */
Chris@0 311 public function testFlood() {
Chris@0 312 $this->setMockContainerService('flood');
Chris@0 313 $this->assertNotNull(\Drupal::flood());
Chris@0 314 }
Chris@0 315
Chris@0 316 /**
Chris@0 317 * Tests the moduleHandler() method.
Chris@0 318 *
Chris@0 319 * @covers ::moduleHandler
Chris@0 320 */
Chris@0 321 public function testModuleHandler() {
Chris@0 322 $this->setMockContainerService('module_handler');
Chris@0 323 $this->assertNotNull(\Drupal::moduleHandler());
Chris@0 324 }
Chris@0 325
Chris@0 326 /**
Chris@0 327 * Tests the typedDataManager() method.
Chris@0 328 *
Chris@0 329 * @covers ::typedDataManager
Chris@0 330 */
Chris@0 331 public function testTypedDataManager() {
Chris@0 332 $this->setMockContainerService('typed_data_manager');
Chris@0 333 $this->assertNotNull(\Drupal::typedDataManager());
Chris@0 334 }
Chris@0 335
Chris@0 336 /**
Chris@0 337 * Tests the token() method.
Chris@0 338 *
Chris@0 339 * @covers ::token
Chris@0 340 */
Chris@0 341 public function testToken() {
Chris@0 342 $this->setMockContainerService('token');
Chris@0 343 $this->assertNotNull(\Drupal::token());
Chris@0 344 }
Chris@0 345
Chris@0 346 /**
Chris@0 347 * Tests the urlGenerator() method.
Chris@0 348 *
Chris@0 349 * @covers ::urlGenerator
Chris@0 350 */
Chris@0 351 public function testUrlGenerator() {
Chris@0 352 $this->setMockContainerService('url_generator');
Chris@0 353 $this->assertNotNull(\Drupal::urlGenerator());
Chris@0 354 }
Chris@0 355
Chris@0 356 /**
Chris@0 357 * Tests the url() method.
Chris@0 358 *
Chris@0 359 * @covers ::url
Chris@0 360 * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
Chris@18 361 *
Chris@18 362 * @group legacy
Chris@18 363 * @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 364 */
Chris@0 365 public function testUrl() {
Chris@0 366 $route_parameters = ['test_parameter' => 'test'];
Chris@0 367 $options = ['test_option' => 'test'];
Chris@0 368 $generator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface');
Chris@0 369 $generator->expects($this->once())
Chris@0 370 ->method('generateFromRoute')
Chris@0 371 ->with('test_route', $route_parameters, $options)
Chris@0 372 ->will($this->returnValue('path_string'));
Chris@0 373 $this->setMockContainerService('url_generator', $generator);
Chris@0 374
Chris@0 375 $this->assertInternalType('string', \Drupal::url('test_route', $route_parameters, $options));
Chris@0 376 }
Chris@0 377
Chris@0 378 /**
Chris@0 379 * Tests the linkGenerator() method.
Chris@0 380 *
Chris@0 381 * @covers ::linkGenerator
Chris@0 382 */
Chris@0 383 public function testLinkGenerator() {
Chris@0 384 $this->setMockContainerService('link_generator');
Chris@0 385 $this->assertNotNull(\Drupal::linkGenerator());
Chris@0 386 }
Chris@0 387
Chris@0 388 /**
Chris@0 389 * Tests the l() method.
Chris@0 390 *
Chris@0 391 * @covers ::l
Chris@0 392 * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate()
Chris@0 393 */
Chris@0 394 public function testL() {
Chris@0 395 $route_parameters = ['test_parameter' => 'test'];
Chris@0 396 $options = ['test_option' => 'test'];
Chris@0 397 $generator = $this->getMock('Drupal\Core\Utility\LinkGeneratorInterface');
Chris@0 398 $url = new Url('test_route', $route_parameters, $options);
Chris@0 399 $generator->expects($this->once())
Chris@0 400 ->method('generate')
Chris@0 401 ->with('Test title', $url)
Chris@0 402 ->will($this->returnValue('link_html_string'));
Chris@0 403 $this->setMockContainerService('link_generator', $generator);
Chris@0 404
Chris@0 405 $this->assertInternalType('string', \Drupal::l('Test title', $url));
Chris@0 406 }
Chris@0 407
Chris@0 408 /**
Chris@0 409 * Tests the translation() method.
Chris@0 410 *
Chris@0 411 * @covers ::translation
Chris@0 412 */
Chris@0 413 public function testTranslation() {
Chris@0 414 $this->setMockContainerService('string_translation');
Chris@0 415 $this->assertNotNull(\Drupal::translation());
Chris@0 416 }
Chris@0 417
Chris@0 418 /**
Chris@0 419 * Tests the languageManager() method.
Chris@0 420 *
Chris@0 421 * @covers ::languageManager
Chris@0 422 */
Chris@0 423 public function testLanguageManager() {
Chris@0 424 $this->setMockContainerService('language_manager');
Chris@0 425 $this->assertNotNull(\Drupal::languageManager());
Chris@0 426 }
Chris@0 427
Chris@0 428 /**
Chris@0 429 * Tests the csrfToken() method.
Chris@0 430 *
Chris@0 431 * @covers ::csrfToken
Chris@0 432 */
Chris@0 433 public function testCsrfToken() {
Chris@0 434 $this->setMockContainerService('csrf_token');
Chris@0 435 $this->assertNotNull(\Drupal::csrfToken());
Chris@0 436 }
Chris@0 437
Chris@0 438 /**
Chris@0 439 * Tests the transliteration() method.
Chris@0 440 *
Chris@0 441 * @covers ::transliteration
Chris@0 442 */
Chris@0 443 public function testTransliteration() {
Chris@0 444 $this->setMockContainerService('transliteration');
Chris@0 445 $this->assertNotNull(\Drupal::transliteration());
Chris@0 446 }
Chris@0 447
Chris@0 448 /**
Chris@0 449 * Tests the formBuilder() method.
Chris@0 450 *
Chris@0 451 * @covers ::formBuilder
Chris@0 452 */
Chris@0 453 public function testFormBuilder() {
Chris@0 454 $this->setMockContainerService('form_builder');
Chris@0 455 $this->assertNotNull(\Drupal::formBuilder());
Chris@0 456 }
Chris@0 457
Chris@0 458 /**
Chris@0 459 * Tests the menuTree() method.
Chris@0 460 *
Chris@0 461 * @covers ::menuTree
Chris@0 462 */
Chris@0 463 public function testMenuTree() {
Chris@0 464 $this->setMockContainerService('menu.link_tree');
Chris@0 465 $this->assertNotNull(\Drupal::menuTree());
Chris@0 466 }
Chris@0 467
Chris@0 468 /**
Chris@0 469 * Tests the pathValidator() method.
Chris@0 470 *
Chris@0 471 * @covers ::pathValidator
Chris@0 472 */
Chris@0 473 public function testPathValidator() {
Chris@0 474 $this->setMockContainerService('path.validator');
Chris@0 475 $this->assertNotNull(\Drupal::pathValidator());
Chris@0 476 }
Chris@0 477
Chris@0 478 /**
Chris@0 479 * Tests the accessManager() method.
Chris@0 480 *
Chris@0 481 * @covers ::accessManager
Chris@0 482 */
Chris@0 483 public function testAccessManager() {
Chris@0 484 $this->setMockContainerService('access_manager');
Chris@0 485 $this->assertNotNull(\Drupal::accessManager());
Chris@0 486 }
Chris@0 487
Chris@0 488 /**
Chris@0 489 * Sets up a mock expectation for the container get() method.
Chris@0 490 *
Chris@0 491 * @param string $service_name
Chris@0 492 * The service name to expect for the get() method.
Chris@0 493 * @param mixed $return
Chris@0 494 * The value to return from the mocked container get() method.
Chris@0 495 */
Chris@0 496 protected function setMockContainerService($service_name, $return = NULL) {
Chris@0 497 $expects = $this->container->expects($this->once())
Chris@0 498 ->method('get')
Chris@0 499 ->with($service_name);
Chris@0 500
Chris@0 501 if (isset($return)) {
Chris@0 502 $expects->will($this->returnValue($return));
Chris@0 503 }
Chris@0 504 else {
Chris@0 505 $expects->will($this->returnValue(TRUE));
Chris@0 506 }
Chris@0 507
Chris@0 508 \Drupal::setContainer($this->container);
Chris@0 509 }
Chris@0 510
Chris@0 511 }