Commit 3053c5e9 authored by catch's avatar catch

Issue #1853540 by damiankloip, dawehner, xjm: Reintroduce Views integration for statistics.module.

parent c7212560
<?php
/**
* @file
* Contains \Drupal\statistics\Tests\Views\IntegrationTest.
*/
namespace Drupal\statistics\Tests\Views;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
/**
* Tests basic integration of views data from the statistics module.
*
* @see
*/
class IntegrationTest extends ViewTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('statistics', 'statistics_test_views');
/**
* Stores the user object that accesses the page.
*
* @var \Drupal\user\Plugin\Core\Entity\User
*/
protected $webUser;
/**
* Stores the node object which is used by the test.
*
* @var \Drupal\node\Plugin\Core\Entity\Node
*/
protected $node;
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_statistics_integration');
public static function getInfo() {
return array(
'name' => 'Statistics: Integration tests',
'description' => 'Tests basic integration of views data from the statistics module.',
'group' => 'Views Modules',
);
}
protected function setUp() {
parent::setUp();
ViewTestData::importTestViews(get_class($this), array('statistics_test_views'));
// Create a new user for viewing nodes.
$this->webUser = $this->drupalCreateUser(array('access content'));
$this->node = $this->drupalCreateNode(array('type' => 'page'));
// Enable access logging.
$this->container->get('config.factory')->get('statistics.settings')
->set('access_log.enabled', 1)
->set('count_content_views', 1)
->save();
$this->drupalLogin($this->webUser);
}
/**
* Tests the integration of the {node_counter} table in views.
*/
public function testNodeCounterIntegration() {
$this->drupalGet('node/' . $this->node->id());
// Manually calling statistics.php, simulating ajax behavior.
// @see \Drupal\statistics\Tests\StatisticsLoggingTest::testLogging().
$post = http_build_query(array('nid' => $this->node->id()));
$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->drupalGet('test_statistics_integration');
$expected = statistics_get($this->node->id());
// Convert the timestamp to year, to match the expected output of the date
// handler.
$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.");
}
}
}
<?php
/**
* @file
* Provide views data and handlers for statistics.module.
*
* @ingroup views_module_handlers
*/
/**
* Implements hook_views_data().
*/
function statistics_views_data() {
$data['node_counter']['table']['group'] = t('Content statistics');
$data['node_counter']['table']['join'] = array(
'node' => array(
'left_field' => 'nid',
'field' => 'nid',
),
);
$data['node_counter']['totalcount'] = array(
'title' => t('Total views'),
'help' => t('The total number of times the node has been viewed.'),
'field' => array(
'id' => 'numeric',
'click sortable' => TRUE,
),
'filter' => array(
'id' => 'numeric',
),
'argument' => array(
'id' => 'numeric',
),
'sort' => array(
'id' => 'standard',
),
);
$data['node_counter']['daycount'] = array(
'title' => t('Views today'),
'help' => t('The total number of times the node has been viewed today.'),
'field' => array(
'id' => 'numeric',
'click sortable' => TRUE,
),
'filter' => array(
'id' => 'numeric',
),
'argument' => array(
'id' => 'numeric',
),
'sort' => array(
'id' => 'standard',
),
);
$data['node_counter']['timestamp'] = array(
'title' => t('Most recent view'),
'help' => t('The most recent time the node has been viewed.'),
'field' => array(
'id' => 'date',
'click sortable' => TRUE,
),
'filter' => array(
'id' => 'date',
),
'argument' => array(
'id' => 'date',
),
'sort' => array(
'id' => 'standard',
),
);
return $data;
}
name = Statistics test views
description = Provides default views for views statistics tests.
package = Testing
version = VERSION
core = 8.x
dependencies[] = statistics
dependencies[] = views
hidden = TRUE
api_version: '3.0'
base_field: nid
base_table: node
core: 8.x
description: ''
disabled: '0'
display:
default:
display_plugin: default
id: default
display_title: Master
position: ''
display_options:
access:
type: perm
cache:
type: none
query:
type: views_query
exposed_form:
type: basic
pager:
type: none
options:
offset: '0'
style:
type: default
row:
type: fields
fields:
title:
id: title
table: node
field: title
label: ''
alter:
alter_text: '0'
make_link: '0'
absolute: '0'
trim: '0'
word_boundary: '0'
ellipsis: '0'
strip_tags: '0'
html: '0'
hide_empty: '0'
empty_zero: '0'
link_to_node: '1'
timestamp:
id: timestamp
table: node_counter
field: timestamp
relationship: none
group_type: group
admin_label: ''
label: 'Most recent view'
exclude: '0'
alter:
alter_text: '0'
text: ''
make_link: '0'
path: ''
absolute: '0'
external: '0'
replace_spaces: '0'
path_case: none
trim_whitespace: '0'
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: '0'
max_length: ''
word_boundary: '1'
ellipsis: '1'
more_link: '0'
more_link_text: ''
more_link_path: ''
strip_tags: '0'
trim: '0'
preserve_tags: ''
html: '0'
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: '1'
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: '1'
empty: ''
hide_empty: '0'
empty_zero: '0'
hide_alter_empty: '1'
date_format: html_year
custom_date_format: ''
timezone: ''
totalcount:
id: totalcount
table: node_counter
field: totalcount
relationship: none
group_type: group
admin_label: ''
label: 'Total views'
exclude: '0'
alter:
alter_text: '0'
text: ''
make_link: '0'
path: ''
absolute: '0'
external: '0'
replace_spaces: '0'
path_case: none
trim_whitespace: '0'
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: '0'
max_length: ''
word_boundary: '1'
ellipsis: '1'
more_link: '0'
more_link_text: ''
more_link_path: ''
strip_tags: '0'
trim: '0'
preserve_tags: ''
html: '0'
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: '1'
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: '1'
empty: ''
hide_empty: '0'
empty_zero: '0'
hide_alter_empty: '1'
set_precision: '0'
precision: '0'
decimal: .
separator: ''
format_plural: '0'
format_plural_singular: '1'
format_plural_plural: '@count'
prefix: ''
suffix: ''
daycount:
id: daycount
table: node_counter
field: daycount
relationship: none
group_type: group
admin_label: ''
label: 'Views today'
exclude: '0'
alter:
alter_text: '0'
text: ''
make_link: '0'
path: ''
absolute: '0'
external: '0'
replace_spaces: '0'
path_case: none
trim_whitespace: '0'
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: '0'
max_length: ''
word_boundary: '1'
ellipsis: '1'
more_link: '0'
more_link_text: ''
more_link_path: ''
strip_tags: '0'
trim: '0'
preserve_tags: ''
html: '0'
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: '1'
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: '1'
empty: ''
hide_empty: '0'
empty_zero: '0'
hide_alter_empty: '1'
set_precision: '0'
precision: '0'
decimal: .
separator: ''
format_plural: '0'
format_plural_singular: '1'
format_plural_plural: '@count'
prefix: ''
suffix: ''
filters:
status:
value: '1'
table: node
field: status
id: status
expose:
operator: '0'
group: '1'
sorts:
created:
id: created
table: node
field: created
order: DESC
page_1:
display_plugin: page
id: page_1
display_title: Page
position: ''
display_options:
path: test_statistics_integration
human_name: 'Test statistics integration'
module: views
id: test_statistics_integration
tag: ''
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