diff --git a/tests/src/Functional/AutomaticUpdatesFunctionalTestBase.php b/tests/src/Functional/AutomaticUpdatesFunctionalTestBase.php index 7cc20dd5c604c7a4f06d0072f1b4fbcaeb2a1ca6..083833bc206ee8a717be0739ea1afbd3fe829209 100644 --- a/tests/src/Functional/AutomaticUpdatesFunctionalTestBase.php +++ b/tests/src/Functional/AutomaticUpdatesFunctionalTestBase.php @@ -8,13 +8,11 @@ use Drupal\automatic_updates\CronUpdateRunner; use Drupal\automatic_updates\CommandExecutor; use Drupal\automatic_updates\UpdateStage; use Drupal\fixture_manipulator\StageFixtureManipulator; -use Drupal\package_manager\PathLocator; +use Drupal\Tests\automatic_updates\Traits\TestSetUpTrait; use Drupal\Tests\BrowserTestBase; use Drupal\Tests\package_manager\Traits\AssertPreconditionsTrait; use Drupal\Tests\package_manager\Traits\ComposerStagerTestTrait; use Drupal\Tests\package_manager\Traits\FixtureManipulatorTrait; -use Drupal\Tests\package_manager\Traits\FixtureUtilityTrait; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Base class for functional tests of the Automatic Updates module. @@ -26,7 +24,7 @@ abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase { use AssertPreconditionsTrait; use ComposerStagerTestTrait; use FixtureManipulatorTrait; - use FixtureUtilityTrait; + use TestSetUpTrait; /** * {@inheritdoc} @@ -41,7 +39,6 @@ abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase { */ protected function setUp(): void { parent::setUp(); - $this->useFixtureDirectoryAsActive(__DIR__ . '/../../../package_manager/tests/fixtures/fake_site'); // @todo Remove in https://www.drupal.org/project/automatic_updates/issues/3284443 $this->config('automatic_updates.settings') ->set('unattended.level', CronUpdateRunner::SECURITY) @@ -49,22 +46,6 @@ abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase { $this->mockActiveCoreVersion('9.8.0'); } - /** - * {@inheritdoc} - */ - protected function installModulesFromClassProperty(ContainerInterface $container): void { - $container->get('module_installer')->install([ - 'package_manager_test_release_history', - ]); - $this->container = $container->get('kernel')->getContainer(); - - // To prevent tests from making real requests to the Internet, use fake - // release metadata that exposes a pretend Drupal 9.8.2 release. - $this->setReleaseMetadata(__DIR__ . '/../../../package_manager/tests/fixtures/release-history/drupal.9.8.2.xml'); - - parent::installModulesFromClassProperty($container); - } - /** * {@inheritdoc} */ @@ -98,30 +79,6 @@ abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase { ->save(); } - /** - * Sets the release metadata file to use when fetching available updates. - * - * @todo Remove this function with use of the trait from the Update module in - * https://drupal.org/i/3348234. - * - * @param string $file - * The path of the XML metadata file to use. - */ - protected function setReleaseMetadata(string $file): void { - $this->assertFileIsReadable($file); - - $this->config('update.settings') - ->set('fetch.url', $this->baseUrl . '/test-release-history') - ->save(); - - [$project] = explode('.', basename($file, '.xml'), 2); - $xml_map = $this->config('update_test.settings')->get('xml_map') ?? []; - $xml_map[$project] = $file; - $this->config('update_test.settings') - ->set('xml_map', $xml_map) - ->save(); - } - /** * Checks for available updates. * @@ -147,36 +104,6 @@ abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase { $this->assertTrue($button->hasClass('button--primary')); } - /** - * Copies a fixture directory to a temporary directory. - * - * @param string $fixture_directory - * The fixture directory. - * - * @return string - * The temporary directory. - */ - protected function copyFixtureToTempDirectory(string $fixture_directory): string { - $temp_directory = $this->root . DIRECTORY_SEPARATOR . $this->siteDirectory . DIRECTORY_SEPARATOR . $this->randomMachineName(20); - static::copyFixtureFilesTo($fixture_directory, $temp_directory); - return $temp_directory; - } - - /** - * Sets a fixture directory to use as the active directory. - * - * @param string $fixture_directory - * The fixture directory. - */ - protected function useFixtureDirectoryAsActive(string $fixture_directory): void { - // Create a temporary directory from our fixture directory that will be - // unique for each test run. This will enable changing files in the - // directory and not affect other tests. - $active_dir = $this->copyFixtureToTempDirectory($fixture_directory); - $this->container->get(PathLocator::class) - ->setPaths($active_dir, $active_dir . '/vendor', '', NULL); - } - /** * Runs the console update command, which will trigger status checks. */ diff --git a/tests/src/Functional/HelpPageTest.php b/tests/src/Functional/HelpPageTest.php index 9e32ed8f8109751e0184b2a058a3613ac89ad7de..c18e761bd51a09c330c52adc4fb1323ee97342d2 100644 --- a/tests/src/Functional/HelpPageTest.php +++ b/tests/src/Functional/HelpPageTest.php @@ -4,14 +4,13 @@ declare(strict_types = 1); namespace Drupal\Tests\automatic_updates\Functional; -use Drupal\Tests\BrowserTestBase; use Drupal\Tests\package_manager\Traits\AssertPreconditionsTrait; /** * @group automatic_updates * @internal */ -class HelpPageTest extends BrowserTestBase { +class HelpPageTest extends AutomaticUpdatesFunctionalTestBase { use AssertPreconditionsTrait; diff --git a/tests/src/FunctionalJavascript/UpdateSettingsFormTest.php b/tests/src/FunctionalJavascript/UpdateSettingsFormTest.php index 32aef05524592e1227e6763ebd081ea490d6528c..5a3010d7bcb275ef09035afdda6198397da384c5 100644 --- a/tests/src/FunctionalJavascript/UpdateSettingsFormTest.php +++ b/tests/src/FunctionalJavascript/UpdateSettingsFormTest.php @@ -4,12 +4,15 @@ namespace Drupal\Tests\automatic_updates\FunctionalJavascript; use Drupal\automatic_updates\CronUpdateRunner; use Drupal\FunctionalJavascriptTests\WebDriverTestBase; +use Drupal\Tests\automatic_updates\Traits\TestSetUpTrait; /** * @group automatic_updates */ class UpdateSettingsFormTest extends WebDriverTestBase { + use TestSetUpTrait; + /** * {@inheritdoc} */ diff --git a/tests/src/Traits/TestSetUpTrait.php b/tests/src/Traits/TestSetUpTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..2b6831b0e9d364b874f764de403ba655022e2696 --- /dev/null +++ b/tests/src/Traits/TestSetUpTrait.php @@ -0,0 +1,93 @@ +<?php + +namespace Drupal\Tests\automatic_updates\Traits; + +use Drupal\package_manager\PathLocator; +use Drupal\Tests\package_manager\Traits\FixtureUtilityTrait; +use Symfony\Component\DependencyInjection\ContainerInterface; + +/** + * Common functions to set up tests correctly. + */ +trait TestSetUpTrait { + + use FixtureUtilityTrait; + + /** + * {@inheritdoc} + */ + protected function installModulesFromClassProperty(ContainerInterface $container): void { + $container->get('module_installer')->install([ + 'package_manager_test_release_history', + 'package_manager_bypass', + ]); + $this->container = $container->get('kernel')->getContainer(); + + // To prevent tests from using the real codebase for Composer we must set + // the fixture path as early as possible. Automatic Updates will run + // readiness checks when the module is installed so this must be done before + // the parent class installs the modules needed for the test. + $this->useFixtureDirectoryAsActive(__DIR__ . '/../../../package_manager/tests/fixtures/fake_site'); + + // To prevent tests from making real requests to the Internet, use fake + // release metadata that exposes a pretend Drupal 9.8.2 release. + $this->setReleaseMetadata(__DIR__ . '/../../../package_manager/tests/fixtures/release-history/drupal.9.8.2.xml'); + + parent::installModulesFromClassProperty($container); + } + + /** + * Copies a fixture directory to a temporary directory. + * + * @param string $fixture_directory + * The fixture directory. + * + * @return string + * The temporary directory. + */ + protected function copyFixtureToTempDirectory(string $fixture_directory): string { + $temp_directory = $this->root . DIRECTORY_SEPARATOR . $this->siteDirectory . DIRECTORY_SEPARATOR . $this->randomMachineName(20); + static::copyFixtureFilesTo($fixture_directory, $temp_directory); + return $temp_directory; + } + + /** + * Sets a fixture directory to use as the active directory. + * + * @param string $fixture_directory + * The fixture directory. + */ + protected function useFixtureDirectoryAsActive(string $fixture_directory): void { + // Create a temporary directory from our fixture directory that will be + // unique for each test run. This will enable changing files in the + // directory and not affect other tests. + $active_dir = $this->copyFixtureToTempDirectory($fixture_directory); + $this->container->get(PathLocator::class) + ->setPaths($active_dir, $active_dir . '/vendor', '', NULL); + } + + /** + * Sets the release metadata file to use when fetching available updates. + * + * @todo Remove this function with use of the trait from the Update module in + * https://drupal.org/i/3348234. + * + * @param string $file + * The path of the XML metadata file to use. + */ + protected function setReleaseMetadata(string $file): void { + $this->assertFileIsReadable($file); + + $this->config('update.settings') + ->set('fetch.url', $this->baseUrl . '/test-release-history') + ->save(); + + [$project] = explode('.', basename($file, '.xml'), 2); + $xml_map = $this->config('update_test.settings')->get('xml_map') ?? []; + $xml_map[$project] = $file; + $this->config('update_test.settings') + ->set('xml_map', $xml_map) + ->save(); + } + +}