IntegrationTest.php 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<?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.
   *
31
   * @var \Drupal\user\UserInterface
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
   */
  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.',
53
      'group' => 'Views module integration',
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    );
  }

  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()));
    global $base_url;
    $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics'). '/statistics.php';
86 87 88
    $client = \Drupal::httpClient();
    $client->setConfig(array('curl.options' => array(CURLOPT_TIMEOUT => 10)));
    $client->post($stats_path, array(), $post)->send();
89 90 91 92 93 94 95 96 97 98 99 100 101 102
    $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.");
    }
  }

}