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.
17 protected $expected = 'Drupal\\module_autoload_test\\SomeClass::testMethod() was invoked.';
20 * Tests that module-provided classes can be loaded when a module is enabled.
22 * @see \Drupal\module_autoload_test\SomeClass
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);
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.');
37 * Tests that module-provided classes can't be loaded if module not installed.
39 * @see \Drupal\module_autoload_test\SomeClass
41 public function testClassLoadingNotInstalledModules() {
42 // Enable the module_test module.
43 \Drupal::service('module_installer')->install(['module_test'], FALSE);
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.');
54 * Tests that module-provided classes can't be loaded from disabled modules.
56 * @see \Drupal\module_autoload_test\SomeClass
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);
62 // Ensure that module_autoload_test is disabled.
63 $this->container->get('module_installer')->uninstall(['module_autoload_test'], FALSE);
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.');
74 * Ensures the negative caches in the class loader don't result in crashes.
76 public function testMultipleModules() {
77 $this->drupalLogin($this->rootUser);
79 "modules[module_install_class_loader_test1][enable]" => TRUE,
80 "modules[module_install_class_loader_test2][enable]" => TRUE,
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.');