public function SearchRankingTest::testRankings in Zircon Profile 8        
                          
                  
                        Same name and namespace in other branches
- 8.0 core/modules/search/src/Tests/SearchRankingTest.php \Drupal\search\Tests\SearchRankingTest::testRankings()
File
 
   - core/modules/search/src/Tests/SearchRankingTest.php, line 47
- Contains \Drupal\search\Tests\SearchRankingTest.
Class
  
  - SearchRankingTest 
- Indexes content and tests ranking factors.
Namespace
  Drupal\search\Tests
Code
public function testRankings() {
  
  $this
    ->addDefaultCommentField('node', 'page');
  
  $node_ranks = array(
    'sticky',
    'promote',
    'relevance',
    'recent',
    'comments',
    'views',
  );
  
  $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':
            
            $settings['created'] = REQUEST_TIME - 3600;
            break;
          case 'comments':
            $settings['comment'][0]['status'] = CommentItemInterface::OPEN;
            break;
        }
      }
      $nodes[$node_rank][$num] = $this
        ->drupalCreateNode($settings);
    }
  }
  
  $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'));
  
  $this
    ->config('statistics.settings')
    ->set('count_content_views', 1)
    ->save();
  
  $nid = $nodes['views'][1]
    ->id();
  db_insert('node_counter')
    ->fields(array(
    'totalcount' => 5,
    'daycount' => 5,
    'timestamp' => REQUEST_TIME,
    'nid' => $nid,
  ))
    ->execute();
  
  $this
    ->cronRun();
  
  $this
    ->drupalGet('admin/config/search/pages/manage/node_search');
  $this
    ->assertText(t('Content ranking'));
  
  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.');
  }
  
  $edit = array();
  foreach ($node_ranks as $node_rank) {
    
    $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.');
    
    $this->nodeSearch = entity_load('search_page', 'node_search');
    
    $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.');
    
    $edit['rankings[' . $node_rank . '][value]'] = 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.');
  }
  
  $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();
  
  $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.');
  
  $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();
  
  $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.');
}