search_api_meilisearch-1.0.x-dev/tests/src/Kernel/Backend/FilterableAttributesSettingsTest.php

tests/src/Kernel/Backend/FilterableAttributesSettingsTest.php
<?php

namespace Drupal\Tests\search_api_meilisearch\Kernel\Backend;

use Drupal\search_api\Item\Field;
use PHPUnit\Framework\Attributes\Group;

/**
 * Provides tests for filterable attributes settings.
 *
 * @group search_api_meilisearch
 */
#[Group('search_api_meilisearch')]
class FilterableAttributesSettingsTest extends BackendKernelTestBase {

  protected const TEST_INDEX = 'kernel_filterable_attributes_test_index';

  /**
   * Tests filterable attributes settings on index operations.
   */
  public function testFilterableSettingsOnIndex(): void {
    $this->prepareTestIndex(self::TEST_INDEX);

    // Tests that attributes are set after index creation.
    $attributes = $this->apiService->getFilterableAttributes(self::TEST_INDEX);
    $this->assertEquals([
      'body',
      'name',
      'search_api_datasource',
      'search_api_id',
      'search_api_language',
      'weight',
    ], $attributes);

    // Tests that attributes are updated after adding field to index.
    $this->addFieldToIndex();
    $attributes = $this->apiService->getFilterableAttributes(self::TEST_INDEX);
    $this->assertEquals([
      'body',
      'created',
      'name',
      'search_api_datasource',
      'search_api_id',
      'search_api_language',
      'weight',
    ], $attributes);

    // Tests that attributes are updated after renaming a field from index.
    $this->renameFieldFromIndex();
    $attributes = $this->apiService->getFilterableAttributes(self::TEST_INDEX);
    $this->assertEquals([
      'body',
      'created_renamed',
      'name',
      'search_api_datasource',
      'search_api_id',
      'search_api_language',
      'weight',
    ], $attributes);

    // Tests that attributes are updated after removing fields from index.
    $this->removeFieldFromIndex();
    $attributes = $this->apiService->getFilterableAttributes(self::TEST_INDEX);
    $this->assertEquals([
      'body',
      'name',
      'search_api_datasource',
      'search_api_id',
      'search_api_language',
    ], $attributes);
  }

  /**
   * Adds fields to search api index.
   */
  protected function addFieldToIndex(): void {
    $field = new Field($this->index, 'created');
    $field->setType('integer')
      ->setDatasourceId('entity:entity_test')
      ->setPropertyPath('created')
      ->setLabel('Created');
    $this->index
      ->addField($field)
      ->save();
  }

  /**
   * Removes fields from search api index.
   */
  protected function removeFieldFromIndex(): void {
    $this->index
      ->removeField('weight')
      ->removeField('created_renamed')
      ->save();
  }

  /**
   * Renames a filed in search api index.
   */
  protected function renameFieldFromIndex(): void {
    $this->index->renameField('created', 'created_renamed');
    $this->index->save();
  }

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

}

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

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