You are here

public function DependencyRemovalTest::testModuleDependency in Search API 8

Tests whether module dependencies are handled correctly.

File

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

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 testModuleDependency() {

  // Test with all types of plugins at once.

  /** @var \Drupal\search_api\Datasource\DatasourceInterface $datasource */
  $datasource = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createDatasourcePlugin($this->index, 'search_api_test');
  $this->index
    ->addDatasource($datasource);
  $datasource = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createDatasourcePlugin($this->index, 'entity:user');
  $this->index
    ->addDatasource($datasource);

  /** @var \Drupal\search_api\Processor\ProcessorInterface $processor */
  $processor = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createProcessorPlugin($this->index, 'search_api_test');
  $this->index
    ->addProcessor($processor);

  /** @var \Drupal\search_api\Tracker\TrackerInterface $tracker */
  $tracker = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createTrackerPlugin($this->index, 'search_api_test');
  $this->index
    ->setTracker($tracker);
  $this->index
    ->save();

  // Check the dependencies were calculated correctly.
  $dependencies = $this->index
    ->getDependencies();
  $this
    ->assertContains('search_api_test', $dependencies['module'], 'Module dependency correctly inserted');

  // When the index resets the tracker, it needs to know the ID of the default
  // tracker.
  $this
    ->installConfig('search_api');

  // Disabling modules in Kernel tests normally doesn't trigger any kind of
  // reaction, just removes it from the list of modules (for example, to avoid
  // calling of a hook). Therefore, we have to trigger that behavior
  // ourselves.
  \Drupal::getContainer()
    ->get('config.manager')
    ->uninstall('module', 'search_api_test');

  // Reload the index and check it's still there.
  $this
    ->reloadIndex();
  $this
    ->assertInstanceOf('Drupal\\search_api\\IndexInterface', $this->index, 'Index not removed');

  // Make sure the dependency has been removed.
  $dependencies = $this->index
    ->getDependencies();
  $dependencies += [
    'module' => [],
  ];
  $this
    ->assertNotContains('search_api_test', $dependencies['module'], 'Module dependency removed from index');

  // Make sure all the plugins have been removed.
  $this
    ->assertNotContains('search_api_test', $this->index
    ->getDatasources(), 'Datasource was removed');
  $this
    ->assertArrayNotHasKey('search_api_test', $this->index
    ->getProcessors(), 'Processor was removed');
  $this
    ->assertEquals('default', $this->index
    ->getTrackerId(), 'Tracker was reset');
}