/**
  * Tests Drupal 6 search settings to Drupal 8 search page entity migration.
  */
 public function testSearchPage()
 {
     $id = 'node_search';
     /** @var \Drupal\search\Entity\SearchPage $search_page */
     $search_page = SearchPage::load($id);
     $this->assertIdentical($id, $search_page->id());
     $configuration = $search_page->getPlugin()->getConfiguration();
     $this->assertIdentical($configuration['rankings'], array('comments' => 5, 'promote' => 0, 'recent' => 0, 'relevance' => 2, 'sticky' => 8, 'views' => 1));
     $this->assertIdentical('node', $search_page->getPath());
     // Test that we can re-import using the EntitySearchPage destination.
     Database::getConnection('default', 'migrate')->update('variable')->fields(array('value' => serialize(4)))->condition('name', 'node_rank_comments')->execute();
     /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
     $migration = $this->getMigration('search_page');
     // Indicate we're rerunning a migration that's already run.
     $migration->getIdMap()->prepareUpdate();
     $this->executeMigration($migration);
     $configuration = SearchPage::load($id)->getPlugin()->getConfiguration();
     $this->assertIdentical(4, $configuration['rankings']['comments']);
 }
Example #2
0
 public function testRankings()
 {
     // Add a comment field.
     $this->addDefaultCommentField('node', 'page');
     // Build a list of the rankings to test.
     $node_ranks = array('sticky', 'promote', 'relevance', 'recent', 'comments', 'views');
     // Create nodes for testing.
     $nodes = array();
     foreach ($node_ranks as $node_rank) {
         $settings = array('type' => 'page', 'comment' => array(array('status' => CommentItemInterface::HIDDEN)), 'title' => 'Drupal rocks', 'body' => array(array('value' => "Drupal's search rocks")), 'created' => REQUEST_TIME - 24 * 3600, 'sticky' => 0, 'promote' => 0);
         foreach (array(0, 1) as $num) {
             if ($num == 1) {
                 switch ($node_rank) {
                     case 'sticky':
                     case 'promote':
                         $settings[$node_rank] = 1;
                         break;
                     case 'relevance':
                         $settings['body'][0]['value'] .= " really rocks";
                         break;
                     case 'recent':
                         // Node is 1 hour hold.
                         $settings['created'] = REQUEST_TIME - 3600;
                         break;
                     case 'comments':
                         $settings['comment'][0]['status'] = CommentItemInterface::OPEN;
                         break;
                 }
             }
             $nodes[$node_rank][$num] = $this->drupalCreateNode($settings);
         }
     }
     // Add a comment to one of the nodes.
     $edit = array();
     $edit['subject[0][value]'] = 'my comment title';
     $edit['comment_body[0][value]'] = 'some random comment';
     $this->drupalGet('comment/reply/node/' . $nodes['comments'][1]->id() . '/comment');
     $this->drupalPostForm(NULL, $edit, t('Preview'));
     $this->drupalPostForm(NULL, $edit, t('Save'));
     // Enable counting of statistics.
     $this->config('statistics.settings')->set('count_content_views', 1)->save();
     // Simulating content views is kind of difficult in the test. Leave that
     // to the Statistics module. So instead go ahead and manually update the
     // counter for this node.
     $nid = $nodes['views'][1]->id();
     db_insert('node_counter')->fields(array('totalcount' => 5, 'daycount' => 5, 'timestamp' => REQUEST_TIME, 'nid' => $nid))->execute();
     // Run cron to update the search index and comment/statistics totals.
     $this->cronRun();
     // Test that the settings form displays the content ranking section.
     $this->drupalGet('admin/config/search/pages/manage/node_search');
     $this->assertText(t('Content ranking'));
     // Check that all rankings are visible and set to 0.
     foreach ($node_ranks as $node_rank) {
         $this->assertTrue($this->xpath('//select[@id="edit-rankings-' . $node_rank . '-value"]//option[@value="0"]'), 'Select list to prioritize ' . $node_rank . ' for node ranks is visible and set to 0.');
     }
     // Test each of the possible rankings.
     $edit = array();
     foreach ($node_ranks as $node_rank) {
         // Enable the ranking we are testing.
         $edit['rankings[' . $node_rank . '][value]'] = 10;
         $this->drupalPostForm('admin/config/search/pages/manage/node_search', $edit, t('Save search page'));
         $this->drupalGet('admin/config/search/pages/manage/node_search');
         $this->assertTrue($this->xpath('//select[@id="edit-rankings-' . $node_rank . '-value"]//option[@value="10"]'), 'Select list to prioritize ' . $node_rank . ' for node ranks is visible and set to 10.');
         // Reload the plugin to get the up-to-date values.
         $this->nodeSearch = SearchPage::load('node_search');
         // Do the search and assert the results.
         $this->nodeSearch->getPlugin()->setSearch('rocks', array(), array());
         $set = $this->nodeSearch->getPlugin()->execute();
         $this->assertEqual($set[0]['node']->id(), $nodes[$node_rank][1]->id(), 'Search ranking "' . $node_rank . '" order.');
         // Clear this ranking for the next test.
         $edit['rankings[' . $node_rank . '][value]'] = 0;
     }
     // Save the final node_rank change then check that all rankings are visible
     // and have been set back to 0.
     $this->drupalPostForm('admin/config/search/pages/manage/node_search', $edit, t('Save search page'));
     $this->drupalGet('admin/config/search/pages/manage/node_search');
     foreach ($node_ranks as $node_rank) {
         $this->assertTrue($this->xpath('//select[@id="edit-rankings-' . $node_rank . '-value"]//option[@value="0"]'), 'Select list to prioritize ' . $node_rank . ' for node ranks is visible and set to 0.');
     }
     // Try with sticky, then promoted. This is a test for issue
     // https://www.drupal.org/node/771596.
     $node_ranks = array('sticky' => 10, 'promote' => 1, 'relevance' => 0, 'recent' => 0, 'comments' => 0, 'views' => 0);
     $configuration = $this->nodeSearch->getPlugin()->getConfiguration();
     foreach ($node_ranks as $var => $value) {
         $configuration['rankings'][$var] = $value;
     }
     $this->nodeSearch->getPlugin()->setConfiguration($configuration);
     $this->nodeSearch->save();
     // Do the search and assert the results. The sticky node should show up
     // first, then the promoted node, then all the rest.
     $this->nodeSearch->getPlugin()->setSearch('rocks', array(), array());
     $set = $this->nodeSearch->getPlugin()->execute();
     $this->assertEqual($set[0]['node']->id(), $nodes['sticky'][1]->id(), 'Search ranking for sticky first worked.');
     $this->assertEqual($set[1]['node']->id(), $nodes['promote'][1]->id(), 'Search ranking for promoted second worked.');
     // Try with recent, then comments. This is a test for issues
     // https://www.drupal.org/node/771596 and
     // https://www.drupal.org/node/303574.
     $node_ranks = array('sticky' => 0, 'promote' => 0, 'relevance' => 0, 'recent' => 10, 'comments' => 1, 'views' => 0);
     $configuration = $this->nodeSearch->getPlugin()->getConfiguration();
     foreach ($node_ranks as $var => $value) {
         $configuration['rankings'][$var] = $value;
     }
     $this->nodeSearch->getPlugin()->setConfiguration($configuration);
     $this->nodeSearch->save();
     // Do the search and assert the results. The recent node should show up
     // first, then the commented node, then all the rest.
     $this->nodeSearch->getPlugin()->setSearch('rocks', array(), array());
     $set = $this->nodeSearch->getPlugin()->execute();
     $this->assertEqual($set[0]['node']->id(), $nodes['recent'][1]->id(), 'Search ranking for recent first worked.');
     $this->assertEqual($set[1]['node']->id(), $nodes['comments'][1]->id(), 'Search ranking for comments second worked.');
 }
 /**
  * Verifies that you can disable individual search plugins.
  */
 function testSearchModuleDisabling()
 {
     // Array of search plugins to test: 'keys' are the keywords to search for,
     // and 'text' is the text to assert is on the results page.
     $plugin_info = array('node_search' => array('keys' => 'pizza', 'text' => $this->searchNode->label()), 'user_search' => array('keys' => $this->searchUser->getUsername(), 'text' => $this->searchUser->getEmail()), 'dummy_search_type' => array('keys' => 'foo', 'text' => 'Dummy search snippet to display'));
     $plugins = array_keys($plugin_info);
     /** @var $entities \Drupal\search\SearchPageInterface[] */
     $entities = SearchPage::loadMultiple();
     // Disable all of the search pages.
     foreach ($entities as $entity) {
         $entity->disable()->save();
     }
     // Test each plugin if it's enabled as the only search plugin.
     foreach ($entities as $entity_id => $entity) {
         // Set this as default.
         $this->drupalGet("admin/config/search/pages/manage/{$entity_id}/set-default");
         // Run a search from the correct search URL.
         $info = $plugin_info[$entity_id];
         $this->drupalGet('search/' . $entity->getPath(), array('query' => array('keys' => $info['keys'])));
         $this->assertResponse(200);
         $this->assertNoText('no results', $entity->label() . ' search found results');
         $this->assertText($info['text'], 'Correct search text found');
         // Verify that other plugin search tab labels are not visible.
         foreach ($plugins as $other) {
             if ($other != $entity_id) {
                 $label = $entities[$other]->label();
                 $this->assertNoText($label, $label . ' search tab is not shown');
             }
         }
         // Run a search from the search block on the node page. Verify you get
         // to this plugin's search results page.
         $terms = array('keys' => $info['keys']);
         $this->submitGetForm('node', $terms, t('Search'));
         $current = $this->getURL();
         $expected = \Drupal::url('search.view_' . $entity->id(), array(), array('query' => array('keys' => $info['keys']), 'absolute' => TRUE));
         $this->assertEqual($current, $expected, 'Block redirected to right search page');
         // Try an invalid search path, which should 404.
         $this->drupalGet('search/not_a_plugin_path');
         $this->assertResponse(404);
         $entity->disable()->save();
     }
     // Test with all search plugins enabled. When you go to the search
     // page or run search, all plugins should be shown.
     foreach ($entities as $entity) {
         $entity->enable()->save();
     }
     // Set the node search as default.
     $this->drupalGet('admin/config/search/pages/manage/node_search/set-default');
     $paths = array(array('path' => 'search/node', 'options' => array('query' => array('keys' => 'pizza'))), array('path' => 'search/node', 'options' => array()));
     foreach ($paths as $item) {
         $this->drupalGet($item['path'], $item['options']);
         foreach ($plugins as $entity_id) {
             $label = $entities[$entity_id]->label();
             $this->assertText($label, format_string('%label search tab is shown', array('%label' => $label)));
         }
     }
 }
 /**
  * Tests updating a search page during import.
  */
 protected function doSearchPageUpdate()
 {
     // Create a test search page with a known label.
     $name = 'search.page.apple';
     $entity = SearchPage::create(['id' => 'apple', 'plugin' => 'search_extra_type_search']);
     $entity->save();
     $this->checkSinglePluginConfigSync($entity, 'configuration', 'boost', 'bi');
     // Read the existing data, and prepare an altered version in sync.
     $custom_data = $original_data = $this->container->get('config.storage')->read($name);
     $custom_data['configuration']['boost'] = 'asdf';
     $this->assertConfigUpdateImport($name, $original_data, $custom_data);
 }