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