You are here

class FacetsTest in Search API Solr 4.x

Same name in this branch
  1. 4.x tests/src/Functional/FacetsTest.php \Drupal\Tests\search_api_solr\Functional\FacetsTest
  2. 4.x modules/search_api_solr_legacy/tests/src/Functional/FacetsTest.php \Drupal\Tests\search_api_solr_legacy\Functional\FacetsTest
Same name and namespace in other branches
  1. 8.3 tests/src/Functional/FacetsTest.php \Drupal\Tests\search_api_solr\Functional\FacetsTest
  2. 8 tests/src/Functional/FacetsTest.php \Drupal\Tests\search_api_solr\Functional\FacetsTest
  3. 8.2 tests/src/Functional/FacetsTest.php \Drupal\Tests\search_api_solr\Functional\FacetsTest

Tests the facets functionality using the Solr backend.

@group search_api_solr

Hierarchy

  • class \Drupal\Tests\search_api_solr\Functional\FacetsTest extends \Drupal\Tests\search_api\Functional\SearchApiBrowserTestBase uses \Drupal\Tests\facets\Functional\BlockTestTrait, \Drupal\Tests\facets\Functional\ExampleContentTrait, \Drupal\Tests\facets\Functional\TestHelperTrait, SolrCommitTrait

Expanded class hierarchy of FacetsTest

1 file declares its use of FacetsTest
FacetsTest.php in modules/search_api_solr_legacy/tests/src/Functional/FacetsTest.php

File

tests/src/Functional/FacetsTest.php, line 19

Namespace

Drupal\Tests\search_api_solr\Functional
View source
class FacetsTest extends SearchApiBrowserTestBase {
  use SolrCommitTrait;
  use BlockTestTrait;
  use ExampleContentTrait {
    indexItems as doIndexItems;
  }
  use TestHelperTrait;

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'block',
    'views',
    'search_api_solr',
    'search_api_solr_test',
    'search_api_solr_test_facets',
    'facets',
  ];

  /**
   * {@inheritdoc}
   */
  protected function tearDown() : void {
    if ($this->indexId) {
      $index = Index::load($this->indexId);
      $index
        ->clear();
      $this
        ->ensureCommit($index);
    }
    parent::tearDown();
  }

  /**
   * Tests basic facets integration.
   */
  public function testFacets() {
    $this->indexId = 'solr_search_index';
    $view = View::load('search_api_test_view');
    $this
      ->assertEquals('search_api_index_solr_search_index', $view
      ->get('base_table'));

    // Create the users used for the tests.
    $admin_user = $this
      ->drupalCreateUser([
      'administer search_api',
      'administer facets',
      'access administration pages',
      'administer blocks',
    ]);
    $this
      ->drupalLogin($admin_user);

    // Check that the test index is on the admin overview.
    $this
      ->drupalGet('admin/config/search/search-api');
    $this
      ->assertSession()
      ->pageTextContains('Test index');
    $this
      ->setUpExampleStructure();
    $this
      ->insertExampleContent();
    $indexed_items = $this
      ->indexItems($this->indexId);
    $this
      ->assertEquals(5, $indexed_items, 'Five items are indexed.');

    // Create a facet, enable 'show numbers'.
    $this
      ->createFacet('Owl', 'owl');
    $edit = [
      'widget' => 'links',
      'widget_config[show_numbers]' => '1',
    ];
    $this
      ->drupalGet('admin/config/search/facets/owl/edit');
    $this
      ->submitForm($edit, 'Save');

    // Verify that the facet results are correct.
    $this
      ->drupalGet('search-api-test-fulltext');
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $this
      ->assertFacetLabel('item (3)');
    $this
      ->assertFacetLabel('article (2)');
    $this
      ->assertSession()
      ->pageTextContains('Displaying 5 search results');
    $this
      ->clickLinkPartialName('item');
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $this
      ->assertSession()
      ->pageTextContains('Displaying 3 search results');
  }

  /**
   * Indexes all (unindexed) items on the specified index.
   *
   * @return int
   *   The number of successfully indexed items.
   *
   * @throws \Drupal\search_api\SearchApiException
   */
  protected function indexItems($index_id) {
    $index_status = $this
      ->doindexItems($index_id);
    $index = Index::load($this->indexId);
    $this
      ->ensureCommit($index);
    return $index_status;
  }

  /**
   * Follows a link by partial name.
   *
   * If the link is discovered and clicked, the test passes. Fail otherwise.
   *
   * @param string|\Drupal\Component\Render\MarkupInterface $label
   *   Text between the anchor tags, uses starts-with().
   * @param int $index
   *   Link position counting from zero.
   *
   * @return string|bool
   *   Page contents on success, or FALSE on failure.
   *
   * @see ::clickLink()
   */
  protected function clickLinkPartialName($label, $index = 0) {
    return $this
      ->clickLinkHelper($label, $index, '//a[starts-with(normalize-space(), :label)]');
  }

  /**
   * Provides a helper for ::clickLink() and ::clickLinkPartialName().
   *
   * @param string|\Drupal\Component\Render\MarkupInterface $label
   *   Text between the anchor tags, uses starts-with().
   * @param int $index
   *   Link position counting from zero.
   * @param string $pattern
   *   A pattern to use for the XPath.
   *
   * @return bool|string
   *   Page contents on success, or FALSE on failure.
   */
  protected function clickLinkHelper($label, $index, $pattern) {

    // Cast MarkupInterface objects to string.
    $label = (string) $label;
    $url_before = $this
      ->getUrl();
    $urls = $this
      ->xpath($pattern, [
      ':label' => $label,
    ]);
    if (isset($urls[$index])) {

      /** @var \Behat\Mink\Element\NodeElement $url */
      $url = $urls[$index];
      $url_target = $this
        ->getAbsoluteUrl($url
        ->getAttribute('href'));
      $message = new FormattableMarkup('Clicked link %label (@url_target) from @url_before', [
        '%label' => $label,
        '@url_target' => $url_target,
        '@url_before' => $url_before,
      ]);
      $this
        ->assertTrue(TRUE, $message);
      return $this
        ->drupalGet($url_target);
    }
    $this
      ->assertTrue(FALSE, new FormattableMarkup('Link %label does not exist on @url_before', [
      '%label' => $label,
      '@url_before' => $url_before,
    ]));
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FacetsTest::$modules public static property 1
FacetsTest::clickLinkHelper protected function Provides a helper for ::clickLink() and ::clickLinkPartialName().
FacetsTest::clickLinkPartialName protected function Follows a link by partial name.
FacetsTest::indexItems protected function Indexes all (unindexed) items on the specified index.
FacetsTest::tearDown protected function
FacetsTest::testFacets public function Tests basic facets integration.
SolrCommitTrait::ensureCommit protected function Explicitly sends a commit command to a Solr server.