GetFilenameTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Drupal\KernelTests\Core\Bootstrap;
  3. use Drupal\Core\DependencyInjection\ContainerBuilder;
  4. use Drupal\KernelTests\KernelTestBase;
  5. /**
  6. * Tests that drupal_get_filename() works correctly.
  7. *
  8. * @group Bootstrap
  9. */
  10. class GetFilenameTest extends KernelTestBase {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public static $modules = ['system'];
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function register(ContainerBuilder $container) {
  19. parent::register($container);
  20. // Use the testing install profile.
  21. $container->setParameter('install_profile', 'testing');
  22. }
  23. /**
  24. * Tests that drupal_get_filename() works when the file is not in database.
  25. */
  26. public function testDrupalGetFilename() {
  27. // Rebuild system.module.files state data.
  28. // @todo Remove as part of https://www.drupal.org/node/2186491
  29. drupal_static_reset('system_rebuild_module_data');
  30. system_rebuild_module_data();
  31. // Retrieving the location of a module.
  32. $this->assertIdentical(drupal_get_filename('module', 'system'), 'core/modules/system/system.info.yml');
  33. // Retrieving the location of a theme.
  34. \Drupal::service('theme_handler')->install(['stark']);
  35. $this->assertIdentical(drupal_get_filename('theme', 'stark'), 'core/themes/stark/stark.info.yml');
  36. // Retrieving the location of a theme engine.
  37. $this->assertIdentical(drupal_get_filename('theme_engine', 'twig'), 'core/themes/engines/twig/twig.info.yml');
  38. // Retrieving the location of a profile. Profiles are a special case with
  39. // a fixed location and naming.
  40. $this->assertIdentical(drupal_get_filename('profile', 'testing'), 'core/profiles/testing/testing.info.yml');
  41. // Generate a non-existing module name.
  42. $non_existing_module = uniqid("", TRUE);
  43. // Set a custom error handler so we can ignore the file not found error.
  44. set_error_handler(function ($severity, $message, $file, $line) {
  45. // Skip error handling if this is a "file not found" error.
  46. if (strstr($message, 'is missing from the file system:')) {
  47. \Drupal::state()->set('get_filename_test_triggered_error', TRUE);
  48. return;
  49. }
  50. throw new \ErrorException($message, 0, $severity, $file, $line);
  51. });
  52. $this->assertNull(drupal_get_filename('module', $non_existing_module), 'Searching for an item that does not exist returns NULL.');
  53. $this->assertTrue(\Drupal::state()->get('get_filename_test_triggered_error'), 'Searching for an item that does not exist triggers an error.');
  54. // Restore the original error handler.
  55. restore_error_handler();
  56. }
  57. }