DrupalTest.php 14 KB

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