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

Entity type provided by a module installed via update hook not available #4103

Merged
merged 12 commits into from
Jul 23, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Commands/core/UpdateDBCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,17 @@ public static function updateDoOnePostUpdate($function, DrushBatchContext $conte
* @param array $results
* @param array $operations
*/
public static function updateFinished($success, $results, $operations)
public function updateFinished($success, $results, $operations)
{
// No code needed but the batch result bookkeeping fails without a finished callback.
$noop = 1;
if ($this->cache_clear) {
// Flush all caches at the end of the batch operation. When Drupal
// core performs database updates it also clears the cache at the
// end. This ensures that we are compatible with updates that rely
// on this behavior.
drupal_flush_all_caches();
}
}


/**
* Start the database update batch process.
* @param $options
Expand Down Expand Up @@ -442,7 +446,7 @@ public function updateBatch($options)
'title' => 'Updating',
'init_message' => 'Starting updates',
'error_message' => 'An unrecoverable error has occurred. You can find the error message below. It is advised to copy it to the clipboard for reference.',
'finished' => '\Drush\Commands\core\UpdateDBCommands::updateFinished',
'finished' => [$this, 'updateFinished'],
'file' => 'core/includes/update.inc',
];
batch_set($batch);
Expand Down
52 changes: 52 additions & 0 deletions tests/functional/UpdateDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,58 @@ public function testBatchUpdateLogMessages()
$this->assertContains($expected_post_update_output, $actual_output);
}

/**
* Tests installing modules with entity type definitions via update hooks.
*/
public function testEnableModuleViaUpdate()
{
$options = [
'yes' => null,
];
$this->setUpDrupal(1, true);
$this->setupModulesForTests(['woot'], Path::join(__DIR__, 'resources/modules/d8'));
$this->drush('pm:enable', ['woot'], $options);

// Force re-run of woot_update_8106().
$this->drush('php:eval', ['drupal_set_installed_schema_version("woot", 8105)'], $options);

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

// Check that the post-update function returns the new entity type ID.
$this->assertContains('[notice] taxonomy_term', $this->getErrorOutputRaw());

// Check that the new entity type is installed.
$this->drush('php:eval', ['woot_get_taxonomy_term_entity_type_id();']);
$this->assertContains('taxonomy_term', $this->getOutputRaw());
}

/**
* Tests installing modules with entity type definitions via post-update hooks.
*/
public function testEnableModuleViaPostUpdate()
{
$options = [
'yes' => null,
];
$this->setUpDrupal(1, true);
$this->setupModulesForTests(['woot'], Path::join(__DIR__, 'resources/modules/d8'));
$this->drush('pm:enable', ['woot'], $options);

// Force re-run of woot_post_update_install_taxonomy().
$this->forcePostUpdate('woot_post_update_install_taxonomy', $options);

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

// Check that the post-update function returns the new entity type ID.
$this->assertContains('[notice] taxonomy_term', $this->getErrorOutputRaw());

// Check that the new entity type is installed.
$this->drush('php:eval', ['woot_get_taxonomy_term_entity_type_id();']);
$this->assertContains('taxonomy_term', $this->getOutputRaw());
}

public function tearDown()
{
$this->recursiveDelete($this->pathPostUpdate, true);
Expand Down
9 changes: 9 additions & 0 deletions tests/functional/resources/modules/d8/woot/woot.install
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ function woot_update_8105(array &$sandbox) {
}
return "Iteration {$sandbox['current']}.";
}

/**
* Install taxonomy.module
*/
function woot_update_8106()
{
\Drupal::service('module_installer')->install(['taxonomy']);
return \Drupal::entityTypeManager()->getDefinition('taxonomy_term')->id();
}
11 changes: 11 additions & 0 deletions tests/functional/resources/modules/d8/woot/woot.module
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ function woot_theme() {

return $theme;
}

/**
* Prints the entity type ID of the taxonomy term entity type.
*
* @see \Unish\UpdateDBTest::testEnableModuleViaUpdate()
* @see \Unish\UpdateDBTest::testEnableModuleViaPostUpdate()
*/
function woot_get_taxonomy_term_entity_type_id()
{
print \Drupal::entityTypeManager()->getDefinition("taxonomy_term")->id();
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@ function woot_post_update_batch(array &$sandbox)
{
return woot_update_8105($sandbox);
}

/**
* Install taxonomy.module
*/
function woot_post_update_install_taxonomy()
{
\Drupal::service('module_installer')->install(['taxonomy']);
return \Drupal::entityTypeManager()->getDefinition('taxonomy_term')->id();
}