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