You are here

public function ConfigOverrideKernelTest::testServerSave in Search API 8

Checks whether saving a server with overrides works correctly.

File

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

Class

ConfigOverrideKernelTest
Contains tests for config entities with overrides.

Namespace

Drupal\Tests\search_api\Kernel\ConfigEntity

Code

public function testServerSave() {

  // Verify that in postInsert() the backend overrides are already applied.
  $passed_config = [];
  $passed_name = NULL;
  $override = function (BackendInterface $backend) use (&$passed_config, &$passed_name) {
    $passed_config = $backend
      ->getConfiguration();
    $passed_name = $backend
      ->getServer()
      ->label();
  };
  $this
    ->setMethodOverride('backend', 'postInsert', $override);
  $this->server
    ->save();
  $this
    ->assertEquals([
    'test' => 'foobar',
  ], $passed_config);
  $this
    ->assertEquals('Overridden server', $passed_name);
  $this
    ->assertEquals('Test server', $this->server
    ->label());
  $this
    ->assertTrue($this->server
    ->status());

  // Save the index.
  $this->index
    ->save();

  // Verify that on load, the overrides are correctly applied.
  $server = Server::load($this->server
    ->id());
  $this
    ->assertEquals('Overridden server', $server
    ->label());
  $this
    ->assertTrue($server
    ->status());
  $this
    ->assertEquals('does not exist', $this->server
    ->getBackendId());

  // Verify that in preUpdate() the backend overrides are already applied.
  $this
    ->setMethodOverride('backend', 'preUpdate', $override);
  $this->server
    ->save();
  $this
    ->assertEquals([
    'test' => 'foobar',
  ], $passed_config);

  // Verify that overriding "status" prevents the server's indexes from being
  // disabled when attempting to disable the server.
  $GLOBALS['config']['search_api.server.test_server']['status'] = TRUE;
  \Drupal::configFactory()
    ->clearStaticCache();

  /** @var \Drupal\search_api\Entity\SearchApiConfigEntityStorage $server_storage */
  $server_storage = \Drupal::entityTypeManager()
    ->getStorage('search_api_server');
  $server = $server_storage
    ->loadOverrideFree($server
    ->id());
  $server
    ->disable()
    ->save();
  \Drupal::configFactory()
    ->clearStaticCache();
  $index = Index::load($this->index
    ->id());
  $this
    ->assertTrue($index
    ->status());
  $server = Server::load($this->server
    ->id());
  $this
    ->assertTrue($server
    ->status());

  // Verify that overrides are not present when loading the server
  // override-free.
  $server = $server_storage
    ->loadOverrideFree($server
    ->id());
  $this
    ->assertEquals('Test server', $server
    ->label());
}