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: * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Chris@0: * Use \Drupal\FunctionalTests\Update\UpdatePathTestBase. Chris@0: * @see https://www.drupal.org/node/2896640 Chris@0: * Chris@0: * @see hook_update_N() Chris@0: */ Chris@0: abstract class UpdatePathTestBase extends WebTestBase { Chris@0: Chris@0: use SchemaCheckTestTrait; 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: $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: Chris@0: // Set the update url. This must be set here rather than in Chris@0: // self::__construct() or the old URL generator will leak additional test Chris@0: // sites. Chris@0: $this->updateUrl = Url::fromRoute('system.db_update'); Chris@0: Chris@0: // These methods are called from parent::setUp(). Chris@0: $this->setBatch(); Chris@0: $this->initUserSession(); Chris@0: $this->prepareSettings(); 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: $this->initSettings(); Chris@0: $request = Request::createFromGlobals(); Chris@0: $container = $this->initKernel($request); Chris@0: $this->initConfig($container); Chris@0: Chris@0: // Add the config directories to settings.php. Chris@0: drupal_install_config_directories(); Chris@0: Chris@0: // Restore the original Simpletest batch. Chris@0: $this->restoreBatch(); 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@0: require_once \Drupal::root() . '/core/includes/update.inc'; 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@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@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: Chris@0: // Ensure there are no failed updates. Chris@0: if ($this->checkFailedUpdates) { Chris@0: $this->assertNoRaw('' . t('Failed:') . ''); 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@0: // Reset the static cache of drupal_get_installed_schema_version() so that Chris@0: // more complex update path testing works. Chris@0: drupal_static_reset('drupal_get_installed_schema_version'); 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: $typed_config->clearCachedDefinitions(); 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: $this->assertFalse($needs_updates, 'After all updates ran, entity schema is up to date.'); Chris@0: if ($needs_updates) { Chris@0: foreach (\Drupal::entityDefinitionUpdateManager() Chris@0: ->getChangeSummary() as $entity_type_id => $summary) { Chris@0: foreach ($summary as $message) { Chris@0: $this->fail($message); Chris@0: } Chris@0: } 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: }