Skip to content

Commit

Permalink
Test post-updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrenssen committed Jan 16, 2018
1 parent 3bdec72 commit c5faa7e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
69 changes: 62 additions & 7 deletions tests/UpdateDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ public function testFailedUpdate()
$this->drush('updatedb', [], $options, null, null, self::EXIT_ERROR);

$expected_output = <<<LOG
-------- ----------- --------------- ----------------------
Module Update ID Type Description
-------- ----------- --------------- ----------------------
woot 8101 hook_update_n Good update.
woot 8102 hook_update_n Failing update.
woot failing post-update Failing post-update.
-------- ----------- --------------- ----------------------
-------- ----------- --------------- -----------------------
Module Update ID Type Description
-------- ----------- --------------- -----------------------
woot 8101 hook_update_n Good update.
woot 8102 hook_update_n Failing update.
woot 8103 hook_update_n Another good update.
woot failing post-update Failing post-update.
-------- ----------- --------------- -----------------------
// Do you wish to run the specified pending updates?: yes.
LOG;
Expand All @@ -98,6 +99,60 @@ public function testFailedUpdate()
$this->assertErrorOutputEquals(preg_replace('# *#', ' ', $this->simplifyOutput($expected_error_output)));
}

/**
* Tests that a failed post-update is handled correctly.
*/
public function testFailedPostUpdate()
{
$sites = $this->setUpDrupal(1, true);
$options = [
'yes' => null,
'root' => $root = $this->webroot(),
'uri' => key($sites),
];
$this->setupModulesForTests($root);
$this->drush('pm-enable', ['woot'], $options);

// Force re-run of woot_update_8101().
$this->drush('php:eval', array('drupal_set_installed_schema_version("woot", 8102)'), $options);

// Force re-run of post-update hooks.
$this->forcePostUpdate('woot_post_update_a', $options);
$this->forcePostUpdate('woot_post_update_failing', $options);

// Run updates.
$this->drush('updatedb', [], $options, null, null, self::EXIT_ERROR);

$expected_output = <<<LOG
-------- ----------- --------------- -------------------------
Module Update ID Type Description
-------- ----------- --------------- -------------------------
woot 8103 hook_update_n Another good update.
woot a post-update Successful post-update.
woot failing post-update Failing post-update.
-------- ----------- --------------- -------------------------
// Do you wish to run the specified pending updates?: yes.
LOG;
$this->assertOutputEquals(preg_replace('# *#', ' ', $this->simplifyOutput($expected_output)));

$expected_error_output = <<<LOG
[notice] Update started: woot_update_8103
[notice] This is the update message from woot_update_8103
[ok] Update completed: woot_update_8103
[notice] Update started: woot_post_update_a
[notice] This is the update message from woot_post_update_a
[ok] Update completed: woot_post_update_a
[notice] Update started: woot_post_update_failing
[error] This is the exception message thrown in woot_post_update_failing
[error] Update failed: woot_post_update_failing
[error] Update aborted by: woot_post_update_failing
[error] Finished performing updates.
LOG;

$this->assertErrorOutputEquals(preg_replace('# *#', ' ', $this->simplifyOutput($expected_error_output)));
}

/**
* Tests that the updatedb command works when new services are introduced.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/resources/modules/d8/woot/woot.install
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ function woot_update_8101() {
function woot_update_8102() {
throw new \Exception('This is the exception message thrown in woot_update_8102');
}

/**
* Another good update.
*/
function woot_update_8103() {
return t('This is the update message from woot_update_8103');
}
12 changes: 11 additions & 1 deletion tests/resources/modules/d8/woot/woot.post_update.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
<?php

/**
* Successful post-update.
*/
function woot_post_update_a()
{
// Note that this is called 'a' so that it will run first. The post-updates
// are executed in alphabetical order.
return t('This is the update message from woot_post_update_a');
}

/**
* Failing post-update.
*/
function woot_post_update_failing()
{
throw new \Exception('post update error');
throw new \Exception('This is the exception message thrown in woot_post_update_failing');
}

/**
Expand Down

0 comments on commit c5faa7e

Please sign in to comment.