function SystemListingTest::testDirectoryPrecedence

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Kernel/Common/SystemListingTest.php \Drupal\Tests\system\Kernel\Common\SystemListingTest::testDirectoryPrecedence()
  2. 8.9.x core/modules/system/tests/src/Kernel/Common/SystemListingTest.php \Drupal\Tests\system\Kernel\Common\SystemListingTest::testDirectoryPrecedence()
  3. 11.x 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 20

Class

SystemListingTest
Tests scanning system directories in drupal_system_listing().

Namespace

Drupal\Tests\system\Kernel\Common

Code

public function testDirectoryPrecedence() : void {
  // 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";
    $module_path = $files[$module]->getPathname();
    $this->assertEquals($expected_uri, $module_path, "Module {$module_path} was found at {$expected_uri}.");
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.