b0412b4336fd20d6c4d0ea1f306561fe442e2a9c
[yaffs-website] / web / core / modules / system / tests / src / Kernel / Common / SystemListingTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Kernel\Common;
4
5 use Drupal\Core\Extension\ExtensionDiscovery;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Tests scanning system directories in drupal_system_listing().
10  *
11  * @group Common
12  */
13 class SystemListingTest extends KernelTestBase {
14   /**
15    * Tests that files in different directories take precedence as expected.
16    */
17   public function testDirectoryPrecedence() {
18     // Define the module files we will search for, and the directory precedence
19     // we expect.
20     $expected_directories = [
21       // When both copies of the module are compatible with Drupal core, the
22       // copy in the profile directory takes precedence.
23       'drupal_system_listing_compatible_test' => [
24         'core/profiles/testing/modules',
25         'core/modules/system/tests/modules',
26       ],
27     ];
28
29     // This test relies on two versions of the same module existing in
30     // different places in the filesystem. Without that, the test has no
31     // meaning, so assert their presence first.
32     foreach ($expected_directories as $module => $directories) {
33       foreach ($directories as $directory) {
34         $filename = "$directory/$module/$module.info.yml";
35         $this->assertTrue(file_exists(\Drupal::root() . '/' . $filename), format_string('@filename exists.', ['@filename' => $filename]));
36       }
37     }
38
39     // Now scan the directories and check that the files take precedence as
40     // expected.
41     $listing = new ExtensionDiscovery(\Drupal::root());
42     $listing->setProfileDirectories(['core/profiles/testing']);
43     $files = $listing->scan('module');
44     foreach ($expected_directories as $module => $directories) {
45       $expected_directory = array_shift($directories);
46       $expected_uri = "$expected_directory/$module/$module.info.yml";
47       $this->assertEqual($files[$module]->getPathname(), $expected_uri, format_string('Module @actual was found at @expected.', [
48         '@actual' => $files[$module]->getPathname(),
49         '@expected' => $expected_uri,
50       ]));
51     }
52   }
53
54   /**
55    * Tests that directories matching file_scan_ignore_directories are ignored
56    */
57   public function testFileScanIgnoreDirectory() {
58     $listing = new ExtensionDiscovery(\Drupal::root(), FALSE);
59     $listing->setProfileDirectories(['core/profiles/testing']);
60     $files = $listing->scan('module');
61     $this->assertArrayHasKey('drupal_system_listing_compatible_test', $files);
62
63     // Reset the static to force a rescan of the directories.
64     $reflected_class = new \ReflectionClass(ExtensionDiscovery::class);
65     $reflected_property = $reflected_class->getProperty('files');
66     $reflected_property->setAccessible(TRUE);
67     $reflected_property->setValue($reflected_class, []);
68
69     $this->setSetting('file_scan_ignore_directories', ['drupal_system_listing_compatible_test']);
70     $listing = new ExtensionDiscovery(\Drupal::root(), FALSE);
71     $listing->setProfileDirectories(['core/profiles/testing']);
72     $files = $listing->scan('module');
73     $this->assertArrayNotHasKey('drupal_system_listing_compatible_test', $files);
74   }
75
76 }