GetFilenameTest.php 2.51 KB
Newer Older
1 2
<?php

3
namespace Drupal\KernelTests\Core\Bootstrap;
4

5
use Drupal\Core\DependencyInjection\ContainerBuilder;
6
use Drupal\KernelTests\KernelTestBase;
7 8

/**
9 10 11
 * Tests that drupal_get_filename() works correctly.
 *
 * @group Bootstrap
12
 */
13 14 15 16 17 18
class GetFilenameTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = ['system'];
19

20 21 22 23 24 25 26 27 28
  /**
   * {@inheritdoc}
   */
  public function register(ContainerBuilder $container) {
    parent::register($container);
    // Use the testing install profile.
    $container->setParameter('install_profile', 'testing');
  }

29
  /**
30
   * Tests that drupal_get_filename() works when the file is not in database.
31 32
   */
  function testDrupalGetFilename() {
33 34 35 36
    // Rebuild system.module.files state data.
    // @todo Remove as part of https://www.drupal.org/node/2186491
    drupal_static_reset('system_rebuild_module_data');
    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(array('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 49

    // Retrieving the location of a profile. Profiles are a special case with
    // a fixed location and naming.
50 51 52 53 54
    $this->assertIdentical(drupal_get_filename('profile', 'testing'), 'core/profiles/testing/testing.info.yml');


    // Generate a non-existing module name.
    $non_existing_module = uniqid("", TRUE);
55

56 57 58 59 60 61 62 63 64 65 66 67 68
    // Set a custom error handler so we can ignore the file not found error.
    set_error_handler(function($severity, $message, $file, $line) {
      // Skip error handling if this is a "file not found" error.
      if (strstr($message, 'is missing from the file system:')) {
        \Drupal::state()->set('get_filename_test_triggered_error', TRUE);
        return;
      }
      throw new \ErrorException($message, 0, $severity, $file, $line);
    });
    $this->assertNull(drupal_get_filename('module', $non_existing_module), 'Searching for an item that does not exist returns NULL.');
    $this->assertTrue(\Drupal::state()->get('get_filename_test_triggered_error'), 'Searching for an item that does not exist triggers an error.');
    // Restore the original error handler.
    restore_error_handler();
69
  }
70

71
}