You are here

public function SystemListingTest::testDirectoryPrecedence in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Kernel/Common/SystemListingTest.php \Drupal\Tests\system\Kernel\Common\SystemListingTest::testDirectoryPrecedence()
  2. 10 core/modules/system/tests/src/Kernel/Common/SystemListingTest.php \Drupal\Tests\system\Kernel\Common\SystemListingTest::testDirectoryPrecedence()

Tests that files in different directories take precedence as expected.

File

core/modules/system/tests/src/Kernel/Common/SystemListingTest.php, line 19

Class

SystemListingTest
Tests scanning system directories in drupal_system_listing().

Namespace

Drupal\Tests\system\Kernel\Common

Code

public function testDirectoryPrecedence() {

  // Define the module files we will search for, and the directory precedence
  // we expect.
  $expected_directories = [
    // When both copies of the module are compatible with Drupal core, the
    // copy in the profile directory takes precedence.
    'drupal_system_listing_compatible_test' => [
      'core/profiles/testing/modules',
      'core/modules/system/tests/modules',
    ],
  ];

  // This test relies on two versions of the same module existing in
  // different places in the filesystem. Without that, the test has no
  // meaning, so assert their presence first.
  foreach ($expected_directories as $module => $directories) {
    foreach ($directories as $directory) {
      $filename = "{$directory}/{$module}/{$module}.info.yml";
      $this
        ->assertFileExists($this->root . '/' . $filename);
    }
  }

  // Now scan the directories and check that the files take precedence as
  // expected.
  $listing = new ExtensionDiscovery($this->root);
  $listing
    ->setProfileDirectories([
    'core/profiles/testing',
  ]);
  $files = $listing
    ->scan('module');
  foreach ($expected_directories as $module => $directories) {
    $expected_directory = array_shift($directories);
    $expected_uri = "{$expected_directory}/{$module}/{$module}.info.yml";
    $this
      ->assertEquals($expected_uri, $files[$module]
      ->getPathname(), new FormattableMarkup('Module @actual was found at @expected.', [
      '@actual' => $files[$module]
        ->getPathname(),
      '@expected' => $expected_uri,
    ]));
  }
}