Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
|
@ -0,0 +1,144 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\statistics\Plugin\Block\StatisticsPopularBlock.
|
||||
*/
|
||||
|
||||
namespace Drupal\statistics\Plugin\Block;
|
||||
|
||||
use Drupal\Core\Access\AccessResult;
|
||||
use Drupal\Core\Block\BlockBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
|
||||
/**
|
||||
* Provides a 'Popular content' block.
|
||||
*
|
||||
* @Block(
|
||||
* id = "statistics_popular_block",
|
||||
* admin_label = @Translation("Popular content")
|
||||
* )
|
||||
*/
|
||||
class StatisticsPopularBlock extends BlockBase {
|
||||
|
||||
/**
|
||||
* Number of day's top views to display.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $day_list;
|
||||
|
||||
/**
|
||||
* Number of all time views to display.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $all_time_list;
|
||||
|
||||
/**
|
||||
* Number of most recent views to display.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $last_list;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return array(
|
||||
'top_day_num' => 0,
|
||||
'top_all_num' => 0,
|
||||
'top_last_num' => 0
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function blockAccess(AccountInterface $account) {
|
||||
$access = AccessResult::allowedIfHasPermission($account, 'access content');
|
||||
if ($account->hasPermission('access content')) {
|
||||
$daytop = $this->configuration['top_day_num'];
|
||||
if (!$daytop || !($result = statistics_title_list('daycount', $daytop)) || !($this->day_list = node_title_list($result, $this->t("Today's:")))) {
|
||||
return AccessResult::forbidden()->inheritCacheability($access);
|
||||
}
|
||||
$alltimetop = $this->configuration['top_all_num'];
|
||||
if (!$alltimetop || !($result = statistics_title_list('totalcount', $alltimetop)) || !($this->all_time_list = node_title_list($result, $this->t('All time:')))) {
|
||||
return AccessResult::forbidden()->inheritCacheability($access);
|
||||
}
|
||||
$lasttop = $this->configuration['top_last_num'];
|
||||
if (!$lasttop || !($result = statistics_title_list('timestamp', $lasttop)) || !($this->last_list = node_title_list($result, $this->t('Last viewed:')))) {
|
||||
return AccessResult::forbidden()->inheritCacheability($access);
|
||||
}
|
||||
return $access;
|
||||
}
|
||||
return AccessResult::forbidden()->inheritCacheability($access);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function blockForm($form, FormStateInterface $form_state) {
|
||||
// Popular content block settings.
|
||||
$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40);
|
||||
$numbers = array('0' => $this->t('Disabled')) + array_combine($numbers, $numbers);
|
||||
$form['statistics_block_top_day_num'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t("Number of day's top views to display"),
|
||||
'#default_value' => $this->configuration['top_day_num'],
|
||||
'#options' => $numbers,
|
||||
'#description' => $this->t('How many content items to display in "day" list.'),
|
||||
);
|
||||
$form['statistics_block_top_all_num'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('Number of all time views to display'),
|
||||
'#default_value' => $this->configuration['top_all_num'],
|
||||
'#options' => $numbers,
|
||||
'#description' => $this->t('How many content items to display in "all time" list.'),
|
||||
);
|
||||
$form['statistics_block_top_last_num'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('Number of most recent views to display'),
|
||||
'#default_value' => $this->configuration['top_last_num'],
|
||||
'#options' => $numbers,
|
||||
'#description' => $this->t('How many content items to display in "recently viewed" list.'),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function blockSubmit($form, FormStateInterface $form_state) {
|
||||
$this->configuration['top_day_num'] = $form_state->getValue('statistics_block_top_day_num');
|
||||
$this->configuration['top_all_num'] = $form_state->getValue('statistics_block_top_all_num');
|
||||
$this->configuration['top_last_num'] = $form_state->getValue('statistics_block_top_last_num');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
$content = array();
|
||||
|
||||
if ($this->day_list) {
|
||||
$content['top_day'] = $this->day_list;
|
||||
$content['top_day']['#suffix'] = '<br />';
|
||||
}
|
||||
|
||||
if ($this->all_time_list) {
|
||||
$content['top_all'] = $this->all_time_list;
|
||||
$content['top_all']['#suffix'] = '<br />';
|
||||
}
|
||||
|
||||
if ($this->last_list) {
|
||||
$content['top_last'] = $this->last_list;
|
||||
$content['top_last']['#suffix'] = '<br />';
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
}
|
104
core/modules/statistics/src/StatisticsSettingsForm.php
Normal file
104
core/modules/statistics/src/StatisticsSettingsForm.php
Normal file
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\statistics\StatisticsSettingsForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\statistics;
|
||||
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\Form\ConfigFormBase;
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Configure statistics settings for this site.
|
||||
*/
|
||||
class StatisticsSettingsForm extends ConfigFormBase {
|
||||
|
||||
/**
|
||||
* The module handler.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ModuleHandlerInterface
|
||||
*/
|
||||
protected $moduleHandler;
|
||||
|
||||
/**
|
||||
* Constructs a \Drupal\user\StatisticsSettingsForm object.
|
||||
*
|
||||
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
||||
* The factory for configuration objects.
|
||||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
||||
* The module handler.
|
||||
*/
|
||||
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
|
||||
parent::__construct($config_factory);
|
||||
|
||||
$this->moduleHandler = $module_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('config.factory'),
|
||||
$container->get('module_handler')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'statistics_settings_form';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getEditableConfigNames() {
|
||||
return ['statistics.settings'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state) {
|
||||
$config = $this->config('statistics.settings');
|
||||
|
||||
// Content counter settings.
|
||||
$form['content'] = array(
|
||||
'#type' => 'details',
|
||||
'#title' => t('Content viewing counter settings'),
|
||||
'#open' => TRUE,
|
||||
);
|
||||
$form['content']['statistics_count_content_views'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Count content views'),
|
||||
'#default_value' => $config->get('count_content_views'),
|
||||
'#description' => t('Increment a counter each time content is viewed.'),
|
||||
);
|
||||
|
||||
return parent::buildForm($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
$this->config('statistics.settings')
|
||||
->set('count_content_views', $form_state->getValue('statistics_count_content_views'))
|
||||
->save();
|
||||
|
||||
// The popular statistics block is dependent on these settings, so clear the
|
||||
// block plugin definitions cache.
|
||||
if ($this->moduleHandler->moduleExists('block')) {
|
||||
\Drupal::service('plugin.manager.block')->clearCachedDefinitions();
|
||||
}
|
||||
|
||||
parent::submitForm($form, $form_state);
|
||||
}
|
||||
}
|
||||
|
162
core/modules/statistics/src/Tests/StatisticsAdminTest.php
Normal file
162
core/modules/statistics/src/Tests/StatisticsAdminTest.php
Normal file
|
@ -0,0 +1,162 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\statistics\Tests\StatisticsAdminTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\statistics\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests the statistics admin.
|
||||
*
|
||||
* @group statistics
|
||||
*/
|
||||
class StatisticsAdminTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'statistics');
|
||||
|
||||
/**
|
||||
* A user that has permission to administer statistics.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $privilegedUser;
|
||||
|
||||
/**
|
||||
* A page node for which to check content statistics.
|
||||
*
|
||||
* @var \Drupal\node\NodeInterface
|
||||
*/
|
||||
protected $testNode;
|
||||
|
||||
/**
|
||||
* The Guzzle HTTP client.
|
||||
*
|
||||
* @var \GuzzleHttp\ClientInterface;
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create Basic page node type.
|
||||
if ($this->profile != 'standard') {
|
||||
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
|
||||
}
|
||||
$this->privilegedUser = $this->drupalCreateUser(array('administer statistics', 'view post access counter', 'create page content'));
|
||||
$this->drupalLogin($this->privilegedUser);
|
||||
$this->testNode = $this->drupalCreateNode(array('type' => 'page', 'uid' => $this->privilegedUser->id()));
|
||||
$this->client = \Drupal::httpClient();
|
||||
$this->client->setDefaultOption('config/curl', array(CURLOPT_TIMEOUT => 10));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the statistics settings page works.
|
||||
*/
|
||||
function testStatisticsSettings() {
|
||||
$config = $this->config('statistics.settings');
|
||||
$this->assertFalse($config->get('count_content_views'), 'Count content view log is disabled by default.');
|
||||
|
||||
// Enable counter on content view.
|
||||
$edit['statistics_count_content_views'] = 1;
|
||||
$this->drupalPostForm('admin/config/system/statistics', $edit, t('Save configuration'));
|
||||
$config = $this->config('statistics.settings');
|
||||
$this->assertTrue($config->get('count_content_views'), 'Count content view log is enabled.');
|
||||
|
||||
// Hit the node.
|
||||
$this->drupalGet('node/' . $this->testNode->id());
|
||||
// Manually calling statistics.php, simulating ajax behavior.
|
||||
$nid = $this->testNode->id();
|
||||
$post = array('nid' => $nid);
|
||||
global $base_url;
|
||||
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php';
|
||||
$this->client->post($stats_path, array('body' => $post));
|
||||
|
||||
// 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->testNode->id());
|
||||
$this->client->post($stats_path, array('body' => $post));
|
||||
$this->assertText('1 view', 'Node is viewed once.');
|
||||
|
||||
$this->drupalGet('node/' . $this->testNode->id());
|
||||
$this->client->post($stats_path, array('body' => $post));
|
||||
$this->assertText('2 views', 'Node is viewed 2 times.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that when a node is deleted, the node counter is deleted too.
|
||||
*/
|
||||
function testDeleteNode() {
|
||||
$this->config('statistics.settings')->set('count_content_views', 1)->save();
|
||||
|
||||
$this->drupalGet('node/' . $this->testNode->id());
|
||||
// Manually calling statistics.php, simulating ajax behavior.
|
||||
$nid = $this->testNode->id();
|
||||
$post = array('nid' => $nid);
|
||||
global $base_url;
|
||||
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php';
|
||||
$this->client->post($stats_path, array('body' => $post));
|
||||
|
||||
$result = db_select('node_counter', 'n')
|
||||
->fields('n', array('nid'))
|
||||
->condition('n.nid', $this->testNode->id())
|
||||
->execute()
|
||||
->fetchAssoc();
|
||||
$this->assertEqual($result['nid'], $this->testNode->id(), 'Verifying that the node counter is incremented.');
|
||||
|
||||
$this->testNode->delete();
|
||||
|
||||
$result = db_select('node_counter', 'n')
|
||||
->fields('n', array('nid'))
|
||||
->condition('n.nid', $this->testNode->id())
|
||||
->execute()
|
||||
->fetchAssoc();
|
||||
$this->assertFalse($result, 'Verifying that the node counter is deleted.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that cron clears day counts and expired access logs.
|
||||
*/
|
||||
function testExpiredLogs() {
|
||||
$this->config('statistics.settings')
|
||||
->set('count_content_views', 1)
|
||||
->save();
|
||||
\Drupal::state()->set('statistics.day_timestamp', 8640000);
|
||||
|
||||
$this->drupalGet('node/' . $this->testNode->id());
|
||||
// Manually calling statistics.php, simulating ajax behavior.
|
||||
$nid = $this->testNode->id();
|
||||
$post = array('nid' => $nid);
|
||||
global $base_url;
|
||||
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php';
|
||||
$this->client->post($stats_path, array('body' => $post));
|
||||
$this->drupalGet('node/' . $this->testNode->id());
|
||||
$this->client->post($stats_path, array('body' => $post));
|
||||
$this->assertText('1 view', 'Node is viewed once.');
|
||||
|
||||
// statistics_cron() will subtract
|
||||
// statistics.settings:accesslog.max_lifetime config from REQUEST_TIME in
|
||||
// the delete query, so wait two secs here to make sure the access log will
|
||||
// be flushed for the node just hit.
|
||||
sleep(2);
|
||||
$this->cronRun();
|
||||
|
||||
$this->drupalGet('admin/reports/pages');
|
||||
$this->assertNoText('node/' . $this->testNode->id(), 'No hit URL found.');
|
||||
|
||||
$result = db_select('node_counter', 'nc')
|
||||
->fields('nc', array('daycount'))
|
||||
->condition('nid', $this->testNode->id(), '=')
|
||||
->execute()
|
||||
->fetchField();
|
||||
$this->assertFalse($result, 'Daycounter is zero.');
|
||||
}
|
||||
}
|
102
core/modules/statistics/src/Tests/StatisticsLoggingTest.php
Normal file
102
core/modules/statistics/src/Tests/StatisticsLoggingTest.php
Normal file
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\statistics\Tests\StatisticsLoggingTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\statistics\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests request logging for cached and uncached pages.
|
||||
*
|
||||
* We subclass WebTestBase rather than StatisticsTestBase, because we
|
||||
* want to test requests from an anonymous user.
|
||||
*
|
||||
* @group statistics
|
||||
*/
|
||||
class StatisticsLoggingTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'statistics', 'block');
|
||||
|
||||
/**
|
||||
* User with permissions to create and edit pages.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $authUser;
|
||||
|
||||
/**
|
||||
* The Guzzle HTTP client.
|
||||
*
|
||||
* @var \GuzzleHttp\ClientInterface;
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create Basic page node type.
|
||||
if ($this->profile != 'standard') {
|
||||
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
|
||||
}
|
||||
|
||||
$this->authUser = $this->drupalCreateUser(array('access content', 'create page content', 'edit own page content'));
|
||||
|
||||
// Ensure we have a node page to access.
|
||||
$this->node = $this->drupalCreateNode(array('title' => $this->randomMachineName(255), 'uid' => $this->authUser->id()));
|
||||
|
||||
// Enable access logging.
|
||||
$this->config('statistics.settings')
|
||||
->set('count_content_views', 1)
|
||||
->save();
|
||||
|
||||
// Clear the logs.
|
||||
db_truncate('node_counter');
|
||||
|
||||
$this->client = \Drupal::httpClient();
|
||||
$this->client->setDefaultOption('config/curl', array(CURLOPT_TIMEOUT => 10));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies node hit counter logging and script placement.
|
||||
*/
|
||||
function testLogging() {
|
||||
global $base_url;
|
||||
$path = 'node/' . $this->node->id();
|
||||
$module_path = drupal_get_path('module', 'statistics');
|
||||
$stats_path = $base_url . '/' . $module_path . '/statistics.php';
|
||||
$expected_library = $module_path . '/statistics.js';
|
||||
$expected_settings = '"statistics":{"data":{"nid":"' . $this->node->id() . '"}';
|
||||
|
||||
// Verify that logging scripts are not found on a non-node page.
|
||||
$this->drupalGet('node');
|
||||
$this->assertNoRaw($expected_library, 'Statistics library JS not found on node page.');
|
||||
$this->assertNoRaw($expected_settings, 'Statistics settings not found on node page.');
|
||||
|
||||
// Verify that logging scripts are not found on a non-existent node page.
|
||||
$this->drupalGet('node/9999');
|
||||
$this->assertNoRaw($expected_library, 'Statistics library JS not found on non-existent node page.');
|
||||
$this->assertNoRaw($expected_settings, 'Statistics settings not found on non-existent node page.');
|
||||
|
||||
// Verify that logging scripts are found on a valid node page.
|
||||
$this->drupalGet($path);
|
||||
$this->assertRaw($expected_library, 'Found statistics library JS on node page.');
|
||||
$this->assertRaw($expected_settings, 'Found statistics settings on node page.');
|
||||
|
||||
// Manually call statistics.php to simulate ajax data collection behavior.
|
||||
$nid = $this->node->id();
|
||||
$post = array('nid' => $nid);
|
||||
$this->client->post($stats_path, array('body' => $post));
|
||||
$node_counter = statistics_get($this->node->id());
|
||||
$this->assertIdentical($node_counter['totalcount'], '1');
|
||||
}
|
||||
|
||||
}
|
57
core/modules/statistics/src/Tests/StatisticsReportsTest.php
Normal file
57
core/modules/statistics/src/Tests/StatisticsReportsTest.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\statistics\Tests\StatisticsReportsTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\statistics\Tests;
|
||||
|
||||
/**
|
||||
* Tests display of statistics report blocks.
|
||||
*
|
||||
* @group statistics
|
||||
*/
|
||||
class StatisticsReportsTest extends StatisticsTestBase {
|
||||
|
||||
/**
|
||||
* Tests the "popular content" block.
|
||||
*/
|
||||
function testPopularContentBlock() {
|
||||
// Clear the block cache to load the Statistics module's block definitions.
|
||||
$this->container->get('plugin.manager.block')->clearCachedDefinitions();
|
||||
|
||||
// Visit a node to have something show up in the block.
|
||||
$node = $this->drupalCreateNode(array('type' => 'page', 'uid' => $this->blockingUser->id()));
|
||||
$this->drupalGet('node/' . $node->id());
|
||||
// Manually calling statistics.php, simulating ajax behavior.
|
||||
$nid = $node->id();
|
||||
$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';
|
||||
$client = \Drupal::httpClient();
|
||||
$client->setDefaultOption('config/curl', array(CURLOPT_TIMEOUT => 10));
|
||||
$client->post($stats_path, array('headers' => $headers, 'body' => $post));
|
||||
|
||||
// Configure and save the block.
|
||||
$this->drupalPlaceBlock('statistics_popular_block', array(
|
||||
'label' => 'Popular content',
|
||||
'top_day_num' => 3,
|
||||
'top_all_num' => 3,
|
||||
'top_last_num' => 3,
|
||||
));
|
||||
|
||||
// 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.');
|
||||
|
||||
// statistics.module doesn't use node entities, prevent the node language
|
||||
// from being added to the options.
|
||||
$this->assertRaw(\Drupal::l($node->label(), $node->urlInfo('canonical', ['language' => NULL])), 'Found link to visited node.');
|
||||
}
|
||||
|
||||
}
|
55
core/modules/statistics/src/Tests/StatisticsTestBase.php
Normal file
55
core/modules/statistics/src/Tests/StatisticsTestBase.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\statistics\Tests\StatisticsTestBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\statistics\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Defines a base class for testing the Statistics module.
|
||||
*/
|
||||
abstract class StatisticsTestBase extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'block', 'ban', 'statistics');
|
||||
|
||||
/**
|
||||
* User with permissions to ban IP's.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $blockingUser;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create Basic page node type.
|
||||
if ($this->profile != 'standard') {
|
||||
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
|
||||
}
|
||||
|
||||
// Create user.
|
||||
$this->blockingUser = $this->drupalCreateUser(array(
|
||||
'access administration pages',
|
||||
'access site reports',
|
||||
'ban IP addresses',
|
||||
'administer blocks',
|
||||
'administer statistics',
|
||||
'administer users',
|
||||
));
|
||||
$this->drupalLogin($this->blockingUser);
|
||||
|
||||
// Enable logging.
|
||||
$this->config('statistics.settings')
|
||||
->set('count_content_views', 1)
|
||||
->save();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\statistics\Tests\StatisticsTokenReplaceTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\statistics\Tests;
|
||||
|
||||
/**
|
||||
* Generates text using placeholders for dummy content to check statistics token
|
||||
* replacement.
|
||||
*
|
||||
* @group statistics
|
||||
*/
|
||||
class StatisticsTokenReplaceTest extends StatisticsTestBase {
|
||||
/**
|
||||
* Creates a node, then tests the statistics tokens generated from it.
|
||||
*/
|
||||
function testStatisticsTokenReplacement() {
|
||||
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
|
||||
|
||||
// Create user and node.
|
||||
$user = $this->drupalCreateUser(array('create page content'));
|
||||
$this->drupalLogin($user);
|
||||
$node = $this->drupalCreateNode(array('type' => 'page', 'uid' => $user->id()));
|
||||
|
||||
// Hit the node.
|
||||
$this->drupalGet('node/' . $node->id());
|
||||
// Manually calling statistics.php, simulating ajax behavior.
|
||||
$nid = $node->id();
|
||||
$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';
|
||||
$client = \Drupal::httpClient();
|
||||
$client->setDefaultOption('config/curl', array(CURLOPT_TIMEOUT => 10));
|
||||
$client->post($stats_path, array('headers' => $headers, 'body' => $post));
|
||||
$statistics = statistics_get($node->id());
|
||||
|
||||
// Generate and test tokens.
|
||||
$tests = array();
|
||||
$tests['[node:total-count]'] = 1;
|
||||
$tests['[node:day-count]'] = 1;
|
||||
$tests['[node:last-view]'] = format_date($statistics['timestamp']);
|
||||
$tests['[node:last-view:short]'] = format_date($statistics['timestamp'], 'short');
|
||||
|
||||
// Test to make sure that we generated something for each token.
|
||||
$this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
|
||||
|
||||
foreach ($tests as $input => $expected) {
|
||||
$output = \Drupal::token()->replace($input, array('node' => $node), array('langcode' => $language_interface->getId()));
|
||||
$this->assertEqual($output, $expected, format_string('Statistics token %token replaced.', array('%token' => $input)));
|
||||
}
|
||||
}
|
||||
}
|
95
core/modules/statistics/src/Tests/Views/IntegrationTest.php
Normal file
95
core/modules/statistics/src/Tests/Views/IntegrationTest.php
Normal file
|
@ -0,0 +1,95 @@
|
|||
<?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.
|
||||
*
|
||||
* @group statistics
|
||||
* @see
|
||||
*/
|
||||
class IntegrationTest extends ViewTestBase {
|
||||
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('statistics', 'statistics_test_views', 'node');
|
||||
|
||||
/**
|
||||
* Stores the user object that accesses the page.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $webUser;
|
||||
|
||||
/**
|
||||
* Stores the node object which is used by the test.
|
||||
*
|
||||
* @var \Drupal\node\Entity\Node
|
||||
*/
|
||||
protected $node;
|
||||
|
||||
/**
|
||||
* Views used by this test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $testViews = array('test_statistics_integration');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
ViewTestData::createTestViews(get_class($this), array('statistics_test_views'));
|
||||
|
||||
// Create a new user for viewing nodes.
|
||||
$this->webUser = $this->drupalCreateUser(array('access content'));
|
||||
|
||||
$this->drupalCreateContentType(array('type' => 'page'));
|
||||
$this->node = $this->drupalCreateNode(array('type' => 'page'));
|
||||
|
||||
// Enable access logging.
|
||||
$this->config('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().
|
||||
global $base_url;
|
||||
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php';
|
||||
$client = \Drupal::httpClient();
|
||||
$client->setDefaultOption('config/curl', array(CURLOPT_TIMEOUT => 10));
|
||||
$client->post($stats_path, array('body' => array('nid' => $this->node->id())));
|
||||
$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.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue