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

Issue #3317385 by phenaproxima, tedbow: Remove staged fixtures from StagedProjectsValidatorTest

parent 0d680aae
No related branches found
No related tags found
No related merge requests found
Showing
with 480 additions and 364 deletions
...@@ -20,5 +20,6 @@ ...@@ -20,5 +20,6 @@
"name": "drupal/core-dev", "name": "drupal/core-dev",
"version": "9.8.0" "version": "9.8.0"
} }
] ],
"dev-package-names": []
} }
<?php
namespace Drupal\Tests\package_manager\Kernel;
use Drupal\Tests\package_manager\Traits\FixtureUtilityTrait;
use PHPUnit\Framework\AssertionFailedError;
/**
* @coversDefaultClass \Drupal\Tests\package_manager\Traits\FixtureUtilityTrait
*
* @group package_manager
*/
class FixtureUtilityTraitTest extends PackageManagerKernelTestBase {
use FixtureUtilityTrait;
/**
* The root directory of the virtual project.
*
* @var string
*/
private string $dir;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->dir = $this->container->get('package_manager.path_locator')
->getProjectRoot();
$this->addPackage($this->dir, ['name' => 'my/package']);
$this->addPackage($this->dir, [
'name' => 'my/dev-package',
'version' => '2.1.0',
'dev_requirement' => TRUE,
]);
}
/**
* @covers ::addPackage
*/
public function testAddPackage(): void {
// Packages cannot be added without a name.
try {
$this->addPackage($this->dir, ['type' => 'unknown']);
$this->fail('Adding an anonymous package should raise an error.');
}
catch (AssertionFailedError $e) {
$this->assertSame("Failed asserting that an array has the key 'name'.", $e->getMessage());
}
// We should not be able to add an existing package.
try {
$this->addPackage($this->dir, ['name' => 'my/package']);
$this->fail('Trying to add an existing package should raise an error.');
}
catch (AssertionFailedError $e) {
$this->assertStringContainsString("Expected package 'my/package' to not be installed, but it was.", $e->getMessage());
}
$expected_packages = [
'my/package' => [
'name' => 'my/package',
],
'my/dev-package' => [
'name' => 'my/dev-package',
'version' => '2.1.0',
'dev_requirement' => TRUE,
],
];
[$installed_json, $installed_php] = $this->getData();
$installed_json['packages'] = array_intersect_key($installed_json['packages'], $expected_packages);
$this->assertSame($expected_packages, $installed_json['packages']);
$this->assertContains('my/dev-package', $installed_json['dev-package-names']);
$this->assertNotContains('my/package', $installed_json['dev-package-names']);
$this->assertSame($expected_packages, $installed_php);
}
/**
* @covers ::modifyPackage
*/
public function testModifyPackage(): void {
// We should not be able to modify a non-existent package.
try {
$this->modifyPackage($this->dir, 'junk/drawer', ['type' => 'library']);
$this->fail('Modifying a non-existent package should raise an error.');
}
catch (AssertionFailedError $e) {
$this->assertStringContainsString("Expected package 'junk/drawer' to be installed, but it wasn't.", $e->getMessage());
}
// Add a key to an existing package.
$this->modifyPackage($this->dir, 'my/package', ['type' => 'metapackage']);
// Change a key in an existing package.
$this->modifyPackage($this->dir, 'my/dev-package', ['version' => '3.2.1']);
// Move an existing package to dev requirements.
$this->addPackage($this->dir, [
'name' => 'my/other-package',
'type' => 'library',
]);
$this->modifyPackage($this->dir, 'my/other-package', ['dev_requirement' => TRUE]);
$expected_packages = [
'my/package' => [
'name' => 'my/package',
'type' => 'metapackage',
],
'my/dev-package' => [
'name' => 'my/dev-package',
'version' => '3.2.1',
'dev_requirement' => TRUE,
],
'my/other-package' => [
'name' => 'my/other-package',
'type' => 'library',
'dev_requirement' => TRUE,
],
];
[$installed_json, $installed_php] = $this->getData();
$installed_json['packages'] = array_intersect_key($installed_json['packages'], $expected_packages);
$this->assertSame($expected_packages, $installed_json['packages']);
$this->assertContains('my/dev-package', $installed_json['dev-package-names']);
$this->assertContains('my/other-package', $installed_json['dev-package-names']);
$this->assertNotContains('my/package', $installed_json['dev-package-names']);
$this->assertSame($expected_packages, $installed_php);
}
/**
* @covers ::removePackage
*/
public function testRemovePackage(): void {
// We should not be able to remove a package that's not installed.
try {
$this->removePackage($this->dir, 'junk/drawer');
$this->fail('Removing a non-existent package should raise an error.');
}
catch (AssertionFailedError $e) {
$this->assertStringContainsString("Expected package 'junk/drawer' to be installed, but it wasn't.", $e->getMessage());
}
$this->removePackage($this->dir, 'my/package');
$this->removePackage($this->dir, 'my/dev-package');
foreach (['json', 'php'] as $extension) {
$contents = file_get_contents("$this->dir/vendor/composer/installed.$extension");
$this->assertStringNotContainsString('my/package', $contents);
$this->assertStringNotContainsString('my/dev-package', $contents);
}
}
/**
* Returns the data from installed.php and installed.json.
*
* @return array[]
* An array of two arrays. The first array will be the contents of
* installed.json, with the `packages` array keyed by package name. The
* second array will be the `versions` array from installed.php.
*/
private function getData(): array {
$installed_json = file_get_contents("$this->dir/vendor/composer/installed.json");
$installed_json = json_decode($installed_json, TRUE, 512, JSON_THROW_ON_ERROR);
$keyed_packages = [];
foreach ($installed_json['packages'] as $package) {
$keyed_packages[$package['name']] = $package;
}
$installed_json['packages'] = $keyed_packages;
$installed_php = require "$this->dir/vendor/composer/installed.php";
return [
$installed_json,
$installed_php['versions'],
];
}
}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\Tests\package_manager\Traits; namespace Drupal\Tests\package_manager\Traits;
use Drupal\Component\Utility\NestedArray;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator; use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
...@@ -76,4 +77,126 @@ trait FixtureUtilityTrait { ...@@ -76,4 +77,126 @@ trait FixtureUtilityTrait {
} }
} }
/**
* Adds a package.
*
* @param string $dir
* The root Composer-managed directory (e.g., the project root or staging
* area).
* @param array $package
* The package info that should be added to installed.json and
* installed.php. Must include a `name` key.
*/
protected function addPackage(string $dir, array $package): void {
$this->assertArrayHasKey('name', $package);
$this->setPackage($dir, $package['name'], $package, FALSE);
}
/**
* Modifies a package's installed info.
*
* @param string $dir
* The root Composer-managed directory (e.g., the project root or staging
* area).
* @param string $name
* The name of the package to modify.
* @param array $package
* The package info that should be updated in installed.json and
* installed.php.
*/
protected function modifyPackage(string $dir, string $name, array $package): void {
$this->setPackage($dir, $name, $package, TRUE);
}
/**
* Removes a package.
*
* @param string $dir
* The root Composer-managed directory (e.g., the project root or staging
* area).
* @param string $name
* The name of the package to remove.
*/
protected function removePackage(string $dir, string $name): void {
$this->setPackage($dir, $name, NULL, TRUE);
}
/**
* Changes a package's installation information in a particular directory.
*
* This function is internal and should not be called directly. Use
* ::addPackage(), ::modifyPackage(), and ::removePackage() instead.
*
* @param string $dir
* The root Composer-managed directory (e.g., the project root or staging
* area).
* @param string $name
* The name of the package to add, update, or remove.
* @param array|null $package
* The package information to be set in installed.json and installed.php, or
* NULL to remove it. Will be merged into the existing information if the
* package is already installed.
* @param bool $should_exist
* Whether or not the package is expected to already be installed.
*/
private function setPackage(string $dir, string $name, ?array $package, bool $should_exist): void {
$file = $dir . '/vendor/composer/installed.json';
$this->assertFileIsWritable($file);
$data = file_get_contents($file);
$data = json_decode($data, TRUE, 512, JSON_THROW_ON_ERROR);
// If the package is already installed, find its numerical index.
$position = NULL;
for ($i = 0; $i < count($data['packages']); $i++) {
if ($data['packages'][$i]['name'] === $name) {
$position = $i;
break;
}
}
// Ensure that we actually expect to find the package already installed (or
// not).
$message = $should_exist
? "Expected package '$name' to be installed, but it wasn't."
: "Expected package '$name' to not be installed, but it was.";
$this->assertSame($should_exist, isset($position), $message);
if (isset($position)) {
// If we're going to be updating the package data, merge the incoming data
// into what we already have.
if ($package) {
$package = NestedArray::mergeDeep($data['packages'][$position], $package);
}
// Remove the existing package; the array will be re-keyed by
// array_splice().
array_splice($data['packages'], $position, 1);
$data['dev-package-names'] = array_diff($data['dev-package-names'], [$name]);
$data['dev-package-names'] = array_values($data['dev-package-names']);
}
// Add the package back to the list, if we have data for it.
if ($package) {
$package['name'] = $name;
$data['packages'][] = $package;
if (!empty($package['dev_requirement'])) {
$data['dev-package-names'][] = $name;
}
}
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$file = $dir . '/vendor/composer/installed.php';
$this->assertFileIsWritable($file);
$data = require $file;
if ($package) {
$data['versions'][$name] = $package;
}
else {
unset($data['versions'][$name]);
}
$data = var_export($data, TRUE);
file_put_contents($file, "<?php\nreturn $data;");
}
} }
# `StagedProjectsValidatorTest` Fixtures
### new_project_added
Simulates a scenario where, while updating Drupal core in a site with no non-core dependencies, a new contrib module and
a new custom module are unexpectedly installed (as runtime and dev dependencies, respectively). Additionally, two new
non-Drupal packages are installed (again, one as a runtime dependency, the other dev).
**Expectation**: The validator should complain about the new modules; the new non-Drupal packages are ignored.
### no_errors
Simulates a scenario where, while updating Drupal core in a site with two unpinned contrib dependencies (one runtime and
one dev), no Drupal packages are updated, but two non-Drupal libraries are removed (again, one a runtime dependency, the
other dev), two are updated (same arrangement), and two are added (ditto).
**Expectation**: The validator to raise no errors; changes to non-Drupal packages are ignored.
### project_removed
Simulates a scenario where, while updating Drupal core in a site with no non-core dependencies, an installed contrib
theme and an installed custom theme are unexpectedly removed (from runtime and dev dependencies, respectively).
Additionally, two installed non-Drupal packages are removed (again, one from a runtime dependency, the other dev). The
existing contrib dependencies' installed versions are unchanged.
**Expectation**: The validator should complain about the removed themes; the removed non-Drupal packages are ignored.
### version_changed
Simulates a scenario where, while updating Drupal core in a site with two unpinned contrib dependencies (one runtime and
one dev), the contrib modules are unexpectedly updated, as are two installed non-Drupal packages (again, one a runtime
dependency, the other dev).
**Expectation**: The validator should complain about the updated modules; the updated non-Drupal packages are ignored.
{
"_readme": [
"This file simulates a list of packages installed in a virtual staging area.",
"It will be compared against active.installed.json.",
"See \\Drupal\\Tests\\automatic_updates\\Kernel\\StatusCheck\\StagedProjectsValidatorTest::testErrors()"
],
"packages": [
{
"name": "drupal/core",
"version": "9.8.1",
"type": "drupal-core",
"extra": {
"drupal-scaffold": {
"file-mapping": {}
}
}
},
{
"name": "drupal/test_module",
"version": "1.3.0",
"type": "drupal-module"
},
{
"name": "drupal/test_module2",
"version": "1.3.1",
"type": "drupal-module"
},
{
"name": "other/new_project",
"description": "This is newly added project but there should be no error because it is not a drupal project",
"version": "1.3.1",
"type": "library"
},
{
"name": "drupal/dev-test_module",
"version": "1.3.0",
"type": "drupal-module"
},
{
"name": "drupal/dev-test_module2",
"version": "1.3.1",
"type": "drupal-custom-module"
},
{
"name": "other/dev-new_project",
"description": "This is newly added project but there should be no error because it is not a drupal project",
"version": "1.3.1",
"type": "library"
}
],
"dev": true,
"dev-package-names": [
"drupal/dev-test_module",
"drupal/dev-test_module2",
"other/dev-new_project"
]
}
<?php
/**
* @file
*/
$projects_dir = __DIR__ . '/../../modules';
return [
'versions' => [
'drupal/test_module' => [
'type' => 'drupal-module',
'install_path' => $projects_dir . '/test_module',
],
'drupal/dev-test_module' => [
'type' => 'drupal-module',
'install_path' => $projects_dir . '/dev-test_module',
],
'drupal/test_module2' => [
'type' => 'drupal-module',
'install_path' => $projects_dir . '/test_module2',
],
'drupal/dev-test_module2' => [
'type' => 'drupal-module',
'install_path' => $projects_dir . '/dev-test_module2',
],
'other/new_project' => [
'type' => 'library',
'install_path' => __DIR__ . '/../../new_project',
],
'other/dev-new_project' => [
'type' => 'library',
'install_path' => __DIR__ . '/../../dev-new_project',
],
],
];
...@@ -4,16 +4,15 @@ ...@@ -4,16 +4,15 @@
* @file * @file
*/ */
$projects_dir = __DIR__ . '/../../modules';
return [ return [
'versions' => [ 'versions' => [
'drupal/test_module' => [ 'drupal/test_module' => [
'type' => 'drupal-module', 'type' => 'drupal-module',
'install_path' => $projects_dir . '/test_module', 'install_path' => '../../modules/test_module',
], ],
'drupal/dev-test_module' => [ 'drupal/dev-test_module' => [
'type' => 'drupal-module', 'type' => 'drupal-module',
'install_path' => $projects_dir . '/dev-test_module', 'install_path' => '../../modules/dev-test_module',
], ],
], ],
]; ];
{
"_readme": [
"This file simulates a list of packages installed in a virtual staging area.",
"It will be compared against active.installed.json.",
"See \\Drupal\\Tests\\automatic_updates\\Kernel\\StatusCheck\\StagedProjectsValidatorTest::testErrors()"
],
"packages": [
{
"name": "drupal/core",
"version": "9.8.1",
"type": "drupal-core",
"extra": {
"drupal-scaffold": {
"file-mapping": {}
}
}
},
{
"name": "drupal/test_module",
"version": "1.3.0",
"type": "drupal-module"
},
{
"name": "other/new_project",
"description": "This is newly added project but there should be no error because it is not a drupal project",
"version": "1.3.1",
"type": "library"
},
{
"name": "other/changed",
"description": "This project version is changed but there should be no error because it is not a Drupal project.",
"version": "1.3.2",
"type": "library"
},
{
"name": "drupal/dev-test_module",
"version": "1.3.0",
"type": "drupal-module"
},
{
"name": "other/dev-new_project",
"description": "This is newly added project but there should be no error because it is not a drupal project",
"version": "1.3.1",
"type": "library"
},
{
"name": "other/dev-changed",
"description": "This project version is changed but there should be no error because it is not a Drupal project.",
"version": "1.3.2",
"type": "library"
}
],
"dev": true,
"dev-package-names": [
"drupal/dev-test_module",
"other/dev-new_project",
"other/dev-changed"
]
}
<?php
/**
* @file
* Simulates that 2 packages are installed in virtual staging area.
*/
$projects_dir = __DIR__ . '/../../';
return [
'versions' => [
'other/new_project' => [
'type' => 'library',
'install_path' => $projects_dir . '/other/new_project',
],
'other/dev-new_project' => [
'type' => 'library',
'install_path' => $projects_dir . '/other/dev-new_project',
],
],
];
{
"_readme": [
"This file simulates a list of packages installed in a virtual staging area.",
"It will be compared against active.installed.json.",
"See \\Drupal\\Tests\\automatic_updates\\Kernel\\StatusCheck\\StagedProjectsValidatorTest::testErrors()"
],
"packages": [
{
"name": "drupal/core",
"version": "9.8.1",
"type": "drupal-core",
"extra": {
"drupal-scaffold": {
"file-mapping": {}
}
}
},
{
"name": "drupal/test_module2",
"version": "1.3.1",
"type": "drupal-module"
},
{
"name": "drupal/dev-test_module2",
"version": "1.3.1",
"type": "drupal-module"
}
],
"dev": true,
"dev-package-names": [
"drupal/dev-test_module2"
]
}
{
"_readme": [
"This file simulates a list of packages installed in a virtual staging area.",
"It will be compared against active.installed.json.",
"See \\Drupal\\Tests\\automatic_updates\\Kernel\\StatusCheck\\StagedProjectsValidatorTest::testErrors()"
],
"packages": [
{
"name": "drupal/core",
"version": "9.8.1",
"type": "drupal-core",
"extra": {
"drupal-scaffold": {
"file-mapping": {}
}
}
},
{
"name": "drupal/test_module",
"version": "1.3.1",
"type": "drupal-module"
},
{
"name": "other/changed",
"description": "This project version is changed but there should be no error because it is not a Drupal project.",
"version": "1.3.2",
"type": "library"
},
{
"name": "drupal/dev-test_module",
"version": "1.3.1",
"type": "drupal-module"
},
{
"name": "other/dev-changed",
"description": "This project version is changed but there should be no error because it is not a Drupal project.",
"version": "1.3.2",
"type": "library"
}
],
"dev": true,
"dev-package-names": [
"drupal/dev-test_module",
"other/dev-changed"
]
}
...@@ -6,6 +6,7 @@ use Drupal\package_manager\Event\PreApplyEvent; ...@@ -6,6 +6,7 @@ use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\Exception\StageValidationException; use Drupal\package_manager\Exception\StageValidationException;
use Drupal\package_manager\ValidationResult; use Drupal\package_manager\ValidationResult;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase; use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
use Drupal\Tests\package_manager\Traits\FixtureUtilityTrait;
/** /**
* @covers \Drupal\automatic_updates\Validator\StagedProjectsValidator * @covers \Drupal\automatic_updates\Validator\StagedProjectsValidator
...@@ -14,6 +15,8 @@ use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase; ...@@ -14,6 +15,8 @@ use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
*/ */
class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase { class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase {
use FixtureUtilityTrait;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -29,28 +32,6 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -29,28 +32,6 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase {
parent::setUp(); parent::setUp();
} }
/**
* Asserts a set of validation results when staged changes are applied.
*
* @param \Drupal\package_manager\ValidationResult[] $expected_results
* The expected validation results.
*/
private function validate(array $expected_results): void {
/** @var \Drupal\automatic_updates\Updater $updater */
$updater = $this->container->get('automatic_updates.updater');
$updater->begin(['drupal' => '9.8.1']);
$updater->stage();
try {
$updater->apply();
// If no exception occurs, ensure we weren't expecting any errors.
$this->assertEmpty($expected_results);
}
catch (StageValidationException $e) {
$this->assertValidationResultsEqual($expected_results, $e->getResults());
}
}
/** /**
* Tests that exceptions are turned into validation errors. * Tests that exceptions are turned into validation errors.
*/ */
...@@ -72,75 +53,188 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -72,75 +53,188 @@ class StagedProjectsValidatorTest extends AutomaticUpdatesKernelTestBase {
$this->container->get('event_dispatcher') $this->container->get('event_dispatcher')
->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX); ->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX);
$this->validate([ /** @var \Drupal\automatic_updates\Updater $updater */
ValidationResult::createError(["Composer could not find the config file: $composer_json\n"]), $updater = $this->container->get('automatic_updates.updater');
$updater->begin(['drupal' => '9.8.1']);
$updater->stage();
$error = ValidationResult::createError(["Composer could not find the config file: $composer_json\n"]);
try {
$updater->apply();
$this->fail('Expected an error, but none was raised.');
}
catch (StageValidationException $e) {
$this->assertValidationResultsEqual([$error], $e->getResults());
}
}
/**
* Tests that an error is raised if Drupal extensions are unexpectedly added.
*/
public function testProjectsAdded(): void {
$this->copyFixtureFolderToActiveDirectory(__DIR__ . '/../../../fixtures/StagedProjectsValidatorTest/new_project_added');
$updater = $this->container->get('automatic_updates.updater');
$updater->begin(['drupal' => '9.8.1']);
$updater->stage();
$stage_dir = $updater->getStageDirectory();
$this->addPackage($stage_dir, [
'name' => 'drupal/test_module2',
'version' => '1.3.1',
'type' => 'drupal-module',
'install_path' => '../../modules/test_module2',
]);
$this->addPackage($stage_dir, [
'name' => 'drupal/dev-test_module2',
'version' => '1.3.1',
'type' => 'drupal-custom-module',
'dev_requirement' => TRUE,
'install_path' => '../../modules/dev-test_module2',
]);
// The validator shouldn't complain about these packages being added or
// removed, since it only cares about Drupal modules and themes.
$this->addPackage($stage_dir, [
'name' => 'other/new_project',
'version' => '1.3.1',
'type' => 'library',
'install_path' => '../other/new_project',
]); ]);
$this->addPackage($stage_dir, [
'name' => 'other/dev-new_project',
'version' => '1.3.1',
'type' => 'library',
'dev_requirement' => TRUE,
'install_path' => '../other/dev-new_project',
]);
$this->removePackage($stage_dir, 'other/removed');
$this->removePackage($stage_dir, 'other/dev-removed');
$messages = [
"module 'drupal/test_module2' installed.",
"custom module 'drupal/dev-test_module2' installed.",
];
$error = ValidationResult::createError($messages, t('The update cannot proceed because the following Drupal projects were installed during the update.'));
try {
$updater->apply();
$this->fail('Expected an error, but none was raised.');
}
catch (StageValidationException $e) {
$this->assertValidationResultsEqual([$error], $e->getResults());
}
} }
/** /**
* Tests validation errors, or lack thereof. * Tests that errors are raised if Drupal extensions are unexpectedly removed.
*
* @param string $root_fixture_directory
* A directory containing to fixtures sub-directories, 'active' and
* 'staged'.
* @param string|null $expected_summary
* The expected error summary, or NULL if no errors are expected.
* @param string[] $expected_messages
* The expected error messages, if any.
*
* @dataProvider providerErrors
*/ */
public function testErrors(string $root_fixture_directory, ?string $expected_summary, array $expected_messages): void { public function testProjectsRemoved(): void {
$this->copyFixtureFolderToActiveDirectory("$root_fixture_directory/active"); $this->copyFixtureFolderToActiveDirectory(__DIR__ . '/../../../fixtures/StagedProjectsValidatorTest/project_removed');
$this->copyFixtureFolderToStageDirectoryOnApply("$root_fixture_directory/staged");
$updater = $this->container->get('automatic_updates.updater');
$expected_results = []; $updater->begin(['drupal' => '9.8.1']);
if ($expected_messages) { $updater->stage();
// @codingStandardsIgnoreLine
$expected_results[] = ValidationResult::createError($expected_messages, t($expected_summary)); $stage_dir = $updater->getStageDirectory();
$this->removePackage($stage_dir, 'drupal/test_theme');
$this->removePackage($stage_dir, 'drupal/dev-test_theme');
// The validator shouldn't complain about these packages being removed,
// since it only cares about Drupal modules and themes.
$this->removePackage($stage_dir, 'other/removed');
$this->removePackage($stage_dir, 'other/dev-removed');
$messages = [
"theme 'drupal/test_theme' removed.",
"custom theme 'drupal/dev-test_theme' removed.",
];
$error = ValidationResult::createError($messages, t('The update cannot proceed because the following Drupal projects were removed during the update.'));
try {
$updater->apply();
$this->fail('Expected an error, but none was raised.');
}
catch (StageValidationException $e) {
$this->assertValidationResultsEqual([$error], $e->getResults());
} }
$this->validate($expected_results);
} }
/** /**
* Data provider for testErrors(). * Tests that errors are raised if Drupal extensions are unexpectedly updated.
*
* @return \string[][]
* The test cases.
*/ */
public function providerErrors(): array { public function testVersionsChanged(): void {
$fixtures_folder = __DIR__ . '/../../../fixtures/StagedProjectsValidatorTest'; $this->copyFixtureFolderToActiveDirectory(__DIR__ . '/../../../fixtures/StagedProjectsValidatorTest/version_changed');
return [
'new_project_added' => [ $updater = $this->container->get('automatic_updates.updater');
"$fixtures_folder/new_project_added", $updater->begin(['drupal' => '9.8.1']);
'The update cannot proceed because the following Drupal projects were installed during the update.', $updater->stage();
[
"module 'drupal/test_module2' installed.", $stage_dir = $updater->getStageDirectory();
"custom module 'drupal/dev-test_module2' installed.", $this->modifyPackage($stage_dir, 'drupal/test_module', [
], 'version' => '1.3.1',
], ]);
'project_removed' => [ $this->modifyPackage($stage_dir, 'drupal/dev-test_module', [
"$fixtures_folder/project_removed", 'version' => '1.3.1',
'The update cannot proceed because the following Drupal projects were removed during the update.', ]);
[ // The validator shouldn't complain about these packages being updated,
"theme 'drupal/test_theme' removed.", // because it only cares about Drupal modules and themes.
"custom theme 'drupal/dev-test_theme' removed.", $this->modifyPackage($stage_dir, 'other/changed', [
], 'version' => '1.3.2',
], ]);
'version_changed' => [ $this->modifyPackage($stage_dir, 'other/dev-changed', [
"$fixtures_folder/version_changed", 'version' => '1.3.2',
'The update cannot proceed because the following Drupal projects were unexpectedly updated. Only Drupal Core updates are currently supported.', ]);
[
"module 'drupal/test_module' from 1.3.0 to 1.3.1.", $messages = [
"module 'drupal/dev-test_module' from 1.3.0 to 1.3.1.", "module 'drupal/test_module' from 1.3.0 to 1.3.1.",
], "module 'drupal/dev-test_module' from 1.3.0 to 1.3.1.",
],
'no_errors' => [
"$fixtures_folder/no_errors",
NULL,
[],
],
]; ];
$error = ValidationResult::createError($messages, t('The update cannot proceed because the following Drupal projects were unexpectedly updated. Only Drupal Core updates are currently supported.'));
try {
$updater->apply();
$this->fail('Expected an error, but none was raised.');
}
catch (StageValidationException $e) {
$this->assertValidationResultsEqual([$error], $e->getResults());
}
}
/**
* Tests that no errors occur if only core and its dependencies are updated.
*/
public function testNoErrors(): void {
$this->copyFixtureFolderToActiveDirectory(__DIR__ . '/../../../fixtures/StagedProjectsValidatorTest/no_errors');
$updater = $this->container->get('automatic_updates.updater');
$updater->begin(['drupal' => '9.8.1']);
$updater->stage();
$stage_dir = $updater->getStageDirectory();
$this->modifyPackage($stage_dir, 'drupal/core', [
'version' => '9.8.1',
]);
// The validator shouldn't care what happens to these packages, since it
// only concerns itself with Drupal modules and themes.
$this->addPackage($stage_dir, [
'name' => 'other/new_project',
'version' => '1.3.1',
'type' => 'library',
'install_path' => '../other/new_project',
]);
$this->addPackage($stage_dir, [
'name' => 'other/dev-new_project',
'version' => '1.3.1',
'type' => 'library',
'dev_requirement' => TRUE,
'install_path' => '../other/dev-new_project',
]);
$this->modifyPackage($stage_dir, 'other/changed', [
'version' => '1.3.2',
]);
$this->modifyPackage($stage_dir, 'other/dev-changed', [
'version' => '1.3.2',
]);
$this->removePackage($stage_dir, 'other/removed');
$this->removePackage($stage_dir, 'other/dev-removed');
$updater->apply();
} }
} }
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