You are here

public function DependencyRemovalTest::testTrackerDependency in Search API 8

Tests a tracker with a dependency that gets removed.

@dataProvider dependencyTestDataProvider

Parameters

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

File

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

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

  // Set the tracker for the index and save it. The tracker 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\Tracker\TrackerInterface $tracker */
  $tracker = \Drupal::getContainer()
    ->get('search_api.plugin_helper')
    ->createTrackerPlugin($this->index, 'search_api_test', [
    'dependencies' => [
      $dependency_key => [
        $dependency_name,
      ],
    ],
  ]);
  $this->index
    ->setTracker($tracker);
  $this->index
    ->save();

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

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

  // Delete the tracker'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], 'Tracker dependency removed from index');

  // Depending on whether the plugin should have removed the dependency or
  // not, make sure the right action was taken.
  $tracker_instance = $this->index
    ->getTrackerInstance();
  $tracker_id = $tracker_instance
    ->getPluginId();
  $tracker_config = $tracker_instance
    ->getConfiguration();
  if ($remove_dependency) {
    $this
      ->assertEquals('search_api_test', $tracker_id, 'Tracker not reset');
    $this
      ->assertEmpty($tracker_config['dependencies'], 'Tracker settings adapted');
  }
  else {
    $this
      ->assertEquals('default', $tracker_id, 'Tracker was reset');
    $this
      ->assertEquals($tracker_instance
      ->defaultConfiguration(), $tracker_config, 'Tracker settings were cleared');
  }
}