You are here

class BackendTest in Search API 8

Tests index and search capabilities using the Database search backend.

@group search_api

Hierarchy

Expanded class hierarchy of BackendTest

See also

\Drupal\search_api_db\Plugin\search_api\backend\Database

File

modules/search_api_db/tests/src/Kernel/BackendTest.php, line 31

Namespace

Drupal\Tests\search_api_db\Kernel
View source
class BackendTest extends BackendTestBase {
  use DatabaseTestsTrait;

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    'search_api_db',
    'search_api_test_db',
  ];

  /**
   * {@inheritdoc}
   */
  protected $serverId = 'database_search_server';

  /**
   * {@inheritdoc}
   */
  protected $indexId = 'database_search_index';

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

    // Create a dummy table that will cause a naming conflict with the backend's
    // default table names, thus testing whether it correctly reacts to such
    // conflicts.
    \Drupal::database()
      ->schema()
      ->createTable('search_api_db_database_search_index', [
      'fields' => [
        'id' => [
          'type' => 'int',
        ],
      ],
    ]);
    $this
      ->installConfig([
      'search_api_test_db',
    ]);

    // Add additional fields to the search index that have the same ID as
    // column names used by this backend, to see whether this leads to any
    // conflicts.
    $index = $this
      ->getIndex();
    $fields_helper = \Drupal::getContainer()
      ->get('search_api.fields_helper');
    $column_names = [
      'item_id',
      'field_name',
      'word',
      'score',
      'value',
    ];
    $field_info = [
      'datasource_id' => 'entity:entity_test_mulrev_changed',
      'property_path' => 'type',
      'type' => 'string',
    ];
    foreach ($column_names as $column_name) {
      $field_info['label'] = "Test field {$column_name}";
      $field = $fields_helper
        ->createField($index, $column_name, $field_info);
      $index
        ->addField($field);
    }
    $index
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  protected function checkBackendSpecificFeatures() {
    $this
      ->checkMultiValuedInfo();
    $this
      ->searchWithRandom();
    $this
      ->setServerMatchMode();
    $this
      ->searchSuccessPartial();
    $this
      ->setServerMatchMode('prefix');
    $this
      ->searchSuccessStartsWith();
    $this
      ->editServerMinChars();
    $this
      ->searchSuccessMinChars();
    $this
      ->checkUnknownOperator();
    $this
      ->checkDbQueryAlter();
    $this
      ->checkFieldIdChanges();
  }

  /**
   * {@inheritdoc}
   */
  protected function backendSpecificRegressionTests() {
    $this
      ->regressionTest2557291();
    $this
      ->regressionTest2511860();
    $this
      ->regressionTest2846932();
    $this
      ->regressionTest2926733();
    $this
      ->regressionTest2938646();
    $this
      ->regressionTest2925464();
    $this
      ->regressionTest2994022();
    $this
      ->regressionTest2916534();
    $this
      ->regressionTest2873023();
    $this
      ->regressionTest3199355();
  }

  /**
   * Tests that all tables and all columns have been created.
   */
  protected function checkServerBackend() {
    $db_info = $this
      ->getIndexDbInfo();
    $normalized_storage_table = $db_info['index_table'];
    $field_infos = $db_info['field_tables'];
    $expected_fields = [
      'body',
      'category',
      'created',
      'field_name',
      'id',
      'item_id',
      'keywords',
      'name',
      'score',
      'search_api_datasource',
      'search_api_language',
      'type',
      'value',
      'width',
      'word',
    ];
    $actual_fields = array_keys($field_infos);
    sort($actual_fields);
    $this
      ->assertEquals($expected_fields, $actual_fields, 'All expected field tables were created.');
    $this
      ->assertTrue(\Drupal::database()
      ->schema()
      ->tableExists($normalized_storage_table), 'Normalized storage table exists.');
    $this
      ->assertHasPrimaryKey($normalized_storage_table, 'Normalized storage table has a primary key.');
    foreach ($field_infos as $field_id => $field_info) {
      if ($field_id != 'search_api_id') {
        $this
          ->assertTrue(\Drupal::database()
          ->schema()
          ->tableExists($field_info['table']));
      }
      else {
        $this
          ->assertEmpty($field_info['table']);
      }
      $this
        ->assertTrue(\Drupal::database()
        ->schema()
        ->fieldExists($normalized_storage_table, $field_info['column']), new FormattableMarkup('Field column %column exists', [
        '%column' => $field_info['column'],
      ]));
    }
  }

  /**
   * Checks whether changes to the index's fields are picked up by the server.
   */
  protected function updateIndex() {

    /** @var \Drupal\search_api\IndexInterface $index */
    $index = $this
      ->getIndex();

    // Remove a field from the index and check if the change is matched in the
    // server configuration.
    $field = $index
      ->getField('keywords');
    if (!$field) {
      throw new \Exception();
    }
    $index
      ->removeField('keywords');
    $index
      ->save();
    $index_fields = array_keys($index
      ->getFields());

    // Include the three "magic" fields we're indexing with the DB backend.
    $index_fields[] = 'search_api_datasource';
    $index_fields[] = 'search_api_language';
    $db_info = $this
      ->getIndexDbInfo();
    $server_fields = array_keys($db_info['field_tables']);
    sort($index_fields);
    sort($server_fields);
    $this
      ->assertEquals($index_fields, $server_fields);

    // Add the field back for the next assertions.
    $index
      ->addField($field)
      ->save();
  }

  /**
   * Verifies that the generated table names are correct.
   */
  protected function checkTableNames() {
    $this
      ->assertEquals('search_api_db_database_search_index_1', $this
      ->getIndexDbInfo()['index_table']);
    $this
      ->assertEquals('search_api_db_database_search_index_text', $this
      ->getIndexDbInfo()['field_tables']['body']['table']);
  }

  /**
   * Verifies that the stored information about multi-valued fields is correct.
   */
  protected function checkMultiValuedInfo() {
    $db_info = $this
      ->getIndexDbInfo();
    $field_info = $db_info['field_tables'];
    $fields = [
      'name',
      'body',
      'type',
      'keywords',
      'category',
      'width',
      'search_api_datasource',
      'search_api_language',
    ];
    $multi_valued = [
      'name',
      'body',
      'keywords',
    ];
    foreach ($fields as $field_id) {
      $this
        ->assertArrayHasKey($field_id, $field_info, "Field info saved for field {$field_id}.");
      if (in_array($field_id, $multi_valued)) {
        $this
          ->assertFalse(empty($field_info[$field_id]['multi-valued']), "Field {$field_id} is stored as multi-value.");
      }
      else {
        $this
          ->assertTrue(empty($field_info[$field_id]['multi-valued']), "Field {$field_id} is not stored as multi-value.");
      }
    }
  }

  /**
   * Edits the server to sets the match mode.
   *
   * @param string $match_mode
   *   The matching mode to set – "words", "partial" or "prefix".
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  protected function setServerMatchMode($match_mode = 'partial') {
    $server = $this
      ->getServer();
    $backend_config = $server
      ->getBackendConfig();
    $backend_config['matching'] = $match_mode;
    $server
      ->setBackendConfig($backend_config);
    $this
      ->assertTrue((bool) $server
      ->save(), 'The server was successfully edited.');
    $this
      ->resetEntityCache();
  }

  /**
   * Tests whether random searches work.
   */
  protected function searchWithRandom() {

    // Run the query 5 times, using random sorting as the first sort and verify
    // that the results are not always the same.
    $first_result = NULL;
    $second_result = NULL;
    for ($i = 1; $i <= 5; $i++) {
      $results = $this
        ->buildSearch('foo', [], NULL, FALSE)
        ->sort('search_api_random')
        ->sort('id')
        ->execute();
      $result_ids = array_keys($results
        ->getResultItems());
      if ($first_result === NULL) {
        $first_result = $second_result = $result_ids;
      }
      elseif ($result_ids !== $first_result) {
        $second_result = $result_ids;
      }

      // Make sure the search still returned the expected items.
      $this
        ->assertCount(4, $result_ids);
      sort($result_ids);
      $this
        ->assertEquals($this
        ->getItemIds([
        1,
        2,
        4,
        5,
      ]), $result_ids);
    }
    $this
      ->assertNotEquals($first_result, $second_result);
  }

  /**
   * Tests whether partial searches work.
   */
  protected function searchSuccessPartial() {
    $results = $this
      ->buildSearch('foobaz')
      ->range(0, 1)
      ->execute();
    $this
      ->assertResults([
      1,
    ], $results, 'Partial search for »foobaz«');
    $results = $this
      ->buildSearch('foo', [], [], FALSE)
      ->sort('search_api_relevance', QueryInterface::SORT_DESC)
      ->sort('id')
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      4,
      3,
      5,
    ], $results, 'Partial search for »foo«');
    $results = $this
      ->buildSearch('foo tes')
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      3,
      4,
    ], $results, 'Partial search for »foo tes«');
    $results = $this
      ->buildSearch('oob est')
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      3,
    ], $results, 'Partial search for »oob est«');
    $results = $this
      ->buildSearch('foo nonexistent')
      ->execute();
    $this
      ->assertResults([], $results, 'Partial search for »foo nonexistent«');
    $results = $this
      ->buildSearch('bar nonexistent')
      ->execute();
    $this
      ->assertResults([], $results, 'Partial search for »bar nonexistent«');
    $keys = [
      '#conjunction' => 'AND',
      'oob',
      [
        '#conjunction' => 'OR',
        'est',
        'nonexistent',
      ],
    ];
    $results = $this
      ->buildSearch($keys)
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      3,
    ], $results, 'Partial search for complex keys');
    $results = $this
      ->buildSearch('foo', [
      'category,item_category',
    ], [], FALSE)
      ->sort('id', QueryInterface::SORT_DESC)
      ->execute();
    $this
      ->assertResults([
      2,
      1,
    ], $results, 'Partial search for »foo« with additional filter');
    $query = $this
      ->buildSearch();
    $conditions = $query
      ->createConditionGroup('OR');
    $conditions
      ->addCondition('name', 'test');
    $conditions
      ->addCondition('body', 'test');
    $query
      ->addConditionGroup($conditions);
    $results = $query
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      3,
      4,
    ], $results, 'Partial search with multi-field fulltext filter');
  }

  /**
   * Tests whether prefix matching works.
   */
  protected function searchSuccessStartsWith() {
    $results = $this
      ->buildSearch('foobaz')
      ->range(0, 1)
      ->execute();
    $this
      ->assertResults([
      1,
    ], $results, 'Prefix search for »foobaz«');
    $results = $this
      ->buildSearch('foo', [], [], FALSE)
      ->sort('search_api_relevance', QueryInterface::SORT_DESC)
      ->sort('id')
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      4,
      3,
      5,
    ], $results, 'Prefix search for »foo«');
    $results = $this
      ->buildSearch('foo tes')
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      3,
      4,
    ], $results, 'Prefix search for »foo tes«');
    $results = $this
      ->buildSearch('oob est')
      ->execute();
    $this
      ->assertResults([], $results, 'Prefix search for »oob est«');
    $results = $this
      ->buildSearch('foo nonexistent')
      ->execute();
    $this
      ->assertResults([], $results, 'Prefix search for »foo nonexistent«');
    $results = $this
      ->buildSearch('bar nonexistent')
      ->execute();
    $this
      ->assertResults([], $results, 'Prefix search for »bar nonexistent«');
    $keys = [
      '#conjunction' => 'AND',
      'foob',
      [
        '#conjunction' => 'OR',
        'tes',
        'nonexistent',
      ],
    ];
    $results = $this
      ->buildSearch($keys)
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      3,
    ], $results, 'Prefix search for complex keys');
    $results = $this
      ->buildSearch('foo', [
      'category,item_category',
    ], [], FALSE)
      ->sort('id', QueryInterface::SORT_DESC)
      ->execute();
    $this
      ->assertResults([
      2,
      1,
    ], $results, 'Prefix search for »foo« with additional filter');
    $query = $this
      ->buildSearch();
    $conditions = $query
      ->createConditionGroup('OR');
    $conditions
      ->addCondition('name', 'test');
    $conditions
      ->addCondition('body', 'test');
    $query
      ->addConditionGroup($conditions);
    $results = $query
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      3,
      4,
    ], $results, 'Prefix search with multi-field fulltext filter');
  }

  /**
   * Edits the server to change the "Minimum word length" setting.
   */
  protected function editServerMinChars() {
    $server = $this
      ->getServer();
    $backend_config = $server
      ->getBackendConfig();
    $backend_config['min_chars'] = 4;
    $backend_config['matching'] = 'words';
    $server
      ->setBackendConfig($backend_config);
    $success = (bool) $server
      ->save();
    $this
      ->assertTrue($success, 'The server was successfully edited.');
    $this
      ->clearIndex();
    $this
      ->indexItems($this->indexId);
    $this
      ->resetEntityCache();
  }

  /**
   * Tests the results of some test searches with minimum word length of 4.
   */
  protected function searchSuccessMinChars() {
    $results = $this
      ->getIndex()
      ->query()
      ->keys('test')
      ->range(1, 2)
      ->execute();
    $this
      ->assertEquals(4, $results
      ->getResultCount(), 'Search for »test« returned correct number of results.');
    $this
      ->assertEquals($this
      ->getItemIds([
      4,
      1,
    ]), array_keys($results
      ->getResultItems()), 'Search for »test« returned correct result.');
    $this
      ->assertEmpty($results
      ->getIgnoredSearchKeys());
    $this
      ->assertEmpty($results
      ->getWarnings());
    $query = $this
      ->buildSearch();
    $conditions = $query
      ->createConditionGroup('OR');
    $conditions
      ->addCondition('name', 'test');
    $conditions
      ->addCondition('body', 'test');
    $query
      ->addConditionGroup($conditions);
    $results = $query
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      3,
      4,
    ], $results, 'Search with multi-field fulltext filter');
    $results = $this
      ->buildSearch(NULL, [
      'body,test foobar',
    ])
      ->execute();
    $this
      ->assertResults([
      3,
    ], $results, 'Search with multi-term fulltext filter');
    $results = $this
      ->getIndex()
      ->query()
      ->keys('test foo')
      ->execute();
    $this
      ->assertResults([
      2,
      4,
      1,
      3,
    ], $results, 'Search for »test foo«', [
      'foo',
    ]);
    $results = $this
      ->buildSearch('foo', [
      'type,item',
    ])
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      3,
    ], $results, 'Search for »foo«', [
      'foo',
    ], [
      'No valid search keys were present in the query.',
    ]);
    $keys = [
      '#conjunction' => 'AND',
      'test',
      [
        '#conjunction' => 'OR',
        'baz',
        'foobar',
      ],
      [
        '#conjunction' => 'OR',
        '#negation' => TRUE,
        'bar',
        'fooblob',
      ],
    ];
    $results = $this
      ->buildSearch($keys)
      ->execute();
    $this
      ->assertResults([
      3,
    ], $results, 'Complex search 1', [
      'baz',
      'bar',
    ]);
    $keys = [
      '#conjunction' => 'AND',
      'test',
      [
        '#conjunction' => 'OR',
        'baz',
        'foobar',
      ],
      [
        '#conjunction' => 'OR',
        '#negation' => TRUE,
        'bar',
        'fooblob',
      ],
    ];
    $results = $this
      ->buildSearch($keys)
      ->execute();
    $this
      ->assertResults([
      3,
    ], $results, 'Complex search 2', [
      'baz',
      'bar',
    ]);
    $results = $this
      ->buildSearch(NULL, [
      'keywords,orange',
    ])
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      5,
    ], $results, 'Filter query 1 on multi-valued field');
    $conditions = [
      'keywords,orange',
      'keywords,apple',
    ];
    $results = $this
      ->buildSearch(NULL, $conditions)
      ->execute();
    $this
      ->assertResults([
      2,
    ], $results, 'Filter query 2 on multi-valued field');
    $results = $this
      ->buildSearch()
      ->addCondition('keywords', 'orange', '<>')
      ->execute();
    $this
      ->assertResults([
      3,
      4,
    ], $results, 'Negated filter on multi-valued field');
    $results = $this
      ->buildSearch()
      ->addCondition('keywords', NULL)
      ->execute();
    $this
      ->assertResults([
      3,
    ], $results, 'Query with NULL filter');
    $results = $this
      ->buildSearch()
      ->addCondition('keywords', NULL, '<>')
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      4,
      5,
    ], $results, 'Query with NOT NULL filter');
  }

  /**
   * Checks that an unknown operator throws an exception.
   */
  protected function checkUnknownOperator() {
    try {
      $this
        ->buildSearch()
        ->addCondition('id', 1, '!=')
        ->execute();
      $this
        ->fail('Unknown operator "!=" did not throw an exception.');
    } catch (SearchApiException $e) {
      $this
        ->assertTrue(TRUE, 'Unknown operator "!=" threw an exception.');
    }
  }

  /**
   * Checks whether the module's specific alter hook and event work correctly.
   */
  protected function checkDbQueryAlter() {
    $query = $this
      ->buildSearch();
    $query
      ->setOption('search_api_test_db_search_api_db_query_alter', TRUE);
    $results = $query
      ->execute();
    $this
      ->assertResults([], $results, 'Query triggering custom alter hook');
    $query = $this
      ->buildSearch();
    $query
      ->setOption('search_api_test_db.event.query_pre_execute.1', TRUE);
    $results = $query
      ->execute();
    $this
      ->assertResults([], $results, 'Query triggering custom alter event 1');
    $query = $this
      ->buildSearch();
    $query
      ->setOption('search_api_test_db.event.query_pre_execute.2', TRUE);
    $results = $query
      ->execute();
    $this
      ->assertResults([], $results, 'Query triggering custom alter event 2');
  }

  /**
   * Checks that field ID changes are treated correctly (without re-indexing).
   */
  protected function checkFieldIdChanges() {
    $this
      ->getIndex()
      ->renameField('type', 'foobar')
      ->save();
    $results = $this
      ->buildSearch(NULL, [
      'foobar,item',
    ])
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      3,
    ], $results, 'Search after renaming a field.');
    $this
      ->getIndex()
      ->renameField('foobar', 'type')
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  protected function checkSecondServer() {

    /** @var \Drupal\search_api\ServerInterface $second_server */
    $second_server = Server::create([
      'id' => 'test2',
      'backend' => 'search_api_db',
      'backend_config' => [
        'database' => 'default:default',
      ],
    ]);
    $second_server
      ->save();
    $query = $this
      ->buildSearch();
    try {
      $second_server
        ->search($query);
      $this
        ->fail('Could execute a query for an index on a different server.');
    } catch (SearchApiException $e) {
      $this
        ->assertTrue(TRUE, 'Executing a query for an index on a different server throws an exception.');
    }
    $second_server
      ->delete();
  }

  /**
   * Tests the case-sensitivity of fulltext searches.
   *
   * @see https://www.drupal.org/node/2557291
   */
  protected function regressionTest2557291() {
    $results = $this
      ->buildSearch('case')
      ->execute();
    $this
      ->assertResults([
      1,
    ], $results, 'Search for lowercase "case"');
    $results = $this
      ->buildSearch('Case')
      ->execute();
    $this
      ->assertResults([
      1,
      3,
    ], $results, 'Search for capitalized "Case"');
    $results = $this
      ->buildSearch('CASE')
      ->execute();
    $this
      ->assertResults([], $results, 'Search for non-existent uppercase version of "CASE"');
    $results = $this
      ->buildSearch('föö')
      ->execute();
    $this
      ->assertResults([
      1,
    ], $results, 'Search for keywords with umlauts');
    $results = $this
      ->buildSearch('smile' . json_decode('"\\u1F601"'))
      ->execute();
    $this
      ->assertResults([
      1,
    ], $results, 'Search for keywords with umlauts');
    $results = $this
      ->buildSearch()
      ->addCondition('keywords', 'grape', '<>')
      ->execute();
    $this
      ->assertResults([
      1,
      3,
    ], $results, 'Negated filter on multi-valued field');
  }

  /**
   * Tests searching for multiple two-letter words.
   *
   * @see https://www.drupal.org/node/2511860
   */
  protected function regressionTest2511860() {
    $query = $this
      ->buildSearch();
    $query
      ->addCondition('body', 'ab xy');
    $results = $query
      ->execute();
    $this
      ->assertEquals(5, $results
      ->getResultCount(), 'Fulltext filters on short words do not change the result.');
    $query = $this
      ->buildSearch();
    $query
      ->addCondition('body', 'ab ab');
    $results = $query
      ->execute();
    $this
      ->assertEquals(5, $results
      ->getResultCount(), 'Fulltext filters on duplicate short words do not change the result.');
  }

  /**
   * Tests changing a field boost to a floating point value.
   *
   * @see https://www.drupal.org/node/2846932
   */
  protected function regressionTest2846932() {
    $index = $this
      ->getIndex();
    $index
      ->getField('body')
      ->setBoost(0.8);
    $index
      ->save();
  }

  /**
   * Tests indexing of text tokens with leading/trailing whitespace.
   *
   * @see https://www.drupal.org/node/2926733
   */
  protected function regressionTest2926733() {
    $index = $this
      ->getIndex();
    $item_id = $this
      ->getItemIds([
      1,
    ])[0];
    $fields_helper = \Drupal::getContainer()
      ->get('search_api.fields_helper');
    $item = $fields_helper
      ->createItem($index, $item_id);
    $field = clone $index
      ->getField('body');
    $value = new TextValue('test');
    $tokens = [];
    foreach ([
      'test',
      ' test',
      '  test',
      'test  ',
      ' test ',
    ] as $token) {
      $tokens[] = new TextToken($token);
    }
    $value
      ->setTokens($tokens);
    $field
      ->setValues([
      $value,
    ]);
    $item
      ->setFields([
      'body' => $field,
    ]);
    $item
      ->setFieldsExtracted(TRUE);
    $index
      ->getServerInstance()
      ->indexItems($index, [
      $item_id => $item,
    ]);

    // Make sure to re-index the proper version of the item to avoid confusing
    // the other tests.
    list($datasource_id, $raw_id) = Utility::splitCombinedId($item_id);
    $index
      ->trackItemsUpdated($datasource_id, [
      $raw_id,
    ]);
    $this
      ->indexItems($index
      ->id());
  }

  /**
   * Tests indexing of items with boost.
   *
   * @see https://www.drupal.org/node/2938646
   */
  protected function regressionTest2938646() {
    $db_info = $this
      ->getIndexDbInfo();
    $text_table = $db_info['field_tables']['body']['table'];
    $item_id = $this
      ->getItemIds([
      1,
    ])[0];
    $select = \Drupal::database()
      ->select($text_table, 't');
    $select
      ->fields('t', [
      'score',
    ])
      ->condition('item_id', $item_id)
      ->condition('word', 'test');
    $select2 = clone $select;

    // Check old score.
    $old_score = $select
      ->execute()
      ->fetchField();
    $this
      ->assertNotSame(FALSE, $old_score);
    $this
      ->assertGreaterThan(0, $old_score);

    // Re-index item with higher boost.
    $index = $this
      ->getIndex();
    $item = $this->container
      ->get('search_api.fields_helper')
      ->createItem($index, $item_id);
    $item
      ->setBoost(2);
    $indexed_ids = $this
      ->indexItemDirectly($index, $item);
    $this
      ->assertEquals([
      $item_id,
    ], $indexed_ids);

    // Verify the field scores changed accordingly.
    $new_score = $select2
      ->execute()
      ->fetchField();
    $this
      ->assertNotSame(FALSE, $new_score);
    $this
      ->assertEquals(2 * $old_score, $new_score);
  }

  /**
   * Tests changing of field types.
   *
   * @see https://www.drupal.org/node/2925464
   */
  protected function regressionTest2925464() {
    $index = $this
      ->getIndex();
    $index
      ->getField('category')
      ->setType('integer');
    $index
      ->save();
    $index
      ->getField('category')
      ->setType('string');
    $index
      ->save();
    $this
      ->indexItems($index
      ->id());
  }

  /**
   * Tests facets functionality for empty result sets.
   *
   * @see https://www.drupal.org/node/2994022
   */
  protected function regressionTest2994022() {
    $query = $this
      ->buildSearch('nonexistent_search_term');
    $facets['category'] = [
      'field' => 'category',
      'limit' => 0,
      'min_count' => 0,
      'missing' => FALSE,
      'operator' => 'and',
    ];
    $query
      ->setOption('search_api_facets', $facets);
    $results = $query
      ->execute();
    $this
      ->assertResults([], $results, 'Non-existent keyword');
    $expected = [
      [
        'count' => 0,
        'filter' => '"article_category"',
      ],
      [
        'count' => 0,
        'filter' => '"item_category"',
      ],
    ];
    $category_facets = $results
      ->getExtraData('search_api_facets')['category'];
    usort($category_facets, [
      $this,
      'facetCompare',
    ]);
    $this
      ->assertEquals($expected, $category_facets, 'Correct facets were returned for minimum count 0');
    $query = $this
      ->buildSearch('nonexistent_search_term');
    $conditions = $query
      ->createConditionGroup('AND', [
      'facet:category',
    ]);
    $conditions
      ->addCondition('category', 'article_category');
    $query
      ->addConditionGroup($conditions);
    $facets['category'] = [
      'field' => 'category',
      'limit' => 0,
      'min_count' => 0,
      'missing' => FALSE,
      'operator' => 'and',
    ];
    $query
      ->setOption('search_api_facets', $facets);
    $results = $query
      ->execute();
    $this
      ->assertResults([], $results, 'Non-existent keyword with filter');
    $expected = [
      [
        'count' => 0,
        'filter' => '"article_category"',
      ],
      [
        'count' => 0,
        'filter' => '"item_category"',
      ],
    ];
    $category_facets = $results
      ->getExtraData('search_api_facets')['category'];
    usort($category_facets, [
      $this,
      'facetCompare',
    ]);
    $this
      ->assertEquals($expected, $category_facets, 'Correct facets were returned for minimum count 0');
  }

  /**
   * Tests edge cases for partial matching.
   *
   * @see https://www.drupal.org/node/2916534
   */
  protected function regressionTest2916534() {
    $old = $this
      ->getServer()
      ->getBackendConfig()['matching'];
    $this
      ->setServerMatchMode();
    $entity_id = count($this->entities) + 1;
    $entity = $this
      ->addTestEntity($entity_id, [
      'name' => 'foo foobar foobar',
      'type' => 'article',
    ]);
    $this
      ->indexItems($this->indexId);
    $results = $this
      ->buildSearch('foo', [], [
      'name',
    ])
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      4,
      $entity_id,
    ], $results, 'Partial search for »foo«');
    $entity
      ->delete();
    $this
      ->setServerMatchMode($old);
  }

  /**
   * Tests whether keywords with special characters work correctly.
   *
   * @see https://www.drupal.org/node/2873023
   */
  protected function regressionTest2873023() {
    $keyword = 'regression@test@2873023';
    $entity_id = count($this->entities) + 1;
    $entity = $this
      ->addTestEntity($entity_id, [
      'name' => $keyword,
      'type' => 'article',
    ]);
    $index = $this
      ->getIndex();
    $this
      ->assertFalse($index
      ->isValidProcessor('tokenizer'));
    $this
      ->indexItems($this->indexId);
    $results = $this
      ->buildSearch($keyword, [], [
      'name',
    ])
      ->execute();
    $this
      ->assertResults([
      $entity_id,
    ], $results, 'Keywords with special characters (Tokenizer disabled)');
    $processor = \Drupal::getContainer()
      ->get('search_api.plugin_helper')
      ->createProcessorPlugin($index, 'tokenizer');
    $index
      ->addProcessor($processor);
    $index
      ->save();
    $this
      ->assertTrue($index
      ->isValidProcessor('tokenizer'));
    $this
      ->indexItems($this->indexId);
    $results = $this
      ->buildSearch($keyword, [], [
      'name',
    ])
      ->execute();
    $this
      ->assertResults([
      $entity_id,
    ], $results, 'Keywords with special characters (Tokenizer enabled)');
    $index
      ->getProcessor('tokenizer')
      ->setConfiguration([
      'spaces' => '\\s',
    ]);
    $index
      ->save();
    $this
      ->indexItems($this->indexId);
    $results = $this
      ->buildSearch($keyword, [], [
      'name',
    ])
      ->execute();
    $this
      ->assertResults([
      $entity_id,
    ], $results, 'Keywords with special characters (Tokenizer with special config)');
    $index
      ->removeProcessor('tokenizer');
    $index
      ->save();
    $this
      ->assertFalse($index
      ->isValidProcessor('tokenizer'));
    $entity
      ->delete();
    unset($this->entities[$entity_id]);
  }

  /**
   * Tests whether string field values with trailing spaces work correctly.
   *
   * @see https://www.drupal.org/node/3199355
   */
  protected function regressionTest3199355() {

    // Index all items before adding a new one, so we can better predict the
    // expected count.
    $this
      ->indexItems($this->indexId);
    $entity_id = count($this->entities) + 1;
    $entity = $this
      ->addTestEntity($entity_id, [
      'keywords' => [
        'foo',
        'foo ',
        ' foo',
        ' foo ',
      ],
      'type' => 'article',
    ]);
    $count = $this
      ->indexItems($this->indexId);
    $this
      ->assertEquals(1, $count);
    $results = $this
      ->buildSearch()
      ->addCondition('keywords', 'foo ')
      ->execute();
    $this
      ->assertResults([
      $entity_id,
    ], $results, 'String filter with trailing space');
    $entity
      ->delete();
    unset($this->entities[$entity_id]);
  }

  /**
   * {@inheritdoc}
   */
  protected function checkIndexWithoutFields() {
    $index = parent::checkIndexWithoutFields();
    $expected = [
      'search_api_datasource',
      'search_api_language',
    ];
    $db_info = $this
      ->getIndexDbInfo($index
      ->id());
    $info_fields = array_keys($db_info['field_tables']);
    sort($info_fields);
    $this
      ->assertEquals($expected, $info_fields);
    return $index;
  }

  /**
   * {@inheritdoc}
   */
  protected function checkModuleUninstall() {
    $db_info = $this
      ->getIndexDbInfo();
    $normalized_storage_table = $db_info['index_table'];
    $field_tables = $db_info['field_tables'];

    // See whether clearing the server works.
    // Regression test for #2156151.
    $server = $this
      ->getServer();
    $index = $this
      ->getIndex();
    $server
      ->deleteAllIndexItems($index);
    $query = $this
      ->buildSearch();
    $results = $query
      ->execute();
    $this
      ->assertEquals(0, $results
      ->getResultCount(), 'Clearing the server worked correctly.');
    $schema = \Drupal::database()
      ->schema();
    $table_exists = $schema
      ->tableExists($normalized_storage_table);
    $this
      ->assertTrue($table_exists, 'The index tables were left in place.');

    // See whether disabling the index correctly removes all of its tables.
    $index
      ->disable()
      ->save();
    $db_info = $this
      ->getIndexDbInfo();
    $this
      ->assertNull($db_info, 'The index was successfully removed from the server.');
    $table_exists = $schema
      ->tableExists($normalized_storage_table);
    $this
      ->assertFalse($table_exists, 'The index tables were deleted.');
    foreach ($field_tables as $field_table) {
      $table_exists = $schema
        ->tableExists($field_table['table']);
      $this
        ->assertFalse($table_exists, "Field table {$field_table['table']} was successfully deleted.");
    }
    $index
      ->enable()
      ->save();

    // Remove first the index and then the server.
    $index
      ->setServer();
    $index
      ->save();
    $db_info = $this
      ->getIndexDbInfo();
    $this
      ->assertNull($db_info, 'The index was successfully removed from the server.');
    $table_exists = $schema
      ->tableExists($normalized_storage_table);
    $this
      ->assertFalse($table_exists, 'The index tables were deleted.');
    foreach ($field_tables as $field_table) {
      $table_exists = $schema
        ->tableExists($field_table['table']);
      $this
        ->assertFalse($table_exists, "Field table {$field_table['table']} was successfully deleted.");
    }

    // Re-add the index to see if the associated tables are also properly
    // removed when the server is deleted.
    $index
      ->setServer($server);
    $index
      ->save();
    $server
      ->delete();
    $db_info = $this
      ->getIndexDbInfo();
    $this
      ->assertNull($db_info, 'The index was successfully removed from the server.');
    $table_exists = $schema
      ->tableExists($normalized_storage_table);
    $this
      ->assertFalse($table_exists, 'The index tables were deleted.');
    foreach ($field_tables as $field_table) {
      $table_exists = $schema
        ->tableExists($field_table['table']);
      $this
        ->assertFalse($table_exists, "Field table {$field_table['table']} was successfully deleted.");
    }

    // Uninstall the module.
    \Drupal::service('module_installer')
      ->uninstall([
      'search_api_db',
    ], FALSE);
    $this
      ->assertFalse(\Drupal::moduleHandler()
      ->moduleExists('search_api_db'), 'The Database Search module was successfully uninstalled.');
    $tables = $schema
      ->findTables('search_api_db_%');
    $expected = [
      'search_api_db_database_search_index' => 'search_api_db_database_search_index',
    ];
    $this
      ->assertEquals($expected, $tables, 'All the tables of the Database Search module have been removed.');
  }

  /**
   * Retrieves the database information for the test index.
   *
   * @param string|null $index_id
   *   (optional) The ID of the index whose database information should be
   *   retrieved.
   *
   * @return array
   *   The database information stored by the backend for the test index.
   */
  protected function getIndexDbInfo($index_id = NULL) {
    $index_id = $index_id ?: $this->indexId;
    return \Drupal::keyValue(Database::INDEXES_KEY_VALUE_STORE_ID)
      ->get($index_id);
  }

  /**
   * Indexes an item directly.
   *
   * @param \Drupal\search_api\IndexInterface $index
   *   The search index to index the item on.
   * @param \Drupal\search_api\Item\ItemInterface $item
   *   The item.
   *
   * @return string[]
   *   The successfully indexed IDs.
   *
   * @throws \Drupal\search_api\SearchApiException
   *   Thrown if indexing failed.
   */
  protected function indexItemDirectly(IndexInterface $index, ItemInterface $item) {
    $items = [
      $item
        ->getId() => $item,
    ];

    // Minimalistic version of code copied from
    // \Drupal\search_api\Entity\Index::indexSpecificItems().
    $index
      ->alterIndexedItems($items);
    \Drupal::moduleHandler()
      ->alter('search_api_index_items', $index, $items);
    $event = new IndexingItemsEvent($index, $items);
    \Drupal::getContainer()
      ->get('event_dispatcher')
      ->dispatch(SearchApiEvents::INDEXING_ITEMS, $event);
    foreach ($items as $item) {

      // This will cache the extracted fields so processors, etc., can retrieve
      // them directly.
      $item
        ->getFields();
    }
    $index
      ->preprocessIndexItems($items);
    $indexed_ids = [];
    if ($items) {
      $indexed_ids = $index
        ->getServerInstance()
        ->indexItems($index, $items);
    }
    return $indexed_ids;
  }

  /**
   * Tests whether a server on a non-default database is handled correctly.
   */
  public function testNonDefaultDatabase() {

    // Clone the primary credentials to a replica connection.
    // Note this will result in two independent connection objects that happen
    // to point to the same place.
    // @see \Drupal\KernelTests\Core\Database\ConnectionTest::testConnectionRouting()
    $connection_info = CoreDatabase::getConnectionInfo('default');
    CoreDatabase::addConnectionInfo('default', 'replica', $connection_info['default']);
    $db1 = CoreDatabase::getConnection('default', 'default');
    $db2 = CoreDatabase::getConnection('replica', 'default');

    // Safety checks copied from the Core test, if these fail something is wrong
    // with Core.
    $this
      ->assertNotNull($db1, 'default connection is a real connection object.');
    $this
      ->assertNotNull($db2, 'replica connection is a real connection object.');
    $this
      ->assertNotSame($db1, $db2, 'Each target refers to a different connection.');

    // Create backends based on each of the two targets and verify they use the
    // right connections.
    $config = [
      'database' => 'default:default',
    ];
    $backend1 = Database::create($this->container, $config, '', []);
    $config['database'] = 'default:replica';
    $backend2 = Database::create($this->container, $config, '', []);
    $this
      ->assertSame($db1, $backend1
      ->getDatabase());
    $this
      ->assertSame($db2, $backend2
      ->getDatabase());

    // Make sure they also use different DBMS compatibility handlers, which also
    // use the correct database connections.
    $dbms_comp1 = $backend1
      ->getDbmsCompatibilityHandler();
    $dbms_comp2 = $backend2
      ->getDbmsCompatibilityHandler();
    $this
      ->assertNotSame($dbms_comp1, $dbms_comp2);
    $this
      ->assertSame($db1, $dbms_comp1
      ->getDatabase());
    $this
      ->assertSame($db2, $dbms_comp2
      ->getDatabase());

    // Finally, make sure the DBMS compatibility handlers also have the correct
    // classes (meaning we used the correct one and didn't just fall back to the
    // generic database).
    $service = $this->container
      ->get('search_api_db.database_compatibility');
    $database_type = $db1
      ->databaseType();
    $service_id = "{$database_type}.search_api_db.database_compatibility";
    $service2 = $this->container
      ->get($service_id);
    $this
      ->assertSame($service2, $service);
    $class = get_class($service);
    $this
      ->assertNotEquals(GenericDatabase::class, $class);
    $this
      ->assertSame($dbms_comp1, $service);
    $this
      ->assertEquals($class, get_class($dbms_comp2));
  }

  /**
   * Tests whether indexing of dates works correctly.
   */
  public function testDateIndexing() {

    // Load all existing entities.
    $storage = \Drupal::entityTypeManager()
      ->getStorage('entity_test_mulrev_changed');
    $storage
      ->delete($storage
      ->loadMultiple());
    $index = Index::load('database_search_index');
    $index
      ->getField('name')
      ->setType('date');
    $index
      ->save();

    // Simulate date field creation in one timezone and indexing in another.
    date_default_timezone_set('America/Chicago');

    // Test different input values, similar to @dataProvider (but with less
    // overhead).
    $t = 1400000000;
    $date_time_format = DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
    $date_format = DateTimeItemInterface::DATE_STORAGE_FORMAT;
    $test_values = [
      'null' => [
        NULL,
        NULL,
      ],
      'timestamp' => [
        $t,
        $t,
      ],
      'string timestamp' => [
        "{$t}",
        $t,
      ],
      'float timestamp' => [
        $t + 0.12,
        $t,
      ],
      'date string' => [
        gmdate($date_time_format, $t),
        $t,
      ],
      'date string with timezone' => [
        date($date_time_format . 'P', $t),
        $t,
      ],
      'date only' => [
        date($date_format, $t),
        // Date-only fields are stored with the default time (12:00:00).
        strtotime(date($date_format, $t) . 'T12:00:00+00:00'),
      ],
    ];

    // Get storage information for quickly checking the indexed value.
    $db_info = $this
      ->getIndexDbInfo();
    $table = $db_info['index_table'];
    $column = $db_info['field_tables']['name']['column'];
    $sql = "SELECT {$column} FROM {{$table}} WHERE item_id = :id";
    $id = 0;
    date_default_timezone_set('Asia/Seoul');
    foreach ($test_values as $label => list($field_value, $expected)) {
      $entity = $this
        ->addTestEntity(++$id, [
        'name' => $field_value,
        'type' => 'item',
      ]);
      $item_id = $this
        ->getItemIds([
        $id,
      ])[0];
      $index
        ->indexSpecificItems([
        $item_id => $entity
          ->getTypedData(),
      ]);
      $args[':id'] = $item_id;
      $indexed_value = \Drupal::database()
        ->query($sql, $args)
        ->fetchField();
      if ($expected === NULL) {
        $this
          ->assertSame($expected, $indexed_value, "Indexing of date field with {$label} value.");
      }
      else {
        $this
          ->assertEquals($expected, $indexed_value, "Indexing of date field with {$label} value.");
      }
    }
  }

  /**
   * Tests negated fulltext searches with substring matching.
   *
   * @param string $match_mode
   *   The match mode to use – "partial", "prefix" or "words".
   *
   * @see https://www.drupal.org/project/search_api/issues/2949962
   *
   * @dataProvider regression2949962DataProvider
   */
  public function testRegression2949962($match_mode) {
    $this
      ->insertExampleContent();
    $this
      ->setServerMatchMode($match_mode);
    $this
      ->indexItems($this->indexId);
    $searches = [
      'not this word' => [
        'keys' => [
          '#conjunction' => 'OR',
          '#negation' => TRUE,
          'test',
        ],
        'expected_results' => [
          1,
          3,
          4,
          5,
        ],
      ],
      'none of these words' => [
        'keys' => [
          '#conjunction' => 'OR',
          '#negation' => TRUE,
          'test',
          'foo',
        ],
        'expected_results' => [
          3,
          5,
        ],
      ],
      'not all of these words' => [
        'keys' => [
          '#conjunction' => 'AND',
          '#negation' => TRUE,
          'foo baz',
        ],
        'expected_results' => [
          2,
          3,
          5,
        ],
      ],
      'complex keywords' => [
        'keys' => [
          [
            'foo',
            'bar',
            '#conjunction' => 'AND',
          ],
          [
            'test',
            '#conjunction' => 'OR',
            '#negation' => TRUE,
          ],
          '#conjunction' => 'AND',
        ],
        'expected_results' => [
          1,
        ],
      ],
    ];
    foreach ($searches as $search) {
      $results = $this
        ->buildSearch($search['keys'], [], [
        'name',
      ])
        ->execute();
      $this
        ->assertResults($search['expected_results'], $results);
    }
  }

  /**
   * Provides test data for testRegression2949962().
   *
   * @return array
   *   An associative array of argument arrays for testRegression2949962().
   */
  public function regression2949962DataProvider() {
    return [
      'Match mode "partial"' => [
        'partial',
      ],
      'Match mode "prefix"' => [
        'prefix',
      ],
      'Match mode "words"' => [
        'words',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssertContentTrait::$content protected property The current raw content.
AssertContentTrait::$drupalSettings protected property The drupalSettings value from the current raw $content.
AssertContentTrait::$elements protected property The XML structure parsed from the current raw $content. 1
AssertContentTrait::$plainTextContent protected property The plain-text content of raw $content (text nodes).
AssertContentTrait::assertEscaped protected function Passes if the raw text IS found escaped on the loaded page, fail otherwise.
AssertContentTrait::assertField protected function Asserts that a field exists with the given name or ID.
AssertContentTrait::assertFieldById protected function Asserts that a field exists with the given ID and value.
AssertContentTrait::assertFieldByName protected function Asserts that a field exists with the given name and value.
AssertContentTrait::assertFieldByXPath protected function Asserts that a field exists in the current page by the given XPath.
AssertContentTrait::assertFieldChecked protected function Asserts that a checkbox field in the current page is checked.
AssertContentTrait::assertFieldsByValue protected function Asserts that a field exists in the current page with a given Xpath result.
AssertContentTrait::assertLink protected function Passes if a link with the specified label is found.
AssertContentTrait::assertLinkByHref protected function Passes if a link containing a given href (part) is found.
AssertContentTrait::assertNoDuplicateIds protected function Asserts that each HTML ID is used for just a single element.
AssertContentTrait::assertNoEscaped protected function Passes if the raw text IS NOT found escaped on the loaded page, fail otherwise.
AssertContentTrait::assertNoField protected function Asserts that a field does not exist with the given name or ID.
AssertContentTrait::assertNoFieldById protected function Asserts that a field does not exist with the given ID and value.
AssertContentTrait::assertNoFieldByName protected function Asserts that a field does not exist with the given name and value.
AssertContentTrait::assertNoFieldByXPath protected function Asserts that a field does not exist or its value does not match, by XPath.
AssertContentTrait::assertNoFieldChecked protected function Asserts that a checkbox field in the current page is not checked.
AssertContentTrait::assertNoLink protected function Passes if a link with the specified label is not found.
AssertContentTrait::assertNoLinkByHref protected function Passes if a link containing a given href (part) is not found.
AssertContentTrait::assertNoLinkByHrefInMainRegion protected function Passes if a link containing a given href is not found in the main region.
AssertContentTrait::assertNoOption protected function Asserts that a select option in the current page does not exist.
AssertContentTrait::assertNoOptionSelected protected function Asserts that a select option in the current page is not checked.
AssertContentTrait::assertNoPattern protected function Triggers a pass if the perl regex pattern is not found in raw content.
AssertContentTrait::assertNoRaw protected function Passes if the raw text is NOT found on the loaded page, fail otherwise.
AssertContentTrait::assertNoText protected function Passes if the page (with HTML stripped) does not contains the text.
AssertContentTrait::assertNoTitle protected function Pass if the page title is not the given string.
AssertContentTrait::assertNoUniqueText protected function Passes if the text is found MORE THAN ONCE on the text version of the page.
AssertContentTrait::assertOption protected function Asserts that a select option in the current page exists.
AssertContentTrait::assertOptionByText protected function Asserts that a select option with the visible text exists.
AssertContentTrait::assertOptionSelected protected function Asserts that a select option in the current page is checked.
AssertContentTrait::assertOptionSelectedWithDrupalSelector protected function Asserts that a select option in the current page is checked.
AssertContentTrait::assertOptionWithDrupalSelector protected function Asserts that a select option in the current page exists.
AssertContentTrait::assertPattern protected function Triggers a pass if the Perl regex pattern is found in the raw content.
AssertContentTrait::assertRaw protected function Passes if the raw text IS found on the loaded page, fail otherwise.
AssertContentTrait::assertText protected function Passes if the page (with HTML stripped) contains the text.
AssertContentTrait::assertTextHelper protected function Helper for assertText and assertNoText.
AssertContentTrait::assertTextPattern protected function Asserts that a Perl regex pattern is found in the plain-text content.
AssertContentTrait::assertThemeOutput protected function Asserts themed output.
AssertContentTrait::assertTitle protected function Pass if the page title is the given string.
AssertContentTrait::assertUniqueText protected function Passes if the text is found ONLY ONCE on the text version of the page.
AssertContentTrait::assertUniqueTextHelper protected function Helper for assertUniqueText and assertNoUniqueText.
AssertContentTrait::buildXPathQuery protected function Builds an XPath query.
AssertContentTrait::constructFieldXpath protected function Helper: Constructs an XPath for the given set of attributes and value.
AssertContentTrait::cssSelect protected function Searches elements using a CSS selector in the raw content.
AssertContentTrait::getAllOptions protected function Get all option elements, including nested options, in a select.
AssertContentTrait::getDrupalSettings protected function Gets the value of drupalSettings for the currently-loaded page.
AssertContentTrait::getRawContent protected function Gets the current raw content.
AssertContentTrait::getSelectedItem protected function Get the selected value from a select field.
AssertContentTrait::getTextContent protected function Retrieves the plain-text content from the current raw content.
AssertContentTrait::getUrl protected function Get the current URL from the cURL handler. 1
AssertContentTrait::parse protected function Parse content returned from curlExec using DOM and SimpleXML.
AssertContentTrait::removeWhiteSpace protected function Removes all white-space between HTML tags from the raw content.
AssertContentTrait::setDrupalSettings protected function Sets the value of drupalSettings for the currently-loaded page.
AssertContentTrait::setRawContent protected function Sets the raw content (e.g. HTML).
AssertContentTrait::xpath protected function Performs an xpath search on the contents of the internal browser.
AssertHelperTrait::castSafeStrings protected static function Casts MarkupInterface objects into strings.
AssertLegacyTrait::assert protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertTrue() instead.
AssertLegacyTrait::assertEqual protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertEquals() instead.
AssertLegacyTrait::assertIdentical protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertSame() instead.
AssertLegacyTrait::assertIdenticalObject protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertEquals() instead.
AssertLegacyTrait::assertNotEqual protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertNotEquals() instead.
AssertLegacyTrait::assertNotIdentical protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertNotSame() instead.
AssertLegacyTrait::pass protected function Deprecated Scheduled for removal in Drupal 10.0.0. Use self::assertTrue() instead.
AssertLegacyTrait::verbose protected function
BackendTest::$indexId protected property A search index ID. Overrides BackendTestBase::$indexId
BackendTest::$modules public static property Modules to enable. Overrides BackendTestBase::$modules
BackendTest::$serverId protected property A search server ID. Overrides BackendTestBase::$serverId
BackendTest::backendSpecificRegressionTests protected function Runs backend specific regression tests. Overrides BackendTestBase::backendSpecificRegressionTests
BackendTest::checkBackendSpecificFeatures protected function Checks backend specific features. Overrides BackendTestBase::checkBackendSpecificFeatures
BackendTest::checkDbQueryAlter protected function Checks whether the module's specific alter hook and event work correctly.
BackendTest::checkFieldIdChanges protected function Checks that field ID changes are treated correctly (without re-indexing).
BackendTest::checkIndexWithoutFields protected function Checks the correct handling of an index without fields. Overrides BackendTestBase::checkIndexWithoutFields
BackendTest::checkModuleUninstall protected function Tests whether removing the configuration again works as it should. Overrides BackendTestBase::checkModuleUninstall
BackendTest::checkMultiValuedInfo protected function Verifies that the stored information about multi-valued fields is correct.
BackendTest::checkSecondServer protected function Tests that a second server doesn't interfere with the first. Overrides BackendTestBase::checkSecondServer
BackendTest::checkServerBackend protected function Tests that all tables and all columns have been created. Overrides BackendTestBase::checkServerBackend
BackendTest::checkTableNames protected function Verifies that the generated table names are correct.
BackendTest::checkUnknownOperator protected function Checks that an unknown operator throws an exception.
BackendTest::editServerMinChars protected function Edits the server to change the "Minimum word length" setting.
BackendTest::getIndexDbInfo protected function Retrieves the database information for the test index.
BackendTest::indexItemDirectly protected function Indexes an item directly.
BackendTest::regression2949962DataProvider public function Provides test data for testRegression2949962().
BackendTest::regressionTest2511860 protected function Tests searching for multiple two-letter words.
BackendTest::regressionTest2557291 protected function Tests the case-sensitivity of fulltext searches.
BackendTest::regressionTest2846932 protected function Tests changing a field boost to a floating point value.
BackendTest::regressionTest2873023 protected function Tests whether keywords with special characters work correctly.
BackendTest::regressionTest2916534 protected function Tests edge cases for partial matching.
BackendTest::regressionTest2925464 protected function Tests changing of field types.
BackendTest::regressionTest2926733 protected function Tests indexing of text tokens with leading/trailing whitespace.
BackendTest::regressionTest2938646 protected function Tests indexing of items with boost.
BackendTest::regressionTest2994022 protected function Tests facets functionality for empty result sets.
BackendTest::regressionTest3199355 protected function Tests whether string field values with trailing spaces work correctly.
BackendTest::searchSuccessMinChars protected function Tests the results of some test searches with minimum word length of 4.
BackendTest::searchSuccessPartial protected function Tests whether partial searches work.
BackendTest::searchSuccessStartsWith protected function Tests whether prefix matching works.
BackendTest::searchWithRandom protected function Tests whether random searches work.
BackendTest::setServerMatchMode protected function Edits the server to sets the match mode.
BackendTest::setUp public function Overrides BackendTestBase::setUp
BackendTest::testDateIndexing public function Tests whether indexing of dates works correctly.
BackendTest::testNonDefaultDatabase public function Tests whether a server on a non-default database is handled correctly.
BackendTest::testRegression2949962 public function Tests negated fulltext searches with substring matching.
BackendTest::updateIndex protected function Checks whether changes to the index's fields are picked up by the server. Overrides BackendTestBase::updateIndex
BackendTestBase::addField protected function Adds a field to a search index.
BackendTestBase::assertResults protected function Asserts that the given result set complies with expectations.
BackendTestBase::buildSearch protected function Builds a search query for testing purposes.
BackendTestBase::checkDefaultIndex protected function Tests the index that was installed through default configuration files.
BackendTestBase::checkDefaultServer protected function Tests the server that was installed through default configuration files.
BackendTestBase::checkFacets protected function Tests whether facets work correctly.
BackendTestBase::clearIndex protected function Clears the test index.
BackendTestBase::disableHtmlFilter protected function Disables the "HTML Filter" processor for the index.
BackendTestBase::enableHtmlFilter protected function Enables the "HTML Filter" processor for the index.
BackendTestBase::facetCompare protected function Compares two facet filters to determine their order.
BackendTestBase::getIndex protected function Retrieves the search index used by this test.
BackendTestBase::getServer protected function Retrieves the search server used by this test.
BackendTestBase::regressionTest1403916 protected function Regression tests for multi word search results sets and wrong facet counts.
BackendTestBase::regressionTest1658964 protected function Regression tests for facets with counts of 0.
BackendTestBase::regressionTest1863672 protected function Regression tests for same content multiple times in the search result.
BackendTestBase::regressionTest1916474 protected function Regression tests for correctly indexing multiple float/decimal fields.
BackendTestBase::regressionTest2007872 protected function Regression tests for missing results when using OR filters.
BackendTestBase::regressionTest2040543 protected function Regression tests for (none) facet shown when feature is set to "no".
BackendTestBase::regressionTest2111753 protected function Regression tests for searching for multiple words using "OR" condition.
BackendTestBase::regressionTest2127001 protected function Regression tests for non-working operator "contains none of these words".
BackendTestBase::regressionTest2136409 protected function Regression tests for handling of NULL filters.
BackendTestBase::regressionTest2284199 protected function Regression tests for problems with taxonomy term parent.
BackendTestBase::regressionTest2469547 protected function Regression tests for facets on fulltext fields.
BackendTestBase::regressionTest2471509 protected function Regression tests for strings longer than 50 chars.
BackendTestBase::regressionTest2616804 protected function Regression tests for multibyte characters exceeding 50 byte.
BackendTestBase::regressionTest2745655 protected function Tests (NOT) NULL conditions on fulltext fields.
BackendTestBase::regressionTest2767609 protected function Regression test for conditions with empty strings as values.
BackendTestBase::regressionTest2783987 protected function Regression test for facet with "min_count" greater than 1.
BackendTestBase::regressionTest2809753 protected function Regression test for multiple facets.
BackendTestBase::regressionTests protected function Executes regression tests for issues that were already fixed.
BackendTestBase::regressionTests2 protected function Executes regression tests which are unpractical to run in between.
BackendTestBase::resetEntityCache protected function Resets the entity cache for the specified entity.
BackendTestBase::searchNoResults protected function Tests that a search on the index doesn't have any results.
BackendTestBase::searchSuccess protected function Tests whether some test searches have the correct results.
BackendTestBase::testBackend public function Tests various indexing scenarios for the search backend.
ConfigTestTrait::configImporter protected function Returns a ConfigImporter object to import test configuration.
ConfigTestTrait::copyConfig protected function Copies configuration objects from source storage to target storage.
DatabaseTestsTrait::assertHasPrimaryKey protected function Asserts that the given table exists and has a primary key.
DatabaseTestsTrait::assertNotHasPrimaryKey protected function Asserts that the given table exists and does not have a primary key.
ExampleContentTrait::$entities protected property The generated test entities, keyed by ID.
ExampleContentTrait::$ids protected property The Search API item IDs of the generated entities.
ExampleContentTrait::addTestEntity protected function Creates and saves a test entity with the given values.
ExampleContentTrait::getItemIds protected function Returns the item IDs for the given entity IDs. 1
ExampleContentTrait::indexItems protected function Indexes all (unindexed) items on the specified index.
ExampleContentTrait::insertExampleContent protected function Creates several test entities.
ExampleContentTrait::removeTestEntity protected function Deletes the test entity with the given ID.
ExampleContentTrait::setUpExampleStructure protected function Sets up the necessary bundles on the test entity type.
KernelTestBase::$backupGlobals protected property Back up and restore any global variables that may be changed by tests.
KernelTestBase::$backupStaticAttributes protected property Back up and restore static class properties that may be changed by tests.
KernelTestBase::$backupStaticAttributesBlacklist protected property Contains a few static class properties for performance.
KernelTestBase::$classLoader protected property
KernelTestBase::$configImporter protected property @todo Move into Config test base class. 7
KernelTestBase::$configSchemaCheckerExclusions protected static property An array of config object names that are excluded from schema checking.
KernelTestBase::$container protected property
KernelTestBase::$databasePrefix protected property
KernelTestBase::$preserveGlobalState protected property Do not forward any global state from the parent process to the processes that run the actual tests.
KernelTestBase::$root protected property The app root.
KernelTestBase::$runTestInSeparateProcess protected property Kernel tests are run in separate processes because they allow autoloading of code from extensions. Running the test in a separate process isolates this behavior from other tests. Subclasses should not override this property.
KernelTestBase::$siteDirectory protected property
KernelTestBase::$strictConfigSchema protected property Set to TRUE to strict check all configuration saved. 6
KernelTestBase::$vfsRoot protected property The virtual filesystem root directory.
KernelTestBase::assertPostConditions protected function 1
KernelTestBase::bootEnvironment protected function Bootstraps a basic test environment.
KernelTestBase::bootKernel private function Bootstraps a kernel for a test.
KernelTestBase::config protected function Configuration accessor for tests. Returns non-overridden configuration.
KernelTestBase::disableModules protected function Disables modules for this test.
KernelTestBase::enableModules protected function Enables modules for this test.
KernelTestBase::getConfigSchemaExclusions protected function Gets the config schema exclusions for this test.
KernelTestBase::getDatabaseConnectionInfo protected function Returns the Database connection info to be used for this test. 1
KernelTestBase::getDatabasePrefix public function
KernelTestBase::getExtensionsForModules private function Returns Extension objects for $modules to enable.
KernelTestBase::getModulesToEnable private static function Returns the modules to enable for this test.
KernelTestBase::initFileCache protected function Initializes the FileCache component.
KernelTestBase::installConfig protected function Installs default configuration for a given list of modules.
KernelTestBase::installEntitySchema protected function Installs the storage schema for a specific entity type.
KernelTestBase::installSchema protected function Installs database tables from a module schema definition.
KernelTestBase::isTestInIsolation Deprecated protected function Returns whether the current test method is running in a separate process.
KernelTestBase::prepareTemplate protected function
KernelTestBase::register public function Registers test-specific services. Overrides ServiceProviderInterface::register 26
KernelTestBase::render protected function Renders a render array. 1
KernelTestBase::setInstallProfile protected function Sets the install profile and rebuilds the container to update it.
KernelTestBase::setSetting protected function Sets an in-memory Settings variable.
KernelTestBase::setUpBeforeClass public static function 1
KernelTestBase::setUpFilesystem protected function Sets up the filesystem, so things like the file directory. 2
KernelTestBase::stop protected function Stops test execution.
KernelTestBase::tearDown protected function 6
KernelTestBase::tearDownCloseDatabaseConnection public function @after
KernelTestBase::vfsDump protected function Dumps the current state of the virtual filesystem to STDOUT.
KernelTestBase::__get Deprecated public function BC: Automatically resolve former KernelTestBase class properties.
KernelTestBase::__sleep public function Prevents serializing any properties.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RandomGeneratorTrait::$randomGenerator protected property The random generator.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers. 1
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate public function Callback for random string validation.
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TestRequirementsTrait::checkModuleRequirements private function Checks missing module requirements.
TestRequirementsTrait::checkRequirements protected function Check module requirements for the Drupal use case. 1
TestRequirementsTrait::getDrupalRoot protected static function Returns the Drupal root directory.