You are here

public function TextSynonymsBehaviorWebTestCase::testText in Synonyms 7

Test synonyms extraction for 'text' field type.

File

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

Class

TextSynonymsBehaviorWebTestCase
Test TextSynonymsBehavior class.

Code

public function testText() {

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

  // Testing mergeEntityAsSynonym() method.
  $node = (object) array(
    'title' => $this
      ->randomName(),
    'type' => 'page',
  );
  node_save($node);
  $this
    ->assertMergeEntityAsSynonym(array(), $node, 'node', array(
    array(
      'value' => $node->title,
    ),
  ), 'on a node entity.');

  // 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(
          'value' => $this
            ->randomName(),
        ),
      ),
    ),
    'found_synonyms' => array(),
  );
  $this
    ->assertSynonymsFind($meta_data, db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $this
    ->randomName()), 'on a field with a value, but when searching for another string.');
  $meta_data = array();
  $synonym = $this
    ->randomName();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => $synonym,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym,
    ),
  );
  $this
    ->assertSynonymsFind($meta_data, db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym), 'on a field with a single value searching for that string');
  $meta_data = array();
  $synonym = $this
    ->randomName();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => $synonym,
        ),
        array(
          'value' => $this
            ->randomName(),
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym,
    ),
  );
  $this
    ->assertSynonymsFind($meta_data, db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym), 'on a field with 2 values searching for one of those 2 values');
  $meta_data = array();
  $synonym = $this
    ->randomName();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => $synonym,
        ),
        array(
          'value' => $this
            ->randomName(),
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym,
    ),
  );
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => $this
            ->randomName(),
        ),
        array(
          'value' => $this
            ->randomName(),
        ),
      ),
    ),
    'found_synonyms' => array(),
  );
  $this
    ->assertSynonymsFind($meta_data, db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym), 'on 2 fields with 2 values each searching for one of those values');
  $meta_data = array();
  $synonym = $this
    ->randomName();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => $synonym,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym,
    ),
  );
  $this
    ->assertSynonymsFind($meta_data, db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, '%' . db_like(drupal_substr($synonym, 1, -1)) . '%', 'LIKE'), 'on a field with a value searching for a string LIKE the %value%');
  $meta_data = array();
  $tag = $this
    ->randomName();
  $synonym1 = $tag . $this
    ->randomName();
  $synonym2 = $tag . $this
    ->randomName();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => $synonym1,
        ),
        array(
          'value' => $synonym2,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym1,
      $synonym2,
    ),
  );
  $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();
  $synonym1 = $this
    ->randomName();
  $synonym2 = $this
    ->randomName();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => $synonym1,
        ),
        array(
          'value' => $synonym2,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym1,
      $synonym2,
    ),
  );
  $this
    ->assertSynonymsFind($meta_data, db_or()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym1)
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym2), 'on a field with 2 values searching for value1 or value2');
  $meta_data = array();
  $synonym = $this
    ->randomName();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => $synonym,
        ),
        array(
          'value' => $this
            ->randomName(),
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym,
    ),
  );
  $this
    ->assertSynonymsFind($meta_data, db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym)
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, db_like(drupal_substr($synonym, 0, -1)) . '%', 'LIKE'), 'on a field with 2 values searching for value1 and LIKE value1%');
  $meta_data = array();
  $synonym1 = $this
    ->randomName();
  $synonym2 = $this
    ->randomName();
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => $synonym1,
        ),
        array(
          'value' => $synonym2,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym1,
      $synonym2,
    ),
  );
  $condition = db_or();
  $condition
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym1);
  $condition
    ->condition(db_and()
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $synonym2)
    ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, db_like(drupal_substr($synonym2, 0, -1)) . '%', 'LIKE'));
  $this
    ->assertSynonymsFind($meta_data, $condition, 'on a field with 2 values searching for (value1 or (value2 AND value2%))');
  $meta_data = array();
  $synonym1 = $this
    ->randomName() . ' ' . $this
    ->randomName() . ' ' . $this
    ->randomName();
  $synonym2 = str_replace(' ', '-', $synonym1);
  $meta_data[] = array(
    'items' => array(
      LANGUAGE_NONE => array(
        array(
          'value' => $synonym1,
        ),
        array(
          'value' => $synonym2,
        ),
      ),
    ),
    'found_synonyms' => array(
      $synonym1,
      $synonym2,
    ),
  );
  $condition = db_and()
    ->where("REPLACE(" . AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER . ", ' ', '-') = :synonym", array(
    ':synonym' => $synonym2,
  ));
  $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");
}