12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247 |
- <?php
- /**
- * @file
- * Tests for the Synonyms module.
- */
- /**
- * Base class for all Synonyms web test cases.
- */
- abstract class SynonymsWebTestCase extends DrupalWebTestCase {
- /**
- * Fully loaded user object of an admin user that has required access rights.
- *
- * @var object
- */
- protected $admin;
- /**
- * SetUp method.
- */
- public function setUp() {
- parent::setUp(array('synonyms'));
- $this->admin = $this->drupalCreateUser(array(
- 'administer taxonomy',
- 'administer content types',
- 'bypass node access',
- 'search content',
- ));
- }
- /**
- * Return last inserted term into the specified vocabulary.
- *
- * @param object $vocabulary
- * Fully loaded taxonomy vocabulary object
- *
- * @return object
- * Fully loaded taxonomy term object of the last interted term into
- * the specified vocabulary
- */
- protected function getLastTerm($vocabulary) {
- drupal_static_reset();
- $tree = taxonomy_get_tree($vocabulary->vid);
- $max = 0;
- $term = NULL;
- foreach ($tree as $v) {
- if ($v->tid > $max) {
- $max = $v->tid;
- $term = $v;
- }
- }
- $term = entity_load_unchanged('taxonomy_term', $term->tid);
- return $term;
- }
- }
- /**
- * Test Synonyms functionality of synonyms module.
- */
- class SynonymsSynonymsWebTestCase extends SynonymsWebTestCase {
- protected $vocabularies = array(
- 'enabled' => TRUE,
- 'disabled' => FALSE,
- );
- /**
- * GetInfo method.
- */
- public function getInfo() {
- return array(
- 'name' => 'Taxonomy synonyms',
- 'description' => 'Ensure that the feature "synonyms" works correctly with taxonomy terms.',
- 'group' => 'Synonyms',
- );
- }
- /**
- * SetUp method.
- */
- public function setUp() {
- parent::setUp();
- // Creating vocabularies.
- $this->drupalLogin($this->admin);
- foreach ($this->vocabularies as $k => $v) {
- $name = $this->randomName();
- $this->drupalPost('admin/structure/taxonomy/add', array(
- 'name' => $name,
- 'machine_name' => $k,
- 'description' => $this->randomName(),
- 'synonyms[synonyms][' . SYNONYMS_DEFAULT_FIELD_NAME . ']' => $v,
- ), 'Save');
- $this->vocabularies[$k] = taxonomy_vocabulary_machine_name_load($k);
- }
- // Flushing cache.
- _field_info_collate_fields(TRUE);
- }
- /**
- * Test the disabled taxonomy synonyms feature.
- */
- public function testSynonymsDisabled() {
- $this->drupalGet('admin/structure/taxonomy/disabled/add');
- $this->assertNoFieldById('edit-synonyms-synonyms-und-0-value');
- $this->drupalGet('admin/structure/taxonomy/enabled/add');
- $this->assertFieldById('edit-synonyms-synonyms-und-0-value');
- // Making sure that after disabling "synonyms" the synonyms field
- // is no longer available on taxonomy term add page.
- $this->drupalPost('admin/structure/taxonomy/enabled/edit', array(
- 'synonyms[synonyms][' . SYNONYMS_DEFAULT_FIELD_NAME . ']' => FALSE,
- ), 'Save');
- $this->drupalGet('admin/structure/taxonomy/enabled/add');
- $this->assertNoFieldById('edit-synonyms-synonyms-und-0-value');
- }
- /**
- * Test the functionality of synonyms.
- */
- 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();
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $this->randomName(),
- 'description[value]' => $this->randomName(),
- ), 'Save');
- $no_synonyms_term = $this->getLastTerm($this->vocabularies['enabled']);
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $this->randomName(),
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $synonym1,
- ), 'Save');
- $one_synonym_term = $this->getLastTerm($this->vocabularies['enabled']);
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $this->randomName(),
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $synonym1,
- ), 'Add another item');
- $this->drupalPost(NULL, array(
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][1][value]' => $synonym2,
- ), 'Save');
- $two_synonyms_term = $this->getLastTerm($this->vocabularies['enabled']);
- // Creating an identical parent term in order to test
- // $parent parameter in our functions.
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $name,
- 'description[value]' => $this->randomName(),
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $synonym,
- ), 'Add another item');
- $this->drupalPost(NULL, array(
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][1][value]' => $parent_synonym,
- ), 'Save');
- $term_parent = $this->getLastTerm($this->vocabularies['enabled']);
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $name,
- 'description[value]' => $this->randomName(),
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $synonym,
- 'parent[]' => array($term_parent->tid),
- ), 'Save');
- $term = $this->getLastTerm($this->vocabularies['enabled']);
- $this->drupalGet('taxonomy/term/' . $term->tid);
- // Asserting the presence of synonym string.
- $this->assertText($synonym, 'The synonym string is present on taxonomy term view page');
- // Testing the function synonyms_get_term_synonyms().
- $synonyms = synonyms_get_term_synonyms($no_synonyms_term);
- $this->assertTrue(empty($synonyms), 'Successfully called synonyms_get_term_synonyms() for a term without synonyms.');
- $synonyms = synonyms_get_term_synonyms($one_synonym_term);
- $this->assertTrue(count($synonyms) == 1 && $synonyms[0]['value'] == $synonym1, 'Sucessfully called synonyms_get_term_synonyms for a term with single synonym.');
- $synonyms = synonyms_get_term_synonyms($two_synonyms_term);
- $this->assertTrue(count($synonyms) == 2 && $synonyms[0]['value'] == $synonym1 && $synonyms[1]['value'] == $synonym2, 'Sucessfully called synonyms_get_term_synonyms for a term with 2 synonyms.');
- // Testing the function synonyms_get_term_by_synonym().
- $tid = synonyms_get_term_by_synonym(drupal_strtoupper($synonym), $this->vocabularies['enabled'], $term_parent->tid);
- $this->assertEqual($tid, $term->tid, 'Sucessfully looked up term by its synonym.');
- $tid = synonyms_get_term_by_synonym(drupal_strtoupper($name), $this->vocabularies['enabled'], $term_parent->tid);
- $this->assertEqual($tid, $term->tid, 'Sucessfully looked up term by its name.');
- // Now submitting a non-existing name.
- $tid = synonyms_get_term_by_synonym($parent_synonym, $this->vocabularies['enabled'], $term_parent->tid);
- $this->assertEqual($tid, 0, 'synonyms_get_term_by_synonym() returns 0 if the term is not found (considering $parent parameter).');
- // Testing the function synonyms_add_term_by_synonym().
- $tid = synonyms_add_term_by_synonym(drupal_strtolower($name), $this->vocabularies['enabled'], $term_parent->tid);
- $this->assertEqual($tid, $term->tid, 'Sucessfully 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->vocabularies['enabled'], $term_parent->tid);
- $this->assertEqual($tid, $term->tid, 'Sucessfully 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->vocabularies['enabled'], $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.');
- $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.');
- // Disabling functionality of synonyms for "enabled" vocabulary
- // and making sure it has cleaned up all its functionality.
- $this->drupalPost('admin/structure/taxonomy/enabled/edit', array(
- 'synonyms[synonyms][' . SYNONYMS_DEFAULT_FIELD_NAME . ']' => FALSE,
- ), 'Save');
- $this->drupalGet('taxonomy/term/' . $term->tid);
- // Asserting the absence of synonym string.
- $this->assertNoText($synonym, 'The synonym string is no longer present on taxonomy term view page after disabling "synonyms" feature for a vocabulary');
- $term = array_pop(entity_load('taxonomy_term', array($term->tid), array(), TRUE));
- $this->assertFalse(isset($term->{SYNONYMS_DEFAULT_FIELD_NAME}), 'The term no longer has synonyms field after disabling "synonyms" feature for a vocabulary');
- // Testing synonyms_get_term_synonums() function.
- $synonyms = synonyms_get_term_synonyms($no_synonyms_term);
- $this->assertTrue(empty($synonyms), 'Successfully called synonyms_get_term_synonyms() on a term without synonyms after disabling "synonyms" feature');
- $synonyms = synonyms_get_term_synonyms($one_synonym_term);
- $this->assertTrue(empty($synonyms), 'Successfully called synonyms_get_term_synonyms() on a term with single synonym after disabling "synonyms" feature');
- $synonyms = synonyms_get_term_synonyms($two_synonyms_term);
- $this->assertTrue(empty($synonyms), 'Successfully called synonyms_get_term_synonyms() on a term with 2 synonyms after disabling "synonyms" feature');
- $tid = synonyms_get_term_by_synonym(drupal_strtoupper($synonym), $this->vocabularies['enabled']);
- $this->assertEqual($tid, 0, 'synonyms_get_term_by_synonym() returns 0 after disabling "synonyms" feature for a vocabulary');
- $tid = synonyms_get_term_by_synonym(drupal_strtoupper($name), $this->vocabularies['enabled'], $term_parent->tid);
- $this->assertEqual($tid, $term->tid, 'synonyms_get_term_by_synonym() returns $term->tid even after disabling "synonyms" feature if looking up by term title');
- // Testing synonyms_add_term_by_synonym() function.
- $tid = synonyms_add_term_by_synonym(drupal_strtolower($name), $this->vocabularies['enabled'], $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 after disabling "synonyms" feature.');
- $tid = synonyms_add_term_by_synonym(drupal_strtolower($synonym), $this->vocabularies['enabled'], $term_parent->tid);
- $new_term = taxonomy_term_load($tid);
- $new_term_parents = array_keys(taxonomy_get_parents($new_term->tid));
- $this->assertFalse(in_array($new_term->tid, array($term->tid, $term_parent->tid)), 'Successfully called synonyms_add_term_by_synonym() using previous synonym and a new term was created (because the vocabulary has disabled "synonyms" feature)');
- $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 with disabled "synonyms" feature.');
- $tid = synonyms_add_term_by_synonym($parent_synonym, $this->vocabularies['enabled']);
- $new_term = array_pop(entity_load('taxonomy_term', array($tid), array(), TRUE));
- $this->assertEqual($parent_synonym, $new_term->name, 'Successfully called synonyms_add_term_by_synonym() on an new title and a new term was created.');
- $this->assertFalse(in_array($new_term->tid, array($term->tid, $term_parent->tid)), 'Successfully called synonyms_add_term_by_synonym() on the synonyn of parent term. New term was created (because the vocabulary has disabled "synonyms" feature)');
- }
- }
- /**
- * Test "Synonym friednly autocomplete" widget of Synonyms module.
- */
- class AutocompleteSynonymsWebTestCase extends SynonymsWebTestCase {
- protected $vocabularies = array(
- 'enabled' => TRUE,
- 'disabled' => FALSE,
- );
- protected $terms = array(
- 'enabled' => array(),
- 'disabled' => array(),
- );
- /**
- * GetInfo method.
- */
- public function getInfo() {
- return array(
- 'name' => 'Taxonomy synonyms autocomplete',
- 'description' => 'Ensure that the "synonym friendly autocomplete" widget works correctly with taxonomy terms.',
- 'group' => 'Synonyms',
- );
- }
- /**
- * SetUp method.
- */
- public function setUp() {
- parent::setUp();
- // Creating vocabularies.
- $this->drupalLogin($this->admin);
- foreach ($this->vocabularies as $k => $v) {
- $name = $this->randomName();
- $this->drupalPost('admin/structure/taxonomy/add', array(
- 'name' => $name,
- 'machine_name' => $k,
- 'description' => $this->randomName(),
- 'synonyms[synonyms][' . SYNONYMS_DEFAULT_FIELD_NAME . ']' => $v,
- ), 'Save');
- $this->vocabularies[$k] = taxonomy_vocabulary_machine_name_load($k);
- }
- // Creating a test content type.
- $this->drupalPost('admin/structure/types/add', array(
- 'name' => 'Synonyms Test Content',
- 'type' => 'synonyms_test_content',
- ), 'Save content type');
- // Attaching each vocabulary term reference field to the new content type.
- foreach ($this->vocabularies as $k => $v) {
- $this->drupalPost('admin/structure/types/manage/synonyms_test_content/fields', array(
- 'fields[_add_new_field][label]' => 'Synonym Terms ' . $k,
- 'fields[_add_new_field][field_name]' => 'synonyms_term_' . $k,
- 'fields[_add_new_field][type]' => 'taxonomy_term_reference',
- 'fields[_add_new_field][widget_type]' => 'synonyms_autocomplete',
- ), 'Save');
- $this->drupalPost(NULL, array(
- 'field[settings][allowed_values][0][vocabulary]' => $v->machine_name,
- ), 'Save field settings');
- $this->drupalPost(NULL, array(
- 'field[cardinality]' => FIELD_CARDINALITY_UNLIMITED,
- ), 'Save settings');
- }
- // Flushing static cache.
- _field_info_collate_fields(TRUE);
- // Now creating taxonomy tree for each vocabularies.
- // For synonym-disbaled vocabulary just one term is good enough.
- $name = $this->randomName();
- $this->drupalPost('admin/structure/taxonomy/disabled/add', array(
- 'name' => $name,
- ), 'Save');
- $this->terms['disabled']['term1'] = $this->getLastTerm($this->vocabularies['disabled']);
- $name .= $this->randomName();
- $this->drupalPost('admin/structure/taxonomy/disabled/add', array(
- 'name' => $name,
- ), 'Save');
- $this->terms['disabled']['term1_longer_name'] = $this->getLastTerm($this->vocabularies['disabled']);
- $this->drupalPost('admin/structure/taxonomy/disabled/add', array(
- 'name' => $this->randomName(),
- ), 'Save');
- $this->terms['disabled']['term2'] = $this->getLastTerm($this->vocabularies['disabled']);
- // For synonym-enabled vocabulary we have to create such a set of input,
- // that would cover all possible branches of the autocomplete callback.
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $this->randomName(),
- ), 'Save');
- $this->terms['enabled']['no_synonyms'] = $this->getLastTerm($this->vocabularies['enabled']);
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $this->randomName(),
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(),
- ), 'Save');
- $this->terms['enabled']['one_synonym'] = $this->getLastTerm($this->vocabularies['enabled']);
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $this->randomName(),
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(),
- ), 'Add another item');
- $this->drupalPost(NULL, array(
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][1][value]' => $this->randomName(),
- ), 'Save');
- $this->terms['enabled']['two_synonyms'] = $this->getLastTerm($this->vocabularies['enabled']);
- $name = $this->randomName();
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $name,
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $name . $this->randomName(),
- ), 'Save');
- $this->terms['enabled']['name_similar_synonym'] = $this->getLastTerm($this->vocabularies['enabled']);
- $name = 'simiar_synonyms_';
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $this->randomName(),
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $name . $this->randomName(),
- ), 'Add another item');
- $this->drupalPost(NULL, array(
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][1][value]' => $name . $this->randomName(),
- ), 'Save');
- $this->terms['enabled']['similar_synonyms'] = $this->getLastTerm($this->vocabularies['enabled']);
- $name = 'one_term_name_another_synonym_';
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $name . $this->randomName(),
- ), 'Save');
- $this->terms['enabled']['name_another_synonym'] = $this->getLastTerm($this->vocabularies['enabled']);
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $this->randomName(),
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $name . $this->randomName(),
- ), 'Save');
- $this->terms['enabled']['synonym_another_name'] = $this->getLastTerm($this->vocabularies['enabled']);
- }
- /**
- * Test auto-creation functionality.
- *
- * Test the auto-creation funcationality of the synonym friendly autocomplete
- * widget type.
- */
- public function testAutoCreation() {
- // Trying enabled auto creation.
- $this->drupalPost('admin/structure/types/manage/synonyms-test-content/fields/field_synonyms_term_enabled', array(
- 'instance[widget][settings][auto_creation]' => TRUE,
- ), 'Save settings');
- $new_term_name = $this->randomName();
- $this->drupalPost('node/add/synonyms-test-content', array(
- 'title' => $this->randomName(),
- 'field_synonyms_term_enabled[' . LANGUAGE_NONE . ']' => $this->terms['enabled']['no_synonyms']->name . ', ' . $new_term_name,
- ), 'Save');
- $this->assertText($this->terms['enabled']['no_synonyms']->name, 'Existing term was assigned to the new node');
- $this->assertText($new_term_name, 'Auto created term was assigned to the new node');
- $term = $this->getLastTerm($this->vocabularies['enabled']);
- $this->assertEqual($term->name, $new_term_name, 'The auto created term has been created');
- // Trying disabled auto creation.
- $this->drupalPost('admin/structure/types/manage/synonyms-test-content/fields/field_synonyms_term_enabled', array(
- 'instance[widget][settings][auto_creation]' => FALSE,
- ), 'Save settings');
- $new_term_name = $this->randomName();
- $this->drupalPost('node/add/synonyms-test-content', array(
- 'title' => $this->randomName(),
- 'field_synonyms_term_enabled[und]' => $this->terms['enabled']['no_synonyms']->name . ', ' . $new_term_name,
- ), 'Save');
- $this->assertText($this->terms['enabled']['no_synonyms']->name, 'Existing term was assigned to the new node');
- $this->assertNoText($new_term_name, 'Auto created term was not assigned to the new node');
- $term = $this->getLastTerm($this->vocabularies['enabled']);
- $this->assertNotEqual($term->name, $new_term_name, 'The auto created term has not been created');
- }
- /**
- * Test autocomplete menu path.
- *
- * Feed all known "buggy" input to synonym friendly autocomplete menu path,
- * in order to test its performance.
- */
- public function testAutocompleteMenuPath() {
- $assertions = array();
- // Testing empty and non-existing name arguments.
- foreach ($this->vocabularies as $v) {
- $assertions[] = array(
- 'vocabulary' => $v->machine_name,
- 'input' => '',
- 'response' => array(),
- 'message' => 'Submitting empty string into a ' . $v->machine_name . ' synonyms vocabulary',
- );
- $assertions[] = array(
- 'vocabulary' => $v->machine_name,
- 'input' => $this->randomName(),
- 'response' => array(),
- 'message' => 'Submitting non existing name into a ' . $v->machine_name . ' synonyms vocabulary',
- );
- }
- // Testing the synonym-disabled vocabulary.
- $terms = $this->terms['disabled'];
- $assertions[] = array(
- 'vocabulary' => 'disabled',
- 'input' => drupal_strtoupper(drupal_substr($terms['term1']->name, 1, -1)),
- 'response' => array($terms['term1']->name => $terms['term1']->name, $terms['term1_longer_name']->name => $terms['term1_longer_name']->name),
- 'message' => 'Submitting a name similar to 2 existing term names into a disabled synonyms vocabulary',
- );
- $assertions[] = array(
- 'vocabulary' => 'disabled',
- 'input' => $terms['term1']->name . ',' . drupal_strtoupper(drupal_substr($terms['term1']->name, 1, -1)),
- 'response' => array($terms['term1']->name . ', ' . $this->terms['disabled']['term1_longer_name']->name => $this->terms['disabled']['term1_longer_name']->name),
- 'message' => 'Submitting one term already chosen along with a name similar to 2 existing term names into a disabled synonyms vocabulary',
- );
- $assertions[] = array(
- 'vocabulary' => 'disabled',
- 'input' => drupal_strtoupper(drupal_substr($terms['term2']->name, 1, -1)),
- 'response' => array($terms['term2']->name => $terms['term2']->name),
- 'message' => 'Submitting a name similar to one existing term name into a disabled synonyms vocabulary',
- );
- $assertions[] = array(
- 'vocabulary' => 'disabled',
- 'input' => drupal_strtolower($terms['term2']->name) . ',' . drupal_strtoupper(drupal_substr($terms['term2']->name, 1, -1)),
- 'response' => array(),
- 'message' => 'Submitting the same term over again into a disabled synonyms vocabulary',
- );
- // Testing the synonym-enabled vocabulary.
- $terms = $this->terms['enabled'];
- $assertions[] = array(
- 'vocabulary' => 'enabled',
- 'input' => drupal_strtoupper($terms['no_synonyms']->name) . ',' . drupal_strtolower(drupal_substr($terms['no_synonyms']->name, 1, -1)),
- 'response' => array(),
- 'message' => 'Submitting the same term over again into an enabled synonyms vocabulary',
- );
- $assertions[] = array(
- 'vocabulary' => 'enabled',
- 'input' => $terms['one_synonym']->name . ',' . $terms['one_synonym']->synonyms_synonyms[LANGUAGE_NONE][0]['safe_value'],
- 'response' => array(),
- 'message' => 'Submitting a synonym of a term over again into an enabled synonyms vocabulary',
- );
- foreach (array('no_synonyms', 'one_synonym', 'two_synonyms') as $k) {
- $assertions[] = array(
- 'vocabulary' => 'enabled',
- 'input' => drupal_strtolower(drupal_substr($terms[$k]->name, 1, -1)),
- 'response' => array($terms[$k]->name => $terms[$k]->name),
- 'message' => 'Submitting a name similar to ' . $k . ' term into an enabled synonyms vocabulary',
- );
- $synonyms = field_get_items('taxonomy_term', $terms[$k], 'synonyms_synonyms');
- if (is_array($synonyms)) {
- foreach ($synonyms as $delta => $item) {
- $assertions[] = array(
- 'vocabulary' => 'enabled',
- 'input' => drupal_strtolower(drupal_substr($item['safe_value'], 1, -1)),
- 'response' => array($terms[$k]->name => $this->synonymAutocompleteResult($terms[$k], $item['safe_value'])),
- 'message' => 'Submitting a name similar to synonym#' . $delta . ' of the term ' . $k . 'into an enabled synonyms vocabulary',
- );
- }
- }
- }
- $assertions[] = array(
- 'vocabulary' => 'enabled',
- 'input' => $terms['name_similar_synonym']->name,
- 'response' => array($terms['name_similar_synonym']->name => $terms['name_similar_synonym']->name),
- 'message' => 'Submitting a keyword similar to name and synonym of a term into an enabled synonyms vocabulary',
- );
- $assertions[] = array(
- 'vocabulary' => 'enabled',
- 'input' => 'simiar_synonyms_',
- 'response' => array($terms['similar_synonyms']->name => $this->synonymAutocompleteResult($terms['similar_synonyms'], $terms['similar_synonyms']->synonyms_synonyms[LANGUAGE_NONE][0]['safe_value'])),
- 'message' => 'Submitting a keyword similar to name and synonym of a term into an enabled synonyms vocabulary',
- );
- $assertions[] = array(
- 'vocabulary' => 'enabled',
- 'input' => 'one_term_name_another_synonym_',
- 'response' => array(
- $terms['name_another_synonym']->name => $terms['name_another_synonym']->name,
- $terms['synonym_another_name']->name => $this->synonymAutocompleteResult($terms['synonym_another_name'], $terms['synonym_another_name']->synonyms_synonyms[LANGUAGE_NONE][0]['safe_value']),
- ),
- 'message' => 'Submitting a keyword similar to name of one term and synonym of another into an enabled synonyms vocabulary yields both included in the results',
- );
- foreach ($assertions as $v) {
- $this->assertAutocompleteMenuPath($v);
- }
- }
- /**
- * Assert output of synonym friendly autocomplete path.
- *
- * @param array $assertion
- * Specially encoded array of assertion. Should include the follwing keys:
- * vocabulary - machine name of vocabulary whose field is asserted
- * input - input string to be fed to autocomplete menu path
- * response - JSON decoded expected response of autocomplete menu path
- * message - Drupal assertion message to be displayed on test results
- * page
- */
- protected function assertAutocompleteMenuPath($assertion) {
- $response = $this->drupalGet('synonyms/autocomplete/field_synonyms_term_' . $assertion['vocabulary'] . '/' . $assertion['input']);
- if (!$response) {
- $this->fail($assertion['message'], 'Autocomplete Menu Path');
- return;
- }
- $response = (array) json_decode($response);
- $is_the_same = count($response) == count($assertion['response']);
- $is_the_same = $is_the_same && count(array_intersect_assoc($response, $assertion['response'])) == count($assertion['response']);
- $this->assertTrue($is_the_same, $assertion['message'], 'Autocomplete Menu Path');
- }
- /**
- * Return expected autocomplete menu path result.
- *
- * The result is prepared as if the term was found by the supplied synonym.
- *
- * @param object $term
- * Fully loaded taxonomy term object for which the result is generated.
- * @param string $synonym
- * Synonym by which the term was hit in the search
- *
- * @return string
- * Formatted autocomplete result
- */
- protected function synonymAutocompleteResult($term, $synonym) {
- return t('@synonym, synonym of %term', array('@synonym' => $synonym, '%term' => $term->name));
- }
- }
- /**
- * Test Synonyms module integration with Drupal search functionality.
- */
- class SearchIndexSynonymsWebTestCase extends SynonymsWebTestCase {
- protected $vocabularies = array(
- 'enabled' => TRUE,
- );
- /**
- * GetInfo method.
- */
- public function getInfo() {
- return array(
- 'name' => 'Synonyms search integration',
- 'description' => 'Ensure that Synonyms module correctly integrates with the Drupal search functionality.',
- 'group' => 'Synonyms',
- );
- }
- /**
- * SetUp method.
- */
- public function setUp() {
- parent::setUp();
- // Creating vocabularies.
- $this->drupalLogin($this->admin);
- foreach ($this->vocabularies as $k => $v) {
- $name = $this->randomName();
- $this->drupalPost('admin/structure/taxonomy/add', array(
- 'name' => $name,
- 'machine_name' => $k,
- 'description' => $this->randomName(),
- 'synonyms[synonyms][' . SYNONYMS_DEFAULT_FIELD_NAME . ']' => $v,
- ), 'Save');
- $this->vocabularies[$k] = taxonomy_vocabulary_machine_name_load($k);
- }
- // Creating a test content type.
- $this->drupalPost('admin/structure/types/add', array(
- 'name' => 'Synonyms Test Content',
- 'type' => 'synonyms_test_content',
- ), 'Save content type');
- // Attaching each vocabulary term reference field to the new content type.
- foreach ($this->vocabularies as $k => $v) {
- $this->drupalPost('admin/structure/types/manage/synonyms_test_content/fields', array(
- 'fields[_add_new_field][label]' => 'Synonym Terms ' . $k,
- 'fields[_add_new_field][field_name]' => 'synonyms_term_' . $k,
- 'fields[_add_new_field][type]' => 'taxonomy_term_reference',
- 'fields[_add_new_field][widget_type]' => 'synonyms_autocomplete',
- ), 'Save');
- $this->drupalPost(NULL, array(
- 'field[settings][allowed_values][0][vocabulary]' => $v->machine_name,
- ), 'Save field settings');
- $this->drupalPost(NULL, array(
- 'field[cardinality]' => FIELD_CARDINALITY_UNLIMITED,
- ), 'Save settings');
- }
- // Flushing static cache.
- _field_info_collate_fields(TRUE);
- }
- /**
- * Test searching 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.
- */
- public function testSearchTermSynonym() {
- // Create a few terms and synonyms.
- $terms = array();
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $this->randomName(),
- ), 'Save');
- $terms['no_synonyms'] = $this->getLastTerm($this->vocabularies['enabled']);
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $this->randomName(),
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(),
- ), 'Save');
- $terms['one_synonym'] = $this->getLastTerm($this->vocabularies['enabled']);
- $this->drupalPost('admin/structure/taxonomy/enabled/add', array(
- 'name' => $this->randomName(),
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][0][value]' => $this->randomName(),
- ), 'Add another item');
- $this->drupalPost(NULL, array(
- SYNONYMS_DEFAULT_FIELD_NAME . '[' . LANGUAGE_NONE . '][1][value]' => $this->randomName(),
- ), 'Save');
- $terms['two_synonyms'] = $this->getLastTerm($this->vocabularies['enabled']);
- $assertions = array();
- // Creating a node, which refereces to all the terms we have.
- $title = $this->randomName();
- $this->drupalPost('node/add/synonyms-test-content', array(
- 'title' => $title,
- 'field_synonyms_term_enabled[' . LANGUAGE_NONE . ']' => $terms['no_synonyms']->name . ', ' . $terms['one_synonym']->name . ', ' . $terms['two_synonyms']->name,
- ), 'Save');
- $node = $this->drupalGetNodeByTitle($title);
- // Rebuilding Search index.
- $this->cronRun();
- foreach ($terms as $k => $v) {
- $assertions[] = array(
- 'keyword' => $v->name,
- 'results' => array($node),
- 'message' => 'Searching by name of the term ' . $k,
- );
- foreach (synonyms_get_term_synonyms($v) as $delta => $synonym) {
- $assertions[] = array(
- 'keyword' => $synonym['value'],
- 'results' => array($node),
- 'message' => 'Searching by synonym #' . $delta . ' of the term ' . $k,
- );
- }
- }
- foreach ($assertions as $assertion) {
- $this->assertSearchResults($assertion);
- }
- }
- /**
- * Assert search results.
- *
- * @param array $assertion
- * Specially encoded array of assertion. Should include the follwing keys:
- * keyword - what keyword has to be supplied to the search mechanism
- * results - array of nodes that are expected to be on search results
- * message - Drupal assertion message to be displayed on test results
- * page
- */
- protected function assertSearchResults($assertion) {
- $response = $this->drupalGet('search/node/' . $assertion['keyword']);
- $matches = array();
- preg_match_all('#\<li[^>]+class="search-result"[^>]*\>(.*?)\</li\>#si', $response, $matches);
- $results = $matches[1];
- if (count($results) != count($assertion['results'])) {
- $this->fail($assertion['message']);
- return;
- }
- $results = implode('', $results);
- foreach ($assertion['results'] as $node) {
- if (strpos($results, 'node/' . $node->nid) === FALSE) {
- $this->fail($assertion['message']);
- return;
- }
- }
- $this->pass($assertion['message']);
- }
- }
- /**
- * Base class for all test cases that test Synonoyms Extractor classes.
- */
- abstract class AbstractSynonymsExtractorWebTestCase extends DrupalWebTestCase {
- /**
- * Taxonomy vocabulary object on whose term all synonyms extracting tests will
- * occur.
- *
- * @var object
- */
- protected $vocabulary;
- /**
- * Class name of a synonyms extractor that is being tested.
- *
- * @var string
- */
- protected $extractor;
- /**
- * Field API field definition array of the field that is tested right now.
- *
- * @var array
- */
- protected $field;
- /**
- * Field API instance definition array of the instance that is tested now.
- *
- * @var array
- */
- protected $instance;
- /**
- * SetUp method.
- *
- * @param string $class
- * Name of class that is being tested
- * @param array $modules
- * Array of extra modules to install for testing a particular synonyms
- * extractor
- */
- public function setUp($class, $modules = array()) {
- array_unshift($modules, 'synonyms');
- parent::setUp($modules);
- $this->vocabulary = (object) array(
- 'name' => 'Test Synonyms Extactor',
- 'machine_name' => 'synonyms_extractor',
- );
- taxonomy_vocabulary_save($this->vocabulary);
- $this->extractor = $class;
- }
- /**
- * Completely set field in the tested vocabulary.
- *
- * Create a field in Field API (if does not exist yet), then create an
- * instance and relate it to our tested vocabulary. Lastly enable this field
- * as a source of synonyms for our tested vocabulary.
- *
- * @param array $field
- * Field definition array as expected by Field API
- * @param array $instance
- * Instance defintion array as expected by Field API
- */
- protected function addFieldInstance($field, $instance) {
- $field = field_create_field($field);
- $instance['entity_type'] = 'taxonomy_term';
- $instance['bundle'] = field_extract_bundle('taxonomy_term', $this->vocabulary);
- field_create_instance($instance);
- $settings = synonyms_vocabulary_settings($this->vocabulary);
- $settings['synonyms'][] = $field['field_name'];
- synonyms_vocabulary_settings_save($this->vocabulary, $settings);
- $this->field = $field;
- $this->instance = $instance;
- }
- /**
- * Test synonymsExtract() method of class.
- *
- * @param array $items
- * Array of field items to be saved in tested term
- * @param array $etalon
- * Expected return of synonymsExtract() method of class
- * @param string $message
- * Any custom message to be added to the standard one and passed to
- * SimpleTest assertion method
- */
- protected function assertSynonymsExtract($items, $etalon, $message = '') {
- $term = (object) array(
- 'name' => $this->randomName(),
- 'vid' => $this->vocabulary->vid,
- );
- $term->{$this->field['field_name']} = $items;
- taxonomy_term_save($term);
- $items = field_get_items('taxonomy_term', $term, $this->field['field_name']);
- $synonyms = is_array($items) ? call_user_func($this->extractor . '::synonymsExtract', $items, $this->field, $this->instance, $term, 'taxonomy_term') : array();
- $this->assertTrue(count(array_intersect($etalon, $synonyms)) == count($etalon), 'Synonyms Extractor ' . $this->extractor . ' passed synonymsExtract() method: ' . $message);
- // Cleaning up.
- taxonomy_term_delete($term->tid);
- }
- /**
- * Test processEntityFieldQuery method of class.
- *
- * @param array $meta_data
- * Array of meta data. Each subarray represents a single term and whether it
- * is expected to be included in the results of EntityFieldQuery. Should
- * have the following structure:
- * items - array of field items. Terms will be automatically created with
- * those items
- * expected - bool, whether the created term should be expected in the
- * results of EntityFieldQuery
- * @param string $tag
- * Corresponding parameter to be passed to processEntityFieldQuery() method
- * @param string $message
- * Any custom message to be added to the standard one and passed to
- * SimpleTest assertion method
- */
- protected function assertProcessEntityFieldQuery($meta_data, $tag, $message = '') {
- // Creating taxonomy terms according to the meta data.
- $terms = array();
- foreach ($meta_data as $v) {
- $term = (object) array(
- 'name' => $this->randomName(),
- 'vid' => $this->vocabulary->vid,
- );
- taxonomy_term_save($term);
- $term->{$this->field['field_name']} = $v['items'];
- taxonomy_term_save($term);
- $term->expected = $v['expected'];
- $terms[] = $term;
- }
- // Preparing and running EntityFieldQuery.
- $efq = new EntityFieldQuery();
- $efq->entityCondition('entity_type', 'taxonomy_term')
- ->entityCondition('bundle', $this->vocabulary->machine_name);
- call_user_func($this->extractor . '::processEntityFieldQuery', $tag, $efq, $this->field, $this->instance);
- $result = $efq->execute();
- $result = isset($result['taxonomy_term']) ? array_keys($result['taxonomy_term']) : array();
- // Asserting results of EntityFieldQuery.
- $pass = TRUE;
- foreach ($terms as $term) {
- $tmp = $term->expected ? in_array($term->tid, $result) : !in_array($term->tid, $result);
- $pass = $pass && $tmp;
- }
- $this->assertTrue($pass, 'Synonyms Extractor ' . $this->extractor . ' passed processEntityFieldQuery() method: ' . $message);
- // Cleaning up.
- foreach ($terms as $term) {
- taxonomy_term_delete($term->tid);
- }
- }
- /**
- * Test mergeEntityAsSynonym method of class.
- *
- * @param array $items
- * Parameter will be passed directly to the extractor class method
- * @param object $synonym_entity
- * Parameter will be passed directly to the extractor class method
- * @param string $synonym_entity_type
- * Parameter will be passed directly to the extractor class method
- * @param array $etalon
- * Array that is expected to be returned by the tested method
- * @param string $message
- * Any custom message to be added to the standard one and passed to
- * SimpleTest assertion method
- */
- protected function assertMergeEntityAsSynonym($items, $synonym_entity, $synonym_entity_type, $etalon, $message = '') {
- $extra_items = call_user_func($this->extractor . '::mergeEntityAsSynonym', $items, $this->field, $this->instance, $synonym_entity, $synonym_entity_type);
- foreach ($etalon as $k => $v) {
- if (count(array_intersect($etalon[$k], $extra_items[$k])) != count($etalon[$k])) {
- $this->fail('Synonyms Extractor ' . $this->extractor . ' passed mergeEntityAsSynonym() method: ' . $message);
- return;
- }
- }
- $this->pass('Synonyms Extractor ' . $this->extractor . ' passed mergeEntityAsSynonym() method: ' . $message);
- }
- }
- /**
- * Test SynonymsSynonymsExtractor class.
- */
- class SynonymsSynonoymsExtractorWebTestCase extends AbstractSynonymsExtractorWebTestCase {
- /**
- * GetInfo method.
- */
- public function getInfo() {
- return array(
- 'name' => 'SynonymsSynonymsExtractor',
- 'description' => 'Ensure that the synonyms module extracts synonyms from text and number fields correctly.',
- 'group' => 'Synonyms',
- );
- }
- /**
- * SetUp method.
- */
- public function setUp() {
- parent::setUp('SynonymsSynonymsExtractor');
- }
- /**
- * Test synonyms extraction for 'text' field type.
- */
- public function testText() {
- $this->addFieldInstance(array(
- 'field_name' => 'text',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- 'type' => 'text',
- ), array(
- 'field_name' => 'text',
- 'entity_type' => 'taxonomy_term',
- 'bundle' => $this->vocabulary->machine_name,
- 'label' => $this->randomName(),
- ));
- // 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 processEntityFieldQuery().
- $this->assertProcessEntityFieldQuery(array(), $this->randomName(), 'on empty field.');
- $tag = $this->randomName();
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- 0 => array('value' => $tag . $this->randomName()),
- 1 => array('value' => $this->randomName()),
- ),
- ),
- 'expected' => TRUE,
- );
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- 0 => array('value' => $this->randomName()),
- ),
- ),
- 'expected' => FALSE,
- );
- $meta_data[] = array(
- 'items' => array(),
- 'expected' => FALSE,
- );
- $this->assertProcessEntityFieldQuery($meta_data, $tag, 'on a field with values.');
- // 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.');
- }
- }
- /**
- * Test TaxonomySynonymsExtractor class.
- */
- class TaxonomySynonoymsExtractorWebTestCase extends AbstractSynonymsExtractorWebTestCase {
- /**
- * Taxonomy vocabulary object terms.
- *
- * Terms of this vocabulary are synonyms of the main vocabulary terms.
- *
- * @var object
- */
- protected $vocabularySource;
- /**
- * GetInfo method.
- */
- public function getInfo() {
- return array(
- 'name' => 'TaxonomySynonymsExtractor',
- 'description' => 'Ensure that the synonyms module extracts synonyms from taxonomy term reference fields correctly.',
- 'group' => 'Synonyms',
- );
- }
- /**
- * SetUp method.
- */
- public function setUp() {
- parent::setUp('TaxonomySynonymsExtractor');
- $this->vocabularySource = (object) array(
- 'name' => $this->randomName(),
- 'machine_name' => 'source_vocabulary',
- );
- taxonomy_vocabulary_save($this->vocabularySource);
- }
- /**
- * Test synonyms extraction for 'taxonomy_term_reference' field type.
- */
- public function testTaxonomyTermReference() {
- $this->addFieldInstance(array(
- 'field_name' => 'term',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- 'type' => 'taxonomy_term_reference',
- 'settings' => array(
- 'allowed_values' => array(
- array(
- 'vocabulary' => $this->vocabularySource->machine_name,
- 'parent' => 0,
- ),
- ),
- ),
- ), array(
- 'field_name' => 'term',
- 'entity_type' => 'taxonomy_term',
- 'bundle' => $this->vocabulary->machine_name,
- 'label' => $this->randomName(),
- ));
- // 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 processEntityFieldQuery().
- $this->assertProcessEntityFieldQuery(array(), $this->randomName(), 'on empty field.');
- $tag = $this->randomName();
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array('tid' => $this->createSynonymTerm($tag . $this->randomName())->tid),
- array('tid' => $this->createSynonymTerm()->tid),
- ),
- ),
- 'expected' => TRUE,
- );
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array('tid' => $this->createSynonymTerm()->tid),
- ),
- ),
- 'expected' => FALSE,
- );
- $meta_data[] = array(
- 'items' => array(),
- 'expected' => FALSE,
- );
- $this->assertProcessEntityFieldQuery($meta_data, $tag, 'on a field with values.');
- // 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.');
- }
- /**
- * Supportive function.
- *
- * Create a taxonomy term in the synonyms source vocabulary with the specified
- * name.
- *
- * @param string $name
- * Name of the term to be created. If nothing is supplied a random string
- * is used
- *
- * @return object
- * Fully loaded taxonomy term object of the just created term
- */
- protected function createSynonymTerm($name = NULL) {
- if (is_null($name)) {
- $name = $this->randomName();
- }
- $synonym_term = (object) array(
- 'name' => $name,
- 'vid' => $this->vocabularySource->vid,
- );
- taxonomy_term_save($synonym_term);
- return $synonym_term;
- }
- }
- /**
- * Test EntityReferenceSynonymsExtractor class.
- */
- class EntityReferenceSynonymsExtractorWebTestCase extends AbstractSynonymsExtractorWebTestCase {
- /**
- * GetInfo method.
- */
- public function getInfo() {
- return array(
- 'name' => 'EntityReferenceSynonymsExtractor',
- 'description' => 'Ensure that the synonyms module extracts synonyms from entity reference fields correctly.',
- 'group' => 'Synonyms',
- );
- }
- /**
- * SetUp method.
- */
- public function setUp() {
- parent::setUp('EntityReferenceSynonymsExtractor', array('entityreference'));
- }
- /**
- * Test synonyms extraction for 'entityreference' field type.
- */
- public function testEntityReference() {
- $this->addFieldInstance(array(
- 'field_name' => 'reference',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- 'type' => 'entityreference',
- // For the sake of experiment we use entityreference field that references
- // nodes of a Drupal standard type to make things easier.
- 'settings' => array(
- 'target_type' => 'node',
- 'handler' => 'base',
- 'handler_settings' => array(
- 'target_bundles' => array('page' => 'page'),
- 'sort' => array('type' => 'none'),
- ),
- ),
- ), array(
- 'field_name' => 'reference',
- 'entity_type' => 'taxonomy_term',
- 'bundle' => $this->vocabulary->machine_name,
- 'label' => $this->randomName(),
- ));
- // Testing synonymsExtract().
- $this->assertSynonymsExtract(array(), array(), 'on empty field.');
- $synonym_entity = $this->createNode();
- $this->assertSynonymsExtract(array(
- LANGUAGE_NONE => array(
- 0 => array(
- 'target_id' => entity_id('node', $synonym_entity),
- ),
- ),
- ), array(entity_label('node', $synonym_entity)), 'on a field that holds one value.');
- // Testing processEntityFieldQuery().
- $this->assertProcessEntityFieldQuery(array(), $this->randomName(), 'on empty field.');
- $tag = $this->randomName();
- $meta_data = array();
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array(
- 'target_id' => entity_id('node', $this->createNode($tag . $this->randomName())),
- ),
- array(
- 'target_id' => entity_id('node', $this->createNode()),
- ),
- ),
- ),
- 'expected' => TRUE,
- );
- $meta_data[] = array(
- 'items' => array(
- LANGUAGE_NONE => array(
- array('target_id' => entity_id('node', $this->createNode())),
- ),
- ),
- 'expected' => FALSE,
- );
- $meta_data[] = array(
- 'items' => array(),
- 'expected' => FALSE,
- );
- $this->assertProcessEntityFieldQuery($meta_data, $tag, 'on a field with values.');
- // Testing mergeEntityAsSynonym() method.
- $node = $this->createNode();
- $this->assertMergeEntityAsSynonym(array(), $node, 'node', array(array('target_id' => $node->nid)), 'on a node.');
- }
- /**
- * Supportive function.
- *
- * Create an entity of necessary entity type (in our test it's node).
- *
- * @param string $label
- * Label to use for the entity that is about to be created
- * @param string $bundle
- * Bundle to use for the entity that is about to be created
- *
- * @return object
- * Fully loaded entity object of the just created entity
- */
- protected function createNode($label = NULL, $bundle = 'page') {
- if (is_null($label)) {
- $label = $this->randomName();
- }
- $entity = (object) array(
- 'type' => $bundle,
- 'title' => $label,
- );
- node_save($entity);
- return $entity;
- }
- }
|