You are here

public function NodeSearchSynonymsWebTestCase::testSearchTermSynonym in Synonyms 7

Test searching nodes by a term synonym.

Since logically term and its synonyms represent the same entity, the idea is that searching by a term synonym should trigger all content referencing that term to be included in search results. Additionally we test that when a synonym is deleted/edited in a term, corresponding content is no longer encountered when searched by ex-synonym.

File

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

Class

NodeSearchSynonymsWebTestCase
Test Synonyms module integration with Drupal search functionality for nodes.

Code

public function testSearchTermSynonym() {

  // Attaching term reference field to the new content type.
  $field = array(
    'type' => 'taxonomy_term_reference',
    'field_name' => 'synonyms_term_enabled',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => $this->vocabulary->machine_name,
          'parent' => 0,
        ),
      ),
    ),
  );
  $field = field_create_field($field);
  $instance = array(
    'field_name' => $field['field_name'],
    'entity_type' => 'node',
    'bundle' => 'synonyms_test_content',
    'label' => 'Synonym Terms',
  );
  field_create_instance($instance);

  // Creating a node, which references all the terms we have.
  $node = (object) array(
    'type' => 'synonyms_test_content',
    'title' => $this
      ->randomName(),
    'synonyms_term_enabled' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $this->terms['no_synonyms']->tid,
        ),
        array(
          'tid' => $this->terms['one_synonym']->tid,
        ),
        array(
          'tid' => $this->terms['two_synonyms']->tid,
        ),
      ),
    ),
  );
  node_save($node);

  // Rebuilding Search index.
  $this
    ->cronRun();
  foreach ($this->terms as $k => $term) {
    $this
      ->assertSearchResults($term->name, array(
      $node,
    ), '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(
          $node,
        ), 'Searching by synonym #' . $delta . ' of the term ' . $k);
      }
    }
  }

  // Removing a synonym from the term. Then asserting node 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 node 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.');
      }
    }
  }
}