You are here

public function TaxonomySynonymsBehaviorWebTestCase::testTaxonomyTermReference in Synonyms 7

Test synonyms extraction for 'taxonomy_term_reference' field type.

File

synonyms_provider_field/synonyms_provider_field.test, line 409
Tests for the Synonyms field provider module.

Class

TaxonomySynonymsBehaviorWebTestCase
Test TaxonomySynonymsBehavior class.

Code

public function testTaxonomyTermReference() {

  // Testing synonymsExtract().
  $this
    ->assertSynonymsExtract(array(), array(), 'on empty field.');
  $synonym_term = $this
    ->createSynonymTerm();
  $this
    ->assertSynonymsExtract(array(
    LANGUAGE_NONE => array(
      0 => array(
        'tid' => $synonym_term->tid,
      ),
    ),
  ), array(
    $synonym_term->name,
  ), 'on a field that holds one value.');

  // Testing mergeEntityAsSynonym() method.
  $synonym_term = $this
    ->createSynonymTerm();
  $this
    ->assertMergeEntityAsSynonym(array(), $synonym_term, 'taxonomy_term', array(
    array(
      'tid' => $synonym_term->tid,
    ),
  ), 'on a term from referenced vocabulary.');

  // Testing synonymFind() method.
  $this
    ->assertSynonymsFind(array(), db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $this
    ->randomName()), 'on empty field');
  $meta_data = array();
  $meta_data[] = array(
    'items' => array(),
    'found_synonyms' => array(),
  );
  $this
    ->assertSynonymsFind($meta_data, db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $this
    ->randomName()), 'on a field without values');
  $meta_data = array();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $this
            ->createSynonymTerm()->tid,
        ),
      ),
    ),
    'found_synonyms' => array(),
  );
  $this
    ->assertSynonymsFind($meta_data, db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $this
    ->randomName()), 'on a field with a value but searching for another string');
  $meta_data = array();
  $synonym_term = $this
    ->createSynonymTerm();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $synonym_term->tid,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym_term->name,
    ),
  );
  $this
    ->assertSynonymsFind($meta_data, db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym_term->name), 'on a field with a single value searching for that string');
  $meta_data = array();
  $synonym_term = $this
    ->createSynonymTerm();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $this
            ->createSynonymTerm()->tid,
        ),
        array(
          'tid' => $synonym_term->tid,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym_term->name,
    ),
  );
  $this
    ->assertSynonymsFind($meta_data, db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym_term->name), 'on a field with 2 values searching for one of those 2 values');
  $meta_data = array();
  $synonym_term = $this
    ->createSynonymTerm();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $synonym_term->tid,
        ),
        array(
          'tid' => $this
            ->createSynonymTerm()->tid,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym_term->name,
    ),
  );
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $this
            ->createSynonymTerm()->tid,
        ),
        array(
          'tid' => $this
            ->createSynonymTerm()->tid,
        ),
      ),
    ),
    'found_synonyms' => array(),
  );
  $this
    ->assertSynonymsFind($meta_data, db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym_term->name), 'on 2 fields with 2 values each searching for one of those values');
  $meta_data = array();
  $synonym_term = $this
    ->createSynonymTerm();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $synonym_term->tid,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym_term->name,
    ),
  );
  $this
    ->assertSynonymsFind($meta_data, db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, '%' . db_like(drupal_substr($synonym_term->name, 1, -1)) . '%', 'LIKE'), 'on a field with a value searching for a string LIKE the %value%');
  $meta_data = array();
  $tag = $this
    ->randomName();
  $synonym_term1 = $this
    ->createSynonymTerm($tag . $this
    ->randomName());
  $synonym_term2 = $this
    ->createSynonymTerm($tag . $this
    ->randomName());
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $synonym_term1->tid,
        ),
        array(
          'tid' => $synonym_term2->tid,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym_term1->name,
      $synonym_term2->name,
    ),
  );
  $this
    ->assertSynonymsFind($meta_data, db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, db_like($tag) . '%', 'LIKE'), 'on a field with 2 similar values searching a string like %both values%');
  $meta_data = array();
  $synonym_term1 = $this
    ->createSynonymTerm();
  $synonym_term2 = $this
    ->createSynonymTerm();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $synonym_term1->tid,
        ),
        array(
          'tid' => $synonym_term2->tid,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym_term1->name,
      $synonym_term2->name,
    ),
  );
  $this
    ->assertSynonymsFind($meta_data, db_or()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym_term1->name)
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym_term2->name), 'on a field with 2 values searching for value1 or value2');
  $meta_data = array();
  $synonym_term = $this
    ->createSynonymTerm();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $synonym_term->tid,
        ),
        array(
          'tid' => $this
            ->createSynonymTerm()->tid,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym_term->name,
    ),
  );
  $this
    ->assertSynonymsFind($meta_data, db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym_term->name)
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, '%' . db_like(drupal_substr($synonym_term->name, 1, -1)) . '%', 'LIKE'), 'on a field with 2 values searching for value1 and LIKE value1%');
  $meta_data = array();
  $synonym_term1 = $this
    ->createSynonymTerm();
  $synonym_term2 = $this
    ->createSynonymTerm();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $synonym_term1->tid,
        ),
        array(
          'tid' => $synonym_term2->tid,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym_term1->name,
      $synonym_term2->name,
    ),
  );
  $condition = db_or();
  $condition
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym_term1->name);
  $condition
    ->condition(db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym_term2->name)
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, '%' . db_like(drupal_substr($synonym_term2->name, 1 - 1)) . '%', 'LIKE'));
  $this
    ->assertSynonymsFind($meta_data, $condition, 'on a field with 2 values searching for (value1 or (value2 AND value2%))');
  $meta_data = array();
  $synonym_term1 = $this
    ->createSynonymTerm($this
    ->randomName() . ' ' . $this
    ->randomName() . ' ' . $this
    ->randomName());
  $synonym_term2 = $this
    ->createSynonymTerm(str_replace(' ', '-', $synonym_term1->name));
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $synonym_term1->tid,
        ),
        array(
          'tid' => $synonym_term2->tid,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym_term1->name,
      $synonym_term2->name,
    ),
  );
  $condition = db_and()
    ->where("REPLACE(" . AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER . ", ' ', '-') = :synonym", array(
    ':synonym' => $synonym_term2->name,
  ));
  $this
    ->assertSynonymsFind($meta_data, $condition, "on a field with 2 values, where 2nd value replaces spaces with dashes in the 1st value, searching for REPLACE(column, ' ', '-') = value2");
}