Skip to content

Commit

Permalink
Fix execution of deploy hooks inside a module with _deploy in the nam…
Browse files Browse the repository at this point in the history
…e. (#5217)

* Test execution of deploy hooks inside a module with _deploy in the name.

Test for #5216

* Support deploy hooks in modules with _deploy in the name.
  • Loading branch information
pfrenssen committed Sep 3, 2022
1 parent 2940512 commit 5f9ca1c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
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());
}
}

0 comments on commit 5f9ca1c

Please sign in to comment.