You are here

public function DependencyRemovalTest::testProcessorDependency in Search API 8

Tests a processor with a dependency that gets removed.

@dataProvider dependencyTestDataProvider

Parameters

bool $remove_dependency: Whether to remove the dependency from the processor when the object depended on is deleted.

File

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

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 testProcessorDependency($remove_dependency) {

  // Add the processor to the index and save it. The processor 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();

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

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

  // Tell the processor plugin whether it should successfully remove the
  // dependency.
  $this
    ->setReturnValue('processor', 'onDependencyRemoval', $remove_dependency);

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

  // 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, one way or the other.
  $dependencies = $this->index
    ->getDependencies();
  $dependencies += [
    $dependency_key => [],
  ];
  $this
    ->assertNotContains($dependency_name, $dependencies[$dependency_key], 'Processor dependency removed from index');

  // Depending on whether the plugin should have removed the dependency or
  // not, make sure the right action was taken.
  $processors = $this->index
    ->getProcessors();
  if ($remove_dependency) {
    $this
      ->assertArrayHasKey('search_api_test', $processors, 'Processor not removed');
    $this
      ->assertEmpty($processors['search_api_test']
      ->getConfiguration(), 'Processor settings adapted');
  }
  else {
    $this
      ->assertArrayNotHasKey('search_api_test', $processors, 'Processor removed');
  }
}