function StagedDBUpdateValidatorTest::testStagedDatabaseUpdate
Tests validation of staged database updates.
@dataProvider providerStagedDatabaseUpdate
Parameters
string $extension_dir: The directory of the extension that should have database updates, relative to the stage directory.
string $file_extension: The extension of the update file, without the leading period. Must be either `install` or `post_update.php`.
\Drupal\package_manager\ValidationResult[] $expected_results: The expected validation results.
File
-
core/
modules/ package_manager/ tests/ src/ Kernel/ StagedDBUpdateValidatorTest.php, line 143
Class
- StagedDBUpdateValidatorTest
- @covers \Drupal\package_manager\Validator\SandboxDatabaseUpdatesValidator @group package_manager @internal
Namespace
Drupal\Tests\package_manager\KernelCode
public function testStagedDatabaseUpdate(string $extension_dir, string $file_extension, array $expected_results) : void {
$extension_name = basename($extension_dir);
$relative_file_path = $extension_dir . '/' . $extension_name . '.' . $file_extension;
$stage = $this->createStage();
$stage->create();
// Nothing has been changed in the stage, so ensure the validator doesn't
// detect any changes.
$this->assertStatusCheckResults([], $stage);
$staged_update_file = $stage->getSandboxDirectory() . '/' . $relative_file_path;
$this->assertFileIsWritable($staged_update_file);
// Now add a "real" update function -- either a schema update or a
// post-update, depending on what $file_extension is -- and ensure that the
// validator detects it.
$update_function_name = match ($file_extension) { 'install' => $extension_name . '_update_1001',
'post_update.php' => $extension_name . '_post_update_' . $this->randomMachineName(),
};
file_put_contents($staged_update_file, "function {$update_function_name}() {}\n", FILE_APPEND);
$this->assertStatusCheckResults($expected_results, $stage);
// Add a bunch of functions which are named similarly to real schema update
// and post-update functions, but not quite right, to ensure they are
// ignored by the validator. Also throw an anonymous function in there to
// ensure those are ignored as well.
$code = <<<END
<?php
function {<span class="php-variable">$extension_name</span>}_update() { \$foo = function () {}; }
function {<span class="php-variable">$extension_name</span>}_update_string_123() {}
function {<span class="php-variable">$extension_name</span>}_update__123() {}
function ({<span class="php-variable">$extension_name</span>}}__post_update_test() {}
function ({<span class="php-variable">$extension_name</span>}}_post_update() {}
END;
file_put_contents($staged_update_file, $code);
$this->assertStatusCheckResults([], $stage);
// If the update file is deleted from the stage, the validator should not
// detect any database updates.
unlink($staged_update_file);
$this->assertStatusCheckResults([], $stage);
// If the update file doesn't exist in the active directory, but does exist
// in the stage with a legitimate schema update or post-update function, the
// validator should detect it.
$project_root = $this->container
->get(PathLocator::class)
->getProjectRoot();
unlink($project_root . '/' . $relative_file_path);
file_put_contents($staged_update_file, "<?php\nfunction {$update_function_name}() {}");
$this->assertStatusCheckResults($expected_results, $stage);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.