You are here

public function IntegrationTest::testSavedConfigs in Search API sorts 8

Tests that only enabled configs are saved.

File

tests/src/Functional/IntegrationTest.php, line 117

Class

IntegrationTest
Tests the default functionality of Search API sorts.

Namespace

Drupal\Tests\search_api_sorts\Functional

Code

public function testSavedConfigs() {
  $this
    ->drupalLogin($this->adminUser);

  // Add sorting on ID, Authored on and Type.
  $this
    ->drupalGet('admin/config/search/search-api/index/' . $this->indexId . '/sorts/' . $this->escapedDisplayId);
  $edit = [
    'sorts[id][status]' => TRUE,
    'sorts[created][status]' => TRUE,
    'sorts[type][status]' => TRUE,
    'default_sort' => 'id',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save settings');
  $configs_to_be_saved = [
    'id',
    'created',
    'type',
  ];
  $configs_not_to_be_saved = [
    'search_api_relevance',
    'keywords',
    'category',
    'width',
  ];

  // Check if the default_sort radio button is checked.
  $page = $this
    ->getSession()
    ->getPage();
  $id_default_sort_checkbox = $page
    ->find('css', '#edit-sorts .form-item-default-sort input[value="id"]');
  $this
    ->assertEquals(TRUE, $id_default_sort_checkbox
    ->isChecked());

  // Assert that only enabled sorts are saved in the database.
  foreach ($configs_to_be_saved as $config_id) {
    $this
      ->assertNotEmpty($this->container
      ->get('entity_type.manager')
      ->getStorage('search_api_sorts_field')
      ->load($this->escapedDisplayId . '_' . $config_id), t("Config @config_name was not saved as expected", [
      '@config_name' => $config_id,
    ]));
  }
  foreach ($configs_not_to_be_saved as $config_id) {
    $this
      ->assertEmpty($this->container
      ->get('entity_type.manager')
      ->getStorage('search_api_sorts_field')
      ->load($this->escapedDisplayId . '_' . $config_id), t("Config @config_name that should not have been saved was saved unexpectedly", [
      '@config_name' => $config_id,
    ]));
  }
}