You are here

public function TermSearchSynonymsWebTestCase::testSearchTermSynonym in Synonyms 7

Test searching terms by their synonyms.

File

synonyms_search/synonyms_search.test, line 381
Tests for the Synonyms Search module.

Class

TermSearchSynonymsWebTestCase
Test Synonyms module integration with Drupal search for taxonomy terms.

Code

public function testSearchTermSynonym() {

  // Rebuilding Search index.
  $this
    ->cronRun();
  foreach ($this->terms as $k => $term) {
    $this
      ->assertSearchResults($term->name, array(
      $term,
    ), 'Searching by name of the term ' . $k);
    $items = field_get_items('taxonomy_term', $term, $this->fields['disabled']['field']['field_name']);
    if (is_array($items)) {
      foreach ($items as $delta => $item) {
        $this
          ->assertSearchResults($item['value'], array(), 'Searching by not enabled search integration field value #' . $delta . ' of term ' . $k);
      }
    }
    $items = field_get_items('taxonomy_term', $term, $this->fields['enabled']['field']['field_name']);
    if (is_array($items)) {
      foreach ($items as $delta => $item) {
        $this
          ->assertSearchResults($item['value'], array(
          $term,
        ), 'Searching by synonym #' . $delta . ' of the term ' . $k);
      }
    }
  }

  // Removing a synonym from the term. Then asserting it got re-indexed with
  // new values of synonyms.
  $deleted_synonym = array_pop($this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE]);
  taxonomy_term_save($this->terms['one_synonym']);
  $this
    ->cronRun();
  $this
    ->assertSearchResults($deleted_synonym['value'], array(), 'Searching by recently deleted synonym of a taxonomy term yields no results.');

  // Editing a synonym in a term. Then asserting it got re-indexed with new
  // values of synonyms.
  $ex_synonym = $this->terms['two_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'];
  $this->terms['two_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'] = $this
    ->randomName();
  taxonomy_term_save($this->terms['two_synonyms']);
  $this
    ->cronRun();
  $this
    ->assertSearchResults($ex_synonym, array(), 'Searching by recently changed synonym of a taxonomy term yields no results.');

  // We disable entire field from search integration and make sure for all
  // synonyms search results are empty.
  synonyms_behavior_implementation_delete($this->behavior_implementation);
  $this
    ->cronRun();
  foreach ($this->terms as $k => $term) {
    $items = field_get_items('taxonomy_term', $term, $this->fields['enabled']['field']['field_name']);
    if (is_array($items)) {
      foreach ($items as $synonym) {
        $this
          ->assertSearchResults($synonym['value'], array(), 'Searching by ' . $k . ' term synonym, which field was recently disabled from search behavior yields no results.');
      }
    }
  }
}