Commit e45b1a76 authored by alexpott's avatar alexpott

Issue #1867382 by Berdir, Sutharsan: Convert drupal_http_request() usage in...

Issue #1867382 by Berdir, Sutharsan:  Convert drupal_http_request() usage in statistics.module to Guzzle.
parent 15f94a42
......@@ -37,6 +37,13 @@ class StatisticsAdminTest extends WebTestBase {
*/
protected $test_node;
/**
* The Guzzle HTTP client.
*
* @var \Guzzle\Http\ClientInterface;
*/
protected $client;
public static function getInfo() {
return array(
'name' => 'Test statistics admin.',
......@@ -55,6 +62,8 @@ function setUp() {
$this->privileged_user = $this->drupalCreateUser(array('administer statistics', 'view post access counter', 'create page content'));
$this->drupalLogin($this->privileged_user);
$this->test_node = $this->drupalCreateNode(array('type' => 'page', 'uid' => $this->privileged_user->uid));
$this->client = \Drupal::httpClient();
$this->client->setConfig(array('curl.options' => array(CURLOPT_TIMEOUT => 10)));
}
/**
......@@ -78,16 +87,16 @@ function testStatisticsSettings() {
$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));
$this->client->post($stats_path, $headers, $post)->send();
// Hit the node again (the counter is incremented after the hit, so
// "1 view" will actually be shown when the node is hit the second time).
$this->drupalGet('node/' . $this->test_node->nid);
drupal_http_request($stats_path, array('method' => 'POST', 'data' => $post, 'headers' => $headers, 'timeout' => 10000));
$this->client->post($stats_path, $headers, $post)->send();
$this->assertText('1 view', 'Node is viewed once.');
$this->drupalGet('node/' . $this->test_node->nid);
drupal_http_request($stats_path, array('method' => 'POST', 'data' => $post, 'headers' => $headers, 'timeout' => 10000));
$this->client->post($stats_path, $headers, $post)->send();
$this->assertText('2 views', 'Node is viewed 2 times.');
}
......@@ -104,7 +113,7 @@ function testDeleteNode() {
$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));
$this->client->post($stats_path, $headers, $post)->send();
$result = db_select('node_counter', 'n')
->fields('n', array('nid'))
......@@ -139,9 +148,9 @@ function testExpiredLogs() {
$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));
$this->client->post($stats_path, $headers, $post)->send();
$this->drupalGet('node/' . $this->test_node->nid);
drupal_http_request($stats_path, array('method' => 'POST', 'data' => $post, 'headers' => $headers, 'timeout' => 10000));
$this->client->post($stats_path, $headers, $post)->send();
$this->assertText('1 view', 'Node is viewed once.');
// statistics_cron() will subtract
......
......@@ -25,6 +25,13 @@ class StatisticsLoggingTest extends WebTestBase {
*/
public static $modules = array('statistics', 'block');
/**
* The Guzzle HTTP client.
*
* @var \Guzzle\Http\ClientInterface;
*/
protected $client;
public static function getInfo() {
return array(
'name' => 'Statistics logging tests',
......@@ -59,6 +66,9 @@ function setUp() {
// Clear the logs.
db_truncate('node_counter');
$this->client = \Drupal::httpClient();
$this->client->setConfig(array('curl.options' => array(CURLOPT_TIMEOUT => 10)));
}
/**
......@@ -79,7 +89,7 @@ function testLogging() {
$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));
$this->client->post($stats_path, $headers, $post)->send();
$this->assertIdentical($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Testing an uncached page.');
$node_counter = statistics_get($this->node->nid);
$this->assertIdentical($node_counter['totalcount'], '1');
......@@ -87,7 +97,7 @@ function testLogging() {
// Verify logging of a cached page.
$this->drupalGet($path);
// Manually calling statistics.php, simulating ajax behavior.
drupal_http_request($stats_path, array('method' => 'POST', 'data' => $post, 'headers' => $headers, 'timeout' => 10000));
$this->client->post($stats_path, $headers, $post)->send();
$this->assertIdentical($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Testing a cached page.');
$node_counter = statistics_get($this->node->nid);
$this->assertIdentical($node_counter['totalcount'], '2');
......@@ -96,7 +106,7 @@ function testLogging() {
$this->drupalLogin($this->auth_user);
$this->drupalGet($path);
// Manually calling statistics.php, simulating ajax behavior.
drupal_http_request($stats_path, array('method' => 'POST', 'data' => $post, 'headers' => $headers, 'timeout' => 10000));
$this->client->post($stats_path, $headers, $post)->send();
$node_counter = statistics_get($this->node->nid);
$this->assertIdentical($node_counter['totalcount'], '3');
......
......@@ -36,7 +36,9 @@ function testPopularContentBlock() {
$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));
$client = \Drupal::httpClient();
$client->setConfig(array('curl.options' => array(CURLOPT_TIMEOUT => 10)));
$client->post($stats_path, $headers, $post)->send();
// Configure and save the block.
$this->drupalPlaceBlock('statistics_popular_block', array('label' => 'Popular content'), array(
......
......@@ -38,7 +38,9 @@ function testStatisticsTokenReplacement() {
$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));
$client = \Drupal::httpClient();
$client->setConfig(array('curl.options' => array(CURLOPT_TIMEOUT => 10)));
$client->post($stats_path, $headers, $post)->send();
$statistics = statistics_get($node->nid);
// Generate and test tokens.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment