synonyms.test 87 KB

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