annotate core/tests/Drupal/Tests/Core/DrupalTest.php @ 0:c75dbcec494b

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