123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- <?php
- /**
- * @file
- * Tests for the Synonyms Search module.
- */
- /**
- * Base class for tests of Synonyms Search module.
- */
- abstract class AbstractSearchSynonymsWebTestCase extends SynonymsWebTestCase {
- protected $behavior = 'search';
- /**
- * What search type is being tested.
- *
- * @var string
- */
- protected $search_type = 'node';
- /**
- * Array of terms that will be used for testing.
- *
- * @var array
- */
- protected $terms = array();
- /**
- * SetUp method.
- */
- public function setUp($modules = array()) {
- $modules[] = 'synonyms_search';
- parent::setUp($modules);
- // Create a few terms and synonyms.
- $term = (object) array(
- 'vid' => $this->vocabulary->vid,
- 'name' => $this->randomName(),
- $this->fields['disabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- ),
- ),
- );
- taxonomy_term_save($term);
- $this->terms['no_synonyms'] = $term;
- $term = (object) array(
- 'vid' => $this->vocabulary->vid,
- 'name' => $this->randomName(),
- $this->fields['enabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- ),
- ),
- $this->fields['disabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- ),
- ),
- );
- taxonomy_term_save($term);
- $this->terms['one_synonym'] = $term;
- $term = (object) array(
- 'vid' => $this->vocabulary->vid,
- 'name' => $this->randomName(),
- $this->fields['enabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- array('value' => $this->randomName()),
- ),
- ),
- $this->fields['disabled']['field']['field_name'] => array(
- LANGUAGE_NONE => array(
- array('value' => $this->randomName()),
- ),
- ),
- );
- taxonomy_term_save($term);
- $this->terms['two_synonyms'] = $term;
- }
- /**
- * Retrieve search results.
- *
- * @param $keyword string
- * Keyword to supply to the search mechanism
- *
- * @return array
- * Array of HTML search results. Each element in this array is a single
- * search result represented in HTML code as Drupal search mechanism outputs
- * it
- */
- protected function getSearchResults($keyword) {
- $response = $this->drupalGet('search/' . $this->search_type . '/' . $keyword);
- $matches = array();
- preg_match_all('#\<li[^>]+class="search-result"[^>]*\>(.*?)\</li\>#si', $response, $matches);
- return $matches[1];
- }
- }
- /**
- * Test Synonyms module integration with Drupal search functionality for nodes.
- */
- class NodeSearchSynonymsWebTestCase extends AbstractSearchSynonymsWebTestCase {
- /**
- * GetInfo method.
- */
- public static function getInfo() {
- return array(
- 'name' => 'Synonyms node search integration',
- 'description' => 'Ensure that Synonyms module correctly integrates with the Drupal search functionality.',
- 'group' => 'Synonyms',
- );
- }
- /**
- * SetUp method.
- */
- public function setUp($modules = array()) {
- parent::setUp($modules);
- // Creating a test content type.
- $this->drupalPost('admin/structure/types/add', array(
- 'name' => 'Synonyms Test Content',
- 'type' => 'synonyms_test_content',
- ), 'Save content type');
- // Attaching term reference field to the new content type.
- $field = array(
- 'type' => 'taxonomy_term_reference',
- 'field_name' => 'synonyms_term_enabled',
- 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
- 'settings' => array(
- 'allowed_values' => array(
- array(
- 'vocabulary' => $this->vocabulary->machine_name,
- 'parent' => 0,
- ),
- ),
- ),
- );
- $field = field_create_field($field);
- $instance = array(
- 'field_name' => $field['field_name'],
- 'entity_type' => 'node',
- 'bundle' => 'synonyms_test_content',
- 'label' => 'Synonym Terms',
- 'widget' => array(
- 'type' => 'synonyms_autocomplete',
- ),
- );
- field_create_instance($instance);
- }
- /**
- * Test searching nodes by a term synonym.
- *
- * Since logically term and its synonyms represent the same entity, the idea
- * is that searching by a term synonym should trigger all content referencing
- * that term to be included in search results. Additionally we test that when
- * a synonym is deleted/edited in a term, corresponding content is no longer
- * encountered when searched by ex-synonym.
- */
- public function testSearchTermSynonym() {
- // Creating a node, which references all the terms we have.
- $node = (object) array(
- 'type' => 'synonyms_test_content',
- 'title' => $this->randomName(),
- 'synonyms_term_enabled' => array(LANGUAGE_NONE => array(
- array('tid' => $this->terms['no_synonyms']->tid),
- array('tid' => $this->terms['one_synonym']->tid),
- array('tid' => $this->terms['two_synonyms']->tid),
- )),
- );
- node_save($node);
- // Rebuilding Search index.
- $this->cronRun();
- foreach ($this->terms as $k => $term) {
- $this->assertSearchResults($term->name, array($node), 'Searching by name of the term ' . $k);
- $items = field_get_items('taxonomy_term', $term, $this->fields['disabled']['field']['field_name']);
- if (is_array($items)) {
- foreach ($items as $delta => $item) {
- $this->assertSearchResults($item['value'], array(), 'Searching by not enabled search integration field value #' . $delta . ' of term ' . $k);
- }
- }
- $items = field_get_items('taxonomy_term', $term, $this->fields['enabled']['field']['field_name']);
- if (is_array($items)) {
- foreach ($items as $delta => $item) {
- $this->assertSearchResults($item['value'], array($node), 'Searching by synonym #' . $delta . ' of the term ' . $k);
- }
- }
- }
- // Removing a synonym from the term. Then asserting node got re-indexed with
- // new values of synonyms.
- $deleted_synonym = array_pop($this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE]);
- taxonomy_term_save($this->terms['one_synonym']);
- $this->cronRun();
- $this->assertSearchResults($deleted_synonym['value'], array(), 'Searching by recently deleted synonym of a taxonomy term yields no results.');
- // Editing a synonym in a term. Then asserting node got re-indexed with new
- // values of synonyms.
- $ex_synonym = $this->terms['two_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'];
- $this->terms['two_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'] = $this->randomName();
- taxonomy_term_save($this->terms['two_synonyms']);
- $this->cronRun();
- $this->assertSearchResults($ex_synonym, array(), 'Searching by recently changed synonym of a taxonomy term yields no results.');
- // We disable entire field from search integration and make sure for all
- // synonyms search results are empty.
- synonyms_behavior_settings_delete($this->fields['enabled']['instance']['id'], $this->behavior);
- $this->cronRun();
- foreach ($this->terms as $k => $term) {
- $items = field_get_items('taxonomy_term', $term, $this->fields['enabled']['field']['field_name']);
- if (is_array($items)) {
- foreach ($items as $synonym) {
- $this->assertSearchResults($synonym['value'], array(), 'Searching by ' . $k . ' term synonym, which field was recently disabled from search behavior yields no results.');
- }
- }
- }
- }
- /**
- * Assert search results.
- *
- * @param $keyword string
- * Keyword to supply to the search mechanism
- * @param $results array
- * Array of fully loaded nodes that are expected to be on search results
- * @param $message string
- * Drupal assertion message to display on test results page
- */
- protected function assertSearchResults($keyword, $results, $message) {
- $matches = $this->getSearchResults($keyword);
- if (count($matches) != count($results)) {
- $this->fail($message);
- return;
- }
- $matches = implode('', $matches);
- foreach ($results as $node) {
- if (strpos($matches, 'node/' . $node->nid) === FALSE) {
- $this->fail($message);
- return;
- }
- }
- $this->pass($message);
- }
- }
- /**
- * Test Synonyms module integration with Drupal search for taxonomy terms.
- */
- class TermSearchSynonymsWebTestCase extends AbstractSearchSynonymsWebTestCase {
- protected $search_type = 'term';
- /**
- * GetInfo method.
- */
- public static function getInfo() {
- return array(
- 'name' => 'Synonyms term search integration',
- 'description' => 'Ensure that Synonyms module correctly integrates with the Term Search module.',
- 'group' => 'Synonyms',
- );
- }
- /**
- * SetUp method.
- */
- public function setUp($modules = array()) {
- $modules[] = 'term_search';
- parent::setUp($modules);
- $active_searches = variable_get('search_active_modules', array('node', 'user'));
- $active_searches[] = 'term_search';
- variable_set('search_active_modules', $active_searches);
- }
- /**
- * Test searching terms by their synonyms.
- */
- public function testSearchTermSynonym() {
- // Rebuilding Search index.
- $this->cronRun();
- foreach ($this->terms as $k => $term) {
- $this->assertSearchResults($term->name, array($term), 'Searching by name of the term ' . $k);
- $items = field_get_items('taxonomy_term', $term, $this->fields['disabled']['field']['field_name']);
- if (is_array($items)) {
- foreach ($items as $delta => $item) {
- $this->assertSearchResults($item['value'], array(), 'Searching by not enabled search integration field value #' . $delta . ' of term ' . $k);
- }
- }
- $items = field_get_items('taxonomy_term', $term, $this->fields['enabled']['field']['field_name']);
- if (is_array($items)) {
- foreach ($items as $delta => $item) {
- $this->assertSearchResults($item['value'], array($term), 'Searching by synonym #' . $delta . ' of the term ' . $k);
- }
- }
- }
- // Removing a synonym from the term. Then asserting it got re-indexed with
- // new values of synonyms.
- $deleted_synonym = array_pop($this->terms['one_synonym']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE]);
- taxonomy_term_save($this->terms['one_synonym']);
- $this->cronRun();
- $this->assertSearchResults($deleted_synonym['value'], array(), 'Searching by recently deleted synonym of a taxonomy term yields no results.');
- // Editing a synonym in a term. Then asserting it got re-indexed with new
- // values of synonyms.
- $ex_synonym = $this->terms['two_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'];
- $this->terms['two_synonyms']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'] = $this->randomName();
- taxonomy_term_save($this->terms['two_synonyms']);
- $this->cronRun();
- $this->assertSearchResults($ex_synonym, array(), 'Searching by recently changed synonym of a taxonomy term yields no results.');
- // We disable entire field from search integration and make sure for all
- // synonyms search results are empty.
- synonyms_behavior_settings_delete($this->fields['enabled']['instance']['id'], $this->behavior);
- $this->cronRun();
- foreach ($this->terms as $k => $term) {
- $items = field_get_items('taxonomy_term', $term, $this->fields['enabled']['field']['field_name']);
- if (is_array($items)) {
- foreach ($items as $synonym) {
- $this->assertSearchResults($synonym['value'], array(), 'Searching by ' . $k . ' term synonym, which field was recently disabled from search behavior yields no results.');
- }
- }
- }
- }
- /**
- * Assert search results.
- *
- * @param $keyword string
- * Keyword to supply to the search mechanism
- * @param $results array
- * Array of fully loaded terms that are expected to be on search results
- * @param $message string
- * Drupal assertion message to display on test results page
- */
- protected function assertSearchResults($keyword, $results, $message) {
- $matches = $this->getSearchResults($keyword);
- if (count($matches) != count($results)) {
- $this->fail($message);
- return;
- }
- $matches = implode('', $matches);
- foreach ($results as $term) {
- if (strpos($matches, 'taxonomy/term/' . $term->tid) === FALSE) {
- $this->fail($message);
- return;
- }
- }
- $this->pass($message);
- }
- }
|