Skip to content
Snippets Groups Projects
Commit 078bd2cb authored by Adam G-H's avatar Adam G-H
Browse files

Issue #3239889 by phenaproxima, tedbow: Add an offline kernel test of UpdateRecommender

parent 678df7e8
No related branches found
No related tags found
No related merge requests found
<?php
namespace Drupal\Tests\automatic_updates\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Base class for functional tests of the Automatic Updates module.
*/
abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['update', 'update_test'];
/**
* Sets the current (running) version of core, as known to the Update module.
*
* @param string $version
* The current version of core.
*/
protected function setCoreVersion(string $version): void {
$this->config('update_test.settings')
->set('system_info.#all.version', $version)
->save();
}
/**
* Sets the release metadata file to use when fetching available updates.
*
* @param string $file
* The path of the XML metadata file to use.
*/
protected function setReleaseMetadata(string $file): void {
$this->config('update.settings')
->set('fetch.url', $this->baseUrl . '/automatic-update-test')
->save();
[$project, $fixture] = explode('.', basename($file, '.xml'), 2);
$this->config('update_test.settings')
->set('xml_map', [
$project => $fixture,
])
->save();
}
}
......@@ -6,19 +6,18 @@ use Drupal\automatic_updates\ComposerStager\Cleaner;
use Drupal\automatic_updates\PathLocator;
use Drupal\automatic_updates\Updater;
use Drupal\Core\Site\Settings;
use Drupal\Tests\BrowserTestBase;
/**
* Tests handling of files and directories during an update.
*
* @group automatic_updates
*/
class FileSystemOperationsTest extends BrowserTestBase {
class FileSystemOperationsTest extends AutomaticUpdatesFunctionalTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['automatic_updates_test', 'update_test'];
protected static $modules = ['automatic_updates_test'];
/**
* {@inheritdoc}
......@@ -91,17 +90,8 @@ class FileSystemOperationsTest extends BrowserTestBase {
// \Drupal\automatic_updates\Validator\UpdateVersionValidator, that need to
// fetch release metadata. We need to ensure that those HTTP request(s)
// succeed, so set them up to point to our fake release metadata.
$this->config('update_test.settings')
->set('xml_map', [
'drupal' => '0.0',
])
->save();
$this->config('update.settings')
->set('fetch.url', $this->baseUrl . '/automatic-update-test')
->save();
$this->config('update_test.settings')
->set('system_info.#all.version', '9.8.0')
->save();
$this->setReleaseMetadata(__DIR__ . '/../../fixtures/release-history/drupal.0.0.xml');
$this->setCoreVersion('9.8.0');
}
/**
......
......@@ -7,14 +7,13 @@ use Drupal\automatic_updates\Exception\UpdateException;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\automatic_updates_test\ReadinessChecker\TestChecker1;
use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait;
use Drupal\Tests\BrowserTestBase;
/**
* @covers \Drupal\automatic_updates\Form\UpdaterForm
*
* @group automatic_updates
*/
class UpdaterFormTest extends BrowserTestBase {
class UpdaterFormTest extends AutomaticUpdatesFunctionalTestBase {
use ValidationTestTrait;
......@@ -31,36 +30,14 @@ class UpdaterFormTest extends BrowserTestBase {
'automatic_updates',
'automatic_updates_test',
'package_manager_bypass',
'update_test',
];
/**
* Sets the running version of core, as known to the Update module.
*
* @param string $version
* The version of core to set. When checking for updates, this is what the
* Update module will think the running version of core is.
*/
private function setCoreVersion(string $version): void {
$this->config('update_test.settings')
->set('system_info.#all.version', $version)
->save();
}
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->config('update_test.settings')
->set('xml_map', [
'drupal' => '0.0',
])
->save();
$this->config('update.settings')
->set('fetch.url', $this->baseUrl . '/automatic-update-test')
->save();
$this->setReleaseMetadata(__DIR__ . '/../../fixtures/release-history/drupal.0.0.xml');
}
/**
......
<?php
namespace Drupal\Tests\automatic_updates\Kernel;
use Drupal\KernelTests\KernelTestBase;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Utils;
/**
* Base class for kernel tests of the Automatic Updates module.
*/
abstract class AutomaticUpdatesKernelTestBase extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['update', 'update_test'];
/**
* Sets the current (running) version of core, as known to the Update module.
*
* @param string $version
* The current version of core.
*/
protected function setCoreVersion(string $version): void {
$this->config('update_test.settings')
->set('system_info.#all.version', $version)
->save();
}
/**
* Sets the release metadata file to use when fetching available updates.
*
* @param string $file
* The path of the XML metadata file to use.
*/
protected function setReleaseMetadata(string $file): void {
$metadata = Utils::tryFopen($file, 'r');
$response = new Response(200, [], Utils::streamFor($metadata));
$handler = new MockHandler([$response]);
$client = new Client([
'handler' => HandlerStack::create($handler),
]);
$this->container->set('http_client', $client);
}
}
<?php
namespace Drupal\Tests\automatic_updates\Kernel;
use Drupal\automatic_updates\UpdateRecommender;
/**
* @covers \Drupal\automatic_updates\UpdateRecommender
*
* @group automatic_updates
*/
class UpdateRecommenderTest extends AutomaticUpdatesKernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'automatic_updates',
'package_manager',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig('update');
}
/**
* Tests fetching the recommended release when an update is available.
*/
public function testUpdateAvailable(): void {
$this->setCoreVersion('9.8.0');
$this->setReleaseMetadata(__DIR__ . '/../../fixtures/release-history/drupal.0.0.xml');
$recommender = new UpdateRecommender();
$recommended_release = $recommender->getRecommendedRelease(TRUE);
$this->assertNotEmpty($recommended_release);
$this->assertSame('9.8.1', $recommended_release->getVersion());
// Getting the recommended release again should not trigger another request.
$this->assertNotEmpty($recommender->getRecommendedRelease());
}
/**
* Tests fetching the recommended release when there is no update available.
*/
public function testNoUpdateAvailable(): void {
$this->setCoreVersion('9.8.1');
$this->setReleaseMetadata(__DIR__ . '/../../fixtures/release-history/drupal.0.0.xml');
$recommender = new UpdateRecommender();
$recommended_release = $recommender->getRecommendedRelease(TRUE);
$this->assertNull($recommended_release);
// Getting the recommended release again should not trigger another request.
$this->assertNull($recommender->getRecommendedRelease());
}
}
......@@ -2,11 +2,6 @@
namespace Drupal\Tests\automatic_updates\Kernel;
use Drupal\KernelTests\KernelTestBase;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Utils;
use Prophecy\Argument;
/**
......@@ -14,7 +9,7 @@ use Prophecy\Argument;
*
* @group automatic_updates
*/
class UpdaterTest extends KernelTestBase {
class UpdaterTest extends AutomaticUpdatesKernelTestBase {
/**
* {@inheritdoc}
......@@ -23,25 +18,16 @@ class UpdaterTest extends KernelTestBase {
'automatic_updates',
'automatic_updates_test',
'package_manager',
'update',
'update_test',
];
/**
* Tests that correct versions are staged after calling ::begin().
*/
public function testCorrectVersionsStaged() {
// Ensure that the HTTP client will fetch our fake release metadata.
$release_data = Utils::tryFopen(__DIR__ . '/../../fixtures/release-history/drupal.0.0.xml', 'r');
$response = new Response(200, [], Utils::streamFor($release_data));
$handler = new MockHandler([$response]);
$client = new Client(['handler' => $handler]);
$this->container->set('http_client', $client);
$this->setReleaseMetadata(__DIR__ . '/../../fixtures/release-history/drupal.0.0.xml');
// Set the running core version to 9.8.0.
$this->config('update_test.settings')
->set('system_info.#all.version', '9.8.0')
->save();
$this->setCoreVersion('9.8.0');
$this->container->get('automatic_updates.updater')->begin([
'drupal' => '9.8.1',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment