3 namespace Drupal\Tests\system\Functional\Module;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests class loading for modules.
12 class ClassLoaderTest extends BrowserTestBase {
15 * The expected result from calling the module-provided class' method.
19 protected $expected = 'Drupal\\module_autoload_test\\SomeClass::testMethod() was invoked.';
24 protected $apcuEnsureUniquePrefix = TRUE;
27 * Tests that module-provided classes can be loaded when a module is enabled.
29 * @see \Drupal\module_autoload_test\SomeClass
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);
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.');
44 * Tests that module-provided classes can't be loaded if module not installed.
46 * @see \Drupal\module_autoload_test\SomeClass
48 public function testClassLoadingNotInstalledModules() {
49 // Enable the module_test module.
50 \Drupal::service('module_installer')->install(['module_test'], FALSE);
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.');
61 * Tests that module-provided classes can't be loaded from disabled modules.
63 * @see \Drupal\module_autoload_test\SomeClass
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);
69 // Ensure that module_autoload_test is disabled.
70 $this->container->get('module_installer')->uninstall(['module_autoload_test'], FALSE);
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.');
81 * Ensures the negative caches in the class loader don't result in crashes.
83 public function testMultipleModules() {
84 $this->drupalLogin($this->rootUser);
86 "modules[module_install_class_loader_test1][enable]" => TRUE,
87 "modules[module_install_class_loader_test2][enable]" => TRUE,
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.');