You are here

class HtmlFilterTest in Search API Solr 4.x

Same name in this branch
  1. 4.x tests/src/Kernel/Processor/HtmlFilterTest.php \Drupal\Tests\search_api_solr\Kernel\Processor\HtmlFilterTest
  2. 4.x modules/search_api_solr_legacy/tests/src/Kernel/Processor/HtmlFilterTest.php \Drupal\Tests\search_api_solr_legacy\Kernel\Processor\HtmlFilterTest
Same name and namespace in other branches
  1. 8.3 tests/src/Kernel/Processor/HtmlFilterTest.php \Drupal\Tests\search_api_solr\Kernel\Processor\HtmlFilterTest

Tests usages of Solr payloads.

@group search_api_solr

Hierarchy

Expanded class hierarchy of HtmlFilterTest

See also

\Drupal\search_api\Plugin\search_api\processor\HtmlFilter

1 file declares its use of HtmlFilterTest
HtmlFilterTest.php in modules/search_api_solr_legacy/tests/src/Kernel/Processor/HtmlFilterTest.php

File

tests/src/Kernel/Processor/HtmlFilterTest.php, line 18

Namespace

Drupal\Tests\search_api_solr\Kernel\Processor
View source
class HtmlFilterTest extends ProcessorTestBase {
  use NodeCreationTrait;
  use SolrBackendTrait;

  /**
   * The nodes created for testing.
   *
   * @var \Drupal\node\Entity\Node[]
   */
  protected $nodes;

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'filter',
    'search_api_solr',
    'search_api_solr_test',
  ];

  /**
   * {@inheritdoc}
   */
  public function setUp($processor = NULL) : void {
    parent::setUp('html_filter');
    $this
      ->enableSolrServer();
    $this
      ->installConfig([
      'filter',
    ]);

    // Create a node type for testing.
    $type = NodeType::create([
      'type' => 'page',
      'name' => 'page',
    ]);
    $type
      ->save();
  }

  /**
   * Tests term boosts.
   */
  public function testBoostTerms() {
    $solr_major_version = $this->server
      ->getBackend()
      ->getSolrConnector()
      ->getSolrMajorVersion();
    if (version_compare($solr_major_version, '6', '<')) {
      $this
        ->markTestSkipped('Term boosting requires Solr >= 6.');
      return;
    }
    $this
      ->assertArrayHasKey('html_filter', $this->index
      ->getProcessors(), 'HTML filter processor is added.');
    $this
      ->createNode([
      'type' => 'page',
      'title' => 'Beautiful Page 1',
    ]);
    $this
      ->createNode([
      'type' => 'page',
      'title' => 'Beautiful <b>Page</b> 2',
    ]);
    $this
      ->createNode([
      'type' => 'page',
      'title' => 'Beautiful Page 3',
    ]);
    $this->index
      ->reindex();
    $this
      ->indexItems();
    $query = new Query($this->index);
    $query
      ->sort('search_api_relevance', QueryInterface::SORT_DESC);
    $query
      ->sort('search_api_id');
    $query
      ->getParseMode()
      ->setConjunction('OR');
    $result = $query
      ->execute();
    $this
      ->assertEquals([
      'entity:node/1:en',
      'entity:node/2:en',
      'entity:node/3:en',
    ], array_keys($result
      ->getResultItems()));
    $query = new Query($this->index);
    $query
      ->keys([
      'beautiful',
    ]);
    $query
      ->sort('search_api_relevance', QueryInterface::SORT_DESC);
    $query
      ->sort('search_api_id');
    $query
      ->getParseMode()
      ->setConjunction('OR');
    $result = $query
      ->execute();
    $this
      ->assertEquals([
      'entity:node/1:en',
      'entity:node/2:en',
      'entity:node/3:en',
    ], array_keys($result
      ->getResultItems()));

    // Rerank query based on payloads for HTML tags boosts on match.
    $query = new Query($this->index);
    $query
      ->keys([
      'page',
    ]);
    $query
      ->sort('search_api_relevance', QueryInterface::SORT_DESC);
    $query
      ->sort('search_api_id');
    $query
      ->getParseMode()
      ->setConjunction('OR');
    $result = $query
      ->execute();
    $this
      ->assertEquals([
      'entity:node/2:en',
      'entity:node/1:en',
      'entity:node/3:en',
    ], array_keys($result
      ->getResultItems()));
    $this
      ->createNode([
      'type' => 'page',
      'title' => "d'avion",
    ]);
    $this
      ->createNode([
      'type' => 'page',
      'title' => "<b>d'avion<b>",
    ]);
    $this
      ->createNode([
      'type' => 'page',
      'title' => '๐Ÿ˜€๐Ÿ˜Ž๐Ÿ‘พ',
    ]);
    $this
      ->createNode([
      'type' => 'page',
      'title' => '<b>More| strange " characters ๐Ÿ˜€๐Ÿ˜Ž๐Ÿ‘พ<b>',
    ]);
    $this
      ->createNode([
      'type' => 'page',
      'title' => 'More| strange " characters ๐Ÿ˜€๐Ÿ˜Ž๐Ÿ‘พ',
    ]);
    $this
      ->indexItems();
    $query = new Query($this->index);
    $query
      ->keys([
      "d'avion",
    ]);
    $query
      ->sort('search_api_relevance', QueryInterface::SORT_DESC);
    $query
      ->sort('search_api_id');
    $query
      ->getParseMode()
      ->setConjunction('OR');
    $result = $query
      ->execute();
    $this
      ->assertEquals([
      'entity:node/5:en',
      'entity:node/4:en',
    ], array_keys($result
      ->getResultItems()));
    $query = new Query($this->index);
    $query
      ->keys([
      '๐Ÿ˜€๐Ÿ˜Ž๐Ÿ‘พ',
    ]);
    $query
      ->sort('search_api_relevance', QueryInterface::SORT_DESC);
    $query
      ->sort('search_api_id');
    $query
      ->getParseMode()
      ->setConjunction('OR');
    $result = $query
      ->execute();
    $this
      ->assertEquals([
      'entity:node/7:en',
      'entity:node/6:en',
      'entity:node/8:en',
    ], array_keys($result
      ->getResultItems()));
    $this
      ->createNode([
      'type' => 'page',
      'title' => "<b>VeryLongStingsWithMoreThanOneHoundredCharactersShouldNotNeitherBeIndexedAsTextNorAsBoostedTokenAndShouldNotLeadToExceptionsDuringIndexing<b>",
    ]);
    $this
      ->indexItems();
    $query = new Query($this->index);
    $query
      ->keys([
      'VeryLongStingsWithMoreThanOneHoundredCharactersShouldNotNeitherBeIndexedAsTextNorAsBoostedTokenAndShouldNotLeadToExceptionsDuringIndexing',
    ]);
    $query
      ->sort('search_api_relevance', QueryInterface::SORT_DESC);
    $query
      ->sort('search_api_id');
    $query
      ->getParseMode()
      ->setConjunction('OR');
    $result = $query
      ->execute();
    $this
      ->assertEquals([], array_keys($result
      ->getResultItems()));
    $this
      ->createNode([
      'type' => 'page',
      'title' => "<b>VeryLongStingsWithMoreThanOneHoundredCharactersShouldNotNeitherBeIndexedAsTextNorAsBoostedTokenAndShouldNotLeadToExceptionsDuringIndexing<b>",
    ]);
    $this
      ->indexItems();
    $query = new Query($this->index);
    $query
      ->keys([
      'VeryLongStingsWithMoreThanOneHoundredCharactersShouldNotNeitherBeIndexedAsTextNorAsBoostedTokenAndShouldNotLeadToExceptionsDuringIndexing',
    ]);
    $query
      ->sort('search_api_relevance', QueryInterface::SORT_DESC);
    $query
      ->sort('search_api_id');
    $query
      ->getParseMode()
      ->setConjunction('OR');
    $result = $query
      ->execute();
    $this
      ->assertEquals([], array_keys($result
      ->getResultItems()));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HtmlFilterTest::$modules public static property 1
HtmlFilterTest::$nodes protected property The nodes created for testing.
HtmlFilterTest::setUp public function
HtmlFilterTest::testBoostTerms public function Tests term boosts.
NodeCreationTrait::createNode protected function Creates a node based on default settings.
NodeCreationTrait::getNodeByTitle public function Get a node from the database based on its title.
SolrBackendTrait::enableSolrServer protected function Swap the DB backend for a Solr backend. 3
SolrBackendTrait::indexItems protected function
SolrBackendTrait::tearDown protected function
SolrCommitTrait::ensureCommit protected function Explicitly sends a commit command to a Solr server.