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