You are here

public function ConfigOverrideKernelTest::testIndexSave in Search API 8

Checks whether saving an index with overrides works correctly.

File

tests/src/Kernel/ConfigEntity/ConfigOverrideKernelTest.php, line 114

Class

ConfigOverrideKernelTest
Contains tests for config entities with overrides.

Namespace

Drupal\Tests\search_api\Kernel\ConfigEntity

Code

public function testIndexSave() {
  $this->server
    ->save();

  // Even though no processors are set on the index, saving it should trigger
  // the test processor's preIndexSave() method (since we added that processor
  // in the override).
  $this
    ->assertEmpty($this->index
    ->getProcessorsByStage(ProcessorInterface::STAGE_PRE_INDEX_SAVE));
  $this->index
    ->save();
  $this
    ->assertEquals([
    'preIndexSave',
  ], $this
    ->getCalledMethods('processor'));
  $this
    ->assertEmpty($this->index
    ->getProcessorsByStage(ProcessorInterface::STAGE_PRE_INDEX_SAVE));

  // Verify the override is correctly present when loading the index.

  /** @var \Drupal\search_api\IndexInterface $index */
  $index = Index::load($this->index
    ->id());
  $this
    ->assertEquals('Overridden index', $index
    ->label());
  $server = $index
    ->getServerInstance();
  $this
    ->assertNotEmpty($server);
  $this
    ->assertEquals('Overridden server', $server
    ->label());
  $this
    ->assertTrue($index
    ->status());

  // Verify that overrides are not present when loading the index
  // override-free.

  /** @var \Drupal\search_api\Entity\SearchApiConfigEntityStorage $index_storage */
  $index_storage = \Drupal::entityTypeManager()
    ->getStorage('search_api_index');
  $index = $index_storage
    ->loadOverrideFree($index
    ->id());
  $this
    ->assertEquals('Test index', $index
    ->label());

  // Try to change the index's name (starting from the override-free index)
  // and verify a copy with overrides is used for post-save operations.
  $args = [];
  $this
    ->setMethodOverride('backend', 'updateIndex', function () use (&$args) {
    $args = func_get_args();
  });
  $index
    ->set('name', 'New index name')
    ->save();
  $this
    ->assertCount(2, $args);
  $index = $args[1];
  $this
    ->assertEquals('Overridden index', $index
    ->label());

  // Verify the override is correctly present when loading the index.
  $index = Index::load($this->index
    ->id());
  $this
    ->assertEquals('Overridden index', $index
    ->label());
  $this
    ->assertEquals('Overridden server', $index
    ->getServerInstance()
    ->label());

  // Verify the new name is included when loading the index override-free.
  $index = $index_storage
    ->loadOverrideFree($index
    ->id());
  $this
    ->assertEquals('New index name', $index
    ->label());
}