Chris@0: databaseDumpFiles variable to the Chris@0: * database dump files, and then call parent::setUp() to run the base setUp() Chris@0: * method in this class. Chris@0: * - In your test method, call $this->runUpdates() to run the necessary updates, Chris@0: * and then use test assertions to verify that the result is what you expect. Chris@0: * - In order to test both with a "bare" database dump as well as with a Chris@0: * database dump filled with content, extend your update path test class with Chris@0: * a new test class that overrides the bare database dump. Refer to Chris@0: * UpdatePathTestBaseFilledTest for an example. Chris@0: * Chris@0: * @ingroup update_api Chris@0: * Chris@0: * @see hook_update_N() Chris@0: */ Chris@0: abstract class UpdatePathTestBase extends BrowserTestBase { Chris@0: Chris@0: use SchemaCheckTestTrait; Chris@18: use RequirementsPageTrait; Chris@0: Chris@0: /** Chris@0: * Modules to enable after the database is loaded. Chris@0: */ Chris@0: protected static $modules = []; Chris@0: Chris@0: /** Chris@0: * The file path(s) to the dumped database(s) to load into the child site. Chris@0: * Chris@0: * The file system/tests/fixtures/update/drupal-8.bare.standard.php.gz is Chris@0: * normally included first -- this sets up the base database from a bare Chris@0: * standard Drupal installation. Chris@0: * Chris@0: * The file system/tests/fixtures/update/drupal-8.filled.standard.php.gz Chris@0: * can also be used in case we want to test with a database filled with Chris@0: * content, and with all core modules enabled. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: protected $databaseDumpFiles = []; Chris@0: Chris@0: /** Chris@0: * The install profile used in the database dump file. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $installProfile = 'standard'; Chris@0: Chris@0: /** Chris@0: * Flag that indicates whether the child site has been updated. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: protected $upgradedSite = FALSE; Chris@0: Chris@0: /** Chris@0: * Array of errors triggered during the update process. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: protected $upgradeErrors = []; Chris@0: Chris@0: /** Chris@0: * Array of modules loaded when the test starts. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: protected $loadedModules = []; Chris@0: Chris@0: /** Chris@0: * Flag to indicate whether zlib is installed or not. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: protected $zlibInstalled = TRUE; Chris@0: Chris@0: /** Chris@0: * Flag to indicate whether there are pending updates or not. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: protected $pendingUpdates = TRUE; Chris@0: Chris@0: /** Chris@0: * The update URL. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $updateUrl; Chris@0: Chris@0: /** Chris@0: * Disable strict config schema checking. Chris@0: * Chris@0: * The schema is verified at the end of running the update. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: protected $strictConfigSchema = FALSE; Chris@0: Chris@0: /** Chris@0: * Fail the test if there are failed updates. Chris@0: * Chris@0: * @var bool Chris@0: */ Chris@0: protected $checkFailedUpdates = TRUE; Chris@0: Chris@0: /** Chris@0: * Constructs an UpdatePathTestCase object. Chris@0: * Chris@0: * @param $test_id Chris@0: * (optional) The ID of the test. Tests with the same id are reported Chris@0: * together. Chris@0: */ Chris@0: public function __construct($test_id = NULL) { Chris@0: parent::__construct($test_id); Chris@0: $this->zlibInstalled = function_exists('gzopen'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Overrides WebTestBase::setUp() for update testing. Chris@0: * Chris@0: * The main difference in this method is that rather than performing the Chris@0: * installation via the installer, a database is loaded. Additional work is Chris@0: * then needed to set various things such as the config directories and the Chris@0: * container that would normally be done via the installer. Chris@0: */ Chris@0: protected function setUp() { Chris@0: $request = Request::createFromGlobals(); Chris@0: Chris@0: // Boot up Drupal into a state where calling the database API is possible. Chris@0: // This is used to initialize the database system, so we can load the dump Chris@0: // files. Chris@0: $autoloader = require $this->root . '/autoload.php'; Chris@0: $kernel = TestRunnerKernel::createFromRequest($request, $autoloader); Chris@0: $kernel->loadLegacyIncludes(); Chris@0: Chris@17: // Set the update url. This must be set here rather than in Chris@17: // self::__construct() or the old URL generator will leak additional test Chris@18: // sites. Additionally, we need to prevent the path alias processor from Chris@18: // running because we might not have a working alias system before running Chris@18: // the updates. Chris@18: $this->updateUrl = Url::fromRoute('system.db_update', [], ['path_processing' => FALSE]); Chris@17: Chris@17: $this->setupBaseUrl(); Chris@17: Chris@17: // Install Drupal test site. Chris@17: $this->prepareEnvironment(); Chris@0: $this->runDbTasks(); Chris@0: // Allow classes to set database dump files. Chris@0: $this->setDatabaseDumpFiles(); Chris@0: Chris@0: // We are going to set a missing zlib requirement property for usage Chris@0: // during the performUpgrade() and tearDown() methods. Also set that the Chris@0: // tests failed. Chris@0: if (!$this->zlibInstalled) { Chris@0: parent::setUp(); Chris@0: return; Chris@0: } Chris@0: $this->installDrupal(); Chris@0: Chris@0: // Add the config directories to settings.php. Chris@0: drupal_install_config_directories(); Chris@0: Chris@0: // Set the container. parent::rebuildAll() would normally do this, but this Chris@0: // not safe to do here, because the database has not been updated yet. Chris@0: $this->container = \Drupal::getContainer(); Chris@0: Chris@0: $this->replaceUser1(); Chris@0: Chris@17: require_once $this->root . '/core/includes/update.inc'; Chris@0: Chris@0: // Setup Mink. Chris@17: $this->initMink(); Chris@0: Chris@0: // Set up the browser test output file. Chris@0: $this->initBrowserOutputFile(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function installDrupal() { Chris@0: $this->initUserSession(); Chris@0: $this->prepareSettings(); Chris@0: $this->doInstall(); Chris@0: $this->initSettings(); Chris@0: Chris@0: $request = Request::createFromGlobals(); Chris@0: $container = $this->initKernel($request); Chris@0: $this->initConfig($container); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doInstall() { Chris@0: $this->runDbTasks(); Chris@0: // Allow classes to set database dump files. Chris@0: $this->setDatabaseDumpFiles(); Chris@0: Chris@0: // Load the database(s). Chris@0: foreach ($this->databaseDumpFiles as $file) { Chris@0: if (substr($file, -3) == '.gz') { Chris@0: $file = "compress.zlib://$file"; Chris@0: } Chris@0: require $file; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: protected function initFrontPage() { Chris@17: // Do nothing as Drupal is not installed yet. Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set database dump files to be used. Chris@0: */ Chris@0: abstract protected function setDatabaseDumpFiles(); Chris@0: Chris@0: /** Chris@0: * Add settings that are missed since the installer isn't run. Chris@0: */ Chris@0: protected function prepareSettings() { Chris@0: parent::prepareSettings(); Chris@0: Chris@0: // Remember the profile which was used. Chris@0: $settings['settings']['install_profile'] = (object) [ Chris@0: 'value' => $this->installProfile, Chris@0: 'required' => TRUE, Chris@0: ]; Chris@0: // Generate a hash salt. Chris@0: $settings['settings']['hash_salt'] = (object) [ Chris@0: 'value' => Crypt::randomBytesBase64(55), Chris@0: 'required' => TRUE, Chris@0: ]; Chris@0: Chris@0: // Since the installer isn't run, add the database settings here too. Chris@0: $settings['databases']['default'] = (object) [ Chris@0: 'value' => Database::getConnectionInfo(), Chris@0: 'required' => TRUE, Chris@0: ]; Chris@0: Chris@18: // Force every update hook to only run one entity per batch. Chris@18: $settings['entity_update_batch_size'] = (object) [ Chris@18: 'value' => 1, Chris@18: 'required' => TRUE, Chris@18: ]; Chris@18: Chris@0: $this->writeSettings($settings); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Helper function to run pending database updates. Chris@0: */ Chris@0: protected function runUpdates() { Chris@0: if (!$this->zlibInstalled) { Chris@0: $this->fail('Missing zlib requirement for update tests.'); Chris@0: return FALSE; Chris@0: } Chris@0: // The site might be broken at the time so logging in using the UI might Chris@0: // not work, so we use the API itself. Chris@0: drupal_rewrite_settings([ Chris@0: 'settings' => [ Chris@0: 'update_free_access' => (object) [ Chris@0: 'value' => TRUE, Chris@0: 'required' => TRUE, Chris@0: ], Chris@0: ], Chris@0: ]); Chris@0: Chris@0: $this->drupalGet($this->updateUrl); Chris@18: $this->updateRequirementsProblem(); Chris@0: $this->clickLink(t('Continue')); Chris@0: Chris@0: $this->doSelectionTest(); Chris@0: // Run the update hooks. Chris@0: $this->clickLink(t('Apply pending updates')); Chris@0: $this->checkForMetaRefresh(); Chris@0: Chris@0: // Ensure there are no failed updates. Chris@0: if ($this->checkFailedUpdates) { Chris@17: $failure = $this->cssSelect('.failure'); Chris@17: if ($failure) { Chris@17: $this->fail('The update failed with the following message: "' . reset($failure)->getText() . '"'); Chris@17: } Chris@0: Chris@0: // Ensure that there are no pending updates. Chris@0: foreach (['update', 'post_update'] as $update_type) { Chris@0: switch ($update_type) { Chris@0: case 'update': Chris@0: $all_updates = update_get_update_list(); Chris@0: break; Chris@0: case 'post_update': Chris@0: $all_updates = \Drupal::service('update.post_update_registry')->getPendingUpdateInformation(); Chris@0: break; Chris@0: } Chris@0: foreach ($all_updates as $module => $updates) { Chris@0: if (!empty($updates['pending'])) { Chris@0: foreach (array_keys($updates['pending']) as $update_name) { Chris@0: $this->fail("The $update_name() update function from the $module module did not run."); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@18: Chris@18: // Ensure that the container is updated if any modules are installed or Chris@18: // uninstalled during the update. Chris@18: /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */ Chris@18: $module_handler = $this->container->get('module_handler'); Chris@18: $config_module_list = $this->config('core.extension')->get('module'); Chris@18: $module_handler_list = $module_handler->getModuleList(); Chris@18: $modules_installed = FALSE; Chris@18: // Modules that are in configuration but not the module handler have been Chris@18: // installed. Chris@18: foreach (array_keys(array_diff_key($config_module_list, $module_handler_list)) as $module) { Chris@18: $module_handler->addModule($module, drupal_get_path('module', $module)); Chris@18: $modules_installed = TRUE; Chris@18: } Chris@18: $modules_uninstalled = FALSE; Chris@18: $module_handler_list = $module_handler->getModuleList(); Chris@18: // Modules that are in the module handler but not configuration have been Chris@18: // uninstalled. Chris@18: foreach (array_keys(array_diff_key($module_handler_list, $config_module_list)) as $module) { Chris@18: $modules_uninstalled = TRUE; Chris@18: unset($module_handler_list[$module]); Chris@18: } Chris@18: if ($modules_installed || $modules_uninstalled) { Chris@18: // Note that resetAll() does not reset the kernel module list so we Chris@18: // have to do that manually. Chris@18: $this->kernel->updateModules($module_handler_list, $module_handler_list); Chris@18: } Chris@18: Chris@18: // If we have successfully clicked 'Apply pending updates' then we need to Chris@18: // clear the caches in the update test runner as this has occurred as part Chris@18: // of the updates. Chris@18: $this->resetAll(); Chris@0: Chris@0: // The config schema can be incorrect while the update functions are being Chris@0: // executed. But once the update has been completed, it needs to be valid Chris@0: // again. Assert the schema of all configuration objects now. Chris@0: $names = $this->container->get('config.storage')->listAll(); Chris@0: /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config */ Chris@0: $typed_config = $this->container->get('config.typed'); Chris@0: foreach ($names as $name) { Chris@0: $config = $this->config($name); Chris@0: $this->assertConfigSchema($typed_config, $name, $config->get()); Chris@0: } Chris@0: Chris@0: // Ensure that the update hooks updated all entity schema. Chris@0: $needs_updates = \Drupal::entityDefinitionUpdateManager()->needsUpdates(); Chris@0: if ($needs_updates) { Chris@16: foreach (\Drupal::entityDefinitionUpdateManager()->getChangeSummary() as $entity_type_id => $summary) { Chris@16: $entity_type_label = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getLabel(); Chris@0: foreach ($summary as $message) { Chris@16: $this->fail("$entity_type_label: $message"); Chris@0: } Chris@0: } Chris@14: // The above calls to `fail()` should prevent this from ever being Chris@14: // called, but it is here in case something goes really wrong. Chris@14: $this->assertFalse($needs_updates, 'After all updates ran, entity schema is up to date.'); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Runs the install database tasks for the driver used by the test runner. Chris@0: */ Chris@0: protected function runDbTasks() { Chris@0: // Create a minimal container so that t() works. Chris@0: // @see install_begin_request() Chris@0: $container = new ContainerBuilder(); Chris@0: $container->setParameter('language.default_values', Language::$defaultValues); Chris@0: $container Chris@0: ->register('language.default', 'Drupal\Core\Language\LanguageDefault') Chris@0: ->addArgument('%language.default_values%'); Chris@0: $container Chris@0: ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager') Chris@0: ->addArgument(new Reference('language.default')); Chris@0: \Drupal::setContainer($container); Chris@0: Chris@0: require_once __DIR__ . '/../../../../includes/install.inc'; Chris@0: $connection = Database::getConnection(); Chris@0: $errors = db_installer_object($connection->driver())->runTasks(); Chris@0: if (!empty($errors)) { Chris@0: $this->fail('Failed to run installer database tasks: ' . implode(', ', $errors)); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Replace User 1 with the user created here. Chris@0: */ Chris@0: protected function replaceUser1() { Chris@0: /** @var \Drupal\user\UserInterface $account */ Chris@0: // @todo: Saving the account before the update is problematic. Chris@0: // https://www.drupal.org/node/2560237 Chris@0: $account = User::load(1); Chris@0: $account->setPassword($this->rootUser->pass_raw); Chris@0: $account->setEmail($this->rootUser->getEmail()); Chris@18: $account->setUsername($this->rootUser->getAccountName()); Chris@0: $account->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the selection page. Chris@0: */ Chris@0: protected function doSelectionTest() { Chris@0: // No-op. Tests wishing to do test the selection page or the general Chris@0: // update.php environment before running update.php can override this method Chris@0: // and implement their required tests. Chris@0: } Chris@0: Chris@0: }