You are here

public function DependencyRemovalTest::testSingleDatasourceDependency in Search API 8

Tests removing the (hard) dependency of the index's single datasource.

File

tests/src/Kernel/ConfigEntity/DependencyRemovalTest.php, line 291

Class

DependencyRemovalTest
Tests what happens when an index's or a server's dependencies are removed.

Namespace

Drupal\Tests\search_api\Kernel\ConfigEntity

Code

public function testSingleDatasourceDependency() {

  // Add the datasource to the index and save it. The datasource configuration
  // contains the dependencies it will return – in our case, we use the test
  // server.
  $dependency_key = $this->dependency
    ->getConfigDependencyKey();
  $dependency_name = $this->dependency
    ->getConfigDependencyName();
  $datasources['search_api_test'] = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createDatasourcePlugin($this->index, 'search_api_test', [
    $dependency_key => [
      $dependency_name,
    ],
  ]);
  $this->index
    ->setDatasources($datasources);
  $this->index
    ->save();

  // Since in this test the index will be removed, we need a mock key/value
  // store (the index will purge any unsaved configuration of it upon
  // deletion, which uses a "user-shared temp store", which in turn uses a
  // key/value store).
  $mock = $this
    ->createMock(KeyValueStoreExpirableInterface::class);
  $mock_factory = $this
    ->createMock(KeyValueExpirableFactoryInterface::class);
  $mock_factory
    ->method('get')
    ->willReturn($mock);
  $this->container
    ->set('keyvalue.expirable', $mock_factory);

  // Delete the datasource's dependency.
  $this->dependency
    ->delete();

  // Reload the index to ensure it was deleted.
  $this
    ->reloadIndex();
  $this
    ->assertNull($this->index, 'Index was removed');
}