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.';
22 * Tests that module-provided classes can be loaded when a module is enabled.
24 * @see \Drupal\module_autoload_test\SomeClass
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);
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.');
39 * Tests that module-provided classes can't be loaded if module not installed.
41 * @see \Drupal\module_autoload_test\SomeClass
43 public function testClassLoadingNotInstalledModules() {
44 // Enable the module_test module.
45 \Drupal::service('module_installer')->install(['module_test'], FALSE);
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.');
56 * Tests that module-provided classes can't be loaded from disabled modules.
58 * @see \Drupal\module_autoload_test\SomeClass
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);
64 // Ensure that module_autoload_test is disabled.
65 $this->container->get('module_installer')->uninstall(['module_autoload_test'], FALSE);
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.');
76 * Ensures the negative caches in the class loader don't result in crashes.
78 public function testMultipleModules() {
79 $this->drupalLogin($this->rootUser);
81 "modules[module_install_class_loader_test1][enable]" => TRUE,
82 "modules[module_install_class_loader_test2][enable]" => TRUE,
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.');