public function SynonymsSynonymsWebTestCase::testSynonyms in Synonyms 7
Test the functionality of synonyms.
File
- ./
synonyms.test, line 179 - Tests for the Synonyms module.
Class
- SynonymsSynonymsWebTestCase
- Test Synonyms functionality of synonyms module.
Code
public function testSynonyms() {
$name = $this
->randomName();
$synonym = $this
->randomName();
$parent_synonym = $this
->randomName();
// Creating terms for testing synonyms_get_term_synonyms().
$synonym1 = $this
->randomName();
$synonym2 = $this
->randomName();
$no_synonyms_term = (object) array(
'vid' => $this->vocabulary->vid,
'name' => $this
->randomName(),
'description' => $this
->randomName(),
$this->fields['disabled']['field']['field_name'] => array(
LANGUAGE_NONE => array(
array(
'value' => $this
->randomName(),
),
),
),
);
taxonomy_term_save($no_synonyms_term);
$one_synonym_term = (object) array(
'vid' => $this->vocabulary->vid,
'name' => $this
->randomName(),
$this->fields['enabled']['field']['field_name'] => array(
LANGUAGE_NONE => array(
array(
'value' => $synonym1,
),
),
),
$this->fields['disabled']['field']['field_name'] => array(
LANGUAGE_NONE => array(
array(
'value' => $this
->randomName(),
),
),
),
);
taxonomy_term_save($one_synonym_term);
$two_synonyms_term = (object) array(
'vid' => $this->vocabulary->vid,
'name' => $this
->randomName(),
$this->fields['enabled']['field']['field_name'] => array(
LANGUAGE_NONE => array(
array(
'value' => $synonym1,
),
array(
'value' => $synonym2,
),
),
),
$this->fields['disabled']['field']['field_name'] => array(
LANGUAGE_NONE => array(
array(
'value' => $this
->randomName(),
),
),
),
);
taxonomy_term_save($two_synonyms_term);
// Creating an identical parent term in order to test $parent parameter in
// our functions.
$term_parent = (object) array(
'vid' => $this->vocabulary->vid,
'name' => $name,
$this->fields['enabled']['field']['field_name'] => array(
LANGUAGE_NONE => array(
array(
'value' => $synonym,
),
array(
'value' => $parent_synonym,
),
),
),
$this->fields['disabled']['field']['field_name'] => array(
LANGUAGE_NONE => array(
array(
'value' => $this
->randomName(),
),
),
),
);
taxonomy_term_save($term_parent);
$term = (object) array(
'vid' => $this->vocabulary->vid,
'name' => $name,
'parent' => $term_parent->tid,
$this->fields['enabled']['field']['field_name'] => array(
LANGUAGE_NONE => array(
array(
'value' => $synonym,
),
),
),
$this->fields['disabled']['field']['field_name'] => array(
LANGUAGE_NONE => array(
array(
'value' => $this
->randomName(),
),
),
),
);
taxonomy_term_save($term);
// We enable the same provider in another synonym behavior too and test that
// we only get unique synonyms, i.e. synonyms do not get doubled for being
// sourced from the same provider twice (once per each behavior).
$behavior_implementation = $this->behavior_implementation;
$behavior_implementation['behavior'] = 'select';
synonyms_behavior_implementation_save($behavior_implementation);
// Testing the 'synonyms' property of 'taxonomy_term' entity.
$synonyms = entity_metadata_wrapper('taxonomy_term', $no_synonyms_term)->synonyms
->value();
$this
->assertTrue(empty($synonyms), 'Successfully retrieved synonyms_get_sanitized() for a term without synonyms.');
$synonyms = entity_metadata_wrapper('taxonomy_term', $one_synonym_term)->synonyms
->value();
$this
->assertTrue(count($synonyms) == 1 && $synonyms[0] == $synonym1, 'Successfully retrieved synonyms_get_sanitized() for a term with a single synonym.');
$synonyms = entity_metadata_wrapper('taxonomy_term', $two_synonyms_term)->synonyms
->value();
$this
->assertTrue(count($synonyms) == 2 && $synonyms[0] == $synonym1 && $synonyms[1] == $synonym2, 'Successfully retrieved synonyms_get_sanitized() for a term with 2 synonyms.');
synonyms_behavior_implementation_delete($behavior_implementation);
// Testing the function synonyms_get_term_by_synonym().
$tid = synonyms_get_term_by_synonym(drupal_strtoupper($synonym), $this->vocabulary, $term_parent->tid);
$this
->assertEqual($tid, $term->tid, 'Successfully looked up term by its synonym.');
$tid = synonyms_get_term_by_synonym(drupal_strtoupper($name), $this->vocabulary, $term_parent->tid);
$this
->assertEqual($tid, $term->tid, 'Successfully looked up term by its name.');
// Now submitting a non-existing name.
$tid = synonyms_get_term_by_synonym($parent_synonym, $this->vocabulary, $term_parent->tid);
$this
->assertEqual($tid, 0, 'synonyms_get_term_by_synonym() returns 0 if the term is not found (due to $parent parameter).');
$tid = synonyms_get_term_by_synonym($term->{$this->fields['disabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], $this->vocabulary);
$this
->assertEqual($tid, 0, 'synonyms_get_term_by_synonym() returns 0 if the term is not found (due to a field not being engaged in "synonyms" behavior).');
// Testing the function synonyms_add_term_by_synonym().
$tid = synonyms_add_term_by_synonym(drupal_strtolower($name), $this->vocabulary, $term_parent->tid);
$this
->assertEqual($tid, $term->tid, 'Successfully called synonyms_add_term_by_synonym() on an existing title and no new term was created.');
$tid = synonyms_add_term_by_synonym(drupal_strtolower($synonym), $this->vocabulary, $term_parent->tid);
$this
->assertEqual($tid, $term->tid, 'Successfully called synonyms_add_term_by_synonym() on an existing synonym and no new term was created.');
drupal_static_reset();
$tid = synonyms_add_term_by_synonym($parent_synonym, $this->vocabulary, $term_parent->tid);
$new_term = taxonomy_term_load($tid);
$new_term_parents = array_keys(taxonomy_get_parents($new_term->tid));
$this
->assertEqual($parent_synonym, $new_term->name, 'Successfully called synonyms_add_term_by_synonym() on a new title and a new term was created (due to parent restriction).');
$this
->assertNotEqual($new_term->tid, $term_parent->tid, 'Successfully called synonyms_add_term_by_synonym() on a synonym of $parent. New term was created instead of returning $parent\'s tid.');
$this
->assertTrue(in_array($term_parent->tid, $new_term_parents), 'Successfully called synonyms_add_term_by_synonym(). New term is assigned as a child to supplied $parent parameter.');
$tid = synonyms_add_term_by_synonym($term->{$this->fields['disabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], $this->vocabulary);
$new_term = taxonomy_term_load($tid);
$this
->assertNotEqual($new_term->tid, $term->tid, 'Successfully called synonyms_add_term_by_synonym() on a new title and a new term was created (due to a field not being engaged in "synonyms" behavior).');
// Testing the function synonyms_get_entity_by_synonym().
$another_vocabulary = (object) array(
'name' => $this
->randomName(),
'machine_name' => 'another_bundle',
);
taxonomy_vocabulary_save($another_vocabulary);
$entity_id = synonyms_get_entity_by_synonym('taxonomy_term', $this
->randomName());
$this
->assertEqual($entity_id, 0, 'synonyms_get_entity_by_synonym() function returns 0 when fails to look up any entity.');
$entity_id = synonyms_get_entity_by_synonym('taxonomy_term', drupal_strtoupper($term_parent->name));
$this
->assertEqual($entity_id, $term_parent->tid, 'synonyms_get_entity_by_synonym() function returns entity ID when a name of an existing entity is supplised.');
$entity_id = synonyms_get_entity_by_synonym('taxonomy_term', drupal_strtoupper($term_parent->name), $another_vocabulary->machine_name);
$this
->assertEqual($entity_id, 0, 'synonyms_get_entity_by_synonym() returns 0 if an existing entity name is provided, but the search is conducted within another bundle.');
$entity_id = synonyms_get_entity_by_synonym('taxonomy_term', drupal_strtolower($synonym2));
$this
->assertEqual($entity_id, $two_synonyms_term->tid, 'synonyms_get_entity_by_synonym() returns entity ID when a synonym of that entity is supplied.');
$entity_id = synonyms_get_entity_by_synonym('taxonomy_term', drupal_strtolower($parent_synonym), $another_vocabulary->machine_name);
$this
->assertEqual($entity_id, 0, 'synonyms_get_entity_by_synonym() returns 0 if a synonym of an existing entity is supplied, but the search is conducted within another bundle.');
$entity_id = synonyms_get_entity_by_synonym('taxonomy_term', $term_parent->{$this->fields['disabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']);
$this
->assertEqual($entity_id, 0, 'synonyms_get_entity_by_synonym() returns 0 if a non-synonym field value is supplied.');
// Testing the function synonyms_synonyms_find().
// Adding one more behavior implementation and making sure 2 of them work
// as expected.
$behavior_implementation = array(
'entity_type' => $this->behavior_implementation['entity_type'],
'bundle' => $this->behavior_implementation['bundle'],
'provider' => synonyms_provider_field_provider_name($this->fields['disabled']['field']),
'behavior' => $this->behavior_implementation['behavior'],
'settings' => $this->behavior_implementation['settings'],
);
synonyms_behavior_implementation_save($behavior_implementation);
$condition = db_and();
$condition
->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $parent_synonym);
$found_synonyms = synonyms_synonyms_find($condition, $this->behavior_implementation['entity_type'], $this->behavior_implementation['bundle']);
$this
->assertEqual(count($found_synonyms), 1, 'Function synonyms_synonyms_find() returns only 1 found synonym for the case when 2 synonym behaviors are enabled.');
$this
->assertTrue($found_synonyms[0]->synonym == $parent_synonym && ($found_synonyms[0]->entity_id = $term_parent->tid), 'Function synonyms_synonyms_find() returns corret synonym information when 2 synonym behaviors are enabled.');
}