You are here

public function DependencyRemovalTest::testSuggesterDependency in Search API Autocomplete 8

Tests that a suggester dependency works correctly.

@dataProvider suggesterDependencyDataProvider

Parameters

bool $removable: TRUE if the suggester's dependency should be removable, FALSE otherwise.

bool $second_suggester: TRUE if a second suggester, apart from the test suggester, should be included in the search, FALSE otherwise.

File

tests/src/Kernel/DependencyRemovalTest.php, line 205

Class

DependencyRemovalTest
Tests dependency handling of the search entity.

Namespace

Drupal\Tests\search_api_autocomplete\Kernel

Code

public function testSuggesterDependency($removable, $second_suggester) {
  $dependency_key = $this->dependency
    ->getConfigDependencyKey();
  $dependency_name = $this->dependency
    ->getConfigDependencyName();
  $suggester_id = 'search_api_autocomplete_test';
  $settings = [
    $suggester_id => [
      'dependencies' => [
        $dependency_key => [
          $dependency_name,
        ],
      ],
    ],
  ];
  if ($second_suggester) {

    // Make the test backend support autocomplete so that the "Server"
    // suggester becomes available.
    $callback = [
      TestsHelper::class,
      'getSupportedFeatures',
    ];
    $this
      ->setMethodOverride('backend', 'getSupportedFeatures', $callback);
    $callback = [
      TestsHelper::class,
      'getAutocompleteSuggestions',
    ];
    $this
      ->setMethodOverride('backend', 'getAutocompleteSuggestions', $callback);
    $settings['server'] = [];
  }
  $this->search
    ->set('suggester_settings', $settings);
  $this->search
    ->save();
  $this
    ->setReturnValue('suggester', 'onDependencyRemoval', $removable);

  // Verify that the dependencies are all included.
  $dependencies = $this->search
    ->getDependencies();
  $this
    ->assertArrayHasKey($dependency_key, $dependencies);
  $this
    ->assertContains($dependency_name, $dependencies[$dependency_key]);
  $this
    ->assertArrayHasKey('module', $dependencies);
  $this
    ->assertContains('search_api_autocomplete_test', $dependencies['module']);

  // Delete the dependency and verify that the result is as expected.
  $this->dependency
    ->delete();
  $search = Search::load($this->search
    ->id());
  $this
    ->assertNotNull($search);
  $this
    ->assertEquals($removable || $second_suggester, $search
    ->status());
  $dependencies = $search
    ->getDependencies();
  $dependencies += [
    $dependency_key => [],
  ];
  $this
    ->assertNotContains($dependency_name, $dependencies[$dependency_key]);
  $this
    ->assertEquals($removable, $search
    ->isValidSuggester($suggester_id));
}