synonyms_search.test 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the Synonyms Search module.
  5. */
  6. /**
  7. * Base class for tests of Synonyms Search module.
  8. */
  9. abstract class AbstractSearchSynonymsWebTestCase extends SynonymsWebTestCase {
  10. protected $behavior = 'search';
  11. /**
  12. * What search type is being tested.
  13. *
  14. * @var string
  15. */
  16. protected $search_type = 'node';
  17. /**
  18. * Array of terms that will be used for testing.
  19. *
  20. * @var array
  21. */
  22. protected $terms = array();
  23. /**
  24. * SetUp method.
  25. */
  26. public function setUp($modules = array()) {
  27. $modules[] = 'synonyms_search';
  28. parent::setUp($modules);
  29. // Create a few terms and synonyms.
  30. $term = (object) array(
  31. 'vid' => $this->vocabulary->vid,
  32. 'name' => $this->randomName(),
  33. $this->fields['disabled']['field']['field_name'] => array(
  34. LANGUAGE_NONE => array(
  35. array('value' => $this->randomName()),
  36. ),
  37. ),
  38. );
  39. taxonomy_term_save($term);
  40. $this->terms['no_synonyms'] = $term;
  41. $term = (object) array(
  42. 'vid' => $this->vocabulary->vid,
  43. 'name' => $this->randomName(),
  44. $this->fields['enabled']['field']['field_name'] => array(
  45. LANGUAGE_NONE => array(
  46. array('value' => $this->randomName()),
  47. ),
  48. ),
  49. $this->fields['disabled']['field']['field_name'] => array(
  50. LANGUAGE_NONE => array(
  51. array('value' => $this->randomName()),
  52. ),
  53. ),
  54. );
  55. taxonomy_term_save($term);
  56. $this->terms['one_synonym'] = $term;
  57. $term = (object) array(
  58. 'vid' => $this->vocabulary->vid,
  59. 'name' => $this->randomName(),
  60. $this->fields['enabled']['field']['field_name'] => array(
  61. LANGUAGE_NONE => array(
  62. array('value' => $this->randomName()),
  63. array('value' => $this->randomName()),
  64. ),
  65. ),
  66. $this->fields['disabled']['field']['field_name'] => array(
  67. LANGUAGE_NONE => array(
  68. array('value' => $this->randomName()),
  69. ),
  70. ),
  71. );
  72. taxonomy_term_save($term);
  73. $this->terms['two_synonyms'] = $term;
  74. }
  75. /**
  76. * Retrieve search results.
  77. *
  78. * @param $keyword string
  79. * Keyword to supply to the search mechanism
  80. *
  81. * @return array
  82. * Array of HTML search results. Each element in this array is a single
  83. * search result represented in HTML code as Drupal search mechanism outputs
  84. * it
  85. */
  86. protected function getSearchResults($keyword) {
  87. $response = $this->drupalGet('search/' . $this->search_type . '/' . $keyword);
  88. $matches = array();
  89. preg_match_all('#\<li[^>]+class="search-result"[^>]*\>(.*?)\</li\>#si', $response, $matches);
  90. return $matches[1];
  91. }
  92. }
  93. /**
  94. * Test Synonyms module integration with Drupal search functionality for nodes.
  95. */
  96. class NodeSearchSynonymsWebTestCase extends AbstractSearchSynonymsWebTestCase {
  97. /**
  98. * GetInfo method.
  99. */
  100. public static function getInfo() {
  101. return array(
  102. 'name' => 'Synonyms node search integration',
  103. 'description' => 'Ensure that Synonyms module correctly integrates with the Drupal search functionality.',
  104. 'group' => 'Synonyms',
  105. );
  106. }
  107. /**
  108. * SetUp method.
  109. */
  110. public function setUp($modules = array()) {
  111. parent::setUp($modules);
  112. // Creating a test content type.
  113. $this->drupalPost('admin/structure/types/add', array(
  114. 'name' => 'Synonyms Test Content',
  115. 'type' => 'synonyms_test_content',
  116. ), 'Save content type');
  117. // Attaching term reference field to the new content type.
  118. $field = array(
  119. 'type' => 'taxonomy_term_reference',
  120. 'field_name' => 'synonyms_term_enabled',
  121. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  122. 'settings' => array(
  123. 'allowed_values' => array(
  124. array(
  125. 'vocabulary' => $this->vocabulary->machine_name,
  126. 'parent' => 0,
  127. ),
  128. ),
  129. ),
  130. );
  131. $field = field_create_field($field);
  132. $instance = array(
  133. 'field_name' => $field['field_name'],
  134. 'entity_type' => 'node',
  135. 'bundle' => 'synonyms_test_content',
  136. 'label' => 'Synonym Terms',
  137. 'widget' => array(
  138. 'type' => 'synonyms_autocomplete',
  139. ),
  140. );
  141. field_create_instance($instance);
  142. }
  143. /**
  144. * Test searching nodes by a term synonym.
  145. *
  146. * Since logically term and its synonyms represent the same entity, the idea
  147. * is that searching by a term synonym should trigger all content referencing
  148. * that term to be included in search results. Additionally we test that when
  149. * a synonym is deleted/edited in a term, corresponding content is no longer
  150. * encountered when searched by ex-synonym.
  151. */
  152. public function testSearchTermSynonym() {
  153. // Creating a node, which references all the terms we have.
  154. $node = (object) array(
  155. 'type' => 'synonyms_test_content',
  156. 'title' => $this->randomName(),
  157. 'synonyms_term_enabled' => array(LANGUAGE_NONE => array(
  158. array('tid' => $this->terms['no_synonyms']->tid),
  159. array('tid' => $this->terms['one_synonym']->tid),
  160. array('tid' => $this->terms['two_synonyms']->tid),
  161. )),
  162. );
  163. node_save($node);
  164. // Rebuilding Search index.
  165. $this->cronRun();
  166. foreach ($this->terms as $k => $term) {
  167. $this->assertSearchResults($term->name, array($node), 'Searching by name of the term ' . $k);
  168. $items = field_get_items('taxonomy_term', $term, $this->fields['disabled']['field']['field_name']);
  169. if (is_array($items)) {
  170. foreach ($items as $delta => $item) {
  171. $this->assertSearchResults($item['value'], array(), 'Searching by not enabled search integration field value #' . $delta . ' of term ' . $k);
  172. }
  173. }
  174. $items = field_get_items('taxonomy_term', $term, $this->fields['enabled']['field']['field_name']);
  175. if (is_array($items)) {
  176. foreach ($items as $delta => $item) {
  177. $this->assertSearchResults($item['value'], array($node), 'Searching by synonym #' . $delta . ' of the term ' . $k);
  178. }
  179. }
  180. }
  181. // Removing a synonym from the term. Then asserting node got re-indexed with
  182. // new values of synonyms.
  183. $deleted_synonym = array_pop($this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE]);
  184. taxonomy_term_save($this->terms['one_synonym']);
  185. $this->cronRun();
  186. $this->assertSearchResults($deleted_synonym['value'], array(), 'Searching by recently deleted synonym of a taxonomy term yields no results.');
  187. // Editing a synonym in a term. Then asserting node got re-indexed with new
  188. // values of synonyms.
  189. $ex_synonym = $this->terms['two_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'];
  190. $this->terms['two_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'] = $this->randomName();
  191. taxonomy_term_save($this->terms['two_synonyms']);
  192. $this->cronRun();
  193. $this->assertSearchResults($ex_synonym, array(), 'Searching by recently changed synonym of a taxonomy term yields no results.');
  194. // We disable entire field from search integration and make sure for all
  195. // synonyms search results are empty.
  196. synonyms_behavior_settings_delete($this->fields['enabled']['instance']['id'], $this->behavior);
  197. $this->cronRun();
  198. foreach ($this->terms as $k => $term) {
  199. $items = field_get_items('taxonomy_term', $term, $this->fields['enabled']['field']['field_name']);
  200. if (is_array($items)) {
  201. foreach ($items as $synonym) {
  202. $this->assertSearchResults($synonym['value'], array(), 'Searching by ' . $k . ' term synonym, which field was recently disabled from search behavior yields no results.');
  203. }
  204. }
  205. }
  206. }
  207. /**
  208. * Assert search results.
  209. *
  210. * @param $keyword string
  211. * Keyword to supply to the search mechanism
  212. * @param $results array
  213. * Array of fully loaded nodes that are expected to be on search results
  214. * @param $message string
  215. * Drupal assertion message to display on test results page
  216. */
  217. protected function assertSearchResults($keyword, $results, $message) {
  218. $matches = $this->getSearchResults($keyword);
  219. if (count($matches) != count($results)) {
  220. $this->fail($message);
  221. return;
  222. }
  223. $matches = implode('', $matches);
  224. foreach ($results as $node) {
  225. if (strpos($matches, 'node/' . $node->nid) === FALSE) {
  226. $this->fail($message);
  227. return;
  228. }
  229. }
  230. $this->pass($message);
  231. }
  232. }
  233. /**
  234. * Test Synonyms module integration with Drupal search for taxonomy terms.
  235. */
  236. class TermSearchSynonymsWebTestCase extends AbstractSearchSynonymsWebTestCase {
  237. protected $search_type = 'term';
  238. /**
  239. * GetInfo method.
  240. */
  241. public static function getInfo() {
  242. return array(
  243. 'name' => 'Synonyms term search integration',
  244. 'description' => 'Ensure that Synonyms module correctly integrates with the Term Search module.',
  245. 'group' => 'Synonyms',
  246. );
  247. }
  248. /**
  249. * SetUp method.
  250. */
  251. public function setUp($modules = array()) {
  252. $modules[] = 'term_search';
  253. parent::setUp($modules);
  254. $active_searches = variable_get('search_active_modules', array('node', 'user'));
  255. $active_searches[] = 'term_search';
  256. variable_set('search_active_modules', $active_searches);
  257. }
  258. /**
  259. * Test searching terms by their synonyms.
  260. */
  261. public function testSearchTermSynonym() {
  262. // Rebuilding Search index.
  263. $this->cronRun();
  264. foreach ($this->terms as $k => $term) {
  265. $this->assertSearchResults($term->name, array($term), 'Searching by name of the term ' . $k);
  266. $items = field_get_items('taxonomy_term', $term, $this->fields['disabled']['field']['field_name']);
  267. if (is_array($items)) {
  268. foreach ($items as $delta => $item) {
  269. $this->assertSearchResults($item['value'], array(), 'Searching by not enabled search integration field value #' . $delta . ' of term ' . $k);
  270. }
  271. }
  272. $items = field_get_items('taxonomy_term', $term, $this->fields['enabled']['field']['field_name']);
  273. if (is_array($items)) {
  274. foreach ($items as $delta => $item) {
  275. $this->assertSearchResults($item['value'], array($term), 'Searching by synonym #' . $delta . ' of the term ' . $k);
  276. }
  277. }
  278. }
  279. // Removing a synonym from the term. Then asserting it got re-indexed with
  280. // new values of synonyms.
  281. $deleted_synonym = array_pop($this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE]);
  282. taxonomy_term_save($this->terms['one_synonym']);
  283. $this->cronRun();
  284. $this->assertSearchResults($deleted_synonym['value'], array(), 'Searching by recently deleted synonym of a taxonomy term yields no results.');
  285. // Editing a synonym in a term. Then asserting it got re-indexed with new
  286. // values of synonyms.
  287. $ex_synonym = $this->terms['two_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'];
  288. $this->terms['two_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'] = $this->randomName();
  289. taxonomy_term_save($this->terms['two_synonyms']);
  290. $this->cronRun();
  291. $this->assertSearchResults($ex_synonym, array(), 'Searching by recently changed synonym of a taxonomy term yields no results.');
  292. // We disable entire field from search integration and make sure for all
  293. // synonyms search results are empty.
  294. synonyms_behavior_settings_delete($this->fields['enabled']['instance']['id'], $this->behavior);
  295. $this->cronRun();
  296. foreach ($this->terms as $k => $term) {
  297. $items = field_get_items('taxonomy_term', $term, $this->fields['enabled']['field']['field_name']);
  298. if (is_array($items)) {
  299. foreach ($items as $synonym) {
  300. $this->assertSearchResults($synonym['value'], array(), 'Searching by ' . $k . ' term synonym, which field was recently disabled from search behavior yields no results.');
  301. }
  302. }
  303. }
  304. }
  305. /**
  306. * Assert search results.
  307. *
  308. * @param $keyword string
  309. * Keyword to supply to the search mechanism
  310. * @param $results array
  311. * Array of fully loaded terms that are expected to be on search results
  312. * @param $message string
  313. * Drupal assertion message to display on test results page
  314. */
  315. protected function assertSearchResults($keyword, $results, $message) {
  316. $matches = $this->getSearchResults($keyword);
  317. if (count($matches) != count($results)) {
  318. $this->fail($message);
  319. return;
  320. }
  321. $matches = implode('', $matches);
  322. foreach ($results as $term) {
  323. if (strpos($matches, 'taxonomy/term/' . $term->tid) === FALSE) {
  324. $this->fail($message);
  325. return;
  326. }
  327. }
  328. $this->pass($message);
  329. }
  330. }