779a288a7bfbbd50b041c47d2e29822ca17fba43
[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   protected $expected = 'Drupal\\module_autoload_test\\SomeClass::testMethod() was invoked.';
18
19   /**
20    * Tests that module-provided classes can be loaded when a module is enabled.
21    *
22    * @see \Drupal\module_autoload_test\SomeClass
23    */
24   public function testClassLoading() {
25     // Enable the module_test and module_autoload_test modules.
26     \Drupal::service('module_installer')->install(['module_test', 'module_autoload_test'], FALSE);
27     $this->resetAll();
28     // Check twice to test an unprimed and primed system_list() cache.
29     for ($i = 0; $i < 2; $i++) {
30       $this->drupalGet('module-test/class-loading');
31       $this->assertResponse(200);
32       $this->assertText($this->expected, 'Autoloader loads classes from an enabled module.');
33     }
34   }
35
36   /**
37    * Tests that module-provided classes can't be loaded if module not installed.
38    *
39    * @see \Drupal\module_autoload_test\SomeClass
40    */
41   public function testClassLoadingNotInstalledModules() {
42     // Enable the module_test module.
43     \Drupal::service('module_installer')->install(['module_test'], FALSE);
44     $this->resetAll();
45     // Check twice to test an unprimed and primed system_list() cache.
46     for ($i = 0; $i < 2; $i++) {
47       $this->drupalGet('module-test/class-loading');
48       $this->assertResponse(200);
49       $this->assertNoText($this->expected, 'Autoloader does not load classes from a disabled module.');
50     }
51   }
52
53   /**
54    * Tests that module-provided classes can't be loaded from disabled modules.
55    *
56    * @see \Drupal\module_autoload_test\SomeClass
57    */
58   public function testClassLoadingDisabledModules() {
59     // Enable the module_test and module_autoload_test modules.
60     \Drupal::service('module_installer')->install(['module_test', 'module_autoload_test'], FALSE);
61     $this->resetAll();
62     // Ensure that module_autoload_test is disabled.
63     $this->container->get('module_installer')->uninstall(['module_autoload_test'], FALSE);
64     $this->resetAll();
65     // Check twice to test an unprimed and primed system_list() cache.
66     for ($i = 0; $i < 2; $i++) {
67       $this->drupalGet('module-test/class-loading');
68       $this->assertResponse(200);
69       $this->assertNoText($this->expected, 'Autoloader does not load classes from a disabled module.');
70     }
71   }
72
73   /**
74    * Ensures the negative caches in the class loader don't result in crashes.
75    */
76   public function testMultipleModules() {
77     $this->drupalLogin($this->rootUser);
78     $edit = [
79       "modules[module_install_class_loader_test1][enable]" => TRUE,
80       "modules[module_install_class_loader_test2][enable]" => TRUE,
81     ];
82     $this->drupalPostForm('admin/modules', $edit, t('Install'));
83     $this->rebuildContainer();
84     $this->assertTrue(\Drupal::moduleHandler()->moduleExists('module_install_class_loader_test2'), 'The module_install_class_loader_test2 module has been installed.');
85   }
86
87 }