synonyms.test 87 KB

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