Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / tests / src / Functional / Module / ClassLoaderTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Module;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests class loading for modules.
9  *
10  * @group Module
11  */
12 class ClassLoaderTest extends BrowserTestBase {
13
14   /**
15    * The expected result from calling the module-provided class' method.
16    *
17    * @var string
18    */
19   protected $expected = 'Drupal\\module_autoload_test\\SomeClass::testMethod() was invoked.';
20
21   /**
22    * Tests that module-provided classes can be loaded when a module is enabled.
23    *
24    * @see \Drupal\module_autoload_test\SomeClass
25    */
26   public function testClassLoading() {
27     // Enable the module_test and module_autoload_test modules.
28     \Drupal::service('module_installer')->install(['module_test', 'module_autoload_test'], FALSE);
29     $this->resetAll();
30     // Check twice to test an unprimed and primed system_list() cache.
31     for ($i = 0; $i < 2; $i++) {
32       $this->drupalGet('module-test/class-loading');
33       $this->assertResponse(200);
34       $this->assertText($this->expected, 'Autoloader loads classes from an enabled module.');
35     }
36   }
37
38   /**
39    * Tests that module-provided classes can't be loaded if module not installed.
40    *
41    * @see \Drupal\module_autoload_test\SomeClass
42    */
43   public function testClassLoadingNotInstalledModules() {
44     // Enable the module_test module.
45     \Drupal::service('module_installer')->install(['module_test'], FALSE);
46     $this->resetAll();
47     // Check twice to test an unprimed and primed system_list() cache.
48     for ($i = 0; $i < 2; $i++) {
49       $this->drupalGet('module-test/class-loading');
50       $this->assertResponse(200);
51       $this->assertNoText($this->expected, 'Autoloader does not load classes from a disabled module.');
52     }
53   }
54
55   /**
56    * Tests that module-provided classes can't be loaded from disabled modules.
57    *
58    * @see \Drupal\module_autoload_test\SomeClass
59    */
60   public function testClassLoadingDisabledModules() {
61     // Enable the module_test and module_autoload_test modules.
62     \Drupal::service('module_installer')->install(['module_test', 'module_autoload_test'], FALSE);
63     $this->resetAll();
64     // Ensure that module_autoload_test is disabled.
65     $this->container->get('module_installer')->uninstall(['module_autoload_test'], FALSE);
66     $this->resetAll();
67     // Check twice to test an unprimed and primed system_list() cache.
68     for ($i = 0; $i < 2; $i++) {
69       $this->drupalGet('module-test/class-loading');
70       $this->assertResponse(200);
71       $this->assertNoText($this->expected, 'Autoloader does not load classes from a disabled module.');
72     }
73   }
74
75   /**
76    * Ensures the negative caches in the class loader don't result in crashes.
77    */
78   public function testMultipleModules() {
79     $this->drupalLogin($this->rootUser);
80     $edit = [
81       "modules[module_install_class_loader_test1][enable]" => TRUE,
82       "modules[module_install_class_loader_test2][enable]" => TRUE,
83     ];
84     $this->drupalPostForm('admin/modules', $edit, t('Install'));
85     $this->rebuildContainer();
86     $this->assertTrue(\Drupal::moduleHandler()->moduleExists('module_install_class_loader_test2'), 'The module_install_class_loader_test2 module has been installed.');
87   }
88
89 }