You are here

public function IntegrationTest::testFramework in Search API sorts 8

Tests sorting.

File

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

Class

IntegrationTest
Tests the default functionality of Search API sorts.

Namespace

Drupal\Tests\search_api_sorts\Functional

Code

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

  // Add sorting on ID.
  $this
    ->drupalGet('admin/config/search/search-api/index/' . $this->indexId . '/sorts');
  $this
    ->drupalGet('admin/config/search/search-api/index/' . $this->indexId . '/sorts/' . $this->escapedDisplayId);
  $edit = [
    'sorts[id][status]' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save settings');

  // Check for non-existence of the block first.
  $this
    ->drupalGet('search-api-sorts-test');
  $this
    ->assertSession()
    ->linkNotExists('ID');
  $block_settings = [
    'region' => 'footer',
    'id' => 'sorts_id',
  ];
  $this
    ->drupalPlaceBlock('search_api_sorts_block:' . $this->displayId, $block_settings);

  // Make sure the block is available and the ID link is shown, check that the
  // sorting applied is in alphabetical order.
  $this
    ->drupalGet('search-api-sorts-test');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->linkExists('ID');
  $this
    ->assertPositions([
    'default | foo bar baz foobaz föö',
    'default | foo test foobuz',
    'default | foo baz',
    'default | bar baz',
  ]);

  // Click on the link and assert that the url now has changed, also check
  // that the sort order is still the same.
  $this
    ->clickLink('ID');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $url = Url::fromUserInput('/search-api-sorts-test', [
    'query' => [
      'sort' => 'id',
      'order' => 'asc',
    ],
  ]);
  $this
    ->assertSession()
    ->addressEquals($url);
  $this
    ->assertPositions([
    'default | foo bar baz foobaz föö',
    'default | foo test foobuz',
    'default | foo baz',
    'default | bar baz',
  ]);

  // Click on the link again and assert that the url is now changed again and
  // that the sort order now also has changed.
  $this
    ->clickLink('ID');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $url = Url::fromUserInput('/search-api-sorts-test', [
    'query' => [
      'sort' => 'id',
      'order' => 'desc',
    ],
  ]);
  $this
    ->assertSession()
    ->addressEquals($url);
  $this
    ->assertPositions([
    'default | bar baz',
    'default | foo baz',
    'default | foo test foobuz',
    'default | foo bar baz foobaz föö',
  ]);

  // Add sorting on type.
  $this
    ->drupalGet('admin/config/search/search-api/index/' . $this->indexId . '/sorts/' . $this->escapedDisplayId);
  $edit = [
    'sorts[id][status]' => TRUE,
    'sorts[search_api_relevance][status]' => TRUE,
    'sorts[type][status]' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save settings');

  // Make sure the ID and type link are available.
  $this
    ->drupalGet('search-api-sorts-test');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->linkExists('ID');
  $this
    ->assertSession()
    ->linkExists('Type');

  // Remove the type field from the index.
  $this
    ->drupalGet('admin/config/search/search-api/index/' . $this->indexId . '/fields');
  $this
    ->getSession()
    ->getPage()
    ->find('css', '#edit-fields-type-remove')
    ->click();
  $this
    ->drupalPostForm(NULL, [], 'Save changes');

  // The type field was removed from the index. Make sure the type field is
  // also removed from the sorts block.
  $this
    ->drupalGet('search-api-sorts-test');
  $this
    ->assertSession()
    ->linkExists('ID');
  $this
    ->assertSession()
    ->linkNotExists('Type');

  // Make sure that the relevance field is not removed. Since this field is
  // hardcoded it's not present in the index so there should be an extra
  // check that this field is not removed when a search_api_index is updated.
  $this
    ->assertSession()
    ->linkExists('Relevance');

  // Make sure the edit link of the search_api_sorts_field redirects to the
  // manage sorts form.
  $this
    ->drupalGet('admin/config/search/search-api/sorts/' . $this->escapedDisplayId . '_id');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->addressEquals('admin/config/search/search-api/index/' . $this->indexId . '/sorts/' . $this->escapedDisplayId);
}