You are here

public function NodeSearchSynonymsWebTestCase::testSearchTermSynonymEntityReference in Synonyms 7

Test searching nodes by a term synonym when referenced by entity reference.

This test pretty much does the same thing as the testSearchTermSynonym() with the only different that the terms are referenced through entity reference field type.

File

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

Class

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

Code

public function testSearchTermSynonymEntityReference() {

  // Attaching entity reference field to the new content type.
  $field = array(
    'type' => 'entityreference',
    'field_name' => 'synonyms_term_enabled',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'settings' => array(
      'target_type' => 'taxonomy_term',
      'handler_settings' => array(
        'target_bundles' => array(
          $this->vocabulary->machine_name,
        ),
      ),
    ),
  );
  $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(
          'target_id' => $this->terms['no_synonyms']->tid,
        ),
        array(
          'target_id' => $this->terms['one_synonym']->tid,
        ),
        array(
          'target_id' => $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.');
      }
    }
  }
}