PHPUnit testing within multisite setups

Created on 2 September 2019, about 5 years ago
Updated 15 May 2024, 4 months ago

Currently it doesn't seem like there's any way of running kernel tests in a multisite setup, where a module is within a site's directory.

Where a module exists at drupal_root/sites/sitename/modules/custom/my_module, it doesn't seem possible to run PHPUnit kernel tests for any tests within that module. (I'd assume functional tests too, but I can't get those working on my machine right now :D)

The problem comes right at the start during setup:

public static $modules = ['my_module'];

It seems that this is because Simpletest installs itself in a new site directory, eg drupal_root/sites/simpletest/, it doesn't have access to the modules in the other directory. As far as I can tell the only directories it scans are drupal_root/core, drupal_root/sites/all, drupal_root and drupal_root/sites/simpletest/###

This was while running the tests directly, eg $ phpunit drupal_root/sites/sitename/modules/custom/my_module/tests/src/NameOfTest.php, so it wasn't an automated discovery/testsuite issue.

If there is a way of running PHPUnit tests within a multisite installation then it doesn't seem to be documented anywhere. This seems like a pretty major omission.

Would it be possible for example to pass in a "site" paramater to the PHPUnit command, so that it runs within a particular site directory? Or allow discovery of modules from other directories?

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
PHPUnit 

Last updated about 11 hours ago

Created by

🇬🇧United Kingdom Sophie.SK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States kevinquillen

    As a workaround, for Kernel tests, you can do this:

      /**
       * {@inheritdoc}
       */
      protected function setUpFilesystem(): void {
        parent::setUpFilesystem();
        $this->setSetting('test_parent_site', 'sites/SITENAME');
      }
    

    for Functional tests:

      /**
       * {@inheritdoc}
       */
      protected function prepareSettings() {
        parent::prepareSettings();
        $directory = DRUPAL_ROOT . '/' . $this->siteDirectory;
        $append = '$settings["test_parent_site"] = "sites/SITENAME";';
        file_put_contents($directory . '/settings.php', $append, FILE_APPEND | LOCK_EX);
      }
    

    this worked for me to get my tests moving.

Production build 0.71.5 2024