From 21dd26383579deec77298abd833f09f38bae0a81 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 26 Feb 2018 16:56:46 +0000 Subject: [PATCH] Issue #2907728 by Lendude, vaplas, dawehner, jibran, alexpott, Mile23: Installer: Convert system functional tests to phpunit --- .../src/Functional/MigrateUpgradeTestBase.php | 22 -- .../ConfigAfterInstallerTestBase.php | 5 + .../ConfigAfterInstallerTestBase.php | 44 +++ ...istributionProfileExistingSettingsTest.php | 7 +- .../Installer/DistributionProfileTest.php | 8 +- ...istributionProfileTranslationQueryTest.php | 25 +- .../DistributionProfileTranslationTest.php | 27 +- ...ConfigDirectorySetNoDirectoryErrorTest.php | 7 +- ...allerConfigDirectorySetNoDirectoryTest.php | 7 +- .../InstallerDatabaseErrorMessagesTest.php | 3 +- .../Installer/InstallerEmptySettingsTest.php | 11 +- .../InstallerExistingConfigDirectoryTest.php | 10 +- .../InstallerExistingDatabaseSettingsTest.php | 7 +- .../InstallerExistingInstallationTest.php | 11 +- ...stingSettingsMismatchProfileBrokenTest.php | 8 +- ...lerExistingSettingsMismatchProfileTest.php | 8 +- ...InstallerExistingSettingsNoProfileTest.php | 9 +- .../InstallerExistingSettingsTest.php | 8 +- .../InstallerLanguageDirectionTest.php | 8 +- .../Installer/InstallerLanguagePageTest.php | 3 +- .../Installer/InstallerTest.php | 14 +- .../Installer/InstallerTestBase.php | 316 ++++++++++++++++++ ...TranslationMultipleLanguageForeignTest.php | 2 +- ...slationMultipleLanguageKeepEnglishTest.php | 2 +- ...stallerTranslationMultipleLanguageTest.php | 4 +- .../InstallerTranslationQueryTest.php | 10 +- .../Installer/InstallerTranslationTest.php | 7 +- .../Installer/MinimalInstallerTest.php | 2 +- .../MultipleDistributionsProfileTest.php | 10 +- .../Installer/SingleVisibleProfileTest.php | 10 +- .../Installer/SiteNameTest.php | 6 +- .../Installer/StandardInstallerTest.php | 4 +- core/tests/Drupal/Tests/BrowserTestBase.php | 52 ++- 33 files changed, 502 insertions(+), 175 deletions(-) create mode 100644 core/tests/Drupal/FunctionalTests/Installer/ConfigAfterInstallerTestBase.php rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/DistributionProfileExistingSettingsTest.php (96%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/DistributionProfileTest.php (94%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/DistributionProfileTranslationQueryTest.php (80%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/DistributionProfileTranslationTest.php (79%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerConfigDirectorySetNoDirectoryErrorTest.php (92%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerConfigDirectorySetNoDirectoryTest.php (91%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerDatabaseErrorMessagesTest.php (92%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerEmptySettingsTest.php (61%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerExistingConfigDirectoryTest.php (82%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerExistingDatabaseSettingsTest.php (91%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerExistingInstallationTest.php (81%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerExistingSettingsMismatchProfileBrokenTest.php (95%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerExistingSettingsMismatchProfileTest.php (95%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerExistingSettingsNoProfileTest.php (93%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerExistingSettingsTest.php (94%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerLanguageDirectionTest.php (85%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerLanguagePageTest.php (93%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerTest.php (91%) create mode 100644 core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerTranslationMultipleLanguageForeignTest.php (92%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerTranslationMultipleLanguageKeepEnglishTest.php (89%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerTranslationMultipleLanguageTest.php (98%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerTranslationQueryTest.php (87%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/InstallerTranslationTest.php (96%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/MinimalInstallerTest.php (91%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/MultipleDistributionsProfileTest.php (91%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/SingleVisibleProfileTest.php (89%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/SiteNameTest.php (86%) rename core/{modules/system/src/Tests => tests/Drupal/FunctionalTests}/Installer/StandardInstallerTest.php (95%) diff --git a/core/modules/migrate_drupal_ui/tests/src/Functional/MigrateUpgradeTestBase.php b/core/modules/migrate_drupal_ui/tests/src/Functional/MigrateUpgradeTestBase.php index 23d5c295f3c..c09b7ca7233 100644 --- a/core/modules/migrate_drupal_ui/tests/src/Functional/MigrateUpgradeTestBase.php +++ b/core/modules/migrate_drupal_ui/tests/src/Functional/MigrateUpgradeTestBase.php @@ -97,28 +97,6 @@ abstract class MigrateUpgradeTestBase extends BrowserTestBase { parent::tearDown(); } - /** - * Transforms a nested array into a flat array suitable for BrowserTestBase::drupalPostForm(). - * - * @param array $values - * A multi-dimensional form values array to convert. - * - * @return array - * The flattened $edit array suitable for BrowserTestBase::drupalPostForm(). - */ - protected function translatePostValues(array $values) { - $edit = []; - // The easiest and most straightforward way to translate values suitable for - // BrowserTestBase::drupalPostForm() is to actually build the POST data string - // and convert the resulting key/value pairs back into a flat array. - $query = http_build_query($values); - foreach (explode('&', $query) as $item) { - list($key, $value) = explode('=', $item); - $edit[urldecode($key)] = urldecode($value); - } - return $edit; - } - /** * Tests the displayed upgrade paths. * diff --git a/core/modules/system/src/Tests/Installer/ConfigAfterInstallerTestBase.php b/core/modules/system/src/Tests/Installer/ConfigAfterInstallerTestBase.php index 380e236cd36..43d8d48ec45 100644 --- a/core/modules/system/src/Tests/Installer/ConfigAfterInstallerTestBase.php +++ b/core/modules/system/src/Tests/Installer/ConfigAfterInstallerTestBase.php @@ -2,6 +2,8 @@ namespace Drupal\system\Tests\Installer; +@trigger_error(__NAMESPACE__ . '\ConfigAfterInstallerTestBase is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\FunctionalTests\Installer\ConfigAfterInstallerTestBase.', E_USER_DEPRECATED); + use Drupal\Core\Config\FileStorage; use Drupal\Core\Config\InstallStorage; use Drupal\Core\Config\StorageInterface; @@ -10,6 +12,9 @@ use Drupal\simpletest\InstallerTestBase; /** * Provides a class for install profiles to check their installed config. + * + * @deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. + * Use \Drupal\FunctionalTests\Installer\ConfigAfterInstallerTestBase. */ abstract class ConfigAfterInstallerTestBase extends InstallerTestBase { diff --git a/core/tests/Drupal/FunctionalTests/Installer/ConfigAfterInstallerTestBase.php b/core/tests/Drupal/FunctionalTests/Installer/ConfigAfterInstallerTestBase.php new file mode 100644 index 00000000000..98bcb05aed9 --- /dev/null +++ b/core/tests/Drupal/FunctionalTests/Installer/ConfigAfterInstallerTestBase.php @@ -0,0 +1,44 @@ +addToAssertionCount(1); + /** @var \Drupal\Core\Config\StorageInterface $active_config_storage */ + $active_config_storage = $this->container->get('config.storage'); + /** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */ + $config_manager = $this->container->get('config.manager'); + + $default_install_path = 'core/profiles/' . $this->profile . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY; + $profile_config_storage = new FileStorage($default_install_path, StorageInterface::DEFAULT_COLLECTION); + + foreach ($profile_config_storage->listAll() as $config_name) { + $result = $config_manager->diff($profile_config_storage, $active_config_storage, $config_name); + try { + $this->assertConfigDiff($result, $config_name, $skipped_config); + } + catch (\Exception $e) { + $this->fail($e->getMessage()); + } + } + } + +} diff --git a/core/modules/system/src/Tests/Installer/DistributionProfileExistingSettingsTest.php b/core/tests/Drupal/FunctionalTests/Installer/DistributionProfileExistingSettingsTest.php similarity index 96% rename from core/modules/system/src/Tests/Installer/DistributionProfileExistingSettingsTest.php rename to core/tests/Drupal/FunctionalTests/Installer/DistributionProfileExistingSettingsTest.php index 48b9fcb753f..f5bcf43df8e 100644 --- a/core/modules/system/src/Tests/Installer/DistributionProfileExistingSettingsTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/DistributionProfileExistingSettingsTest.php @@ -1,12 +1,11 @@ info = [ 'type' => 'profile', 'core' => \Drupal::CORE_COMPATIBILITY, @@ -71,7 +71,6 @@ class DistributionProfileExistingSettingsTest extends InstallerTestBase { ], ]; mkdir($this->settings['config_directories'][CONFIG_SYNC_DIRECTORY]->value, 0777, TRUE); - parent::setUp(); } /** diff --git a/core/modules/system/src/Tests/Installer/DistributionProfileTest.php b/core/tests/Drupal/FunctionalTests/Installer/DistributionProfileTest.php similarity index 94% rename from core/modules/system/src/Tests/Installer/DistributionProfileTest.php rename to core/tests/Drupal/FunctionalTests/Installer/DistributionProfileTest.php index 3d2c4c44ab6..7306e975534 100644 --- a/core/modules/system/src/Tests/Installer/DistributionProfileTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/DistributionProfileTest.php @@ -1,10 +1,9 @@ info = [ 'type' => 'profile', 'core' => \Drupal::CORE_COMPATIBILITY, @@ -36,8 +36,6 @@ class DistributionProfileTest extends InstallerTestBase { $path = $this->siteDirectory . '/profiles/mydistro'; mkdir($path, 0777, TRUE); file_put_contents("$path/mydistro.info.yml", Yaml::encode($this->info)); - - parent::setUp(); } /** diff --git a/core/modules/system/src/Tests/Installer/DistributionProfileTranslationQueryTest.php b/core/tests/Drupal/FunctionalTests/Installer/DistributionProfileTranslationQueryTest.php similarity index 80% rename from core/modules/system/src/Tests/Installer/DistributionProfileTranslationQueryTest.php rename to core/tests/Drupal/FunctionalTests/Installer/DistributionProfileTranslationQueryTest.php index 85b1e44ec43..d3bac1acfd8 100644 --- a/core/modules/system/src/Tests/Installer/DistributionProfileTranslationQueryTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/DistributionProfileTranslationQueryTest.php @@ -1,16 +1,15 @@ info = [ 'type' => 'profile', 'core' => \Drupal::CORE_COMPATIBILITY, @@ -43,22 +43,19 @@ class DistributionProfileTranslationQueryTest extends InstallerTestBase { ], ]; // File API functions are not available yet. - $path = $this->siteDirectory . '/profiles/mydistro'; + $path = $this->root . DIRECTORY_SEPARATOR . $this->siteDirectory . '/profiles/mydistro'; mkdir($path, 0777, TRUE); file_put_contents("$path/mydistro.info.yml", Yaml::encode($this->info)); - - parent::setUp(); + // Place a custom local translation in the translations directory. + mkdir($this->root . '/' . $this->siteDirectory . '/files/translations', 0777, TRUE); + file_put_contents($this->root . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.de.po', $this->getPo('de')); + file_put_contents($this->root . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.fr.po', $this->getPo('fr')); } /** * {@inheritdoc} */ protected function visitInstaller() { - // Place a custom local translation in the translations directory. - mkdir(\Drupal::root() . '/' . $this->siteDirectory . '/files/translations', 0777, TRUE); - file_put_contents(\Drupal::root() . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.de.po', $this->getPo('de')); - file_put_contents(\Drupal::root() . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.fr.po', $this->getPo('fr')); - // Pass a different language code than the one set in the distribution // profile. This distribution language should still be used. // The unrouted URL assembler does not exist at this point, so we build the @@ -88,11 +85,11 @@ class DistributionProfileTranslationQueryTest extends InstallerTestBase { // The language should have been automatically detected, all following // screens should be translated already. $elements = $this->xpath('//input[@type="submit"]/@value'); - $this->assertEqual((string) current($elements), 'Save and continue de'); + $this->assertEqual(current($elements)->getText(), 'Save and continue de'); $this->translations['Save and continue'] = 'Save and continue de'; // Check the language direction. - $direction = (string) current($this->xpath('/html/@dir')); + $direction = $this->getSession()->getPage()->find('xpath', '/@dir')->getText(); $this->assertEqual($direction, 'ltr'); // Verify that the distribution name appears. diff --git a/core/modules/system/src/Tests/Installer/DistributionProfileTranslationTest.php b/core/tests/Drupal/FunctionalTests/Installer/DistributionProfileTranslationTest.php similarity index 79% rename from core/modules/system/src/Tests/Installer/DistributionProfileTranslationTest.php rename to core/tests/Drupal/FunctionalTests/Installer/DistributionProfileTranslationTest.php index a4c8091ecdf..6e87495693e 100644 --- a/core/modules/system/src/Tests/Installer/DistributionProfileTranslationTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/DistributionProfileTranslationTest.php @@ -1,16 +1,15 @@ info = [ 'type' => 'profile', 'core' => \Drupal::CORE_COMPATIBILITY, @@ -43,22 +43,13 @@ class DistributionProfileTranslationTest extends InstallerTestBase { ], ]; // File API functions are not available yet. - $path = $this->siteDirectory . '/profiles/mydistro'; + $path = $this->root . DIRECTORY_SEPARATOR . $this->siteDirectory . '/profiles/mydistro'; mkdir($path, 0777, TRUE); file_put_contents("$path/mydistro.info.yml", Yaml::encode($this->info)); - parent::setUp(); - } - - /** - * {@inheritdoc} - */ - protected function visitInstaller() { // Place a custom local translation in the translations directory. - mkdir(\Drupal::root() . '/' . $this->siteDirectory . '/files/translations', 0777, TRUE); - file_put_contents(\Drupal::root() . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.de.po', $this->getPo('de')); - - parent::visitInstaller(); + mkdir($this->root . '/' . $this->siteDirectory . '/files/translations', 0777, TRUE); + file_put_contents($this->root . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.de.po', $this->getPo('de')); } /** @@ -83,11 +74,11 @@ class DistributionProfileTranslationTest extends InstallerTestBase { // The language should have been automatically detected, all following // screens should be translated already. $elements = $this->xpath('//input[@type="submit"]/@value'); - $this->assertEqual((string) current($elements), 'Save and continue de'); + $this->assertEqual(current($elements)->getText(), 'Save and continue de'); $this->translations['Save and continue'] = 'Save and continue de'; // Check the language direction. - $direction = (string) current($this->xpath('/html/@dir')); + $direction = current($this->xpath('/@dir'))->getText(); $this->assertEqual($direction, 'ltr'); // Verify that the distribution name appears. diff --git a/core/modules/system/src/Tests/Installer/InstallerConfigDirectorySetNoDirectoryErrorTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerConfigDirectorySetNoDirectoryErrorTest.php similarity index 92% rename from core/modules/system/src/Tests/Installer/InstallerConfigDirectorySetNoDirectoryErrorTest.php rename to core/tests/Drupal/FunctionalTests/Installer/InstallerConfigDirectorySetNoDirectoryErrorTest.php index 2ab980feadd..7de0652fe93 100644 --- a/core/modules/system/src/Tests/Installer/InstallerConfigDirectorySetNoDirectoryErrorTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerConfigDirectorySetNoDirectoryErrorTest.php @@ -1,9 +1,8 @@ configDirectory = $this->publicFilesDirectory . '/config_' . Crypt::randomBytesBase64(); $this->settings['config_directories'][CONFIG_SYNC_DIRECTORY] = (object) [ 'value' => $this->configDirectory . '/sync', @@ -32,7 +32,6 @@ class InstallerConfigDirectorySetNoDirectoryErrorTest extends InstallerTestBase mkdir($this->publicFilesDirectory); // Create a file so the directory can not be created. file_put_contents($this->configDirectory, 'Test'); - parent::setUp(); } /** diff --git a/core/modules/system/src/Tests/Installer/InstallerConfigDirectorySetNoDirectoryTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerConfigDirectorySetNoDirectoryTest.php similarity index 91% rename from core/modules/system/src/Tests/Installer/InstallerConfigDirectorySetNoDirectoryTest.php rename to core/tests/Drupal/FunctionalTests/Installer/InstallerConfigDirectorySetNoDirectoryTest.php index 11febf2d6f0..71e8b0594d9 100644 --- a/core/modules/system/src/Tests/Installer/InstallerConfigDirectorySetNoDirectoryTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerConfigDirectorySetNoDirectoryTest.php @@ -1,9 +1,8 @@ syncDirectory = $this->publicFilesDirectory . '/config_' . Crypt::randomBytesBase64() . '/sync'; $this->settings['config_directories'][CONFIG_SYNC_DIRECTORY] = (object) [ 'value' => $this->syncDirectory, @@ -33,7 +33,6 @@ class InstallerConfigDirectorySetNoDirectoryTest extends InstallerTestBase { 'value' => $this->publicFilesDirectory . '/config_custom', 'required' => TRUE, ]; - parent::setUp(); } /** diff --git a/core/modules/system/src/Tests/Installer/InstallerDatabaseErrorMessagesTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerDatabaseErrorMessagesTest.php similarity index 92% rename from core/modules/system/src/Tests/Installer/InstallerDatabaseErrorMessagesTest.php rename to core/tests/Drupal/FunctionalTests/Installer/InstallerDatabaseErrorMessagesTest.php index c98a5de0b73..b6ad437677d 100644 --- a/core/modules/system/src/Tests/Installer/InstallerDatabaseErrorMessagesTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerDatabaseErrorMessagesTest.php @@ -1,9 +1,8 @@ siteDirectory . '/settings.php'); - parent::setUp(); + $path = $this->root . DIRECTORY_SEPARATOR . $this->siteDirectory; + file_put_contents($path . '/settings.php', ''); } /** diff --git a/core/modules/system/src/Tests/Installer/InstallerExistingConfigDirectoryTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerExistingConfigDirectoryTest.php similarity index 82% rename from core/modules/system/src/Tests/Installer/InstallerExistingConfigDirectoryTest.php rename to core/tests/Drupal/FunctionalTests/Installer/InstallerExistingConfigDirectoryTest.php index b06b4caf6ec..2fd69fa511b 100644 --- a/core/modules/system/src/Tests/Installer/InstallerExistingConfigDirectoryTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerExistingConfigDirectoryTest.php @@ -1,8 +1,6 @@ siteDirectory . '/config_read_only', 0444); + protected function prepareEnvironment() { + parent::prepareEnvironment(); + mkdir($this->root . DIRECTORY_SEPARATOR . $this->siteDirectory . '/config_read_only', 0444); $this->expectedFilePerms = fileperms($this->siteDirectory . '/config_read_only'); $this->settings['config_directories'][CONFIG_SYNC_DIRECTORY] = (object) [ 'value' => $this->siteDirectory . '/config_read_only', 'required' => TRUE, ]; - parent::setUp(); } /** diff --git a/core/modules/system/src/Tests/Installer/InstallerExistingDatabaseSettingsTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerExistingDatabaseSettingsTest.php similarity index 91% rename from core/modules/system/src/Tests/Installer/InstallerExistingDatabaseSettingsTest.php rename to core/tests/Drupal/FunctionalTests/Installer/InstallerExistingDatabaseSettingsTest.php index 63d3ce407a2..caf8b48097c 100644 --- a/core/modules/system/src/Tests/Installer/InstallerExistingDatabaseSettingsTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerExistingDatabaseSettingsTest.php @@ -1,8 +1,7 @@ $connection_info, 'required' => TRUE, ]; - parent::setUp(); } /** diff --git a/core/modules/system/src/Tests/Installer/InstallerExistingInstallationTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerExistingInstallationTest.php similarity index 81% rename from core/modules/system/src/Tests/Installer/InstallerExistingInstallationTest.php rename to core/tests/Drupal/FunctionalTests/Installer/InstallerExistingInstallationTest.php index 442de6c107d..7b29d4acc83 100644 --- a/core/modules/system/src/Tests/Installer/InstallerExistingInstallationTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerExistingInstallationTest.php @@ -1,8 +1,6 @@ settings['settings']['hash_salt'] = (object) [ @@ -54,8 +54,6 @@ class InstallerExistingSettingsMismatchProfileBrokenTest extends InstallerTestBa ], ]; mkdir($this->settings['config_directories'][CONFIG_SYNC_DIRECTORY]->value, 0777, TRUE); - - parent::setUp(); } /** diff --git a/core/modules/system/src/Tests/Installer/InstallerExistingSettingsMismatchProfileTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerExistingSettingsMismatchProfileTest.php similarity index 95% rename from core/modules/system/src/Tests/Installer/InstallerExistingSettingsMismatchProfileTest.php rename to core/tests/Drupal/FunctionalTests/Installer/InstallerExistingSettingsMismatchProfileTest.php index 0349fe51524..7d82300d5d3 100644 --- a/core/modules/system/src/Tests/Installer/InstallerExistingSettingsMismatchProfileTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerExistingSettingsMismatchProfileTest.php @@ -1,10 +1,9 @@ settings['settings']['hash_salt'] = (object) [ @@ -54,8 +54,6 @@ class InstallerExistingSettingsMismatchProfileTest extends InstallerTestBase { ], ]; mkdir($this->settings['config_directories'][CONFIG_SYNC_DIRECTORY]->value, 0777, TRUE); - - parent::setUp(); } /** diff --git a/core/modules/system/src/Tests/Installer/InstallerExistingSettingsNoProfileTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerExistingSettingsNoProfileTest.php similarity index 93% rename from core/modules/system/src/Tests/Installer/InstallerExistingSettingsNoProfileTest.php rename to core/tests/Drupal/FunctionalTests/Installer/InstallerExistingSettingsNoProfileTest.php index 3405cb598ae..fdcd13c77f0 100644 --- a/core/modules/system/src/Tests/Installer/InstallerExistingSettingsNoProfileTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerExistingSettingsNoProfileTest.php @@ -1,9 +1,8 @@ settings['settings']['hash_salt'] = (object) [ @@ -46,8 +47,6 @@ class InstallerExistingSettingsNoProfileTest extends InstallerTestBase { ], ]; mkdir($this->settings['config_directories'][CONFIG_SYNC_DIRECTORY]->value, 0777, TRUE); - - parent::setUp(); } /** diff --git a/core/modules/system/src/Tests/Installer/InstallerExistingSettingsTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerExistingSettingsTest.php similarity index 94% rename from core/modules/system/src/Tests/Installer/InstallerExistingSettingsTest.php rename to core/tests/Drupal/FunctionalTests/Installer/InstallerExistingSettingsTest.php index 4f09b6b7c01..a6059e150a1 100644 --- a/core/modules/system/src/Tests/Installer/InstallerExistingSettingsTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerExistingSettingsTest.php @@ -1,9 +1,8 @@ settings['settings']['hash_salt'] = (object) [ @@ -57,8 +57,6 @@ class InstallerExistingSettingsTest extends InstallerTestBase { ], ]; mkdir($this->settings['config_directories'][CONFIG_SYNC_DIRECTORY]->value, 0777, TRUE); - - parent::setUp(); } /** diff --git a/core/modules/system/src/Tests/Installer/InstallerLanguageDirectionTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerLanguageDirectionTest.php similarity index 85% rename from core/modules/system/src/Tests/Installer/InstallerLanguageDirectionTest.php rename to core/tests/Drupal/FunctionalTests/Installer/InstallerLanguageDirectionTest.php index 5e2c15a82d0..da63ec9b308 100644 --- a/core/modules/system/src/Tests/Installer/InstallerLanguageDirectionTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerLanguageDirectionTest.php @@ -1,8 +1,6 @@ xpath('//input[@type="submit"]/@value'); - $this->assertEqual((string) current($elements), 'Save and continue Arabic'); + $this->assertEqual(current($elements)->getText(), 'Save and continue Arabic'); $this->translations['Save and continue'] = 'Save and continue Arabic'; // Verify that language direction is right-to-left. - $direction = (string) current($this->xpath('/html/@dir')); + $direction = current($this->xpath('/@dir'))->getText(); $this->assertEqual($direction, 'rtl'); } diff --git a/core/modules/system/src/Tests/Installer/InstallerLanguagePageTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerLanguagePageTest.php similarity index 93% rename from core/modules/system/src/Tests/Installer/InstallerLanguagePageTest.php rename to core/tests/Drupal/FunctionalTests/Installer/InstallerLanguagePageTest.php index ce3d1ecac25..a040da965ca 100644 --- a/core/modules/system/src/Tests/Installer/InstallerLanguagePageTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerLanguagePageTest.php @@ -1,9 +1,8 @@ assertRaw('core/themes/seven/css/components/buttons.css'); + $this->assertRaw("core/themes/seven/css/components/buttons.css"); $this->assertRaw(''); // Assert that the expected title is present. - $this->assertEqual('Choose language', $this->cssSelect('main h2')[0]); + $this->assertEqual('Choose language', $this->cssSelect('main h2')[0]->getText()); parent::setUpLanguage(); } @@ -51,7 +49,7 @@ class InstallerTest extends InstallerTestBase { */ protected function setUpProfile() { // Assert that the expected title is present. - $this->assertEqual('Select an installation profile', $this->cssSelect('main h2')[0]); + $this->assertEqual('Select an installation profile', $this->cssSelect('main h2')[0]->getText()); $result = $this->xpath('//span[contains(@class, :class) and contains(text(), :text)]', [':class' => 'visually-hidden', ':text' => 'Select an installation profile']); $this->assertEqual(count($result), 1, "Title/Label not displayed when '#title_display' => 'invisible' attribute is set"); @@ -63,7 +61,7 @@ class InstallerTest extends InstallerTestBase { */ protected function setUpSettings() { // Assert that the expected title is present. - $this->assertEqual('Database configuration', $this->cssSelect('main h2')[0]); + $this->assertEqual('Database configuration', $this->cssSelect('main h2')[0]->getText()); parent::setUpSettings(); } @@ -73,7 +71,7 @@ class InstallerTest extends InstallerTestBase { */ protected function setUpSite() { // Assert that the expected title is present. - $this->assertEqual('Configure site', $this->cssSelect('main h2')[0]); + $this->assertEqual('Configure site', $this->cssSelect('main h2')[0]->getText()); parent::setUpSite(); } diff --git a/core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php new file mode 100644 index 00000000000..54039718cba --- /dev/null +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php @@ -0,0 +1,316 @@ + 'Save and continue', + ]; + + /** + * Whether the installer has completed. + * + * @var bool + */ + protected $isInstalled = FALSE; + + /** + * {@inheritdoc} + */ + protected function setUp() { + $this->isInstalled = FALSE; + + $this->setupBaseUrl(); + + $this->prepareDatabasePrefix(); + + // Install Drupal test site. + $this->prepareEnvironment(); + + // Define information about the user 1 account. + $this->rootUser = new UserSession([ + 'uid' => 1, + 'name' => 'admin', + 'mail' => 'admin@example.com', + 'pass_raw' => $this->randomMachineName(), + ]); + + // If any $settings are defined for this test, copy and prepare an actual + // settings.php, so as to resemble a regular installation. + if (!empty($this->settings)) { + // Not using File API; a potential error must trigger a PHP warning. + copy(DRUPAL_ROOT . '/sites/default/default.settings.php', DRUPAL_ROOT . '/' . $this->siteDirectory . '/settings.php'); + $this->writeSettings($this->settings); + } + + // Note that FunctionalTestSetupTrait::installParameters() returns form + // input values suitable for a programmed + // \Drupal::formBuilder()->submitForm(). + // @see InstallerTestBase::translatePostValues() + $this->parameters = $this->installParameters(); + + // Set up a minimal container (required by BrowserTestBase). Set cookie and + // server information so that XDebug works. + // @see install_begin_request() + $request = Request::create($GLOBALS['base_url'] . '/core/install.php', 'GET', [], $_COOKIE, [], $_SERVER); + $this->container = new ContainerBuilder(); + $request_stack = new RequestStack(); + $request_stack->push($request); + $this->container + ->set('request_stack', $request_stack); + $this->container + ->setParameter('language.default_values', Language::$defaultValues); + $this->container + ->register('language.default', 'Drupal\Core\Language\LanguageDefault') + ->addArgument('%language.default_values%'); + $this->container + ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager') + ->addArgument(new Reference('language.default')); + $this->container + ->register('http_client', 'GuzzleHttp\Client') + ->setFactory('http_client_factory:fromOptions'); + $this->container + ->register('http_client_factory', 'Drupal\Core\Http\ClientFactory') + ->setArguments([new Reference('http_handler_stack')]); + $handler_stack = HandlerStack::create(); + $test_http_client_middleware = new TestHttpClientMiddleware(); + $handler_stack->push($test_http_client_middleware(), 'test.http_client.middleware'); + $this->container + ->set('http_handler_stack', $handler_stack); + + $this->container + ->set('app.root', DRUPAL_ROOT); + \Drupal::setContainer($this->container); + + // Setup Mink. + $this->initMink(); + + $this->visitInstaller(); + + // Select language. + $this->setUpLanguage(); + + // Select profile. + $this->setUpProfile(); + + // Address the requirements problem screen, if any. + $this->setUpRequirementsProblem(); + + // Configure settings. + $this->setUpSettings(); + + // @todo Allow test classes based on this class to act on further installer + // screens. + + // Configure site. + $this->setUpSite(); + + if ($this->isInstalled) { + // Import new settings.php written by the installer. + $request = Request::createFromGlobals(); + $class_loader = require $this->container->get('app.root') . '/autoload.php'; + Settings::initialize($this->container->get('app.root'), DrupalKernel::findSitePath($request), $class_loader); + foreach ($GLOBALS['config_directories'] as $type => $path) { + $this->configDirectories[$type] = $path; + } + + // After writing settings.php, the installer removes write permissions + // from the site directory. To allow drupal_generate_test_ua() to write + // a file containing the private key for drupal_valid_test_ua(), the site + // directory has to be writable. + // BrowserTestBase::tearDown() will delete the entire test site directory. + // Not using File API; a potential error must trigger a PHP warning. + chmod($this->container->get('app.root') . '/' . $this->siteDirectory, 0777); + $this->kernel = DrupalKernel::createFromRequest($request, $class_loader, 'prod', FALSE); + $this->kernel->prepareLegacyRequest($request); + $this->container = $this->kernel->getContainer(); + + // Manually configure the test mail collector implementation to prevent + // tests from sending out emails and collect them in state instead. + $this->container->get('config.factory') + ->getEditable('system.mail') + ->set('interface.default', 'test_mail_collector') + ->save(); + } + } + + /** + * {@inheritdoc} + */ + protected function initFrontPage() { + // We don't want to visit the front page with the installer when + // initializing Mink, so we do nothing here. + } + + /** + * Visits the interactive installer. + */ + protected function visitInstaller() { + $this->drupalGet($GLOBALS['base_url'] . '/core/install.php'); + } + + /** + * Installer step: Select language. + */ + protected function setUpLanguage() { + $edit = [ + 'langcode' => $this->langcode, + ]; + $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']); + } + + /** + * Installer step: Select installation profile. + */ + protected function setUpProfile() { + $edit = [ + 'profile' => $this->profile, + ]; + $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']); + } + + /** + * Installer step: Configure settings. + */ + protected function setUpSettings() { + $edit = $this->translatePostValues($this->parameters['forms']['install_settings_form']); + $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']); + } + + /** + * Installer step: Requirements problem. + * + * Override this method to test specific requirements warnings or errors + * during the installer. + * + * @see system_requirements() + */ + protected function setUpRequirementsProblem() { + // By default, skip the "recommended PHP version" warning on older test + // environments. This allows the installer to be tested consistently on + // both recommended PHP versions and older (but still supported) versions. + if (version_compare(phpversion(), '7.0') < 0) { + $this->continueOnExpectedWarnings(['PHP']); + } + } + + /** + * Final installer step: Configure site. + */ + protected function setUpSite() { + $edit = $this->translatePostValues($this->parameters['forms']['install_configure_form']); + $this->drupalPostForm(NULL, $edit, $this->translations['Save and continue']); + // If we've got to this point the site is installed using the regular + // installation workflow. + $this->isInstalled = TRUE; + } + + /** + * {@inheritdoc} + * + * FunctionalTestSetupTrait::refreshVariables() tries to operate on persistent + * storage, which is only available after the installer completed. + */ + protected function refreshVariables() { + if ($this->isInstalled) { + parent::refreshVariables(); + } + } + + /** + * Continues installation when an expected warning is found. + * + * @param string[] $expected_warnings + * A list of warning summaries to expect on the requirements screen (e.g. + * 'PHP', 'PHP OPcode caching', etc.). If only the expected warnings + * are found, the test will click the "continue anyway" link to go to the + * next screen of the installer. If an expected warning is not found, or if + * a warning not in the list is present, a fail is raised. + */ + protected function continueOnExpectedWarnings($expected_warnings = []) { + // Don't try to continue if there are errors. + if (strpos($this->getTextContent(), 'Errors found') !== FALSE) { + return; + } + // Allow only details elements that are directly after the warning header + // or each other. There is no guaranteed wrapper we can rely on across + // distributions. When there are multiple warnings, the selectors will be: + // - h3#warning+details summary + // - h3#warning+details+details summary + // - etc. + // We add one more selector than expected warnings to confirm that there + // isn't any other warning before clicking the link. + // @todo Make this more reliable in + // https://www.drupal.org/project/drupal/issues/2927345. + $selectors = []; + for ($i = 0; $i <= count($expected_warnings); $i++) { + $selectors[] = 'h3#warning' . implode('', array_fill(0, $i + 1, '+details')) . ' summary'; + } + $warning_elements = $this->cssSelect(implode(', ', $selectors)); + + // Confirm that there are only the expected warnings. + $warnings = []; + foreach ($warning_elements as $warning) { + $warnings[] = trim($warning->getText()); + } + $this->assertEquals($expected_warnings, $warnings); + $this->clickLink('continue anyway'); + $this->checkForMetaRefresh(); + } + +} diff --git a/core/modules/system/src/Tests/Installer/InstallerTranslationMultipleLanguageForeignTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationMultipleLanguageForeignTest.php similarity index 92% rename from core/modules/system/src/Tests/Installer/InstallerTranslationMultipleLanguageForeignTest.php rename to core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationMultipleLanguageForeignTest.php index 8bf739bc5bd..58100cdbf3d 100644 --- a/core/modules/system/src/Tests/Installer/InstallerTranslationMultipleLanguageForeignTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationMultipleLanguageForeignTest.php @@ -1,6 +1,6 @@ xpath('//input[@type="submit"]/@value'); - $this->assertEqual((string) current($elements), 'Save and continue de'); + $this->assertEqual(current($elements)->getText(), 'Save and continue de'); $this->translations['Save and continue'] = 'Save and continue de'; // Check the language direction. - $direction = (string) current($this->xpath('/html/@dir')); + $direction = current($this->xpath('/@dir'))->getText(); $this->assertEqual($direction, 'ltr'); } diff --git a/core/modules/system/src/Tests/Installer/InstallerTranslationTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationTest.php similarity index 96% rename from core/modules/system/src/Tests/Installer/InstallerTranslationTest.php rename to core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationTest.php index 7a803e700b5..974d2825883 100644 --- a/core/modules/system/src/Tests/Installer/InstallerTranslationTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationTest.php @@ -1,9 +1,8 @@ xpath('//input[@type="submit"]/@value'); - $this->assertEqual((string) current($elements), 'Save and continue de'); + $this->assertEqual(current($elements)->getText(), 'Save and continue de'); $this->translations['Save and continue'] = 'Save and continue de'; // Check the language direction. - $direction = (string) current($this->xpath('/html/@dir')); + $direction = current($this->xpath('/@dir'))->getText(); $this->assertEqual($direction, 'ltr'); } diff --git a/core/modules/system/src/Tests/Installer/MinimalInstallerTest.php b/core/tests/Drupal/FunctionalTests/Installer/MinimalInstallerTest.php similarity index 91% rename from core/modules/system/src/Tests/Installer/MinimalInstallerTest.php rename to core/tests/Drupal/FunctionalTests/Installer/MinimalInstallerTest.php index eb6778d0955..baefe0fe1c6 100644 --- a/core/modules/system/src/Tests/Installer/MinimalInstallerTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/MinimalInstallerTest.php @@ -1,6 +1,6 @@ siteDirectory . '/profiles/' . $name; + $path = $this->root . DIRECTORY_SEPARATOR . $this->siteDirectory . '/profiles/' . $name; mkdir($path, 0777, TRUE); file_put_contents("$path/$name.info.yml", Yaml::encode($info)); } // Install the first distribution. $this->profile = 'distribution_one'; - - parent::setUp(); } /** diff --git a/core/modules/system/src/Tests/Installer/SingleVisibleProfileTest.php b/core/tests/Drupal/FunctionalTests/Installer/SingleVisibleProfileTest.php similarity index 89% rename from core/modules/system/src/Tests/Installer/SingleVisibleProfileTest.php rename to core/tests/Drupal/FunctionalTests/Installer/SingleVisibleProfileTest.php index 3e93a0b4fc0..b7c92dfdad2 100644 --- a/core/modules/system/src/Tests/Installer/SingleVisibleProfileTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/SingleVisibleProfileTest.php @@ -1,9 +1,8 @@ mink->setDefaultSessionName('default'); $this->registerSessions(); - // According to the W3C WebDriver specification a cookie can only be set if - // the cookie domain is equal to the domain of the active document. When the - // browser starts up the active document is not our domain but 'about:blank' - // or similar. To be able to set our User-Agent and Xdebug cookies at the - // start of the test we now do a request to the front page so the active - // document matches the domain. - // @see https://w3c.github.io/webdriver/webdriver-spec.html#add-cookie - // @see https://www.w3.org/Bugs/Public/show_bug.cgi?id=20975 - $session = $this->getSession(); - $session->visit($this->baseUrl); + $this->initFrontPage(); return $session; } + /** + * Visits the front page when initializing Mink. + * + * According to the W3C WebDriver specification a cookie can only be set if + * the cookie domain is equal to the domain of the active document. When the + * browser starts up the active document is not our domain but 'about:blank' + * or similar. To be able to set our User-Agent and Xdebug cookies at the + * start of the test we now do a request to the front page so the active + * document matches the domain. + * + * @see https://w3c.github.io/webdriver/webdriver-spec.html#add-cookie + * @see https://www.w3.org/Bugs/Public/show_bug.cgi?id=20975 + */ + protected function initFrontPage() { + $session = $this->getSession(); + $session->visit($this->baseUrl); + } + /** * Gets an instance of the default Mink driver. * @@ -593,6 +602,7 @@ abstract class BrowserTestBase extends TestCase { * A new web-assert option for asserting the presence of elements with. */ public function assertSession($name = NULL) { + $this->addToAssertionCount(1); return new WebAssert($this->getSession($name), $this->baseUrl); } @@ -1307,6 +1317,28 @@ abstract class BrowserTestBase extends TestCase { return $caller; } + /** + * Transforms a nested array into a flat array suitable for drupalPostForm(). + * + * @param array $values + * A multi-dimensional form values array to convert. + * + * @return array + * The flattened $edit array suitable for BrowserTestBase::drupalPostForm(). + */ + protected function translatePostValues(array $values) { + $edit = []; + // The easiest and most straightforward way to translate values suitable for + // BrowserTestBase::drupalPostForm() is to actually build the POST data + // string and convert the resulting key/value pairs back into a flat array. + $query = http_build_query($values); + foreach (explode('&', $query) as $item) { + list($key, $value) = explode('=', $item); + $edit[urldecode($key)] = urldecode($value); + } + return $edit; + } + /** * Checks for meta refresh tag and if found call drupalGet() recursively. *