View source  
  <?php
namespace Drupal\nodeviewcount\Tests;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\KernelTests\AssertContentTrait;
use Drupal\node\NodeInterface;
use Drupal\Tests\Traits\Core\CronRunTrait;
class NodeViewCountBaseFunctionalityTest extends NodeViewCountTestBase {
  use CronRunTrait;
  use AssertContentTrait;
  use StringTranslationTrait;
  
  public static $modules = [
    'views',
  ];
  
  public function testExpiredLogs() {
    $this
      ->sendAjaxStatistics($this->firstTestTrackedNode
      ->id(), 0);
    $this
      ->sendAjaxStatistics($this->secondTestTrackedNode
      ->id(), 0);
    sleep(2);
    $this
      ->cronRun();
    $query = $this->connection
      ->select('nodeviewcount', 'nvc')
      ->fields('nvc', [
      'id',
    ]);
    $result = $query
      ->execute()
      ->fetchAll();
    $this
      ->assertEqual(count($result), 2, ' Nodeviewcount statistics is not deleted after cron run.');
    $node_view_count_settings = $this
      ->config('nodeviewcount.settings');
    $node_view_count_settings
      ->set('logs_life_time', 1)
      ->save();
    sleep(2);
    $this
      ->cronRun();
    $result = $query
      ->execute()
      ->fetchAll();
    $this
      ->assertEqual(count($result), 0, ' Nodeviewcount statistics is deleted after cron run.');
  }
  
  public function testNodesWithFullViewMode() {
    $this
      ->checkFullViewMode('anonymous', $this->firstTestTrackedNode, TRUE);
    $this
      ->checkFullViewMode('anonymous', $this->testNotTrackedNode, FALSE);
    $this
      ->checkFullViewMode('logged', $this->firstTestTrackedNode, TRUE);
    $this
      ->checkFullViewMode('logged', $this->testNotTrackedNode, FALSE);
    $this
      ->checkFullViewMode('administrator', $this->firstTestTrackedNode, FALSE);
    $this
      ->checkFullViewMode('administrator', $this->testNotTrackedNode, FALSE);
  }
  
  public function testNodesWithTeaserViewMode() {
    $this
      ->checkTeaserViewMode('anonymous', TRUE);
    $this
      ->checkTeaserViewMode('logged', TRUE);
    $this
      ->checkTeaserViewMode('administrator', FALSE);
    $this
      ->drupalLogin($this->adminUser);
    $edit['view_modes[teaser]'] = FALSE;
    $this
      ->drupalPostForm('admin/config/content/nodeviewcount', $edit, $this
      ->t('Save configuration'));
    $this
      ->drupalGet('admin/config/content/nodeviewcount');
    $this
      ->drupalLogout();
    $this
      ->checkTeaserViewMode('anonymous', FALSE);
    $this
      ->checkTeaserViewMode('logged', FALSE);
    $this
      ->checkTeaserViewMode('administrator', FALSE);
  }
  
  public function testAjaxCall() {
    $this
      ->sendAjaxStatistics($this->firstTestTrackedNode
      ->id(), 0);
    $this
      ->sendAjaxStatistics($this->firstTestTrackedNode
      ->id(), 1);
    $result = $this->connection
      ->select('nodeviewcount', 'nvc')
      ->fields('nvc', [
      'nid',
    ])
      ->condition('nvc.nid', $this->firstTestTrackedNode
      ->id())
      ->execute()
      ->fetchAll();
    $this
      ->assertEqual(count($result), 2, 'Verifying that the node counter is incremented.');
  }
  
  protected function checkFullViewMode($user_role, NodeInterface $node, $expected_result) {
    $user_id = 0;
    
    if ($user_role !== 'anonymous') {
      $user = $this
        ->createUserWithRole($user_role);
      $user_id = $user
        ->id();
      $this
        ->drupalLogin($user);
    }
    
    $this
      ->drupalGet('node/' . $node
      ->id());
    
    if ($expected_result) {
      $this
        ->assertRaw('nodeviewcount.js', 'Nodeviewcount statistics library is included.');
      $settings = $this
        ->getDrupalSettings();
      $expectedSettings = [
        $node
          ->id() => [
          'nid' => $node
            ->id(),
          'uid' => $user_id,
          'uip' => '127.0.0.1',
          'view_mode' => 'full',
        ],
      ];
      $this
        ->assertEqual($expectedSettings, $settings['nodeviewcount']['data'], 'drupalSettings has right node information.');
    }
    else {
      $this
        ->assertNoRaw('nodeviewcount.js', 'Nodeviewcount statistics library is not included.');
    }
    
    if ($user_role !== 'anonymous') {
      $this
        ->drupalLogout();
    }
  }
  
  public function checkTeaserViewMode($user_role, $expected_result) {
    $user_id = 0;
    if ($user_role !== 'anonymous') {
      $user = $this
        ->createUserWithRole($user_role);
      $user_id = $user
        ->id();
      $this
        ->drupalLogin($user);
    }
    $this
      ->drupalGet('node');
    if ($expected_result) {
      $this
        ->assertRaw('nodeviewcount.js', 'Nodeviewcount statistics library is included.');
      $settings = $this
        ->getDrupalSettings();
      $expectedSettings = [
        $this->firstTestTrackedNode
          ->id() => [
          'nid' => $this->firstTestTrackedNode
            ->id(),
          'uid' => $user_id,
          'uip' => '127.0.0.1',
          'view_mode' => 'teaser',
        ],
        $this->secondTestTrackedNode
          ->id() => [
          'nid' => $this->secondTestTrackedNode
            ->id(),
          'uid' => $user_id,
          'uip' => '127.0.0.1',
          'view_mode' => 'teaser',
        ],
      ];
      $this
        ->assertEqual($expectedSettings, $settings['nodeviewcount']['data'], 'drupalSettings to mark node as read are present.');
    }
    else {
      $this
        ->assertNoRaw('nodeviewcount.js', 'Nodeviewcount statistics library is not included.');
    }
    if ($user_role !== 'anonymous') {
      $this
        ->drupalLogout();
    }
  }
}