synonyms.test 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the Synonyms module.
  5. */
  6. /**
  7. * Base class for all Synonyms web test cases.
  8. */
  9. abstract class SynonymsWebTestCase extends DrupalWebTestCase {
  10. /**
  11. * Fully loaded user object of an admin user that has required access rights.
  12. *
  13. * @var object
  14. */
  15. protected $admin;
  16. /**
  17. * Taxonomy vocabulary within which the whole testing happens.
  18. *
  19. * @var object
  20. */
  21. protected $vocabulary;
  22. /**
  23. * Text fields that can be used for general purpose testing of behaviors.
  24. *
  25. * The var is firstly keyed by either 'enabled' or 'disabled', correspondingly
  26. * representing whether the behavior is enabled or disabled on that field. And
  27. * the inner array has 2 keys:
  28. * - field: (array) field definition array
  29. * - instance: (array) instance definition array
  30. *
  31. * @var array
  32. */
  33. protected $fields = array(
  34. 'enabled' => array(
  35. 'field' => array(
  36. 'field_name' => 'text_enabled',
  37. 'type' => 'text',
  38. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  39. ),
  40. 'instance' => array(
  41. 'entity_type' => 'taxonomy_term',
  42. ),
  43. ),
  44. 'disabled' => array(
  45. 'field' => array(
  46. 'field_name' => 'text_disabled',
  47. 'type' => 'text',
  48. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  49. ),
  50. 'instance' => array(
  51. 'entity_type' => 'taxonomy_term',
  52. ),
  53. ),
  54. );
  55. /**
  56. * Behavior implementation that is being tested.
  57. *
  58. * @var array
  59. */
  60. protected $behavior_implementation = array(
  61. 'provider' => NULL,
  62. 'entity_type' => 'taxonomy_term',
  63. 'bundle' => NULL,
  64. 'behavior' => NULL,
  65. 'settings' => array(),
  66. );
  67. /**
  68. * SetUp method.
  69. */
  70. public function setUp($modules = array()) {
  71. array_unshift($modules, 'synonyms_provider_field');
  72. array_unshift($modules, 'synonyms');
  73. parent::setUp($modules);
  74. $this->admin = $this->drupalCreateUser(array(
  75. 'administer taxonomy',
  76. 'administer content types',
  77. 'bypass node access',
  78. 'search content',
  79. ));
  80. // Creating vocabularies.
  81. $this->drupalLogin($this->admin);
  82. $this->vocabulary = (object) array(
  83. 'name' => $this->randomName(),
  84. 'machine_name' => 'synonyms_test',
  85. 'description' => $this->randomName(),
  86. );
  87. taxonomy_vocabulary_save($this->vocabulary);
  88. $this->fields['enabled']['field'] = field_create_field($this->fields['enabled']['field']);
  89. $this->fields['enabled']['field'] = field_info_field($this->fields['enabled']['field']['field_name']);
  90. if (!isset($this->fields['enabled']['instance']['bundle'])) {
  91. $this->fields['enabled']['instance']['bundle'] = $this->vocabulary->machine_name;
  92. }
  93. $this->fields['enabled']['instance']['field_name'] = $this->fields['enabled']['field']['field_name'];
  94. $this->fields['enabled']['instance'] = field_create_instance($this->fields['enabled']['instance']);
  95. $this->fields['enabled']['instance'] = field_info_instance($this->fields['enabled']['instance']['entity_type'], $this->fields['enabled']['instance']['field_name'], $this->fields['enabled']['instance']['bundle']);
  96. $this->fields['disabled']['field'] = field_create_field($this->fields['disabled']['field']);
  97. $this->fields['disabled']['field'] = field_info_field($this->fields['disabled']['field']['field_name']);
  98. if (!isset($this->fields['disabled']['instance']['bundle'])) {
  99. $this->fields['disabled']['instance']['bundle'] = $this->vocabulary->machine_name;
  100. }
  101. $this->fields['disabled']['instance']['field_name'] = $this->fields['disabled']['field']['field_name'];
  102. $this->fields['disabled']['instance'] = field_create_instance($this->fields['disabled']['instance']);
  103. $this->fields['disabled']['instance'] = field_info_instance($this->fields['disabled']['instance']['entity_type'], $this->fields['disabled']['instance']['field_name'], $this->fields['disabled']['instance']['bundle']);
  104. if (!$this->behavior_implementation['bundle']) {
  105. $this->behavior_implementation['bundle'] = $this->fields['enabled']['instance']['bundle'];
  106. }
  107. if (!$this->behavior_implementation['provider']) {
  108. $this->behavior_implementation['provider'] = synonyms_provider_field_provider_name($this->fields['enabled']['field']);
  109. }
  110. synonyms_behavior_implementation_save($this->behavior_implementation);
  111. foreach (synonyms_behavior_get($this->behavior_implementation['behavior'], $this->behavior_implementation['entity_type'], $this->behavior_implementation['bundle'], TRUE) as $behavior_implementation) {
  112. if ($behavior_implementation['provider'] == $this->behavior_implementation['provider']) {
  113. $this->behavior_implementation = $behavior_implementation;
  114. break;
  115. }
  116. }
  117. }
  118. /**
  119. * Return last inserted term into the specified vocabulary.
  120. *
  121. * @param object $vocabulary
  122. * Fully loaded taxonomy vocabulary object
  123. *
  124. * @return object
  125. * Fully loaded taxonomy term object of the last inserted term into
  126. * the specified vocabulary
  127. */
  128. protected function getLastTerm($vocabulary) {
  129. $tid = db_select('taxonomy_term_data', 't');
  130. $tid->addExpression('MAX(t.tid)');
  131. $tid->condition('vid', $vocabulary->vid);
  132. $tid = $tid->execute()->fetchField();
  133. return entity_load_unchanged('taxonomy_term', $tid);
  134. }
  135. }
  136. /**
  137. * Test Synonyms functionality of synonyms module.
  138. */
  139. class SynonymsSynonymsWebTestCase extends SynonymsWebTestCase {
  140. /**
  141. * GetInfo method.
  142. */
  143. public static function getInfo() {
  144. return array(
  145. 'name' => 'Synonyms',
  146. 'description' => 'Ensure that the general synonyms functionality works correctly.',
  147. 'group' => 'Synonyms',
  148. );
  149. }
  150. /**
  151. * SetUp method.
  152. */
  153. public function setUp($modules = array()) {
  154. $this->behavior_implementation['behavior'] = 'autocomplete';
  155. parent::setUp($modules);
  156. }
  157. /**
  158. * Test the functionality of synonyms.
  159. */
  160. public function testSynonyms() {
  161. $name = $this->randomName();
  162. $synonym = $this->randomName();
  163. $parent_synonym = $this->randomName();
  164. // Creating terms for testing synonyms_get_term_synonyms().
  165. $synonym1 = $this->randomName();
  166. $synonym2 = $this->randomName();
  167. $no_synonyms_term = (object) array(
  168. 'vid' => $this->vocabulary->vid,
  169. 'name' => $this->randomName(),
  170. 'description' => $this->randomName(),
  171. $this->fields['disabled']['field']['field_name'] => array(
  172. LANGUAGE_NONE => array(array('value' => $this->randomName())),
  173. ),
  174. );
  175. taxonomy_term_save($no_synonyms_term);
  176. $one_synonym_term = (object) array(
  177. 'vid' => $this->vocabulary->vid,
  178. 'name' => $this->randomName(),
  179. $this->fields['enabled']['field']['field_name'] => array(
  180. LANGUAGE_NONE => array(array('value' => $synonym1)),
  181. ),
  182. $this->fields['disabled']['field']['field_name'] => array(
  183. LANGUAGE_NONE => array(array('value' => $this->randomName())),
  184. ),
  185. );
  186. taxonomy_term_save($one_synonym_term);
  187. $two_synonyms_term = (object) array(
  188. 'vid' => $this->vocabulary->vid,
  189. 'name'=> $this->randomName(),
  190. $this->fields['enabled']['field']['field_name'] => array(
  191. LANGUAGE_NONE => array(
  192. array('value' => $synonym1),
  193. array('value' => $synonym2),
  194. ),
  195. ),
  196. $this->fields['disabled']['field']['field_name'] => array(
  197. LANGUAGE_NONE => array(array('value' => $this->randomName())),
  198. ),
  199. );
  200. taxonomy_term_save($two_synonyms_term);
  201. // Creating an identical parent term in order to test $parent parameter in
  202. // our functions.
  203. $term_parent = (object) array(
  204. 'vid' => $this->vocabulary->vid,
  205. 'name' => $name,
  206. $this->fields['enabled']['field']['field_name'] => array(
  207. LANGUAGE_NONE => array(
  208. array('value' => $synonym),
  209. array('value' => $parent_synonym),
  210. ),
  211. ),
  212. $this->fields['disabled']['field']['field_name'] => array(
  213. LANGUAGE_NONE => array(array('value' => $this->randomName())),
  214. ),
  215. );
  216. taxonomy_term_save($term_parent);
  217. $term = (object) array(
  218. 'vid' => $this->vocabulary->vid,
  219. 'name' => $name,
  220. 'parent' => $term_parent->tid,
  221. $this->fields['enabled']['field']['field_name'] => array(
  222. LANGUAGE_NONE => array(
  223. array('value' => $synonym),
  224. ),
  225. ),
  226. $this->fields['disabled']['field']['field_name'] => array(
  227. LANGUAGE_NONE => array(array('value' => $this->randomName())),
  228. ),
  229. );
  230. taxonomy_term_save($term);
  231. // We enable the same provider in another synonym behavior too and test that
  232. // we only get unique synonyms, i.e. synonyms do not get doubled for being
  233. // sourced from the same provider twice (once per each behavior).
  234. $behavior_implementation = $this->behavior_implementation;
  235. $behavior_implementation['behavior'] = 'select';
  236. synonyms_behavior_implementation_save($behavior_implementation);
  237. // Testing the 'synonyms' property of 'taxonomy_term' entity.
  238. $synonyms = entity_metadata_wrapper('taxonomy_term', $no_synonyms_term)->synonyms->value();
  239. $this->assertTrue(empty($synonyms), 'Successfully retrieved synonyms_get_sanitized() for a term without synonyms.');
  240. $synonyms = entity_metadata_wrapper('taxonomy_term', $one_synonym_term)->synonyms->value();
  241. $this->assertTrue(count($synonyms) == 1 && $synonyms[0] == $synonym1, 'Successfully retrieved synonyms_get_sanitized() for a term with a single synonym.');
  242. $synonyms = entity_metadata_wrapper('taxonomy_term', $two_synonyms_term)->synonyms->value();
  243. $this->assertTrue(count($synonyms) == 2 && $synonyms[0] == $synonym1 && $synonyms[1] == $synonym2, 'Successfully retrieved synonyms_get_sanitized() for a term with 2 synonyms.');
  244. synonyms_behavior_implementation_delete($behavior_implementation);
  245. // Testing the function synonyms_get_term_by_synonym().
  246. $tid = synonyms_get_term_by_synonym(drupal_strtoupper($synonym), $this->vocabulary, $term_parent->tid);
  247. $this->assertEqual($tid, $term->tid, 'Successfully looked up term by its synonym.');
  248. $tid = synonyms_get_term_by_synonym(drupal_strtoupper($name), $this->vocabulary, $term_parent->tid);
  249. $this->assertEqual($tid, $term->tid, 'Successfully looked up term by its name.');
  250. // Now submitting a non-existing name.
  251. $tid = synonyms_get_term_by_synonym($parent_synonym, $this->vocabulary, $term_parent->tid);
  252. $this->assertEqual($tid, 0, 'synonyms_get_term_by_synonym() returns 0 if the term is not found (due to $parent parameter).');
  253. $tid = synonyms_get_term_by_synonym($term->{$this->fields['disabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], $this->vocabulary);
  254. $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).');
  255. // Testing the function synonyms_add_term_by_synonym().
  256. $tid = synonyms_add_term_by_synonym(drupal_strtolower($name), $this->vocabulary, $term_parent->tid);
  257. $this->assertEqual($tid, $term->tid, 'Successfully called synonyms_add_term_by_synonym() on an existing title and no new term was created.');
  258. $tid = synonyms_add_term_by_synonym(drupal_strtolower($synonym), $this->vocabulary, $term_parent->tid);
  259. $this->assertEqual($tid, $term->tid, 'Successfully called synonyms_add_term_by_synonym() on an existing synonym and no new term was created.');
  260. drupal_static_reset();
  261. $tid = synonyms_add_term_by_synonym($parent_synonym, $this->vocabulary, $term_parent->tid);
  262. $new_term = taxonomy_term_load($tid);
  263. $new_term_parents = array_keys(taxonomy_get_parents($new_term->tid));
  264. $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).');
  265. $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.');
  266. $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.');
  267. $tid = synonyms_add_term_by_synonym($term->{$this->fields['disabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], $this->vocabulary);
  268. $new_term = taxonomy_term_load($tid);
  269. $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).');
  270. // Testing the function synonyms_get_entity_by_synonym().
  271. $another_vocabulary = (object) array(
  272. 'name' => $this->randomName(),
  273. 'machine_name' => 'another_bundle',
  274. );
  275. taxonomy_vocabulary_save($another_vocabulary);
  276. $entity_id = synonyms_get_entity_by_synonym('taxonomy_term', $this->randomName());
  277. $this->assertEqual($entity_id, 0, 'synonyms_get_entity_by_synonym() function returns 0 when fails to look up any entity.');
  278. $entity_id = synonyms_get_entity_by_synonym('taxonomy_term', drupal_strtoupper($term_parent->name));
  279. $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.');
  280. $entity_id = synonyms_get_entity_by_synonym('taxonomy_term', drupal_strtoupper($term_parent->name), $another_vocabulary->machine_name);
  281. $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.');
  282. $entity_id = synonyms_get_entity_by_synonym('taxonomy_term', drupal_strtolower($synonym2));
  283. $this->assertEqual($entity_id, $two_synonyms_term->tid, 'synonyms_get_entity_by_synonym() returns entity ID when a synonym of that entity is supplied.');
  284. $entity_id = synonyms_get_entity_by_synonym('taxonomy_term', drupal_strtolower($parent_synonym), $another_vocabulary->machine_name);
  285. $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.');
  286. $entity_id = synonyms_get_entity_by_synonym('taxonomy_term', $term_parent->{$this->fields['disabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']);
  287. $this->assertEqual($entity_id, 0, 'synonyms_get_entity_by_synonym() returns 0 if a non-synonym field value is supplied.');
  288. }
  289. }
  290. /**
  291. * Test "Synonyms friendly autocomplete" widget of Synonyms module.
  292. */
  293. abstract class AbstractAutocompleteSynonymsWebTestCase extends SynonymsWebTestCase {
  294. /**
  295. * Array of fully loaded taxonomy term entities to be used in this test.
  296. *
  297. * @var array
  298. */
  299. protected $terms = array();
  300. /**
  301. * Entity type to which a term reference field with tested widget is attached.
  302. *
  303. * @var string
  304. */
  305. protected $entity_type = 'node';
  306. /**
  307. * Bundle to which a term reference field with tested widget is attached.
  308. *
  309. * @var string
  310. */
  311. protected $bundle = 'synonyms_test_content';
  312. /**
  313. * Field definition array of the field that will be attached to
  314. * $this->entity_type with synonyms-friendly autocomplete widget.
  315. *
  316. * @var array
  317. */
  318. protected $reference_field = array();
  319. /**
  320. * Field instance definition array of the field that will be attached to
  321. * $this->entity_type with synonyms friendly autocomplete widget.
  322. *
  323. * @var array
  324. */
  325. protected $reference_instance = array();
  326. /**
  327. * SetUp method.
  328. */
  329. public function setUp($modules = array()) {
  330. $this->behavior_implementation['behavior'] = 'autocomplete';
  331. $this->behavior_implementation['settings'] = array(
  332. 'wording' => '@synonym @field_name @entity',
  333. );
  334. $modules[] = 'synonyms_provider_property';
  335. parent::setUp($modules);
  336. // We'll also enable the ID property to be source of synonyms so we can
  337. // extra test multiple providers at the same time.
  338. $behavior_implementation = $this->behavior_implementation;
  339. $entity_info = entity_get_info($behavior_implementation['entity_type']);
  340. $behavior_implementation['provider'] = synonyms_provider_property_provider_name($entity_info['entity keys']['id']);
  341. synonyms_behavior_implementation_save($behavior_implementation);
  342. // Creating a test content type.
  343. $this->drupalPost('admin/structure/types/add', array(
  344. 'name' => 'Synonyms Test Content',
  345. 'type' => $this->bundle,
  346. ), 'Save content type');
  347. drupal_static_reset();
  348. $this->createTerms();
  349. }
  350. /**
  351. * Test autocomplete menu path.
  352. *
  353. * Feed all known "buggy" input to synonym friendly autocomplete menu path,
  354. * in order to test its performance.
  355. */
  356. public function testAutocompleteMenuPath() {
  357. $this->assertAutocompleteMenuPath('', array(), 'Submitting empty string into autocomplete path returns empty result.');
  358. $this->assertAutocompleteMenuPath($this->randomName(), array(), 'Submitting a non existing name into autocomplete path returns empty result.');
  359. $this->assertAutocompleteMenuPath($this->terms['term1']->{$this->fields['disabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], array(), 'Submitting a value for a field with disabled autocomplete behavior yields empty result.');
  360. $this->assertAutocompleteMenuPath(drupal_strtoupper(drupal_substr($this->entityLabel($this->terms['term1']), 1, -1)), array(
  361. $this->entityLabel($this->terms['term1']) => $this->entityLabel($this->terms['term1']),
  362. $this->entityLabel($this->terms['term1_longer_name']) => $this->entityLabel($this->terms['term1_longer_name']),
  363. ), 'Submitting a name similar to 2 existing term names yields both terms included in the autocomplete response.');
  364. $this->assertAutocompleteMenuPath($this->entityLabel($this->terms['term1']) . ', ' . drupal_strtoupper(drupal_substr($this->entityLabel($this->terms['term1']), 1, -1)), array(
  365. $this->entityLabel($this->terms['term1']) . ', ' . $this->entityLabel($this->terms['term1_longer_name']) => $this->entityLabel($this->terms['term1_longer_name']),
  366. ), 'Submitting one term already chosen along with a name similar to 2 existing term names yields only suggested a new term.');
  367. $this->assertAutocompleteMenuPath(drupal_strtoupper(drupal_substr($this->entityLabel($this->terms['no_synonyms']), 1, -1)), array(
  368. $this->entityLabel($this->terms['no_synonyms']) => $this->entityLabel($this->terms['no_synonyms']),
  369. ), 'Submitting a name similar to one existing term name into autocomplete path yields that term included.');
  370. $this->assertAutocompleteMenuPath(drupal_strtolower($this->entityLabel($this->terms['no_synonyms'])) . ', ' . drupal_strtoupper(drupal_substr($this->entityLabel($this->terms['no_synonyms']), 1, -1)), array(), 'Submitting the same term over again into autocomplete path yields no results.');
  371. $this->assertAutocompleteMenuPath($this->entityLabel($this->terms['one_synonym']) . ', ' . $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], array(), 'Submitting a synonym of a term over again into autocomplete path yields no results.');
  372. foreach (array('no_synonyms', 'one_synonym', 'two_synonyms') as $k) {
  373. $this->assertAutocompleteMenuPath(drupal_strtolower(drupal_substr($this->entityLabel($this->terms[$k]), 1, -1)), array(
  374. $this->entityLabel($this->terms[$k]) => $this->entityLabel($this->terms[$k]),
  375. ), 'Submitting a name similar to ' . $k . ' term into autocomplete path yields the term included.');
  376. $synonyms = field_get_items($this->behavior_implementation['entity_type'], $this->terms[$k], $this->fields['enabled']['field']['field_name']);
  377. if (is_array($synonyms)) {
  378. foreach ($synonyms as $delta => $item) {
  379. $this->assertAutocompleteMenuPath(drupal_strtolower(drupal_substr($item['value'], 1, -1)), array(
  380. $this->entityLabel($this->terms[$k]) => $this->synonymAutocompleteResult($this->terms[$k], $item['value']),
  381. ), 'Submitting a name similar to synonym#' . $delta . ' of the term ' . $k . ' into autocomplete path yields the term included.');
  382. }
  383. }
  384. }
  385. $this->assertAutocompleteMenuPath('one_term_name_another_synonym_', array(
  386. $this->entityLabel($this->terms['name_another_synonym']) => $this->entityLabel($this->terms['name_another_synonym']),
  387. $this->entityLabel($this->terms['synonym_another_name']) => $this->synonymAutocompleteResult($this->terms['synonym_another_name'], $this->terms['synonym_another_name']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  388. ), 'Submitting a name similar to name of one term and synonym of another into autocomplete path yields both terms included.');
  389. // Enabling another field in the autocomplete suggestions to make sure 2 and
  390. // more fields can participate in the action.
  391. $behavior_implementation = $this->behavior_implementation;
  392. $behavior_implementation['provider'] = synonyms_provider_field_provider_name($this->fields['disabled']['field']);
  393. synonyms_behavior_implementation_save($behavior_implementation);
  394. $this->terms['one_synonym']->{$this->fields['disabled']['field']['field_name']} = $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']};
  395. entity_save($this->behavior_implementation['entity_type'], $this->terms['one_synonym']);
  396. $this->assertAutocompleteMenuPath($this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], array(
  397. $this->entityLabel($this->terms['one_synonym']) => $this->synonymAutocompleteResult($this->terms['one_synonym'], $this->terms['one_synonym']->{$this->fields['disabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], $behavior_implementation),
  398. $this->entityLabel($this->terms['one_synonym']) . ' ' => $this->synonymAutocompleteResult($this->terms['one_synonym'], $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  399. ), 'Autocomplete works correctly when more than 1 field participates in the autocomplete behavior.');
  400. synonyms_behavior_implementation_delete($behavior_implementation);
  401. // Submit a name that is similar to one of our terms and is similar to a
  402. // a term from another vocabulary, which should not participate in the
  403. // autocomplete. We do this trick in different flavors, all with the idea
  404. // to make sure the bundles that should not participate in the field values
  405. // are not suggested by the autocomplete menu path. The different flavors
  406. // are:
  407. // - non valid term similar to valid term
  408. // - non valid term similar to valid synonym
  409. // - non valid synonym similar to valid term
  410. // - non valid synonym similar to valid synonym
  411. $instance = field_create_instance(array(
  412. 'field_name' => $this->fields['enabled']['field']['field_name'],
  413. 'entity_type' => $this->fields['enabled']['instance']['entity_type'],
  414. 'bundle' => 'another_vocabulary',
  415. ));
  416. $instance = field_info_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']);
  417. $behavior_implementation = $this->behavior_implementation;
  418. $behavior_implementation['bundle'] = $instance['bundle'];
  419. synonyms_behavior_implementation_save($behavior_implementation);
  420. $this->assertAutocompleteMenuPath($this->entityLabel($this->terms['no_synonyms']), array(
  421. $this->entityLabel($this->terms['no_synonyms']) => $this->entityLabel($this->terms['no_synonyms']),
  422. ), 'Submitting term name similar to term from another bundle does not include the term from another bundle.');
  423. $this->assertAutocompleteMenuPath($this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], array(
  424. $this->entityLabel($this->terms['one_synonym']) => $this->synonymAutocompleteResult($this->terms['one_synonym'], $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  425. ), 'Submitting synonym similar to term from another bundle does not include the term from another bundle.');
  426. $this->assertAutocompleteMenuPath($this->entityLabel($this->terms['no_synonyms']), array(
  427. $this->entityLabel($this->terms['no_synonyms']) => $this->entityLabel($this->terms['no_synonyms']),
  428. ), 'Submitting term name similar to a synonym of a term from another bundle does not include the term from another bundle.');
  429. $this->assertAutocompleteMenuPath($this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], array(
  430. $this->entityLabel($this->terms['one_synonym']) => $this->synonymAutocompleteResult($this->terms['one_synonym'], $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  431. ), 'Submitting synonym similar to a synonym of a term from another bundle does not include the term from another bundle.');
  432. }
  433. /**
  434. * Test autocomplete text field validation.
  435. *
  436. * In particular, this test does the following:
  437. * - test submitting a synonym into the text field (that should be converted
  438. * into its entity)
  439. * - test submitting the same entity name twice (only one reference should be
  440. * saved)
  441. * - test submitting entity name and one of its synonyms (only one reference
  442. * should be saved)
  443. * - test submitting 2 different synonyms of the same entity (only one
  444. * reference should be saved)
  445. */
  446. public function testAutocompleteTextField() {
  447. $this->drupalPost('node/add/synonyms-test-content', array(
  448. 'title' => $this->randomName(),
  449. $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . ']' => $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  450. ), 'Save');
  451. $this->assertText($this->entityLabel($this->terms['one_synonym']), 'Submitting a synonym into autocomplete text field results into term being saved.');
  452. $this->drupalPost('node/add/synonyms-test-content', array(
  453. 'title' => $this->randomName(),
  454. $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . ']' => drupal_implode_tags(array($this->entityLabel($this->terms['one_synonym']), $this->entityLabel($this->terms['one_synonym']))),
  455. ), 'Save');
  456. $this->assertUniqueText($this->entityLabel($this->terms['one_synonym']), 'Submitting the same term name twice into autocomplete text field results in saving the term only once in the field.');
  457. $this->drupalPost('node/add/synonyms-test-content', array(
  458. 'title' => $this->randomName(),
  459. $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . ']' => drupal_implode_tags(array($this->entityLabel($this->terms['one_synonym']), $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'])),
  460. ), 'Save');
  461. $this->assertUniqueText($this->entityLabel($this->terms['one_synonym']), 'Submitting term name and one of its synonyms results in saving the term only once in the field.');
  462. $this->drupalPost('node/add/synonyms-test-content', array(
  463. 'title' => $this->randomName(),
  464. $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . ']' => drupal_implode_tags(array($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][1]['value'])),
  465. ), 'Save');
  466. $this->assertUniqueText($this->entityLabel($this->terms['two_synonyms']), 'Submitting 2 different synonyms of the same term results in saving the term only once in the field.');
  467. }
  468. /**
  469. * Test 'Suggestions Size' setting of synonyms-friendly autocomplete widget.
  470. */
  471. public function testWidgetSettingsSuggestionSize() {
  472. $suggestion_size = 1;
  473. $this->reference_instance['widget']['settings']['suggestion_size'] = $suggestion_size;
  474. field_update_instance($this->reference_instance);
  475. // If size was bigger than 1, we'd get suggested 2 terms: 'term1' and
  476. // 'term1_longer_name'.
  477. $this->assertAutocompleteMenuPath($this->entityLabel($this->terms['term1']), array(
  478. $this->entityLabel($this->terms['term1']) => $this->entityLabel($this->terms['term1']),
  479. ), 'Suggestions Size option is respected in autocomplete widget for entity suggestion entries.');
  480. $this->assertAutocompleteMenuPath($this->entityLabel($this->terms['name_similar_synonym']), array(
  481. $this->entityLabel($this->terms['name_similar_synonym']) => $this->entityLabel($this->terms['name_similar_synonym']),
  482. ), 'Suggestions Size option is respected in autocomplete widget for entity and synonym suggestion entries.');
  483. $this->assertAutocompleteMenuPath(drupal_substr($this->terms['similar_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], 0, 8), array(
  484. $this->entityLabel($this->terms['similar_synonyms']) => $this->synonymAutocompleteResult($this->terms['similar_synonyms'], $this->terms['similar_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  485. ), 'Suggestions Size option is respected in autocomplete widget for synonyms suggestion entries.');
  486. $this->assertAutocompleteMenuPath('one_term_name_another_synonym_', array(
  487. $this->entityLabel($this->terms['name_another_synonym'])=> $this->entityLabel($this->terms['name_another_synonym']),
  488. ), 'Suggestions Size option is respected in autocomplete widget for the case when there is match by entity name and by synonyms; and preference is given to the match by entity name.');
  489. }
  490. /**
  491. * Test 'Suggest only one entry per term' setting of autocomplete widget.
  492. */
  493. public function testWidgetSettingsSuggestOnlyUnique() {
  494. // Testing disabled "Suggest only one entry per term" setting.
  495. $this->assertAutocompleteMenuPath($this->entityLabel($this->terms['name_similar_synonym']), array(
  496. $this->entityLabel($this->terms['name_similar_synonym']) => $this->entityLabel($this->terms['name_similar_synonym']),
  497. $this->entityLabel($this->terms['name_similar_synonym']) . ' ' => $this->synonymAutocompleteResult($this->terms['name_similar_synonym'], $this->terms['name_similar_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  498. ), 'Both term and its synonym are shown when "Suggest only one entry per term" is off.');
  499. $this->assertAutocompleteMenuPath(drupal_substr($this->terms['similar_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], 0, 8), array(
  500. $this->entityLabel($this->terms['similar_synonyms']) => $this->synonymAutocompleteResult($this->terms['similar_synonyms'], $this->terms['similar_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  501. $this->entityLabel($this->terms['similar_synonyms']). ' ' => $this->synonymAutocompleteResult($this->terms['similar_synonyms'], $this->terms['similar_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value']),
  502. ), 'Multiple synonyms are shown when "Suggest only one entry per term" is off.');
  503. // Testing enabled "Suggest only one entry per term" setting.
  504. $this->reference_instance['widget']['settings']['suggest_only_unique'] = TRUE;
  505. field_update_instance($this->reference_instance);
  506. $this->assertAutocompleteMenuPath($this->entityLabel($this->terms['name_similar_synonym']), array(
  507. $this->entityLabel($this->terms['name_similar_synonym']) => $this->entityLabel($this->terms['name_similar_synonym']),
  508. ), 'Only term is shown and synonym is not shown when "Suggest only one entry per term" is on.');
  509. $this->assertAutocompleteMenuPath(drupal_substr($this->terms['similar_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'], 0, 8), array(
  510. $this->entityLabel($this->terms['similar_synonyms']) => $this->synonymAutocompleteResult($this->terms['similar_synonyms'], $this->terms['similar_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  511. ), 'Only single synonym is shown when "Suggest only one entry per term" is on.');
  512. }
  513. /**
  514. * Assert output of synonym friendly autocomplete path.
  515. *
  516. * @param string $input
  517. * String of input to supply to the autocomplete path
  518. * @param array $standard
  519. * Expected output from the autocomplete path. Supply it as an associative
  520. * array
  521. * @param string $message
  522. * Drupal assertion message to be displayed on the rest results page
  523. */
  524. protected function assertAutocompleteMenuPath($input, $standard, $message) {
  525. $response = $this->drupalGet($this->reference_instance['widget']['settings']['synonyms_autocomplete_path'] . '/' . $this->reference_field['field_name'] . '/' . $this->entity_type . '/' . $this->bundle . '/' . $input);
  526. if (!$response) {
  527. $this->fail($message, 'Autocomplete Menu Path');
  528. return;
  529. }
  530. $response = (array) json_decode($response);
  531. $is_the_same = count($response) == count($standard);
  532. $is_the_same = $is_the_same && count(array_intersect_assoc($response, $standard)) == count($standard);
  533. $this->assertTrue($is_the_same, $message, 'Autocomplete Menu Path');
  534. }
  535. /**
  536. * Return expected autocomplete menu path result.
  537. *
  538. * The result is prepared as if the entity was found by the supplied synonym.
  539. *
  540. * @param object $entity
  541. * Fully loaded entity for which the result is generated.
  542. * @param string $synonym
  543. * Synonym by which the entity was hit in the search
  544. * @param array $behavior_implementation
  545. * Behavior implementation array which the $synonym originates from. If
  546. * omitted, standard $this->behavior_implementation is considered
  547. *
  548. * @return string
  549. * Formatted autocomplete result
  550. */
  551. protected function synonymAutocompleteResult($entity, $synonym, $behavior_implementation = NULL) {
  552. if (!$behavior_implementation) {
  553. $behavior_implementation = $this->behavior_implementation;
  554. }
  555. $provider = synonyms_behavior_implementation_info($behavior_implementation['entity_type'], $behavior_implementation['bundle'], $behavior_implementation['behavior']);
  556. $provider = $provider[$behavior_implementation['provider']];
  557. return format_string($behavior_implementation['settings']['wording'], array(
  558. '@synonym' => $synonym,
  559. '@entity' => entity_label($behavior_implementation['entity_type'], $entity),
  560. '@field_name' => drupal_strtolower($provider['label']),
  561. ));
  562. }
  563. /**
  564. * Supportive method to retrieve label of a provided entity.
  565. *
  566. * @param object $entity
  567. * Entity, whose label should be returned
  568. *
  569. * @return string
  570. * Label of the provided entity
  571. */
  572. protected function entityLabel($entity) {
  573. return entity_label($this->behavior_implementation['entity_type'], $entity);
  574. }
  575. /**
  576. * Method to initiate all necessary terms for testing.
  577. */
  578. protected function createTerms() {
  579. $name = $this->randomName();
  580. $term = (object) array(
  581. 'vid' => $this->vocabulary->vid,
  582. 'name' => $name,
  583. $this->fields['disabled']['field']['field_name'] => array(
  584. LANGUAGE_NONE => array(
  585. array('value' => $this->randomName()),
  586. ),
  587. ),
  588. );
  589. taxonomy_term_save($term);
  590. $this->terms['term1'] = $term;
  591. $name .= $this->randomName();
  592. $term = (object) array(
  593. 'vid' => $this->vocabulary->vid,
  594. 'name' => $name,
  595. $this->fields['disabled']['field']['field_name'] => array(
  596. LANGUAGE_NONE => array(
  597. array('value' => $this->randomName()),
  598. ),
  599. ),
  600. );
  601. taxonomy_term_save($term);
  602. $this->terms['term1_longer_name'] = $term;
  603. $term = (object) array(
  604. 'vid' => $this->vocabulary->vid,
  605. 'name' => $this->randomName(),
  606. $this->fields['disabled']['field']['field_name'] => array(
  607. LANGUAGE_NONE => array(
  608. array('value' => $this->randomName()),
  609. ),
  610. ),
  611. );
  612. taxonomy_term_save($term);
  613. $this->terms['no_synonyms'] = $term;
  614. $term = (object) array(
  615. 'vid' => $this->vocabulary->vid,
  616. 'name' => $this->randomName(),
  617. $this->fields['enabled']['field']['field_name'] => array(
  618. LANGUAGE_NONE => array(
  619. array('value' => $this->randomName()),
  620. ),
  621. ),
  622. $this->fields['disabled']['field']['field_name'] => array(
  623. LANGUAGE_NONE => array(
  624. array('value' => $this->randomName()),
  625. ),
  626. ),
  627. );
  628. taxonomy_term_save($term);
  629. $this->terms['one_synonym'] = $term;
  630. $term = (object) array(
  631. 'vid' => $this->vocabulary->vid,
  632. 'name' => $this->randomName(),
  633. $this->fields['enabled']['field']['field_name'] => array(
  634. LANGUAGE_NONE => array(
  635. array('value' => $this->randomName()),
  636. array('value' => $this->randomName()),
  637. ),
  638. ),
  639. $this->fields['disabled']['field']['field_name'] => array(
  640. LANGUAGE_NONE => array(
  641. array('value' => $this->randomName()),
  642. ),
  643. ),
  644. );
  645. taxonomy_term_save($term);
  646. $this->terms['two_synonyms'] = $term;
  647. $name = $this->randomName();
  648. $term = (object) array(
  649. 'vid' => $this->vocabulary->vid,
  650. 'name' => $name,
  651. $this->fields['enabled']['field']['field_name'] => array(
  652. LANGUAGE_NONE => array(
  653. array('value' => $name . $this->randomName()),
  654. ),
  655. ),
  656. $this->fields['disabled']['field']['field_name'] => array(
  657. LANGUAGE_NONE => array(
  658. array('value' => $this->randomName()),
  659. ),
  660. ),
  661. );
  662. taxonomy_term_save($term);
  663. $this->terms['name_similar_synonym'] = $term;
  664. $name = 'similar_synonyms_';
  665. $term = (object) array(
  666. 'vid' => $this->vocabulary->vid,
  667. 'name' => $this->randomName(),
  668. $this->fields['enabled']['field']['field_name'] => array(
  669. LANGUAGE_NONE => array(
  670. array('value' => $name . $this->randomName()),
  671. array('value' => $name . $this->randomName()),
  672. ),
  673. ),
  674. $this->fields['disabled']['field']['field_name'] => array(
  675. LANGUAGE_NONE => array(
  676. array('value' => $this->randomName()),
  677. ),
  678. ),
  679. );
  680. taxonomy_term_save($term);
  681. $this->terms['similar_synonyms'] = $term;
  682. $name = 'one_term_name_another_synonym_';
  683. $term = (object) array(
  684. 'vid' => $this->vocabulary->vid,
  685. 'name' => $name . $this->randomName(),
  686. $this->fields['disabled']['field']['field_name'] => array(
  687. LANGUAGE_NONE => array(
  688. array('value' => $this->randomName()),
  689. ),
  690. ),
  691. );
  692. taxonomy_term_save($term);
  693. $this->terms['name_another_synonym'] = $term;
  694. $term = (object) array(
  695. 'vid' => $this->vocabulary->vid,
  696. 'name' => $this->randomName(),
  697. $this->fields['enabled']['field']['field_name'] => array(
  698. LANGUAGE_NONE => array(
  699. array('value' => $name . $this->randomName()),
  700. ),
  701. ),
  702. $this->fields['disabled']['field']['field_name'] => array(
  703. LANGUAGE_NONE => array(
  704. array('value' => $this->randomName()),
  705. ),
  706. ),
  707. );
  708. taxonomy_term_save($term);
  709. $this->terms['synonym_another_name'] = $term;
  710. $another_vocabulary = (object) array(
  711. 'name' => $this->randomName(),
  712. 'machine_name' => 'another_vocabulary',
  713. );
  714. taxonomy_vocabulary_save($another_vocabulary);
  715. $term_similar_term = (object) array(
  716. 'name' => $this->terms['no_synonyms']->name,
  717. 'vid' => $another_vocabulary->vid,
  718. );
  719. taxonomy_term_save($term_similar_term);
  720. $term_similar_synonym = (object) array(
  721. 'name' => $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  722. 'vid' => $another_vocabulary->vid,
  723. );
  724. taxonomy_term_save($term_similar_synonym);
  725. $synonym_similar_term = (object) array(
  726. 'name' => $this->randomName(),
  727. 'vid' => $another_vocabulary->vid,
  728. $this->fields['enabled']['field']['field_name'] => array(LANGUAGE_NONE => array(
  729. $this->terms['no_synonyms']->name,
  730. )),
  731. );
  732. taxonomy_term_save($synonym_similar_term);
  733. $synonym_similar_synonym = (object) array(
  734. 'name' => $this->randomName(),
  735. 'vid' => $another_vocabulary->vid,
  736. $this->fields['enabled']['field']['field_name'] => array(LANGUAGE_NONE => array(
  737. $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  738. )),
  739. );
  740. taxonomy_term_save($synonym_similar_synonym);
  741. }
  742. }
  743. /**
  744. * Test synonyms friendly autocomplete widget for taxonomy term reference field.
  745. */
  746. class TaxonomyTermReferenceAutocompleteSynonymsWebTestCase extends AbstractAutocompleteSynonymsWebTestCase {
  747. /**
  748. * GetInfo method.
  749. */
  750. public static function getInfo() {
  751. return array(
  752. 'name' => 'Taxonomy synonyms autocomplete',
  753. 'description' => 'Ensure that the "synonym friendly autocomplete" widget works correctly with taxonomy term reference field type.',
  754. 'group' => 'Synonyms',
  755. );
  756. }
  757. public function setUp($modules = array()) {
  758. parent::setUp($modules);
  759. $this->reference_field = array(
  760. 'type' => 'taxonomy_term_reference',
  761. 'field_name' => 'synonyms_term_enabled',
  762. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  763. 'settings' => array(
  764. 'allowed_values' => array(
  765. array(
  766. 'vocabulary' => $this->vocabulary->machine_name,
  767. 'parent' => 0,
  768. ),
  769. ),
  770. ),
  771. );
  772. $this->reference_field = field_create_field($this->reference_field);
  773. $this->reference_instance = array(
  774. 'field_name' => $this->reference_field['field_name'],
  775. 'entity_type' => 'node',
  776. 'bundle' => $this->bundle,
  777. 'label' => 'Synonym Terms Autcomplete',
  778. 'widget' => array(
  779. 'type' => 'synonyms_autocomplete_taxonomy_term',
  780. ),
  781. );
  782. $this->reference_instance['widget']['settings'] = field_info_widget_settings($this->reference_instance['widget']['type']);
  783. $this->reference_instance = field_create_instance($this->reference_instance);
  784. }
  785. /**
  786. * Test auto-creation functionality.
  787. *
  788. * Test the auto-creation functionality of the synonym friendly autocomplete
  789. * widget type for taxonomy term reference field type.
  790. */
  791. public function testAutoCreation() {
  792. // Trying enabled auto creation.
  793. $this->reference_instance['widget']['settings']['auto_creation'] = TRUE;
  794. field_update_instance($this->reference_instance);
  795. $new_term_name = $this->terms['no_synonyms']->{$this->fields['disabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'];
  796. $this->drupalPost('node/add/synonyms-test-content', array(
  797. 'title' => $this->randomName(),
  798. 'synonyms_term_enabled[' . LANGUAGE_NONE . ']' => $this->terms['no_synonyms']->name . ', ' . $new_term_name . ', ' . $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  799. ), 'Save');
  800. $this->assertText($this->terms['no_synonyms']->name, 'Existing term was assigned to the new node');
  801. $this->assertText($new_term_name, 'Auto created term was assigned to the new node when Auto creation is on.');
  802. $this->assertText($this->terms['one_synonym']->name, 'Submitting a synonym into autocomplete widget results in the term, to which the synonym belongs, being assigned to the just created entity (when Auto creation is on).');
  803. $term = $this->getLastTerm($this->vocabulary);
  804. $this->assertEqual($term->name, $new_term_name, 'The auto created term has been created when Auto creation is on.');
  805. // Trying disabled auto creation.
  806. $this->reference_instance['widget']['settings']['auto_creation'] = FALSE;
  807. field_update_instance($this->reference_instance);
  808. $new_term_name = $this->terms['term1']->{$this->fields['disabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'];
  809. $this->drupalPost('node/add/synonyms-test-content', array(
  810. 'title' => $this->randomName(),
  811. 'synonyms_term_enabled[' . LANGUAGE_NONE . ']' => $this->terms['no_synonyms']->name . ', ' . $new_term_name . ', ' . $this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  812. ), 'Save');
  813. $this->assertText($this->terms['no_synonyms']->name, 'Existing term was assigned to the new node');
  814. $this->assertNoText($new_term_name, 'Auto created term was not assigned to the new node when Auto creation is off.');
  815. $this->assertText($this->terms['one_synonym']->name, 'Submitting a synonym into autocomplete widget results in the term, to which the synonym belongs, being assigned to the just created entity (when Auto creation is off).');
  816. $term = $this->getLastTerm($this->vocabulary);
  817. $this->assertNotEqual($term->name, $new_term_name, 'The auto created term has not been created when Auto creation is off.');
  818. }
  819. }
  820. /**
  821. * Test synonyms friendly autocomplete widget for entity reference field type.
  822. */
  823. class EntityReferenceAutocompleteSynonymsWebTestCase extends AbstractAutocompleteSynonymsWebTestCase {
  824. /**
  825. * GetInfo method.
  826. */
  827. public static function getInfo() {
  828. return array(
  829. 'name' => 'Entity reference synonyms autocomplete',
  830. 'description' => 'Ensure that the "synonym friendly autocomplete" widget works correctly with entityreference field type.',
  831. 'group' => 'Synonyms',
  832. );
  833. }
  834. public function setUp($modules = array()) {
  835. $modules[] = 'entityreference';
  836. parent::setUp($modules);
  837. $this->reference_field = array(
  838. 'type' => 'entityreference',
  839. 'field_name' => 'synonyms_term_enabled',
  840. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  841. 'settings' => array(
  842. 'target_type' => 'taxonomy_term',
  843. 'handler_settings' => array(
  844. 'target_bundles' => drupal_map_assoc(array($this->fields['enabled']['instance']['bundle'])),
  845. ),
  846. ),
  847. );
  848. $this->reference_field = field_create_field($this->reference_field);
  849. $this->reference_instance = array(
  850. 'field_name' => $this->reference_field['field_name'],
  851. 'entity_type' => 'node',
  852. 'bundle' => $this->bundle,
  853. 'label' => 'Synonym Entity Autcomplete',
  854. 'widget' => array(
  855. 'type' => 'synonyms_autocomplete_entity',
  856. ),
  857. );
  858. $this->reference_instance['widget']['settings'] = field_info_widget_settings($this->reference_instance['widget']['type']);
  859. $this->reference_instance = field_create_instance($this->reference_instance);
  860. }
  861. }
  862. /**
  863. * Test "Synonyms friendly select" widget of Synonyms module.
  864. */
  865. abstract class AbstractSelectSynonymsWebTestCase extends SynonymsWebTestCase {
  866. /**
  867. * Array of fully loaded taxonomy term entities to be used in this test.
  868. *
  869. * @var array
  870. */
  871. protected $terms = array();
  872. /**
  873. * Entity type to which a field with tested widget is attached.
  874. *
  875. * @var string
  876. */
  877. protected $entity_type = 'node';
  878. /**
  879. * Bundle to which a field with tested widget is attached.
  880. *
  881. * @var string
  882. */
  883. protected $bundle = 'synonyms_test_content';
  884. /**
  885. * Field definition array of the field that will be attached to
  886. * $this->entity_type with synonyms-friendly select widget.
  887. *
  888. * @var array
  889. */
  890. protected $reference_field = array();
  891. /**
  892. * Field instance definition array of the field that will be attached to
  893. * $this->entity_type with synonyms friendly select widget.
  894. *
  895. * @var array
  896. */
  897. protected $reference_instance = array();
  898. public function setUp($modules = array()) {
  899. $this->behavior_implementation['behavior'] = 'select';
  900. $this->behavior_implementation['settings'] = array(
  901. 'wording' => '@synonym @entity @field_name',
  902. );
  903. parent::setUp($modules);
  904. // Creating a test content type.
  905. $this->drupalPost('admin/structure/types/add', array(
  906. 'name' => 'Synonyms Test Content',
  907. 'type' => $this->bundle,
  908. ), 'Save content type');
  909. $this->createTerms();
  910. }
  911. /**
  912. * Test sorting options of the widget.
  913. */
  914. abstract public function testWidgetSorting();
  915. /**
  916. * Test main functionality of the widget.
  917. */
  918. abstract public function testWidget();
  919. /**
  920. * Assert correctness of the synonyms-friendly select widget.
  921. *
  922. * @param array $options
  923. * Array of what options must be present in the select form element. It
  924. * should consist of arrays that follow such structure:
  925. * - entity: (object) Entity this option represents
  926. * - synonym: (string) If the option comes from a synonym, then include it
  927. * here
  928. * - selected: (bool) Place here TRUE if this option should be selected by
  929. * default
  930. * @param string $message
  931. * Assert message that will be passed on to SimpleTest internals
  932. */
  933. protected function assertSynonymsSelect($options, $message = '') {
  934. $multiple = $this->reference_field['cardinality'] > 1 || $this->reference_field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
  935. $element = array(
  936. '#options' => array(),
  937. '#value' => $multiple ? array() : 'nothing',
  938. );
  939. if (!$multiple) {
  940. $element['#options'][''] = t('- None -');
  941. }
  942. foreach ($options as $v) {
  943. if (!isset($v['synonym'])) {
  944. $v['synonym'] = NULL;
  945. }
  946. $key = $this->synonymSelectKey($v['entity'], $v['synonym']);
  947. $label = entity_label($this->behavior_implementation['entity_type'], $v['entity']);
  948. if ($v['synonym']) {
  949. $provider = synonyms_behavior_implementation_info($this->behavior_implementation['entity_type'], $this->behavior_implementation['bundle'], $this->behavior_implementation['behavior']);
  950. $provider = $provider[$this->behavior_implementation['provider']];
  951. $label = format_string($this->behavior_implementation['settings']['wording'], array(
  952. '@synonym' => $v['synonym'],
  953. '@entity'=> $label,
  954. '@field_name' => $provider['label'],
  955. ));
  956. }
  957. if (isset($v['selected']) && $v['selected']) {
  958. if ($multiple) {
  959. $element['#value'][] = $key;
  960. }
  961. else {
  962. $element['#value'] = $key;
  963. }
  964. }
  965. $element['#options'][$key] = $this->synonymsSelectOptionPrefix($v['entity'], $v['synonym']) . $label;
  966. }
  967. $this->assertRaw('>' . form_select_options($element) . '</select>', $message, 'Synonyms friendly select');
  968. }
  969. /**
  970. * Generate necessary prefix before the main wording of the label in select.
  971. *
  972. * It may be useful to insert some kind of hierarchy indention, for example.
  973. *
  974. * @param object $entity
  975. * Fully loaded entity object whose prefix is requested in the option label
  976. * of the synonyms friendly select widget
  977. * @param string $synonym
  978. * If this option comes from a synonym, that synonym will be supplied here
  979. *
  980. * @return string
  981. * Any suitable prefix before the main wording of the option for this
  982. * particular option
  983. */
  984. protected function synonymsSelectOptionPrefix($entity, $synonym = NULL) {
  985. return '';
  986. }
  987. /**
  988. * Form a key for the option of a synonyms friendly select.
  989. *
  990. * @param object $entity
  991. * Fully loaded entity for which to generate the key
  992. * @param string $synonym
  993. * If the option, whose key is being generated, comes from a synonym, then
  994. * supply it here
  995. *
  996. * @return string
  997. * Key for the option of a synonym friendly select
  998. */
  999. protected function synonymSelectKey($entity, $synonym = NULL) {
  1000. $entity_id = entity_extract_ids($this->behavior_implementation['entity_type'], $entity);
  1001. $key = $entity_id[0];
  1002. if ($synonym) {
  1003. $key .= ':' . drupal_html_class($synonym);
  1004. }
  1005. return $key;
  1006. }
  1007. /**
  1008. * Method to initiate all necessary terms for testing.
  1009. */
  1010. protected function createTerms() {
  1011. $this->terms['parent_term'] = (object) array(
  1012. 'vid' => $this->vocabulary->vid,
  1013. 'name' => 'Parent Term',
  1014. $this->fields['enabled']['field']['field_name'] => array(
  1015. LANGUAGE_NONE => array(
  1016. array('value' => 'Parent TermA' . $this->randomName()),
  1017. array('value' => 'Parent TermZ' . $this->randomName()),
  1018. ),
  1019. ),
  1020. );
  1021. taxonomy_term_save($this->terms['parent_term']);
  1022. $this->terms['child_term'] = (object) array(
  1023. 'vid' => $this->vocabulary->vid,
  1024. 'name' => 'Child Term',
  1025. 'parent' => $this->terms['parent_term']->tid,
  1026. $this->fields['enabled']['field']['field_name'] => array(
  1027. LANGUAGE_NONE => array(
  1028. array('value' => 'Child TermZ' . $this->randomName()),
  1029. array('value' => 'Child TermA' . $this->randomName()),
  1030. ),
  1031. ),
  1032. );
  1033. taxonomy_term_save($this->terms['child_term']);
  1034. $this->terms['normal_term'] = (object) array(
  1035. 'vid' => $this->vocabulary->vid,
  1036. 'name' => 'Normal Term',
  1037. $this->fields['enabled']['field']['field_name'] => array(
  1038. LANGUAGE_NONE => array(
  1039. array('value' => 'Normal TermA' . $this->randomName()),
  1040. array('value' => 'Normal TermZ' . $this->randomName()),
  1041. ),
  1042. ),
  1043. );
  1044. taxonomy_term_save($this->terms['normal_term']);
  1045. }
  1046. }
  1047. /**
  1048. * Test synonyms friendly select widget for taxonomy term reference field.
  1049. */
  1050. class TaxonomyTermReferenceSelectSynonymsWebTestCase extends AbstractSelectSynonymsWebTestCase {
  1051. public static function getInfo() {
  1052. return array(
  1053. 'name' => 'Taxonomy synonyms select',
  1054. 'description' => 'Ensure that the "synonym friendly select" widget works correctly with taxonomy term reference field.',
  1055. 'group' => 'Synonyms',
  1056. );
  1057. }
  1058. public function setUp($modules = array()) {
  1059. parent::setUp($modules);
  1060. $this->reference_field = array(
  1061. 'type' => 'taxonomy_term_reference',
  1062. 'field_name' => 'synonyms_term',
  1063. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1064. 'settings' => array(
  1065. 'allowed_values' => array(
  1066. array(
  1067. 'vocabulary' => $this->vocabulary->machine_name,
  1068. 'parent' => 0,
  1069. ),
  1070. ),
  1071. ),
  1072. );
  1073. $this->reference_field = field_create_field($this->reference_field);
  1074. $this->reference_instance = array(
  1075. 'field_name' => $this->reference_field['field_name'],
  1076. 'entity_type' => 'node',
  1077. 'bundle' => $this->bundle,
  1078. 'label' => 'Synonym Terms Select',
  1079. 'widget' => array(
  1080. 'type' => 'synonyms_select_taxonomy_term',
  1081. ),
  1082. );
  1083. $this->reference_instance = field_create_instance($this->reference_instance);
  1084. }
  1085. public function testWidgetSorting() {
  1086. $cardinality = array(
  1087. 1 => 1,
  1088. FIELD_CARDINALITY_UNLIMITED => 'unlimited',
  1089. );
  1090. $required = array(
  1091. TRUE => 'required',
  1092. FALSE => 'not required',
  1093. );
  1094. foreach ($cardinality as $cardinality_k => $cardinality_v) {
  1095. foreach ($required as $required_k => $required_v) {
  1096. $this->reference_field['cardinality'] = $cardinality_k;
  1097. field_update_field($this->reference_field);
  1098. $this->reference_instance['required'] = $required_k;
  1099. $this->reference_instance['widget']['settings']['sort'] = 'weight';
  1100. field_update_instance($this->reference_instance);
  1101. $this->terms['parent_term']->weight = 0;
  1102. taxonomy_term_save($this->terms['parent_term']);
  1103. $options = array();
  1104. $options[] = array(
  1105. 'entity' => $this->terms['normal_term'],
  1106. );
  1107. $options[] = array(
  1108. 'entity' => $this->terms['normal_term'],
  1109. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1110. );
  1111. $options[] = array(
  1112. 'entity' => $this->terms['normal_term'],
  1113. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1114. );
  1115. $options[] = array(
  1116. 'entity' => $this->terms['parent_term'],
  1117. );
  1118. $options[] = array(
  1119. 'entity' => $this->terms['parent_term'],
  1120. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1121. );
  1122. $options[] = array(
  1123. 'entity' => $this->terms['parent_term'],
  1124. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1125. );
  1126. $options[] = array(
  1127. 'entity' => $this->terms['child_term'],
  1128. );
  1129. $options[] = array(
  1130. 'entity' => $this->terms['child_term'],
  1131. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1132. );
  1133. $options[] = array(
  1134. 'entity' => $this->terms['child_term'],
  1135. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1136. );
  1137. $this->drupalGet('node/add/synonyms-test-content');
  1138. $this->assertSynonymsSelect($options, 'Synonyms select sorting by weight works for the cardinality of ' . $cardinality_v . ' and ' . $required_v);
  1139. $this->terms['parent_term']->weight = -1000;
  1140. taxonomy_term_save($this->terms['parent_term']);
  1141. $options = array();
  1142. $options[] = array(
  1143. 'entity' => $this->terms['parent_term'],
  1144. );
  1145. $options[] = array(
  1146. 'entity' => $this->terms['parent_term'],
  1147. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1148. );
  1149. $options[] = array(
  1150. 'entity' => $this->terms['parent_term'],
  1151. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1152. );
  1153. $options[] = array(
  1154. 'entity' => $this->terms['child_term'],
  1155. );
  1156. $options[] = array(
  1157. 'entity' => $this->terms['child_term'],
  1158. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1159. );
  1160. $options[] = array(
  1161. 'entity' => $this->terms['child_term'],
  1162. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1163. );
  1164. $options[] = array(
  1165. 'entity' => $this->terms['normal_term'],
  1166. );
  1167. $options[] = array(
  1168. 'entity' => $this->terms['normal_term'],
  1169. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1170. );
  1171. $options[] = array(
  1172. 'entity' => $this->terms['normal_term'],
  1173. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1174. );
  1175. $this->drupalGet('node/add/synonyms-test-content');
  1176. $this->assertSynonymsSelect($options, 'Synonyms select sorting by weight works after changing weights of terms for the cardinality of ' . $cardinality_v . ' and ' . $required_v);
  1177. $this->reference_instance['widget']['settings']['sort'] = 'name';
  1178. field_update_instance($this->reference_instance);
  1179. $options = array();
  1180. $options[] = array(
  1181. 'entity' => $this->terms['normal_term'],
  1182. );
  1183. $options[] = array(
  1184. 'entity' => $this->terms['normal_term'],
  1185. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1186. );
  1187. $options[] = array(
  1188. 'entity' => $this->terms['normal_term'],
  1189. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1190. );
  1191. $options[] = array(
  1192. 'entity' => $this->terms['parent_term'],
  1193. );
  1194. $options[] = array(
  1195. 'entity' => $this->terms['child_term'],
  1196. );
  1197. $options[] = array(
  1198. 'entity' => $this->terms['child_term'],
  1199. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1200. );
  1201. $options[] = array(
  1202. 'entity' => $this->terms['child_term'],
  1203. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1204. );
  1205. $options[] = array(
  1206. 'entity' => $this->terms['parent_term'],
  1207. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1208. );
  1209. $options[] = array(
  1210. 'entity' => $this->terms['parent_term'],
  1211. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1212. );
  1213. $this->drupalGet('node/add/synonyms-test-content');
  1214. $this->assertSynonymsSelect($options, 'Synonyms select sorting by name works for the cardinality of ' . $cardinality_v . ' and ' . $required_v);
  1215. }
  1216. }
  1217. }
  1218. public function testWidget() {
  1219. $name = $this->randomName();
  1220. $this->drupalPost('node/add/synonyms-test-content', array(
  1221. 'title' => $name,
  1222. $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . '][]' => array(
  1223. $this->synonymSelectKey($this->terms['parent_term'], $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  1224. $this->terms['child_term']->tid,
  1225. $this->terms['normal_term']->tid,
  1226. $this->synonymSelectKey($this->terms['normal_term'], $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  1227. ),
  1228. ), 'Save');
  1229. $node = $this->drupalGetNodeByTitle($name);
  1230. $this->drupalGet('node/' . $node->nid);
  1231. $this->assertText($this->terms['parent_term']->name, 'Term is saved when its synonym is submitted through synonyms friendly select for the unlimited cardinality.');
  1232. $this->assertText($this->terms['child_term']->name, 'Term is saved when it is submitted through synonyms friendly select for the unlimited cardinality.');
  1233. $this->assertUniqueText($this->terms['normal_term']->name, 'Term is saved only once when the term and its synonym are submitted through synonyms friendly select for the unlimited cardinality.');
  1234. $options = array();
  1235. $options[] = array(
  1236. 'entity' => $this->terms['normal_term'],
  1237. 'selected' => TRUE,
  1238. );
  1239. $options[] = array(
  1240. 'entity' => $this->terms['normal_term'],
  1241. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1242. );
  1243. $options[] = array(
  1244. 'entity' => $this->terms['normal_term'],
  1245. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1246. );
  1247. $options[] = array(
  1248. 'entity' => $this->terms['parent_term'],
  1249. 'selected' => TRUE,
  1250. );
  1251. $options[] = array(
  1252. 'entity' => $this->terms['parent_term'],
  1253. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1254. );
  1255. $options[] = array(
  1256. 'entity' => $this->terms['parent_term'],
  1257. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1258. );
  1259. $options[] = array(
  1260. 'entity' => $this->terms['child_term'],
  1261. 'selected' => TRUE,
  1262. );
  1263. $options[] = array(
  1264. 'entity' => $this->terms['child_term'],
  1265. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1266. );
  1267. $options[] = array(
  1268. 'entity' => $this->terms['child_term'],
  1269. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1270. );
  1271. $this->drupalGet('node/' . $node->nid . '/edit');
  1272. $this->assertSynonymsSelect($options, 'Default values are set correctly in the synonyms friendly select widget when working with field cardinality more than 1.');
  1273. $this->reference_field['cardinality'] = 2;
  1274. field_update_field($this->reference_field);
  1275. $name = $this->randomName();
  1276. $this->drupalPost('node/add/synonyms-test-content', array(
  1277. 'title' => $name,
  1278. $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . '][]' => array(
  1279. $this->synonymSelectKey($this->terms['parent_term'], $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  1280. $this->terms['child_term']->tid,
  1281. $this->terms['normal_term']->tid,
  1282. ),
  1283. ), 'Save');
  1284. $this->assertText('this field cannot hold more than 2 values.', 'Submitting 3 entries into a field with cardinality of 2, that refer to 3 terms, results in a form error.');
  1285. $this->drupalPost('node/add/synonyms-test-content', array(
  1286. 'title' => $name,
  1287. $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . '][]' => array(
  1288. $this->synonymSelectKey($this->terms['parent_term'], $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  1289. $this->terms['normal_term']->tid,
  1290. $this->synonymSelectKey($this->terms['normal_term'], $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  1291. ),
  1292. ), 'Save');
  1293. $node = $this->drupalGetNodeByTitle($name);
  1294. $this->drupalGet('node/' . $node->nid);
  1295. $this->assertUniqueText($this->terms['parent_term']->name, 'Submitting 3 entries into a field with cardinality of 2, that refer to only 2 terms, results in form getting submitted. Term #1 is saved.');
  1296. $this->assertUniqueText($this->terms['normal_term']->name, 'Term #2 is saved.');
  1297. $this->reference_field['cardinality'] = 1;
  1298. field_update_field($this->reference_field);
  1299. $name = $this->randomName();
  1300. $this->drupalPost('node/add/synonyms-test-content', array(
  1301. 'title' => $name,
  1302. $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . ']' => $this->synonymSelectKey($this->terms['parent_term'], $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  1303. ), 'Save');
  1304. $node = $this->drupalGetNodeByTitle($name);
  1305. $this->drupalGet('node/' . $node->nid);
  1306. $this->assertText($this->terms['parent_term']->name, 'Term is saved when its synonym is submitted through synonyms friendly select for the cardinality of 1.');
  1307. $options = array();
  1308. $options[] = array(
  1309. 'entity' => $this->terms['normal_term'],
  1310. );
  1311. $options[] = array(
  1312. 'entity' => $this->terms['normal_term'],
  1313. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1314. );
  1315. $options[] = array(
  1316. 'entity' => $this->terms['normal_term'],
  1317. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1318. );
  1319. $options[] = array(
  1320. 'entity' => $this->terms['parent_term'],
  1321. 'selected' => TRUE,
  1322. );
  1323. $options[] = array(
  1324. 'entity' => $this->terms['parent_term'],
  1325. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1326. );
  1327. $options[] = array(
  1328. 'entity' => $this->terms['parent_term'],
  1329. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1330. );
  1331. $options[] = array(
  1332. 'entity' => $this->terms['child_term'],
  1333. );
  1334. $options[] = array(
  1335. 'entity' => $this->terms['child_term'],
  1336. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1337. );
  1338. $options[] = array(
  1339. 'entity' => $this->terms['child_term'],
  1340. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1341. );
  1342. $this->drupalGet('node/' . $node->nid . '/edit');
  1343. $this->assertSynonymsSelect($options, 'Default values are set correctly in the synonyms friendly select widget when working with the field cardinality of 1.');
  1344. $this->drupalPost('node/' . $node->nid . '/edit', array(
  1345. $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . ']' => $this->terms['child_term']->tid,
  1346. ), 'Save');
  1347. $this->drupalGet('node/' . $node->nid);
  1348. $this->assertNoText($this->terms['parent_term']->name, 'After updating entity the old term is removed.');
  1349. $this->assertText($this->terms['child_term']->name, 'Term is saved when it is submitted through synonyms friendly select for the cardinality of 1.');
  1350. $options = array();
  1351. $options[] = array(
  1352. 'entity' => $this->terms['normal_term'],
  1353. );
  1354. $options[] = array(
  1355. 'entity' => $this->terms['normal_term'],
  1356. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1357. );
  1358. $options[] = array(
  1359. 'entity' => $this->terms['normal_term'],
  1360. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1361. );
  1362. $options[] = array(
  1363. 'entity' => $this->terms['parent_term'],
  1364. );
  1365. $options[] = array(
  1366. 'entity' => $this->terms['parent_term'],
  1367. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1368. );
  1369. $options[] = array(
  1370. 'entity' => $this->terms['parent_term'],
  1371. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1372. );
  1373. $options[] = array(
  1374. 'entity' => $this->terms['child_term'],
  1375. 'selected' => TRUE,
  1376. );
  1377. $options[] = array(
  1378. 'entity' => $this->terms['child_term'],
  1379. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1380. );
  1381. $options[] = array(
  1382. 'entity' => $this->terms['child_term'],
  1383. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1384. );
  1385. $this->drupalGet('node/' . $node->nid . '/edit');
  1386. $this->assertSynonymsSelect($options, 'Default values are set correctly in the synonyms friendly select widget when working with the field cardinality of 1.');
  1387. }
  1388. protected function synonymsSelectOptionPrefix($entity, $synonym = NULL) {
  1389. $depth = count(taxonomy_get_parents_all($entity->tid)) - 1;
  1390. return str_repeat('-', $depth) . parent::synonymsSelectOptionPrefix($entity, $synonym);
  1391. }
  1392. }
  1393. /**
  1394. * Test synonyms friendly select widget for entity reference field type.
  1395. */
  1396. class EntityReferenceSelectSynonymsWebTestCase extends AbstractSelectSynonymsWebTestCase {
  1397. public static function getInfo() {
  1398. return array(
  1399. 'name' => 'Entity reference synonyms select',
  1400. 'description' => 'Ensure that the "synonym friendly select" widget works correctly with entity reference field.',
  1401. 'group' => 'Synonyms',
  1402. );
  1403. }
  1404. public function setUp($modules = array()) {
  1405. $modules[] = 'entityreference';
  1406. parent::setUp($modules);
  1407. $this->reference_field = array(
  1408. 'type' => 'entityreference',
  1409. 'field_name' => 'synonyms_term',
  1410. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1411. 'settings' => array(
  1412. 'target_type' => 'taxonomy_term',
  1413. 'handler_settings' => array(),
  1414. ),
  1415. );
  1416. $this->reference_field = field_create_field($this->reference_field);
  1417. $this->reference_instance = array(
  1418. 'field_name' => $this->reference_field['field_name'],
  1419. 'entity_type' => 'node',
  1420. 'bundle' => $this->bundle,
  1421. 'label' => 'Synonym Terms Select',
  1422. 'widget' => array(
  1423. 'type' => 'synonyms_select_entity',
  1424. ),
  1425. );
  1426. $this->reference_instance = field_create_instance($this->reference_instance);
  1427. }
  1428. public function testWidgetSorting() {
  1429. $cardinality = array(
  1430. 1 => 1,
  1431. FIELD_CARDINALITY_UNLIMITED => 'unlimited',
  1432. );
  1433. $required = array(
  1434. TRUE => 'required',
  1435. FALSE => 'not required',
  1436. );
  1437. foreach ($cardinality as $cardinality_k => $cardinality_v) {
  1438. foreach ($required as $required_k => $required_v) {
  1439. $this->reference_field['cardinality'] = $cardinality_k;
  1440. field_update_field($this->reference_field);
  1441. $this->reference_instance['required'] = $required_k;
  1442. field_update_instance($this->reference_instance);
  1443. $options = array();
  1444. $options[] = array(
  1445. 'entity' => $this->terms['child_term'],
  1446. );
  1447. $options[] = array(
  1448. 'entity' => $this->terms['child_term'],
  1449. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1450. );
  1451. $options[] = array(
  1452. 'entity' => $this->terms['child_term'],
  1453. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1454. );
  1455. $options[] = array(
  1456. 'entity' => $this->terms['normal_term'],
  1457. );
  1458. $options[] = array(
  1459. 'entity' => $this->terms['normal_term'],
  1460. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1461. );
  1462. $options[] = array(
  1463. 'entity' => $this->terms['normal_term'],
  1464. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1465. );
  1466. $options[] = array(
  1467. 'entity' => $this->terms['parent_term'],
  1468. );
  1469. $options[] = array(
  1470. 'entity' => $this->terms['parent_term'],
  1471. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1472. );
  1473. $options[] = array(
  1474. 'entity' => $this->terms['parent_term'],
  1475. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1476. );
  1477. $this->drupalGet('node/add/synonyms-test-content');
  1478. $this->assertSynonymsSelect($options, 'Synonyms select sorting by name works for the cardinality of ' . $cardinality_v . ' and ' . $required_v);
  1479. }
  1480. }
  1481. }
  1482. public function testWidget() {
  1483. $name = $this->randomName();
  1484. $this->drupalPost('node/add/synonyms-test-content', array(
  1485. 'title' => $name,
  1486. $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . '][]' => array(
  1487. $this->synonymSelectKey($this->terms['parent_term'], $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  1488. $this->terms['child_term']->tid,
  1489. $this->terms['normal_term']->tid,
  1490. $this->synonymSelectKey($this->terms['normal_term'], $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  1491. ),
  1492. ), 'Save');
  1493. $node = $this->drupalGetNodeByTitle($name);
  1494. $this->drupalGet('node/' . $node->nid);
  1495. $this->assertText($this->terms['parent_term']->name, 'Term is saved when its synonym is submitted through synonyms friendly select for the unlimited cardinality.');
  1496. $this->assertText($this->terms['child_term']->name, 'Term is saved when it is submitted through synonyms friendly select for the unlimited cardinality.');
  1497. $this->assertUniqueText($this->terms['normal_term']->name, 'Term is saved only once when the term and its synonym are submitted through synonyms friendly select for the unlimited cardinality.');
  1498. $options = array();
  1499. $options[] = array(
  1500. 'entity' => $this->terms['child_term'],
  1501. 'selected' => TRUE,
  1502. );
  1503. $options[] = array(
  1504. 'entity' => $this->terms['child_term'],
  1505. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1506. );
  1507. $options[] = array(
  1508. 'entity' => $this->terms['child_term'],
  1509. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1510. );
  1511. $options[] = array(
  1512. 'entity' => $this->terms['normal_term'],
  1513. 'selected' => TRUE,
  1514. );
  1515. $options[] = array(
  1516. 'entity' => $this->terms['normal_term'],
  1517. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1518. );
  1519. $options[] = array(
  1520. 'entity' => $this->terms['normal_term'],
  1521. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1522. );
  1523. $options[] = array(
  1524. 'entity' => $this->terms['parent_term'],
  1525. 'selected' => TRUE,
  1526. );
  1527. $options[] = array(
  1528. 'entity' => $this->terms['parent_term'],
  1529. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1530. );
  1531. $options[] = array(
  1532. 'entity' => $this->terms['parent_term'],
  1533. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1534. );
  1535. $this->drupalGet('node/' . $node->nid . '/edit');
  1536. $this->assertSynonymsSelect($options, 'Default values are set correctly in the synonyms friendly select widget when working with field cardinality more than 1.');
  1537. $this->reference_field['cardinality'] = 2;
  1538. field_update_field($this->reference_field);
  1539. $name = $this->randomName();
  1540. $this->drupalPost('node/add/synonyms-test-content', array(
  1541. 'title' => $name,
  1542. $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . '][]' => array(
  1543. $this->synonymSelectKey($this->terms['parent_term'], $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  1544. $this->terms['child_term']->tid,
  1545. $this->terms['normal_term']->tid,
  1546. ),
  1547. ), 'Save');
  1548. $this->assertText('this field cannot hold more than 2 values.', 'Submitting 3 entries into a field with cardinality of 2, that refer to 3 terms, results in a form error.');
  1549. $this->drupalPost('node/add/synonyms-test-content', array(
  1550. 'title' => $name,
  1551. $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . '][]' => array(
  1552. $this->synonymSelectKey($this->terms['parent_term'], $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  1553. $this->terms['normal_term']->tid,
  1554. $this->synonymSelectKey($this->terms['normal_term'], $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  1555. ),
  1556. ), 'Save');
  1557. $node = $this->drupalGetNodeByTitle($name);
  1558. $this->drupalGet('node/' . $node->nid);
  1559. $this->assertUniqueText($this->terms['parent_term']->name, 'Submitting 3 entries into a field with cardinality of 2, that refer to only 2 terms, results in form getting submitted. Term #1 is saved.');
  1560. $this->assertUniqueText($this->terms['normal_term']->name, 'Term #2 is saved.');
  1561. $this->reference_field['cardinality'] = 1;
  1562. field_update_field($this->reference_field);
  1563. $name = $this->randomName();
  1564. $this->drupalPost('node/add/synonyms-test-content', array(
  1565. 'title' => $name,
  1566. $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . ']' => $this->synonymSelectKey($this->terms['parent_term'], $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  1567. ), 'Save');
  1568. $node = $this->drupalGetNodeByTitle($name);
  1569. $this->drupalGet('node/' . $node->nid);
  1570. $this->assertText($this->terms['parent_term']->name, 'Term is saved when its synonym is submitted through synonyms friendly select for the cardinality of 1.');
  1571. $options = array();
  1572. $options[] = array(
  1573. 'entity' => $this->terms['child_term'],
  1574. );
  1575. $options[] = array(
  1576. 'entity' => $this->terms['child_term'],
  1577. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1578. );
  1579. $options[] = array(
  1580. 'entity' => $this->terms['child_term'],
  1581. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1582. );
  1583. $options[] = array(
  1584. 'entity' => $this->terms['normal_term'],
  1585. );
  1586. $options[] = array(
  1587. 'entity' => $this->terms['normal_term'],
  1588. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1589. );
  1590. $options[] = array(
  1591. 'entity' => $this->terms['normal_term'],
  1592. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1593. );
  1594. $options[] = array(
  1595. 'entity' => $this->terms['parent_term'],
  1596. 'selected' => TRUE,
  1597. );
  1598. $options[] = array(
  1599. 'entity' => $this->terms['parent_term'],
  1600. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1601. );
  1602. $options[] = array(
  1603. 'entity' => $this->terms['parent_term'],
  1604. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1605. );
  1606. $this->drupalGet('node/' . $node->nid . '/edit');
  1607. $this->assertSynonymsSelect($options, 'Default values are set correctly in the synonyms friendly select widget when working with the field cardinality of 1.');
  1608. $this->drupalPost('node/' . $node->nid . '/edit', array(
  1609. $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . ']' => $this->terms['child_term']->tid,
  1610. ), 'Save');
  1611. $this->drupalGet('node/' . $node->nid);
  1612. $this->assertNoText($this->terms['parent_term']->name, 'After updating entity the old term is removed.');
  1613. $this->assertText($this->terms['child_term']->name, 'Term is saved when it is submitted through synonyms friendly select for the cardinality of 1.');
  1614. $options = array();
  1615. $options[] = array(
  1616. 'entity' => $this->terms['child_term'],
  1617. 'selected' => TRUE,
  1618. );
  1619. $options[] = array(
  1620. 'entity' => $this->terms['child_term'],
  1621. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1622. );
  1623. $options[] = array(
  1624. 'entity' => $this->terms['child_term'],
  1625. 'synonym' => $this->terms['child_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1626. );
  1627. $options[] = array(
  1628. 'entity' => $this->terms['normal_term'],
  1629. );
  1630. $options[] = array(
  1631. 'entity' => $this->terms['normal_term'],
  1632. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1633. );
  1634. $options[] = array(
  1635. 'entity' => $this->terms['normal_term'],
  1636. 'synonym' => $this->terms['normal_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1637. );
  1638. $options[] = array(
  1639. 'entity' => $this->terms['parent_term'],
  1640. );
  1641. $options[] = array(
  1642. 'entity' => $this->terms['parent_term'],
  1643. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  1644. );
  1645. $options[] = array(
  1646. 'entity' => $this->terms['parent_term'],
  1647. 'synonym' => $this->terms['parent_term']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  1648. );
  1649. $this->drupalGet('node/' . $node->nid . '/edit');
  1650. $this->assertSynonymsSelect($options, 'Default values are set correctly in the synonyms friendly select widget when working with the field cardinality of 1.');
  1651. }
  1652. }
  1653. /**
  1654. * Test 'term_synonyms' cTools argument plugin implementation.
  1655. */
  1656. class TaxonomyTermArgumentSynonymsWebTestCase extends SynonymsWebTestCase {
  1657. /**
  1658. * GetInfo method.
  1659. */
  1660. public static function getInfo() {
  1661. return array(
  1662. 'name' => 'Term synonyms cTools argument plugin',
  1663. 'description' => 'Ensure that synonyms friendly taxonomy terms cTools argument plugin works correctly.',
  1664. 'group' => 'Synonyms',
  1665. );
  1666. }
  1667. /**
  1668. * SetUp method.
  1669. */
  1670. public function setUp($modules = array()) {
  1671. $this->behavior_implementation['behavior'] = 'autocomplete';
  1672. $this->behavior_implementation['settings'] = array(
  1673. 'wording' => '@synonym',
  1674. );
  1675. parent::setUp($modules);
  1676. }
  1677. /**
  1678. * Test the term synonym argument.
  1679. */
  1680. public function testTermSynonymArgument() {
  1681. $term = (object) array(
  1682. 'name' => 'Term name',
  1683. 'vid' => $this->vocabulary->vid,
  1684. $this->fields['enabled']['field']['field_name'] => array(LANGUAGE_NONE => array(
  1685. array('value' => 'Synonym of term'),
  1686. )),
  1687. );
  1688. taxonomy_term_save($term);
  1689. ctools_include('context');
  1690. $argument = array(
  1691. 'name' => 'term_synonyms',
  1692. 'vids' => array(),
  1693. 'transform' => FALSE,
  1694. 'identifier' => 'Just Testing',
  1695. 'keyword' => 'term',
  1696. 'id' => 0,
  1697. );
  1698. $result = ctools_context_get_context_from_argument($argument, $this->randomName());
  1699. $this->assertNull($result, 'Term synonym does not look up anything when random string is supplied.');
  1700. $result = ctools_context_get_context_from_argument($argument, drupal_strtolower($term->name));
  1701. $this->assertEqual($result->data->tid, $term->tid, 'Term synonym argument correctly looks up the term by its name.');
  1702. $result = ctools_context_get_context_from_argument($argument, drupal_strtolower($term->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']));
  1703. $this->assertEqual($result->data->tid, $term->tid, 'Term synonym argument correctly looks up the term by its synonym.');
  1704. $argument['transform'] = TRUE;
  1705. $result = ctools_context_get_context_from_argument($argument, str_replace(' ', '-', $term->name));
  1706. $this->assertEqual($result->data->tid, $term->tid, 'Term synonym argument correctly looks up the term by its name, if the spaces are repaced with dashes.');
  1707. $result = ctools_context_get_context_from_argument($argument, str_replace(' ', '-', $term->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']));
  1708. $this->assertEqual($result->data->tid, $term->tid, 'Term synonym argument correctly looks up the term by its synonym, if the spaces are repaced with dashes.');
  1709. $argument['transform'] = FALSE;
  1710. $another_vocabulary = (object) array(
  1711. 'name' => $this->randomName(),
  1712. 'machine_name' => 'another_vocabulary',
  1713. );
  1714. taxonomy_vocabulary_save($another_vocabulary);
  1715. $argument['vids'][$another_vocabulary->vid] = $another_vocabulary->vid;
  1716. $result = ctools_context_get_context_from_argument($argument, drupal_strtolower($term->name));
  1717. $this->assertNull($result, 'Term synonym argument does not look up anything when term name is supplied, but the search is limited to another vocabulary.');
  1718. $result = ctools_context_get_context_from_argument($argument, drupal_strtolower($term->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']));
  1719. $this->assertNull($result, 'Term synonym argument does not look up anything when term synonym is supplied, but the search is limited to another vocabulary.');
  1720. }
  1721. }