View source
<?php
namespace Drupal\statistics\Tests\Views;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
class IntegrationTest extends ViewTestBase {
public static $modules = array(
'statistics',
'statistics_test_views',
'node',
);
protected $webUser;
protected $node;
public static $testViews = array(
'test_statistics_integration',
);
protected function setUp() {
parent::setUp();
ViewTestData::createTestViews(get_class($this), array(
'statistics_test_views',
));
$this->webUser = $this
->drupalCreateUser(array(
'access content',
));
$this
->drupalCreateContentType(array(
'type' => 'page',
));
$this->node = $this
->drupalCreateNode(array(
'type' => 'page',
));
$this
->config('statistics.settings')
->set('access_log.enabled', 1)
->set('count_content_views', 1)
->save();
$this
->drupalLogin($this->webUser);
}
public function testNodeCounterIntegration() {
$this
->drupalGet('node/' . $this->node
->id());
global $base_url;
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
$client = \Drupal::service('http_client_factory')
->fromOptions([
'config/curl',
array(
CURLOPT_TIMEOUT => 10,
),
]);
$client
->post($stats_path, array(
'form_params' => array(
'nid' => $this->node
->id(),
),
));
$this
->drupalGet('test_statistics_integration');
$expected = statistics_get($this->node
->id());
$expected['timestamp'] = date('Y', $expected['timestamp']);
foreach ($expected as $field => $value) {
$xpath = "//div[contains(@class, views-field-{$field})]/span[@class = 'field-content']";
$this
->assertFieldByXpath($xpath, $value, "The {$field} output matches the expected.");
}
}
}