2013-03-07 02:48:50 +00:00
|
|
|
<?php
|
|
|
|
|
2013-10-03 06:30:07 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Autoloader for Drupal PHPUnit testing.
|
|
|
|
*
|
|
|
|
* @see phpunit.xml.dist
|
|
|
|
*/
|
2013-03-07 02:48:50 +00:00
|
|
|
|
2021-12-29 18:34:48 +00:00
|
|
|
use Drupal\TestTools\PhpUnitCompatibility\ClassWriter;
|
2016-04-30 14:11:15 +00:00
|
|
|
|
2013-10-03 06:30:07 +00:00
|
|
|
/**
|
|
|
|
* Finds all valid extension directories recursively within a given directory.
|
|
|
|
*
|
|
|
|
* @param string $scan_directory
|
|
|
|
* The directory that should be recursively scanned.
|
2020-05-20 22:11:54 +00:00
|
|
|
*
|
2013-10-03 06:30:07 +00:00
|
|
|
* @return array
|
|
|
|
* An associative array of extension directories found within the scanned
|
|
|
|
* directory, keyed by extension name.
|
|
|
|
*/
|
|
|
|
function drupal_phpunit_find_extension_directories($scan_directory) {
|
2017-03-04 01:20:24 +00:00
|
|
|
$extensions = [];
|
2013-10-03 06:30:07 +00:00
|
|
|
$dirs = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($scan_directory, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS));
|
|
|
|
foreach ($dirs as $dir) {
|
2023-03-18 11:30:09 +00:00
|
|
|
if (str_contains($dir->getPathname(), '.info.yml')) {
|
2016-06-24 16:36:21 +00:00
|
|
|
// Cut off ".info.yml" from the filename for use as the extension name. We
|
|
|
|
// use getRealPath() so that we can scan extensions represented by
|
|
|
|
// directory aliases.
|
Issue #2304461 by dawehner, neclimdul, naveenvalecha, jibran, tim.plunkett, phenaproxima, Xano, hussainweb, sun, hitesh-jain, amateescu, alexpott, daffie, Mile23, Wim Leers, larowlan: KernelTestBaseTNG™
2015-08-18 00:25:27 +00:00
|
|
|
$extensions[substr($dir->getFilename(), 0, -9)] = $dir->getPathInfo()
|
|
|
|
->getRealPath();
|
2013-04-24 09:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-03 06:30:07 +00:00
|
|
|
return $extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns directories under which contributed extensions may exist.
|
|
|
|
*
|
2016-06-24 16:36:21 +00:00
|
|
|
* @param string $root
|
|
|
|
* (optional) Path to the root of the Drupal installation.
|
|
|
|
*
|
2013-10-03 06:30:07 +00:00
|
|
|
* @return array
|
|
|
|
* An array of directories under which contributed extensions may exist.
|
|
|
|
*/
|
2016-06-24 16:36:21 +00:00
|
|
|
function drupal_phpunit_contrib_extension_directory_roots($root = NULL) {
|
|
|
|
if ($root === NULL) {
|
2020-04-23 16:15:14 +00:00
|
|
|
$root = dirname(__DIR__, 2);
|
2016-06-24 16:36:21 +00:00
|
|
|
}
|
2017-03-04 01:20:24 +00:00
|
|
|
$paths = [
|
Issue #2304461 by dawehner, neclimdul, naveenvalecha, jibran, tim.plunkett, phenaproxima, Xano, hussainweb, sun, hitesh-jain, amateescu, alexpott, daffie, Mile23, Wim Leers, larowlan: KernelTestBaseTNG™
2015-08-18 00:25:27 +00:00
|
|
|
$root . '/core/modules',
|
|
|
|
$root . '/core/profiles',
|
2021-04-26 08:23:10 +00:00
|
|
|
$root . '/core/themes',
|
Issue #2304461 by dawehner, neclimdul, naveenvalecha, jibran, tim.plunkett, phenaproxima, Xano, hussainweb, sun, hitesh-jain, amateescu, alexpott, daffie, Mile23, Wim Leers, larowlan: KernelTestBaseTNG™
2015-08-18 00:25:27 +00:00
|
|
|
$root . '/modules',
|
|
|
|
$root . '/profiles',
|
2016-08-18 14:00:25 +00:00
|
|
|
$root . '/themes',
|
2017-03-04 01:20:24 +00:00
|
|
|
];
|
Issue #2304461 by dawehner, neclimdul, naveenvalecha, jibran, tim.plunkett, phenaproxima, Xano, hussainweb, sun, hitesh-jain, amateescu, alexpott, daffie, Mile23, Wim Leers, larowlan: KernelTestBaseTNG™
2015-08-18 00:25:27 +00:00
|
|
|
$sites_path = $root . '/sites';
|
2013-10-03 06:30:07 +00:00
|
|
|
// Note this also checks sites/../modules and sites/../profiles.
|
|
|
|
foreach (scandir($sites_path) as $site) {
|
Issue #2304461 by dawehner, neclimdul, naveenvalecha, jibran, tim.plunkett, phenaproxima, Xano, hussainweb, sun, hitesh-jain, amateescu, alexpott, daffie, Mile23, Wim Leers, larowlan: KernelTestBaseTNG™
2015-08-18 00:25:27 +00:00
|
|
|
if ($site[0] === '.' || $site === 'simpletest') {
|
|
|
|
continue;
|
|
|
|
}
|
2013-10-03 06:30:07 +00:00
|
|
|
$path = "$sites_path/$site";
|
|
|
|
$paths[] = is_dir("$path/modules") ? realpath("$path/modules") : NULL;
|
|
|
|
$paths[] = is_dir("$path/profiles") ? realpath("$path/profiles") : NULL;
|
2016-08-18 14:00:25 +00:00
|
|
|
$paths[] = is_dir("$path/themes") ? realpath("$path/themes") : NULL;
|
2013-10-03 06:30:07 +00:00
|
|
|
}
|
2021-09-23 10:01:26 +00:00
|
|
|
return array_filter($paths);
|
2013-03-07 02:48:50 +00:00
|
|
|
}
|
2013-04-04 20:53:37 +00:00
|
|
|
|
2013-10-03 06:30:07 +00:00
|
|
|
/**
|
|
|
|
* Registers the namespace for each extension directory with the autoloader.
|
|
|
|
*
|
|
|
|
* @param array $dirs
|
|
|
|
* An associative array of extension directories, keyed by extension name.
|
Issue #2304461 by dawehner, neclimdul, naveenvalecha, jibran, tim.plunkett, phenaproxima, Xano, hussainweb, sun, hitesh-jain, amateescu, alexpott, daffie, Mile23, Wim Leers, larowlan: KernelTestBaseTNG™
2015-08-18 00:25:27 +00:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* An associative array of extension directories, keyed by their namespace.
|
2013-10-03 06:30:07 +00:00
|
|
|
*/
|
Issue #2304461 by dawehner, neclimdul, naveenvalecha, jibran, tim.plunkett, phenaproxima, Xano, hussainweb, sun, hitesh-jain, amateescu, alexpott, daffie, Mile23, Wim Leers, larowlan: KernelTestBaseTNG™
2015-08-18 00:25:27 +00:00
|
|
|
function drupal_phpunit_get_extension_namespaces($dirs) {
|
2017-03-04 01:20:24 +00:00
|
|
|
$namespaces = [];
|
2013-10-03 06:30:07 +00:00
|
|
|
foreach ($dirs as $extension => $dir) {
|
2014-01-04 03:50:45 +00:00
|
|
|
if (is_dir($dir . '/src')) {
|
2014-06-01 21:20:56 +00:00
|
|
|
// Register the PSR-4 directory for module-provided classes.
|
Issue #2304461 by dawehner, neclimdul, naveenvalecha, jibran, tim.plunkett, phenaproxima, Xano, hussainweb, sun, hitesh-jain, amateescu, alexpott, daffie, Mile23, Wim Leers, larowlan: KernelTestBaseTNG™
2015-08-18 00:25:27 +00:00
|
|
|
$namespaces['Drupal\\' . $extension . '\\'][] = $dir . '/src';
|
2014-01-04 03:50:45 +00:00
|
|
|
}
|
2024-03-04 12:38:06 +00:00
|
|
|
if (is_dir($dir . '/tests/src')) {
|
|
|
|
// Register the PSR-4 directory for PHPUnit-based suites.
|
|
|
|
$namespaces['Drupal\\Tests\\' . $extension . '\\'][] = $dir . '/tests/src';
|
2014-01-04 03:50:45 +00:00
|
|
|
}
|
2013-10-03 06:30:07 +00:00
|
|
|
}
|
Issue #2304461 by dawehner, neclimdul, naveenvalecha, jibran, tim.plunkett, phenaproxima, Xano, hussainweb, sun, hitesh-jain, amateescu, alexpott, daffie, Mile23, Wim Leers, larowlan: KernelTestBaseTNG™
2015-08-18 00:25:27 +00:00
|
|
|
return $namespaces;
|
2013-10-03 06:30:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 17:51:39 +00:00
|
|
|
// We define the COMPOSER_INSTALL constant, so that PHPUnit knows where to
|
|
|
|
// autoload from. This is needed for tests run in isolation mode, because
|
|
|
|
// phpunit.xml.dist is located in a non-default directory relative to the
|
|
|
|
// PHPUnit executable.
|
|
|
|
if (!defined('PHPUNIT_COMPOSER_INSTALL')) {
|
|
|
|
define('PHPUNIT_COMPOSER_INSTALL', __DIR__ . '/../../autoload.php');
|
|
|
|
}
|
2015-12-12 01:02:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate class loader with additional namespaces for tests.
|
|
|
|
*
|
|
|
|
* We run this in a function to avoid setting the class loader to a global
|
Issue #3219513 by quietone, smustgrave, Vidushi Mehta, sourabhjain, rpayanm, longwave, xjm, dww, catch: Fix spelling for words used once, beginning with 'n' -> 'p', inclusive
2023-08-01 10:07:26 +00:00
|
|
|
* that can change. This change can cause unpredictable false positives for the
|
|
|
|
* PHPUnit global state change watcher. The class loader can be retrieved from
|
2015-12-12 01:02:55 +00:00
|
|
|
* composer at any time by requiring autoload.php.
|
|
|
|
*/
|
|
|
|
function drupal_phpunit_populate_class_loader() {
|
|
|
|
|
|
|
|
/** @var \Composer\Autoload\ClassLoader $loader */
|
|
|
|
$loader = require __DIR__ . '/../../autoload.php';
|
|
|
|
|
|
|
|
// Start with classes in known locations.
|
2019-10-04 22:05:43 +00:00
|
|
|
$loader->add('Drupal\\BuildTests', __DIR__);
|
2015-12-12 01:02:55 +00:00
|
|
|
$loader->add('Drupal\\Tests', __DIR__);
|
2018-04-05 22:50:12 +00:00
|
|
|
$loader->add('Drupal\\TestSite', __DIR__);
|
2015-12-12 01:02:55 +00:00
|
|
|
$loader->add('Drupal\\KernelTests', __DIR__);
|
Issue #2469713 by dawehner, pfrenssen, alexpott, elachlan, Mixologic, larowlan, hussainweb, benjy, jibran, Wim Leers, isntall: Step 2: Create a JavaScriptTestBase using PhantomJs Driver/Binary
2016-04-07 11:25:35 +00:00
|
|
|
$loader->add('Drupal\\FunctionalTests', __DIR__);
|
|
|
|
$loader->add('Drupal\\FunctionalJavascriptTests', __DIR__);
|
2019-10-14 13:23:50 +00:00
|
|
|
$loader->add('Drupal\\TestTools', __DIR__);
|
2015-12-12 01:02:55 +00:00
|
|
|
|
|
|
|
if (!isset($GLOBALS['namespaces'])) {
|
|
|
|
// Scan for arbitrary extension namespaces from core and contrib.
|
|
|
|
$extension_roots = drupal_phpunit_contrib_extension_directory_roots();
|
|
|
|
|
|
|
|
$dirs = array_map('drupal_phpunit_find_extension_directories', $extension_roots);
|
2017-03-04 01:20:24 +00:00
|
|
|
$dirs = array_reduce($dirs, 'array_merge', []);
|
2015-12-12 01:02:55 +00:00
|
|
|
$GLOBALS['namespaces'] = drupal_phpunit_get_extension_namespaces($dirs);
|
|
|
|
}
|
|
|
|
foreach ($GLOBALS['namespaces'] as $prefix => $paths) {
|
|
|
|
$loader->addPsr4($prefix, $paths);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $loader;
|
2020-02-12 09:28:33 +00:00
|
|
|
}
|
2015-12-12 01:02:55 +00:00
|
|
|
|
|
|
|
// Do class loader population.
|
2020-02-14 12:21:47 +00:00
|
|
|
$loader = drupal_phpunit_populate_class_loader();
|
2021-06-21 20:32:27 +00:00
|
|
|
class_alias('\Drupal\Tests\DocumentElement', '\Behat\Mink\Element\DocumentElement', TRUE);
|
2020-02-14 12:21:47 +00:00
|
|
|
|
|
|
|
ClassWriter::mutateTestBase($loader);
|
2013-10-03 06:30:07 +00:00
|
|
|
|
2013-05-07 23:29:47 +00:00
|
|
|
// Set sane locale settings, to ensure consistent string, dates, times and
|
|
|
|
// numbers handling.
|
2014-06-26 10:47:01 +00:00
|
|
|
// @see \Drupal\Core\DrupalKernel::bootEnvironment()
|
2024-04-23 08:44:54 +00:00
|
|
|
setlocale(LC_ALL, 'C.UTF-8', 'C');
|
2013-08-28 01:12:12 +00:00
|
|
|
|
2018-05-08 16:23:11 +00:00
|
|
|
// Set appropriate configuration for multi-byte strings.
|
|
|
|
mb_internal_encoding('utf-8');
|
|
|
|
mb_language('uni');
|
|
|
|
|
2013-08-28 01:12:12 +00:00
|
|
|
// Set the default timezone. While this doesn't cause any tests to fail, PHP
|
2015-08-11 08:15:35 +00:00
|
|
|
// complains if 'date.timezone' is not set in php.ini. The Australia/Sydney
|
|
|
|
// timezone is chosen so all tests are run using an edge case scenario (UTC+10
|
|
|
|
// and DST). This choice is made to prevent timezone related regressions and
|
|
|
|
// reduce the fragility of the testing system in general.
|
|
|
|
date_default_timezone_set('Australia/Sydney');
|
2015-08-26 23:29:29 +00:00
|
|
|
|
2022-06-17 14:41:39 +00:00
|
|
|
// Ensure ignored deprecation patterns listed in .deprecation-ignore.txt are
|
|
|
|
// considered in testing.
|
|
|
|
if (getenv('SYMFONY_DEPRECATIONS_HELPER') === FALSE) {
|
|
|
|
$deprecation_ignore_filename = realpath(__DIR__ . "/../.deprecation-ignore.txt");
|
|
|
|
putenv("SYMFONY_DEPRECATIONS_HELPER=ignoreFile=$deprecation_ignore_filename");
|
|
|
|
}
|
2023-11-25 11:33:10 +00:00
|
|
|
|
|
|
|
// Drupal expects to be run from its root directory. This ensures all test types
|
|
|
|
// are consistent.
|
|
|
|
chdir(dirname(__DIR__, 2));
|