function StatisticsReportsTest::testPopularContentBlock

Tests the "popular content" block.

File

drupal/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsReportsTest.php, line 72
Definition of Drupal\statistics\Tests\StatisticsReportsTest.

Class

StatisticsReportsTest
Tests that report pages render properly, and that access logging works.

Namespace

Drupal\statistics\Tests

Code

function testPopularContentBlock() {

  // Visit a node to have something show up in the block.
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'page',
    'uid' => $this->blocking_user->uid,
  ));
  $this
    ->drupalGet('node/' . $node->nid);

  // Manually calling statistics.php, simulating ajax behavior.
  $nid = $node->nid;
  $post = http_build_query(array(
    'nid' => $nid,
  ));
  $headers = array(
    'Content-Type' => 'application/x-www-form-urlencoded',
  );
  global $base_url;
  $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
  drupal_http_request($stats_path, array(
    'method' => 'POST',
    'data' => $post,
    'headers' => $headers,
    'timeout' => 10000,
  ));

  // Configure and save the block.
  $block = block_load('statistics', 'popular');
  $block->theme = variable_get('theme_default', 'stark');
  $block->status = 1;
  $block->pages = '';
  $block->region = 'sidebar_first';
  $block->cache = -1;
  $block->visibility = 0;
  $edit = array(
    'statistics_block_popular_top_day_limit' => 3,
    'statistics_block_popular_top_all_limit' => 3,
    'statistics_block_popular_top_recent_limit' => 3,
  );
  module_invoke('statistics', 'block_save', 'popular', $edit);
  drupal_write_record('block', $block);

  // Get some page and check if the block is displayed.
  $this
    ->drupalGet('user');
  $this
    ->assertText('Popular content', 'Found the popular content block.');
  $this
    ->assertText("Today's", "Found today's popular content.");
  $this
    ->assertText('All time', 'Found the all time popular content.');
  $this
    ->assertText('Last viewed', 'Found the last viewed popular content.');
  $this
    ->assertRaw(l($node
    ->label(), 'node/' . $node->nid), 'Found link to visited node.');
}