search_api_meilisearch-1.0.x-dev/tests/src/Kernel/Api/SearchTest.php

tests/src/Kernel/Api/SearchTest.php
<?php

namespace Drupal\Tests\search_api_meilisearch\Kernel\Api;

use Drupal\search_api_meilisearch\Api\MeilisearchApiException;
use Drupal\Tests\search_api_meilisearch\Kernel\MeilisearchKernelTestBase;
use Drupal\Tests\search_api_meilisearch\Traits\Api\DocumentsCreationTrait;
use Drupal\Tests\search_api_meilisearch\Traits\Api\IndexCreationTrait;
use PHPUnit\Framework\Attributes\Group;

/**
 * Provides search tests.
 *
 * @group search_api_meilisearch
 */
#[Group('search_api_meilisearch')]
class SearchTest extends MeilisearchKernelTestBase {

  use IndexCreationTrait;
  use DocumentsCreationTrait;

  protected const TEST_INDEX = 'kernel_search_test_index';

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $documents = [
      [
        'id' => 'entity_node_8_en',
        'title' => 'Die Hard',
        'body' => 'Die Hard tells the story of a New York City detective named John McClane who is separated from his wife. She invites him to a Christmas party at her office.',
        'status' => FALSE,
      ],
      [
        'id' => 'entity_node_9_en',
        'title' => 'The Matrix',
        'body' => 'Thomas Anderson, a computer programmer, is led to fight an underground war against powerful computers who have constructed his entire reality with a system called the Matrix.',
        'status' => TRUE,
      ],
      [
        'id' => 'entity_node_10_en',
        'title' => 'Clerks',
        'body' => 'Two store clerks, Dante and Randal, trouble customers, discuss movies, laze around and play hockey on the store roof all the time.',
        'status' => TRUE,
      ],
    ];

    $this->createIndex(self::TEST_INDEX, $this->apiService);
    $this->createDocuments(self::TEST_INDEX, $documents, $this->apiService);
  }

  /**
   * Tests searching on an index without filters.
   */
  public function testSearchWithoutFilters(): void {
    $results = $this->apiService->search(self::TEST_INDEX, '');
    $this->assertCount(3, $results);

    $results = $this->apiService->search(self::TEST_INDEX, 'his');
    $this->assertCount(2, $results);
    $this->assertEquals(2, $results->getHitsCount());
    $this->assertEquals([
      [
        'id' => 'entity_node_8_en',
        'title' => 'Die Hard',
        'body' => 'Die Hard tells the story of a New York City detective named John McClane who is separated from his wife. She invites him to a Christmas party at her office.',
        'status' => FALSE,
      ],
      [
        'id' => 'entity_node_9_en',
        'title' => 'The Matrix',
        'body' => 'Thomas Anderson, a computer programmer, is led to fight an underground war against powerful computers who have constructed his entire reality with a system called the Matrix.',
        'status' => TRUE,
      ],
    ], $results->getHits());
  }

  /**
   * Tests searching on an index with filters without configured filters.
   */
  public function testSearchWithFiltersAndNoConfigurableFiltersSet() {
    $this->expectException(MeilisearchApiException::class);
    $this->apiService->search(self::TEST_INDEX, '', ['filter' => 'status = false']);
  }

  /**
   * Tests searching on an index with filters with configured filters.
   */
  public function testSearchWithFiltersAndConfigurableFiltersSet() {
    $task = $this->apiService->setFilterableAttributes(self::TEST_INDEX, ['status']);
    $this->apiService->waitForUpdate($task['taskUid']);
    $results = $this->apiService->search(self::TEST_INDEX, '', ['filter' => 'status = false']);
    $this->assertCount(1, $results);
    $this->assertEquals(1, $results->getHitsCount());
    $this->assertEquals([
      [
        'id' => 'entity_node_8_en',
        'title' => 'Die Hard',
        'body' => 'Die Hard tells the story of a New York City detective named John McClane who is separated from his wife. She invites him to a Christmas party at her office.',
        'status' => FALSE,
      ],
    ], $results->getHits());
  }

  /**
   * Test searching on an index with rankingScore as relevancy returned.
   */
  public function testSearchWithRelevancy() {
    // With empty search query ranking score/relevancy default value 1.0.
    $results = $this->apiService->search(self::TEST_INDEX, '', ['showRankingScore' => TRUE]);
    $this->assertCount(3, $results);
    $this->assertEquals([
      [
        'id' => 'entity_node_8_en',
        'title' => 'Die Hard',
        'body' => 'Die Hard tells the story of a New York City detective named John McClane who is separated from his wife. She invites him to a Christmas party at her office.',
        'status' => FALSE,
        '_rankingScore' => 1.0,
      ],
      [
        'id' => 'entity_node_9_en',
        'title' => 'The Matrix',
        'body' => 'Thomas Anderson, a computer programmer, is led to fight an underground war against powerful computers who have constructed his entire reality with a system called the Matrix.',
        'status' => TRUE,
        '_rankingScore' => 1.0,
      ],
      [
        'id' => 'entity_node_10_en',
        'title' => 'Clerks',
        'body' => 'Two store clerks, Dante and Randal, trouble customers, discuss movies, laze around and play hockey on the store roof all the time.',
        'status' => TRUE,
        '_rankingScore' => 1.0,
      ],
    ], $results->getHits());

    // With a search query relevancy does not default to 1.0.
    $results = $this->apiService->search(self::TEST_INDEX, 'his', ['showRankingScore' => TRUE]);
    $this->assertCount(2, $results);
    $this->assertEquals(2, $results->getHitsCount());
    $this->assertNotEquals([
      [
        'id' => 'entity_node_8_en',
        'title' => 'Die Hard',
        'body' => 'Die Hard tells the story of a New York City detective named John McClane who is separated from his wife. She invites him to a Christmas party at her office.',
        'status' => FALSE,
        '_rankingScore' => 1.0,
      ],
      [
        'id' => 'entity_node_9_en',
        'title' => 'The Matrix',
        'body' => 'Thomas Anderson, a computer programmer, is led to fight an underground war against powerful computers who have constructed his entire reality with a system called the Matrix.',
        'status' => TRUE,
        '_rankingScore' => 1.0,
      ],
    ], $results->getHits());
  }

  /**
   * {@inheritdoc}
   */
  protected function getTestIndexes(): array {
    return [self::TEST_INDEX];
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc