Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix execution of deploy hooks inside a module with _deploy in the name. #5217

Merged
merged 2 commits into from Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Drupal/Commands/core/DeployHookCommands.php
Expand Up @@ -171,9 +171,19 @@ public static function updateDoOneDeployHook(string $function, DrushBatchContext
return;
}

list($module, $name) = explode('_deploy_', $function, 2);
$filename = $module . '.deploy';
\Drupal::moduleHandler()->loadInclude($module, 'php', $filename);
// Module names can include '_deploy', so deploy functions like
// module_deploy_deploy_name() are ambiguous. Check every occurrence.
$components = explode('_', $function);
foreach (array_keys($components, 'deploy', TRUE) as $position) {
$module = implode('_', array_slice($components, 0, $position));
$name = implode('_', array_slice($components, $position + 1));
$filename = $module . '.deploy';
\Drupal::moduleHandler()->loadInclude($module, 'php', $filename);
if (function_exists($function)) {
break;
}
}

if (function_exists($function)) {
if (empty($context['results'][$module][$name]['type'])) {
Drush::logger()->notice("Deploy hook started: $function");
Expand Down
17 changes: 17 additions & 0 deletions tests/fixtures/modules/woot_deploy/woot_deploy.deploy.php
@@ -0,0 +1,17 @@
<?php

/**
* This is a NAME.deploy.php file. It contains "deploy" functions. These are
* one-time functions that run *after* config is imported during a deployment.
* These are a higher level alternative to hook_update_n and hook_post_update_NAME
* functions. See https://www.drush.org/latest/deploycommand/#authoring-update-functions
* for a detailed comparison.
*/

/**
* Deploy hook in module containing _deploy.
*/
function woot_deploy_deploy_function()
{
return 'This is the update message from ' . __FUNCTION__;
}
5 changes: 5 additions & 0 deletions tests/fixtures/modules/woot_deploy/woot_deploy.info.yml
@@ -0,0 +1,5 @@
name: Woot Deploy
type: module
description: Deploys Mightily.
core_version_requirement: ^9 || ^10
package: Other
18 changes: 18 additions & 0 deletions tests/functional/DeployHookTest.php
Expand Up @@ -94,4 +94,22 @@ public function testSkipDeployHooks()
$this->drush('deploy:hook-status', [], $options, null, null, self::EXIT_SUCCESS);
$this->assertStringContainsString('[]', $this->getOutput());
}

public function testDeployHooksInModuleWithDeployInName()
{
$this->setUpDrupal(1, true);
$options = [
'yes' => null,
];
$this->setupModulesForTests(['woot_deploy'], Path::join(__DIR__, '/../fixtures/modules'));
$this->drush('pm-install', ['woot_deploy'], $options);

// Run deploy hooks.
$this->drush('deploy:hook', [], $options, null, null, self::EXIT_SUCCESS);

$this->assertStringContainsString('[notice] Deploy hook started: woot_deploy_deploy_function', $this->getErrorOutput());
$this->assertStringContainsString('[notice] This is the update message from woot_deploy_deploy_function', $this->getErrorOutput());
$this->assertStringContainsString('[notice] Performed: woot_deploy_deploy_function', $this->getErrorOutput());
$this->assertStringContainsString('[success] Finished performing deploy hooks.', $this->getErrorOutput());
}
}