You are here

protected function BackendTest::checkModuleUninstall in Search API 8

Tests whether removing the configuration again works as it should.

Overrides BackendTestBase::checkModuleUninstall

File

modules/search_api_db/tests/src/Kernel/BackendTest.php, line 874

Class

BackendTest
Tests index and search capabilities using the Database search backend.

Namespace

Drupal\Tests\search_api_db\Kernel

Code

protected function checkModuleUninstall() {
  $db_info = $this
    ->getIndexDbInfo();
  $normalized_storage_table = $db_info['index_table'];
  $field_tables = $db_info['field_tables'];

  // See whether clearing the server works.
  // Regression test for #2156151.
  $server = $this
    ->getServer();
  $index = $this
    ->getIndex();
  $server
    ->deleteAllIndexItems($index);
  $query = $this
    ->buildSearch();
  $results = $query
    ->execute();
  $this
    ->assertEquals(0, $results
    ->getResultCount(), 'Clearing the server worked correctly.');
  $schema = \Drupal::database()
    ->schema();
  $table_exists = $schema
    ->tableExists($normalized_storage_table);
  $this
    ->assertTrue($table_exists, 'The index tables were left in place.');

  // See whether disabling the index correctly removes all of its tables.
  $index
    ->disable()
    ->save();
  $db_info = $this
    ->getIndexDbInfo();
  $this
    ->assertNull($db_info, 'The index was successfully removed from the server.');
  $table_exists = $schema
    ->tableExists($normalized_storage_table);
  $this
    ->assertFalse($table_exists, 'The index tables were deleted.');
  foreach ($field_tables as $field_table) {
    $table_exists = $schema
      ->tableExists($field_table['table']);
    $this
      ->assertFalse($table_exists, "Field table {$field_table['table']} was successfully deleted.");
  }
  $index
    ->enable()
    ->save();

  // Remove first the index and then the server.
  $index
    ->setServer();
  $index
    ->save();
  $db_info = $this
    ->getIndexDbInfo();
  $this
    ->assertNull($db_info, 'The index was successfully removed from the server.');
  $table_exists = $schema
    ->tableExists($normalized_storage_table);
  $this
    ->assertFalse($table_exists, 'The index tables were deleted.');
  foreach ($field_tables as $field_table) {
    $table_exists = $schema
      ->tableExists($field_table['table']);
    $this
      ->assertFalse($table_exists, "Field table {$field_table['table']} was successfully deleted.");
  }

  // Re-add the index to see if the associated tables are also properly
  // removed when the server is deleted.
  $index
    ->setServer($server);
  $index
    ->save();
  $server
    ->delete();
  $db_info = $this
    ->getIndexDbInfo();
  $this
    ->assertNull($db_info, 'The index was successfully removed from the server.');
  $table_exists = $schema
    ->tableExists($normalized_storage_table);
  $this
    ->assertFalse($table_exists, 'The index tables were deleted.');
  foreach ($field_tables as $field_table) {
    $table_exists = $schema
      ->tableExists($field_table['table']);
    $this
      ->assertFalse($table_exists, "Field table {$field_table['table']} was successfully deleted.");
  }

  // Uninstall the module.
  \Drupal::service('module_installer')
    ->uninstall([
    'search_api_db',
  ], FALSE);
  $this
    ->assertFalse(\Drupal::moduleHandler()
    ->moduleExists('search_api_db'), 'The Database Search module was successfully uninstalled.');
  $tables = $schema
    ->findTables('search_api_db_%');
  $expected = [
    'search_api_db_database_search_index' => 'search_api_db_database_search_index',
  ];
  $this
    ->assertEquals($expected, $tables, 'All the tables of the Database Search module have been removed.');
}