4c5448718ed2e3f8d27af1c43c36166a97bebba4
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Bootstrap / GetFilenameTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Bootstrap;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests that drupal_get_filename() works correctly.
10  *
11  * @group Bootstrap
12  */
13 class GetFilenameTest extends KernelTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = ['system'];
19
20   /**
21    * {@inheritdoc}
22    */
23   public function register(ContainerBuilder $container) {
24     parent::register($container);
25     // Use the testing install profile.
26     $container->setParameter('install_profile', 'testing');
27   }
28
29   /**
30    * Tests that drupal_get_filename() works when the file is not in database.
31    */
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();
37
38     // Retrieving the location of a module.
39     $this->assertIdentical(drupal_get_filename('module', 'system'), 'core/modules/system/system.info.yml');
40
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');
44
45     // Retrieving the location of a theme engine.
46     $this->assertIdentical(drupal_get_filename('theme_engine', 'twig'), 'core/themes/engines/twig/twig.info.yml');
47
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');
51
52
53     // Generate a non-existing module name.
54     $non_existing_module = uniqid("", TRUE);
55
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);
61         return;
62       }
63       throw new \ErrorException($message, 0, $severity, $file, $line);
64     });
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();
69   }
70
71 }