Initial Drupal 11 with DDEV setup

This commit is contained in:
gluebox
2025-10-08 11:39:17 -04:00
commit 89ef74b305
25344 changed files with 2599172 additions and 0 deletions

View File

@ -0,0 +1,295 @@
<?php
declare(strict_types=1);
namespace Drupal\FunctionalTests\Update;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Html;
use Drupal\Core\Site\Settings;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Database\Database;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Url;
use Drupal\Tests\UpdatePathTestTrait;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a base class for writing an update test.
*
* To write an update test:
* - Write the hook_update_N() implementations that you are testing.
* - Create one or more database dump files, which will set the database to the
* "before updates" state. Normally, these will add some configuration data to
* the database, set up some tables/fields, etc.
* - Create a class that extends this class.
* - Ensure that the test is in the legacy group. Tests can have multiple
* groups.
* - In your setUp() method, point the $this->databaseDumpFiles variable to the
* database dump files, and then call parent::setUp() to run the base setUp()
* method in this class.
* - In your test method, call $this->runUpdates() to run the necessary updates,
* and then use test assertions to verify that the result is what you expect.
* - In order to test both with a "bare" database dump as well as with a
* database dump filled with content, extend your update path test class with
* a new test class that overrides the bare database dump. Refer to
* UpdatePathTestBaseFilledTest for an example.
*
* @ingroup update_api
*
* @see hook_update_N()
*/
abstract class UpdatePathTestBase extends BrowserTestBase {
use UpdatePathTestTrait {
runUpdates as doRunUpdates;
}
/**
* Modules to install after the database is loaded.
*
* @var string[]
*/
protected static $modules = [];
/**
* The file path(s) to the dumped database(s) to load into the child site.
*
* The file system/tests/fixtures/update/drupal-10.3.0.bare.standard.php.gz is
* normally included first -- this sets up the base database from a bare
* standard Drupal installation.
*
* The file system/tests/fixtures/update/drupal-10.3.0.filled.standard.php.gz
* can also be used in case we want to test with a database filled with
* content, and with all core modules enabled.
*
* @var array
*/
protected $databaseDumpFiles = [];
/**
* The update URL.
*
* @var string
*/
protected $updateUrl;
/**
* Disable strict config schema checking.
*
* The schema is verified at the end of running the update.
*
* @var bool
*/
protected $strictConfigSchema = FALSE;
/**
* Overrides BrowserTestBase::installDrupal() for update testing.
*
* The main difference in this method is that rather than performing the
* installation via the installer, a database is loaded. Additional work is
* then needed to set various things such as the config directories and the
* container that would normally be done via the installer.
*/
public function installDrupal() {
// Set the update URL. This must be set here rather than in
// self::__construct() or the old URL generator will leak additional test
// sites. Additionally, we need to prevent the path alias processor from
// running because we might not have a working alias system before running
// the updates.
$this->updateUrl = Url::fromRoute('system.db_update', [], ['path_processing' => FALSE]);
$this->initUserSession();
$this->prepareSettings();
$this->doInstall();
$this->initSettings();
$request = Request::createFromGlobals();
$container = $this->initKernel($request);
$this->initConfig($container);
// Add the config directories to settings.php.
$sync_directory = Settings::get('config_sync_directory');
\Drupal::service('file_system')->prepareDirectory($sync_directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
// Ensure the default temp directory exist and is writable. The configured
// temp directory may be removed during update.
\Drupal::service('file_system')->prepareDirectory($this->tempFilesDirectory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
// Set the container. parent::rebuildAll() would normally do this, but this
// not safe to do here, because the database has not been updated yet.
$this->container = \Drupal::getContainer();
$this->replaceUser1();
require_once $this->root . '/core/includes/update.inc';
}
/**
* {@inheritdoc}
*/
protected function doInstall() {
$this->runDbTasks();
// Allow classes to set database dump files.
$this->setDatabaseDumpFiles();
// Load the database(s).
foreach ($this->databaseDumpFiles as $file) {
if (str_ends_with($file, '.gz')) {
$file = "compress.zlib://$file";
}
require $file;
}
}
/**
* {@inheritdoc}
*/
protected function initFrontPage() {
// Do nothing as Drupal is not installed yet.
}
/**
* Set database dump files to be used.
*/
abstract protected function setDatabaseDumpFiles();
/**
* Add settings that are missed since the installer isn't run.
*/
protected function prepareSettings() {
parent::prepareSettings();
// Generate a hash salt.
$settings['settings']['hash_salt'] = (object) [
'value' => Crypt::randomBytesBase64(55),
'required' => TRUE,
];
// Since the installer isn't run, add the database settings here too.
$settings['databases']['default'] = (object) [
'value' => Database::getConnectionInfo(),
'required' => TRUE,
];
// Force every update hook to only run one entity per batch.
$settings['settings']['entity_update_batch_size'] = (object) [
'value' => 1,
'required' => TRUE,
];
// Set up sync directory.
$settings['settings']['config_sync_directory'] = (object) [
'value' => $this->publicFilesDirectory . '/config_sync',
'required' => TRUE,
];
$this->writeSettings($settings);
}
/**
* Helper function to run pending database updates.
*/
protected function runUpdates() {
$this->doRunUpdates($this->updateUrl);
}
/**
* Runs the install database tasks for the driver used by the test runner.
*/
protected function runDbTasks() {
// Create a minimal container so that t() works.
// @see install_begin_request()
$container = new ContainerBuilder();
$container->setParameter('language.default_values', Language::$defaultValues);
$container
->register('language.default', 'Drupal\Core\Language\LanguageDefault')
->addArgument('%language.default_values%');
$container
->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
->addArgument(new Reference('language.default'));
\Drupal::setContainer($container);
// Run database tasks and check for errors.
$installer_class = Database::getConnectionInfo()['default']['namespace'] . "\\Install\\Tasks";
$errors = (new $installer_class())->runTasks();
if (!empty($errors)) {
$this->fail('Failed to run installer database tasks: ' . implode(', ', $errors));
}
}
/**
* Replace User 1 with the user created here.
*/
protected function replaceUser1() {
// We try not to save content entities in hook_update_N() because the schema
// might be out of sync, or hook invocations might rely on other schemas
// that also aren't updated yet. Hence we are directly updating the database
// tables with the values.
Database::getConnection()->update('users_field_data')
->fields([
'name' => $this->rootUser->getAccountName(),
'pass' => \Drupal::service('password')->hash($this->rootUser->pass_raw),
'mail' => $this->rootUser->getEmail(),
])
->condition('uid', 1)
->execute();
}
/**
* Tests that the database was properly loaded.
*/
protected function testDatabaseLoaded() {
// Set a value in the cache to prove caches are cleared.
\Drupal::service('cache.default')->set(__CLASS__, 'Test');
/** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
$update_registry = \Drupal::service('update.update_hook_registry');
foreach (['user' => 10000, 'node' => 8700, 'system' => 10201, 'update_test_schema' => 8000] as $module => $schema) {
$this->assertEquals($schema, $update_registry->getInstalledVersion($module), "Module $module schema is $schema");
}
// Ensure that all {router} entries can be unserialized. If they cannot be
// unserialized a notice will be thrown by PHP.
$result = \Drupal::database()->select('router', 'r')
->fields('r', ['name', 'route'])
->execute()
->fetchAllKeyed(0, 1);
// For the purpose of fetching the notices and displaying more helpful error
// messages, let's override the error handler temporarily.
set_error_handler(function ($severity, $message, $filename, $lineno) {
throw new \ErrorException($message, 0, $severity, $filename, $lineno);
});
foreach ($result as $route_name => $route) {
try {
unserialize($route);
}
catch (\Exception $e) {
$this->fail(sprintf('Error "%s" while unserializing route %s', $e->getMessage(), Html::escape($route_name)));
}
}
restore_error_handler();
// Before accessing the site we need to run updates first or the site might
// be broken.
$this->runUpdates();
$this->assertEquals('standard', \Drupal::config('core.extension')->get('profile'));
$this->assertEquals('Site-Install', \Drupal::config('system.site')->get('name'));
$this->drupalGet('<front>');
$this->assertSession()->pageTextContains('Site-Install');
// Ensure that the database tasks have been run during set up. Neither MySQL
// nor SQLite make changes that are testable.
$database = $this->container->get('database');
if ($database->driver() == 'pgsql') {
$this->assertEquals('on', $database->query("SHOW standard_conforming_strings")->fetchField());
$this->assertEquals('escape', $database->query("SHOW bytea_output")->fetchField());
}
// Ensure the test runners cache has been cleared.
$this->assertFalse(\Drupal::service('cache.default')->get(__CLASS__));
}
}

View File

@ -0,0 +1,187 @@
<?php
declare(strict_types=1);
namespace Drupal\FunctionalTests\Update;
use Drupal\Core\Database\Database;
use Drupal\Core\Site\Settings;
/**
* Tests the update path base class.
*
* @group Update
*/
class UpdatePathTestBaseTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['update_test_schema'];
/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles(): void {
$this->databaseDumpFiles[] = __DIR__ . '/../../../../modules/system/tests/fixtures/update/drupal-10.3.0.bare.standard.php.gz';
$this->databaseDumpFiles[] = __DIR__ . '/../../../../modules/system/tests/fixtures/update/drupal-8.update-test-schema-enabled.php';
$this->databaseDumpFiles[] = __DIR__ . '/../../../../modules/system/tests/fixtures/update/drupal-8.update-test-semver-update-n-enabled.php';
}
/**
* Tests that the database was properly loaded.
*/
public function testDatabaseProperlyLoaded(): void {
$this->testDatabaseLoaded();
}
/**
* Tests that updates are properly run.
*/
public function testUpdateHookN(): void {
$connection = Database::getConnection();
// Increment the schema version.
\Drupal::state()->set('update_test_schema_version', 8001);
$this->runUpdates();
// Ensure that after running the updates the update functions have been
// loaded. If they have not then the tests carried out in
// \Drupal\Tests\UpdatePathTestTrait::runUpdates() can result in false
// positives.
$this->assertTrue(function_exists('update_test_semver_update_n_update_8001'), 'The update_test_semver_update_n_update_8001() has been loaded');
$select = $connection->select('watchdog');
$select->orderBy('wid', 'DESC');
$select->range(0, 5);
$select->fields('watchdog', ['message']);
$container_cannot_be_saved_messages = array_filter(iterator_to_array($select->execute()), function ($row) {
return str_contains($row->message, 'Container cannot be saved to cache.');
});
$this->assertEquals([], $container_cannot_be_saved_messages);
// Ensure schema has changed.
/** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
$update_registry = \Drupal::service('update.update_hook_registry');
$this->assertEquals(8001, $update_registry->getInstalledVersion('update_test_schema'));
$this->assertEquals(8001, $update_registry->getInstalledVersion('update_test_semver_update_n'));
// Ensure the index was added for column a.
$this->assertTrue($connection->schema()->indexExists('update_test_schema_table', 'test'), 'Version 8001 of the update_test_schema module is installed.');
// Ensure update_test_semver_update_n_update_8001 was run.
$this->assertEquals('Yes, I was run. Thanks for testing!', \Drupal::state()->get('update_test_semver_update_n_update_8001'));
}
/**
* Tests that path aliases are not processed during database updates.
*/
public function testPathAliasProcessing(): void {
// Add a path alias for the '/admin' system path.
$values = [
'path' => '/admin/structure',
'alias' => '/admin-structure-alias',
'langcode' => 'und',
'status' => 1,
];
$database = \Drupal::database();
$id = $database->insert('path_alias')
->fields($values + ['uuid' => \Drupal::service('uuid')->generate()])
->execute();
$revision_id = $database->insert('path_alias_revision')
->fields($values + ['id' => $id, 'revision_default' => 1])
->execute();
$database->update('path_alias')
->fields(['revision_id' => $revision_id])
->condition('id', $id)
->execute();
// Increment the schema version.
\Drupal::state()->set('update_test_schema_version', 8002);
$this->runUpdates();
// Check that the alias defined earlier is not used during the update
// process.
$this->assertSession()->linkByHrefExists('/admin/structure');
$this->assertSession()->linkByHrefNotExists('/admin-structure-alias');
$account = $this->createUser(['administer site configuration', 'access administration pages', 'access site reports']);
$this->drupalLogin($account);
// Go to the status report page and check that the alias is used.
$this->drupalGet('admin/reports/status');
$this->assertSession()->linkByHrefNotExists('/admin/structure');
$this->assertSession()->linkByHrefExists('/admin-structure-alias');
}
/**
* Tests that test running environment is updated when module list changes.
*
* @see update_test_schema_update_8003()
*/
public function testModuleListChange(): void {
// Set a value in the cache to prove caches are cleared.
\Drupal::service('cache.default')->set(__CLASS__, 'Test');
// Ensure that modules are installed and uninstalled as expected prior to
// running updates.
$extension_config = $this->config('core.extension')->get();
$this->assertArrayHasKey('page_cache', $extension_config['module']);
$this->assertArrayNotHasKey('module_test', $extension_config['module']);
$module_list = \Drupal::moduleHandler()->getModuleList();
$this->assertArrayHasKey('page_cache', $module_list);
$this->assertArrayNotHasKey('module_test', $module_list);
$namespaces = \Drupal::getContainer()->getParameter('container.namespaces');
$this->assertArrayHasKey('Drupal\page_cache', $namespaces);
$this->assertArrayNotHasKey('Drupal\module_test', $namespaces);
// Increment the schema version so that update_test_schema_update_8003()
// runs.
\Drupal::state()->set('update_test_schema_version', 8003);
$this->runUpdates();
// Ensure that test running environment has been updated with the changes to
// the module list.
$extension_config = $this->config('core.extension')->get();
$this->assertArrayNotHasKey('page_cache', $extension_config['module']);
$this->assertArrayHasKey('module_test', $extension_config['module']);
$module_list = \Drupal::moduleHandler()->getModuleList();
$this->assertArrayNotHasKey('page_cache', $module_list);
$this->assertArrayHasKey('module_test', $module_list);
$namespaces = \Drupal::getContainer()->getParameter('container.namespaces');
$this->assertArrayNotHasKey('Drupal\page_cache', $namespaces);
$this->assertArrayHasKey('Drupal\module_test', $namespaces);
// Ensure the test runners cache has been cleared.
$this->assertFalse(\Drupal::service('cache.default')->get(__CLASS__));
}
/**
* Tests that schema can be excluded from testing.
*
* @see \Drupal\FunctionalTests\Update\UpdatePathTestBase::runUpdates()
* @see \Drupal\Core\Test\TestSetupTrait::$configSchemaCheckerExclusions
*/
public function testSchemaChecking(): void {
// Create some configuration that should be skipped.
$this->config('config_schema_test.no_schema')->set('foo', 'bar')->save();
$this->runUpdates();
$this->assertSame('bar', $this->config('config_schema_test.no_schema')->get('foo'));
}
/**
* Tests that setup is done correctly.
*/
public function testSetup(): void {
$this->assertCount(3, $this->databaseDumpFiles);
$this->assertSame(1, Settings::get('entity_update_batch_size'));
}
}

View File

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Drupal\FunctionalTests\Update;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
/**
* Tests that update.php is accessible even if there are unstable modules.
*
* @group Update
*/
class UpdateReducedThemeRegistryTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['update_test_broken_theme_hook'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests that the update page can be accessed.
*/
public function testUpdatePageWithBrokenThemeHook(): void {
require_once $this->root . '/core/includes/update.inc';
$this->writeSettings([
'settings' => [
'update_free_access' => (object) [
'value' => TRUE,
'required' => TRUE,
],
],
]);
$this->drupalGet(Url::fromRoute('system.db_update'));
$this->assertSession()->statusCodeEquals(200);
}
}