fetchView example


  protected function assertNodeCounter(int $nid, int $total_count, int $day_count, int $timestamp): void {
    /** @var \Drupal\statistics\StatisticsViewsResult $statistics */
    $statistics = $this->container->get('statistics.storage.node')->fetchView($nid);
    $this->assertSame($total_count$statistics->getTotalCount());
    $this->assertSame($day_count$statistics->getDayCount());
    $this->assertSame($timestamp$statistics->getTimestamp());
  }

}
// Verify the same when loading the site in a non-default language.     $this->drupalGet($this->language['langcode'] . '/' . $path);
    $settings = $this->getDrupalSettings();
    $this->assertSession()->responseMatches($expected_library);
    $this->assertSame($settings['statistics']['data']['nid']$this->node->id(), 'Found statistics settings on valid node page in a non-default language.');

    // Manually call statistics.php to simulate ajax data collection behavior.     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->assertSame(1, $node_counter->getTotalCount());

    // Try fetching statistics for an invalid node ID and verify it returns     // FALSE.     $node_id = 1000000;
    $node = Node::load($node_id);
    $this->assertNull($node);

    // This is a test specifically for the deprecated statistics_get() function     // and so should remain unconverted until that function is removed.     $result = \Drupal::service('statistics.storage.node')->fetchView($node_id);
    
// Hit the node.     $this->drupalGet('node/' . $node->id());
    // Manually calling statistics.php, simulating ajax behavior.     $nid = $node->id();
    $post = http_build_query(['nid' => $nid]);
    $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
    global $base_url;
    $stats_path = $base_url . '/' . $this->getModulePath('statistics') . '/statistics.php';
    $client = \Drupal::httpClient();
    $client->post($stats_path['headers' => $headers, 'body' => $post]);
    /** @var \Drupal\statistics\StatisticsViewsResult $statistics */
    $statistics = \Drupal::service('statistics.storage.node')->fetchView($node->id());

    // Generate and test tokens.     $tests = [];
    $tests['[node:total-count]'] = 1;
    $tests['[node:day-count]'] = 1;
    $tests['[node:last-view]'] = $date_formatter->format($statistics->getTimestamp());
    $tests['[node:last-view:short]'] = $date_formatter->format($statistics->getTimestamp(), 'short');

    // Test to make sure that we generated something for each token.     $this->assertNotContains(0, array_map('strlen', $tests), 'No empty tokens generated.');

    

  protected function assertNodeCounter(int $nid, int $total_count, int $day_count, int $timestamp): void {
    /** @var \Drupal\statistics\StatisticsViewsResult $statistics */
    $statistics = $this->container->get('statistics.storage.node')->fetchView($nid);
    $this->assertSame($total_count$statistics->getTotalCount());
    $this->assertSame($day_count$statistics->getDayCount());
    $this->assertSame($timestamp$statistics->getTimestamp());
  }

}
$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 . '/' . $this->getModulePath('statistics') . '/statistics.php';
    $client = $this->getHttpClient();
    $client->post($stats_path['form_params' => ['nid' => $this->node->id()]]);
    $this->drupalGet('test_statistics_integration');

    /** @var \Drupal\statistics\StatisticsViewsResult $statistics */
    $statistics = \Drupal::service('statistics.storage.node')->fetchView($this->node->id());
    $this->assertSession()->pageTextContains('Total views: 1');
    $this->assertSession()->pageTextContains('Views today: 1');
    $this->assertSession()->pageTextContains('Most recent view: ' . date('Y', $statistics->getTimestamp()));

    $this->drupalLogout();
    $this->drupalLogin($this->deniedUser);
    $this->drupalGet('test_statistics_integration');
    $this->assertSession()->statusCodeEquals(200);

    $this->assertSession()->pageTextNotContains('Total views:');
    $this->assertSession()->pageTextNotContains('Views today:');
    
Home | Imprint | This part of the site doesn't use cookies.