annotate core/modules/statistics/tests/src/Functional/StatisticsLoggingTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\statistics\Functional;
Chris@0 4
Chris@18 5 use Drupal\Core\Database\Database;
Chris@0 6 use Drupal\Tests\BrowserTestBase;
Chris@0 7 use Drupal\node\Entity\Node;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Tests request logging for cached and uncached pages.
Chris@0 11 *
Chris@0 12 * We subclass WebTestBase rather than StatisticsTestBase, because we
Chris@0 13 * want to test requests from an anonymous user.
Chris@0 14 *
Chris@0 15 * @group statistics
Chris@0 16 */
Chris@0 17 class StatisticsLoggingTest extends BrowserTestBase {
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Modules to enable.
Chris@0 21 *
Chris@0 22 * @var array
Chris@0 23 */
Chris@0 24 public static $modules = ['node', 'statistics', 'block', 'locale'];
Chris@0 25
Chris@0 26 /**
Chris@0 27 * User with permissions to create and edit pages.
Chris@0 28 *
Chris@0 29 * @var \Drupal\user\UserInterface
Chris@0 30 */
Chris@0 31 protected $authUser;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Associative array representing a hypothetical Drupal language.
Chris@0 35 *
Chris@0 36 * @var array
Chris@0 37 */
Chris@0 38 protected $language;
Chris@0 39
Chris@0 40 /**
Chris@0 41 * The Guzzle HTTP client.
Chris@0 42 *
Chris@17 43 * @var \GuzzleHttp\Client
Chris@0 44 */
Chris@0 45 protected $client;
Chris@0 46
Chris@0 47 protected function setUp() {
Chris@0 48 parent::setUp();
Chris@0 49
Chris@0 50 // Create Basic page node type.
Chris@0 51 if ($this->profile != 'standard') {
Chris@0 52 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
Chris@0 53 }
Chris@0 54
Chris@0 55 $this->authUser = $this->drupalCreateUser([
Chris@0 56 // For node creation.
Chris@0 57 'access content',
Chris@0 58 'create page content',
Chris@0 59 'edit own page content',
Chris@0 60 // For language negotiation administration.
Chris@0 61 'administer languages',
Chris@0 62 'access administration pages',
Chris@0 63 ]);
Chris@0 64
Chris@0 65 // Ensure we have a node page to access.
Chris@0 66 $this->node = $this->drupalCreateNode(['title' => $this->randomMachineName(255), 'uid' => $this->authUser->id()]);
Chris@0 67
Chris@0 68 // Add a custom language and enable path-based language negotiation.
Chris@0 69 $this->drupalLogin($this->authUser);
Chris@0 70 $this->language = [
Chris@0 71 'predefined_langcode' => 'custom',
Chris@0 72 'langcode' => 'xx',
Chris@0 73 'label' => $this->randomMachineName(16),
Chris@0 74 'direction' => 'ltr',
Chris@0 75 ];
Chris@0 76 $this->drupalPostForm('admin/config/regional/language/add', $this->language, t('Add custom language'));
Chris@0 77 $this->drupalPostForm('admin/config/regional/language/detection', ['language_interface[enabled][language-url]' => 1], t('Save settings'));
Chris@0 78 $this->drupalLogout();
Chris@0 79
Chris@0 80 // Enable access logging.
Chris@0 81 $this->config('statistics.settings')
Chris@0 82 ->set('count_content_views', 1)
Chris@0 83 ->save();
Chris@0 84
Chris@0 85 // Clear the logs.
Chris@18 86 Database::getConnection()->truncate('node_counter');
Chris@0 87 $this->client = \Drupal::httpClient();
Chris@0 88 }
Chris@0 89
Chris@0 90 /**
Chris@0 91 * Verifies node hit counter logging and script placement.
Chris@0 92 */
Chris@0 93 public function testLogging() {
Chris@0 94 $path = 'node/' . $this->node->id();
Chris@0 95 $module_path = drupal_get_path('module', 'statistics');
Chris@0 96 $stats_path = base_path() . $module_path . '/statistics.php';
Chris@0 97 $lib_path = base_path() . $module_path . '/statistics.js';
Chris@0 98 $expected_library = '/<script src=".*?' . preg_quote($lib_path, '/.') . '.*?">/is';
Chris@0 99
Chris@0 100 // Verify that logging scripts are not found on a non-node page.
Chris@0 101 $this->drupalGet('node');
Chris@0 102 $settings = $this->getDrupalSettings();
Chris@18 103 $this->assertSession()->responseNotMatches($expected_library, 'Statistics library JS not found on node page.');
Chris@0 104 $this->assertFalse(isset($settings['statistics']), 'Statistics settings not found on node page.');
Chris@0 105
Chris@0 106 // Verify that logging scripts are not found on a non-existent node page.
Chris@0 107 $this->drupalGet('node/9999');
Chris@0 108 $settings = $this->getDrupalSettings();
Chris@18 109 $this->assertSession()->responseNotMatches($expected_library, 'Statistics library JS not found on non-existent node page.');
Chris@0 110 $this->assertFalse(isset($settings['statistics']), 'Statistics settings not found on node page.');
Chris@0 111
Chris@0 112 // Verify that logging scripts are found on a valid node page.
Chris@0 113 $this->drupalGet($path);
Chris@0 114 $settings = $this->getDrupalSettings();
Chris@0 115 $this->assertPattern($expected_library, 'Found statistics library JS on node page.');
Chris@0 116 $this->assertIdentical($this->node->id(), $settings['statistics']['data']['nid'], 'Found statistics settings on node page.');
Chris@0 117
Chris@0 118 // Verify the same when loading the site in a non-default language.
Chris@0 119 $this->drupalGet($this->language['langcode'] . '/' . $path);
Chris@0 120 $settings = $this->getDrupalSettings();
Chris@0 121 $this->assertPattern($expected_library, 'Found statistics library JS on a valid node page in a non-default language.');
Chris@0 122 $this->assertIdentical($this->node->id(), $settings['statistics']['data']['nid'], 'Found statistics settings on valid node page in a non-default language.');
Chris@0 123
Chris@0 124 // Manually call statistics.php to simulate ajax data collection behavior.
Chris@0 125 global $base_root;
Chris@0 126 $post = ['nid' => $this->node->id()];
Chris@0 127 $this->client->post($base_root . $stats_path, ['form_params' => $post]);
Chris@0 128 $node_counter = statistics_get($this->node->id());
Chris@16 129 $this->assertIdentical($node_counter['totalcount'], 1);
Chris@0 130
Chris@0 131 // Try fetching statistics for an invalid node ID and verify it returns
Chris@0 132 // FALSE.
Chris@0 133 $node_id = 1000000;
Chris@0 134 $node = Node::load($node_id);
Chris@0 135 $this->assertNull($node);
Chris@0 136
Chris@0 137 // This is a test specifically for the deprecated statistics_get() function
Chris@0 138 // and so should remain unconverted until that function is removed.
Chris@0 139 $result = statistics_get($node_id);
Chris@0 140 $this->assertIdentical($result, FALSE);
Chris@0 141 }
Chris@0 142
Chris@0 143 }