Chris@17: drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); Chris@17: Chris@17: // Create a plugin instance. Chris@17: $this->nodeSearch = SearchPage::load('node_search'); Chris@17: Chris@17: // Log in with sufficient privileges. Chris@17: $this->drupalLogin($this->drupalCreateUser(['post comments', 'skip comment approval', 'create page content', 'administer search'])); Chris@17: } Chris@17: Chris@17: public function testRankings() { Chris@17: // Add a comment field. Chris@17: $this->addDefaultCommentField('node', 'page'); Chris@17: Chris@17: // Build a list of the rankings to test. Chris@17: $node_ranks = ['sticky', 'promote', 'relevance', 'recent', 'comments', 'views']; Chris@17: Chris@17: // Create nodes for testing. Chris@17: $nodes = []; Chris@17: foreach ($node_ranks as $node_rank) { Chris@17: $settings = [ Chris@17: 'type' => 'page', Chris@17: 'comment' => [ Chris@17: ['status' => CommentItemInterface::HIDDEN], Chris@17: ], Chris@17: 'title' => 'Drupal rocks', Chris@17: 'body' => [['value' => "Drupal's search rocks"]], Chris@17: // Node is one day old. Chris@17: 'created' => REQUEST_TIME - 24 * 3600, Chris@17: 'sticky' => 0, Chris@17: 'promote' => 0, Chris@17: ]; Chris@17: foreach ([0, 1] as $num) { Chris@17: if ($num == 1) { Chris@17: switch ($node_rank) { Chris@17: case 'sticky': Chris@17: case 'promote': Chris@17: $settings[$node_rank] = 1; Chris@17: break; Chris@17: case 'relevance': Chris@17: $settings['body'][0]['value'] .= " really rocks"; Chris@17: break; Chris@17: case 'recent': Chris@17: // Node is 1 hour hold. Chris@17: $settings['created'] = REQUEST_TIME - 3600; Chris@17: break; Chris@17: case 'comments': Chris@17: $settings['comment'][0]['status'] = CommentItemInterface::OPEN; Chris@17: break; Chris@17: } Chris@17: } Chris@17: $nodes[$node_rank][$num] = $this->drupalCreateNode($settings); Chris@17: } Chris@17: } Chris@17: Chris@17: // Add a comment to one of the nodes. Chris@17: $edit = []; Chris@17: $edit['subject[0][value]'] = 'my comment title'; Chris@17: $edit['comment_body[0][value]'] = 'some random comment'; Chris@17: $this->drupalGet('comment/reply/node/' . $nodes['comments'][1]->id() . '/comment'); Chris@17: $this->drupalPostForm(NULL, $edit, t('Preview')); Chris@17: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@17: Chris@17: // Enable counting of statistics. Chris@17: $this->config('statistics.settings')->set('count_content_views', 1)->save(); Chris@17: Chris@17: // Simulating content views is kind of difficult in the test. Leave that Chris@17: // to the Statistics module. So instead go ahead and manually update the Chris@17: // counter for this node. Chris@17: $nid = $nodes['views'][1]->id(); Chris@18: Database::getConnection()->insert('node_counter') Chris@17: ->fields(['totalcount' => 5, 'daycount' => 5, 'timestamp' => REQUEST_TIME, 'nid' => $nid]) Chris@17: ->execute(); Chris@17: Chris@17: // Run cron to update the search index and comment/statistics totals. Chris@17: $this->cronRun(); Chris@17: Chris@17: // Test that the settings form displays the content ranking section. Chris@17: $this->drupalGet('admin/config/search/pages/manage/node_search'); Chris@17: $this->assertText(t('Content ranking')); Chris@17: Chris@17: // Check that all rankings are visible and set to 0. Chris@17: foreach ($node_ranks as $node_rank) { Chris@17: $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.'); Chris@17: } Chris@17: Chris@17: // Test each of the possible rankings. Chris@17: $edit = []; Chris@17: foreach ($node_ranks as $node_rank) { Chris@17: // Enable the ranking we are testing. Chris@17: $edit['rankings[' . $node_rank . '][value]'] = 10; Chris@17: $this->drupalPostForm('admin/config/search/pages/manage/node_search', $edit, t('Save search page')); Chris@17: $this->drupalGet('admin/config/search/pages/manage/node_search'); Chris@17: $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.'); Chris@17: Chris@17: // Reload the plugin to get the up-to-date values. Chris@17: $this->nodeSearch = SearchPage::load('node_search'); Chris@17: // Do the search and assert the results. Chris@17: $this->nodeSearch->getPlugin()->setSearch('rocks', [], []); Chris@17: $set = $this->nodeSearch->getPlugin()->execute(); Chris@17: $this->assertEqual($set[0]['node']->id(), $nodes[$node_rank][1]->id(), 'Search ranking "' . $node_rank . '" order.'); Chris@17: Chris@17: // Clear this ranking for the next test. Chris@17: $edit['rankings[' . $node_rank . '][value]'] = 0; Chris@17: } Chris@17: Chris@17: // Save the final node_rank change then check that all rankings are visible Chris@17: // and have been set back to 0. Chris@17: $this->drupalPostForm('admin/config/search/pages/manage/node_search', $edit, t('Save search page')); Chris@17: $this->drupalGet('admin/config/search/pages/manage/node_search'); Chris@17: foreach ($node_ranks as $node_rank) { Chris@17: $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.'); Chris@17: } Chris@17: Chris@17: // Try with sticky, then promoted. This is a test for issue Chris@17: // https://www.drupal.org/node/771596. Chris@17: $node_ranks = [ Chris@17: 'sticky' => 10, Chris@17: 'promote' => 1, Chris@17: 'relevance' => 0, Chris@17: 'recent' => 0, Chris@17: 'comments' => 0, Chris@17: 'views' => 0, Chris@17: ]; Chris@17: $configuration = $this->nodeSearch->getPlugin()->getConfiguration(); Chris@17: foreach ($node_ranks as $var => $value) { Chris@17: $configuration['rankings'][$var] = $value; Chris@17: } Chris@17: $this->nodeSearch->getPlugin()->setConfiguration($configuration); Chris@17: $this->nodeSearch->save(); Chris@17: Chris@17: // Do the search and assert the results. The sticky node should show up Chris@17: // first, then the promoted node, then all the rest. Chris@17: $this->nodeSearch->getPlugin()->setSearch('rocks', [], []); Chris@17: $set = $this->nodeSearch->getPlugin()->execute(); Chris@17: $this->assertEqual($set[0]['node']->id(), $nodes['sticky'][1]->id(), 'Search ranking for sticky first worked.'); Chris@17: $this->assertEqual($set[1]['node']->id(), $nodes['promote'][1]->id(), 'Search ranking for promoted second worked.'); Chris@17: Chris@17: // Try with recent, then comments. This is a test for issues Chris@17: // https://www.drupal.org/node/771596 and Chris@17: // https://www.drupal.org/node/303574. Chris@17: $node_ranks = [ Chris@17: 'sticky' => 0, Chris@17: 'promote' => 0, Chris@17: 'relevance' => 0, Chris@17: 'recent' => 10, Chris@17: 'comments' => 1, Chris@17: 'views' => 0, Chris@17: ]; Chris@17: $configuration = $this->nodeSearch->getPlugin()->getConfiguration(); Chris@17: foreach ($node_ranks as $var => $value) { Chris@17: $configuration['rankings'][$var] = $value; Chris@17: } Chris@17: $this->nodeSearch->getPlugin()->setConfiguration($configuration); Chris@17: $this->nodeSearch->save(); Chris@17: Chris@17: // Do the search and assert the results. The recent node should show up Chris@17: // first, then the commented node, then all the rest. Chris@17: $this->nodeSearch->getPlugin()->setSearch('rocks', [], []); Chris@17: $set = $this->nodeSearch->getPlugin()->execute(); Chris@17: $this->assertEqual($set[0]['node']->id(), $nodes['recent'][1]->id(), 'Search ranking for recent first worked.'); Chris@17: $this->assertEqual($set[1]['node']->id(), $nodes['comments'][1]->id(), 'Search ranking for comments second worked.'); Chris@17: Chris@17: } Chris@17: Chris@17: /** Chris@17: * Test rankings of HTML tags. Chris@17: */ Chris@17: public function testHTMLRankings() { Chris@17: $full_html_format = FilterFormat::create([ Chris@17: 'format' => 'full_html', Chris@17: 'name' => 'Full HTML', Chris@17: ]); Chris@17: $full_html_format->save(); Chris@17: Chris@17: // Test HTML tags with different weights. Chris@17: $sorted_tags = ['h1', 'h2', 'h3', 'h4', 'a', 'h5', 'h6', 'notag']; Chris@17: $shuffled_tags = $sorted_tags; Chris@17: Chris@17: // Shuffle tags to ensure HTML tags are ranked properly. Chris@17: shuffle($shuffled_tags); Chris@17: $settings = [ Chris@17: 'type' => 'page', Chris@17: 'title' => 'Simple node', Chris@17: ]; Chris@17: $nodes = []; Chris@17: foreach ($shuffled_tags as $tag) { Chris@17: switch ($tag) { Chris@17: case 'a': Chris@17: $settings['body'] = [['value' => \Drupal::l('Drupal Rocks', new Url('')), 'format' => 'full_html']]; Chris@17: break; Chris@17: case 'notag': Chris@17: $settings['body'] = [['value' => 'Drupal Rocks']]; Chris@17: break; Chris@17: default: Chris@17: $settings['body'] = [['value' => "<$tag>Drupal Rocks", 'format' => 'full_html']]; Chris@17: break; Chris@17: } Chris@17: $nodes[$tag] = $this->drupalCreateNode($settings); Chris@17: } Chris@17: Chris@17: // Update the search index. Chris@17: $this->nodeSearch->getPlugin()->updateIndex(); Chris@17: search_update_totals(); Chris@17: Chris@17: $this->nodeSearch->getPlugin()->setSearch('rocks', [], []); Chris@17: // Do the search and assert the results. Chris@17: $set = $this->nodeSearch->getPlugin()->execute(); Chris@17: Chris@17: // Test the ranking of each tag. Chris@17: foreach ($sorted_tags as $tag_rank => $tag) { Chris@17: // Assert the results. Chris@17: if ($tag == 'notag') { Chris@17: $this->assertEqual($set[$tag_rank]['node']->id(), $nodes[$tag]->id(), 'Search tag ranking for plain text order.'); Chris@17: } Chris@17: else { Chris@17: $this->assertEqual($set[$tag_rank]['node']->id(), $nodes[$tag]->id(), 'Search tag ranking for "<' . $sorted_tags[$tag_rank] . '>" order.'); Chris@17: } Chris@17: } Chris@17: Chris@17: // Test tags with the same weight against the sorted tags. Chris@17: $unsorted_tags = ['u', 'b', 'i', 'strong', 'em']; Chris@17: foreach ($unsorted_tags as $tag) { Chris@17: $settings['body'] = [['value' => "<$tag>Drupal Rocks", 'format' => 'full_html']]; Chris@17: $node = $this->drupalCreateNode($settings); Chris@17: Chris@17: // Update the search index. Chris@17: $this->nodeSearch->getPlugin()->updateIndex(); Chris@17: search_update_totals(); Chris@17: Chris@17: $this->nodeSearch->getPlugin()->setSearch('rocks', [], []); Chris@17: // Do the search and assert the results. Chris@17: $set = $this->nodeSearch->getPlugin()->execute(); Chris@17: Chris@17: // Ranking should always be second to last. Chris@17: $set = array_slice($set, -2, 1); Chris@17: Chris@17: // Assert the results. Chris@17: $this->assertEqual($set[0]['node']->id(), $node->id(), 'Search tag ranking for "<' . $tag . '>" order.'); Chris@17: Chris@17: // Delete node so it doesn't show up in subsequent search results. Chris@17: $node->delete(); Chris@17: } Chris@17: } Chris@17: Chris@17: }