View source
<?php
namespace Drupal\Tests\statistics\Functional;
use Drupal\Core\Database\Database;
use Drupal\Tests\BrowserTestBase;
use Drupal\node\Entity\Node;
class StatisticsLoggingTest extends BrowserTestBase {
public static $modules = [
'node',
'statistics',
'block',
'locale',
];
protected $defaultTheme = 'stark';
protected $authUser;
protected $language;
protected $client;
protected function setUp() {
parent::setUp();
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
}
$this->authUser = $this
->drupalCreateUser([
'access content',
'create page content',
'edit own page content',
'administer languages',
'access administration pages',
]);
$this->node = $this
->drupalCreateNode([
'title' => $this
->randomMachineName(255),
'uid' => $this->authUser
->id(),
]);
$this
->drupalLogin($this->authUser);
$this->language = [
'predefined_langcode' => 'custom',
'langcode' => 'xx',
'label' => $this
->randomMachineName(16),
'direction' => 'ltr',
];
$this
->drupalPostForm('admin/config/regional/language/add', $this->language, t('Add custom language'));
$this
->drupalPostForm('admin/config/regional/language/detection', [
'language_interface[enabled][language-url]' => 1,
], t('Save settings'));
$this
->drupalLogout();
$this
->config('statistics.settings')
->set('count_content_views', 1)
->save();
Database::getConnection()
->truncate('node_counter');
$this->client = \Drupal::httpClient();
}
public function testLogging() {
$path = 'node/' . $this->node
->id();
$module_path = drupal_get_path('module', 'statistics');
$stats_path = base_path() . $module_path . '/statistics.php';
$lib_path = base_path() . $module_path . '/statistics.js';
$expected_library = '/<script src=".*?' . preg_quote($lib_path, '/.') . '.*?">/is';
$this
->drupalGet('node');
$settings = $this
->getDrupalSettings();
$this
->assertSession()
->responseNotMatches($expected_library, 'Statistics library JS not found on node page.');
$this
->assertFalse(isset($settings['statistics']), 'Statistics settings not found on node page.');
$this
->drupalGet('node/9999');
$settings = $this
->getDrupalSettings();
$this
->assertSession()
->responseNotMatches($expected_library, 'Statistics library JS not found on non-existent node page.');
$this
->assertFalse(isset($settings['statistics']), 'Statistics settings not found on node page.');
$this
->drupalGet($path);
$settings = $this
->getDrupalSettings();
$this
->assertPattern($expected_library);
$this
->assertIdentical($this->node
->id(), $settings['statistics']['data']['nid'], 'Found statistics settings on node page.');
$this
->drupalGet($this->language['langcode'] . '/' . $path);
$settings = $this
->getDrupalSettings();
$this
->assertPattern($expected_library);
$this
->assertIdentical($this->node
->id(), $settings['statistics']['data']['nid'], 'Found statistics settings on valid node page in a non-default language.');
global $base_root;
$post = [
'nid' => $this->node
->id(),
];
$this->client
->post($base_root . $stats_path, [
'form_params' => $post,
]);
$node_counter = \Drupal::service('statistics.storage.node')
->fetchView($this->node
->id());
$this
->assertIdentical(1, $node_counter
->getTotalCount());
$node_id = 1000000;
$node = Node::load($node_id);
$this
->assertNull($node);
$result = \Drupal::service('statistics.storage.node')
->fetchView($node_id);
$this
->assertIdentical($result, FALSE);
}
}