Core and composer updates

This commit is contained in:
Rob Davies 2017-07-03 16:47:07 +01:00
parent a82634bb98
commit 62cac30480
1118 changed files with 21770 additions and 6306 deletions

View file

@ -0,0 +1,173 @@
<?php
namespace Drupal\Tests\statistics\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\CronRunTrait;
/**
* Tests the statistics admin.
*
* @group statistics
*/
class StatisticsAdminTest extends BrowserTestBase {
use CronRunTrait;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['node', 'statistics'];
/**
* A user that has permission to administer statistics.
*
* @var \Drupal\user\UserInterface
*/
protected $privilegedUser;
/**
* A page node for which to check content statistics.
*
* @var \Drupal\node\NodeInterface
*/
protected $testNode;
/**
* The Guzzle HTTP client.
*
* @var \GuzzleHttp\Client;
*/
protected $client;
protected function setUp() {
parent::setUp();
// Set the max age to 0 to simplify testing.
$this->config('statistics.settings')->set('display_max_age', 0)->save();
// Create Basic page node type.
if ($this->profile != 'standard') {
$this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
}
$this->privilegedUser = $this->drupalCreateUser(['administer statistics', 'view post access counter', 'create page content']);
$this->drupalLogin($this->privilegedUser);
$this->testNode = $this->drupalCreateNode(['type' => 'page', 'uid' => $this->privilegedUser->id()]);
$this->client = \Drupal::httpClient();
}
/**
* Verifies that the statistics settings page works.
*/
public function testStatisticsSettings() {
$config = $this->config('statistics.settings');
$this->assertFalse($config->get('count_content_views'), 'Count content view log is disabled by default.');
// Enable counter on content view.
$edit['statistics_count_content_views'] = 1;
$this->drupalPostForm('admin/config/system/statistics', $edit, t('Save configuration'));
$config = $this->config('statistics.settings');
$this->assertTrue($config->get('count_content_views'), 'Count content view log is enabled.');
// Hit the node.
$this->drupalGet('node/' . $this->testNode->id());
// Manually calling statistics.php, simulating ajax behavior.
$nid = $this->testNode->id();
$post = ['nid' => $nid];
global $base_url;
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
$this->client->post($stats_path, ['form_params' => $post]);
// Hit the node again (the counter is incremented after the hit, so
// "1 view" will actually be shown when the node is hit the second time).
$this->drupalGet('node/' . $this->testNode->id());
$this->client->post($stats_path, ['form_params' => $post]);
$this->assertText('1 view', 'Node is viewed once.');
$this->drupalGet('node/' . $this->testNode->id());
$this->client->post($stats_path, ['form_params' => $post]);
$this->assertText('2 views', 'Node is viewed 2 times.');
// Increase the max age to test that nodes are no longer immediately
// updated, visit the node once more to populate the cache.
$this->config('statistics.settings')->set('display_max_age', 3600)->save();
$this->drupalGet('node/' . $this->testNode->id());
$this->assertText('3 views', 'Node is viewed 3 times.');
$this->client->post($stats_path, ['form_params' => $post]);
$this->drupalGet('node/' . $this->testNode->id());
$this->assertText('3 views', 'Views counter was not updated.');
}
/**
* Tests that when a node is deleted, the node counter is deleted too.
*/
public function testDeleteNode() {
$this->config('statistics.settings')->set('count_content_views', 1)->save();
$this->drupalGet('node/' . $this->testNode->id());
// Manually calling statistics.php, simulating ajax behavior.
$nid = $this->testNode->id();
$post = ['nid' => $nid];
global $base_url;
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
$this->client->post($stats_path, ['form_params' => $post]);
$result = db_select('node_counter', 'n')
->fields('n', ['nid'])
->condition('n.nid', $this->testNode->id())
->execute()
->fetchAssoc();
$this->assertEqual($result['nid'], $this->testNode->id(), 'Verifying that the node counter is incremented.');
$this->testNode->delete();
$result = db_select('node_counter', 'n')
->fields('n', ['nid'])
->condition('n.nid', $this->testNode->id())
->execute()
->fetchAssoc();
$this->assertFalse($result, 'Verifying that the node counter is deleted.');
}
/**
* Tests that cron clears day counts and expired access logs.
*/
public function testExpiredLogs() {
$this->config('statistics.settings')
->set('count_content_views', 1)
->save();
\Drupal::state()->set('statistics.day_timestamp', 8640000);
$this->drupalGet('node/' . $this->testNode->id());
// Manually calling statistics.php, simulating ajax behavior.
$nid = $this->testNode->id();
$post = ['nid' => $nid];
global $base_url;
$stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
$this->client->post($stats_path, ['form_params' => $post]);
$this->drupalGet('node/' . $this->testNode->id());
$this->client->post($stats_path, ['form_params' => $post]);
$this->assertText('1 view', 'Node is viewed once.');
// statistics_cron() will subtract
// statistics.settings:accesslog.max_lifetime config from REQUEST_TIME in
// the delete query, so wait two secs here to make sure the access log will
// be flushed for the node just hit.
sleep(2);
$this->cronRun();
$this->drupalGet('admin/reports/pages');
$this->assertNoText('node/' . $this->testNode->id(), 'No hit URL found.');
$result = db_select('node_counter', 'nc')
->fields('nc', ['daycount'])
->condition('nid', $this->testNode->id(), '=')
->execute()
->fetchField();
$this->assertFalse($result, 'Daycounter is zero.');
}
}

View file

@ -0,0 +1,142 @@
<?php
namespace Drupal\Tests\statistics\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\node\Entity\Node;
/**
* Tests request logging for cached and uncached pages.
*
* We subclass WebTestBase rather than StatisticsTestBase, because we
* want to test requests from an anonymous user.
*
* @group statistics
*/
class StatisticsLoggingTest extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['node', 'statistics', 'block', 'locale'];
/**
* User with permissions to create and edit pages.
*
* @var \Drupal\user\UserInterface
*/
protected $authUser;
/**
* Associative array representing a hypothetical Drupal language.
*
* @var array
*/
protected $language;
/**
* The Guzzle HTTP client.
*
* @var \GuzzleHttp\Client;
*/
protected $client;
protected function setUp() {
parent::setUp();
// Create Basic page node type.
if ($this->profile != 'standard') {
$this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
}
$this->authUser = $this->drupalCreateUser([
// For node creation.
'access content',
'create page content',
'edit own page content',
// For language negotiation administration.
'administer languages',
'access administration pages',
]);
// Ensure we have a node page to access.
$this->node = $this->drupalCreateNode(['title' => $this->randomMachineName(255), 'uid' => $this->authUser->id()]);
// Add a custom language and enable path-based language negotiation.
$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();
// Enable access logging.
$this->config('statistics.settings')
->set('count_content_views', 1)
->save();
// Clear the logs.
db_truncate('node_counter');
$this->client = \Drupal::httpClient();
}
/**
* Verifies node hit counter logging and script placement.
*/
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';
// Verify that logging scripts are not found on a non-node page.
$this->drupalGet('node');
$settings = $this->getDrupalSettings();
$this->assertNoPattern($expected_library, 'Statistics library JS not found on node page.');
$this->assertFalse(isset($settings['statistics']), 'Statistics settings not found on node page.');
// Verify that logging scripts are not found on a non-existent node page.
$this->drupalGet('node/9999');
$settings = $this->getDrupalSettings();
$this->assertNoPattern($expected_library, 'Statistics library JS not found on non-existent node page.');
$this->assertFalse(isset($settings['statistics']), 'Statistics settings not found on node page.');
// Verify that logging scripts are found on a valid node page.
$this->drupalGet($path);
$settings = $this->getDrupalSettings();
$this->assertPattern($expected_library, 'Found statistics library JS on node page.');
$this->assertIdentical($this->node->id(), $settings['statistics']['data']['nid'], 'Found statistics settings on node page.');
// Verify the same when loading the site in a non-default language.
$this->drupalGet($this->language['langcode'] . '/' . $path);
$settings = $this->getDrupalSettings();
$this->assertPattern($expected_library, 'Found statistics library JS on a valid node page in a non-default language.');
$this->assertIdentical($this->node->id(), $settings['statistics']['data']['nid'], '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 = statistics_get($this->node->id());
$this->assertIdentical($node_counter['totalcount'], '1');
// 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 = statistics_get($node_id);
$this->assertIdentical($result, FALSE);
}
}

View file

@ -0,0 +1,63 @@
<?php
namespace Drupal\Tests\statistics\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
/**
* Tests display of statistics report blocks.
*
* @group statistics
*/
class StatisticsReportsTest extends StatisticsTestBase {
use AssertPageCacheContextsAndTagsTrait;
/**
* Tests the "popular content" block.
*/
public function testPopularContentBlock() {
// Clear the block cache to load the Statistics module's block definitions.
$this->container->get('plugin.manager.block')->clearCachedDefinitions();
// Visit a node to have something show up in the block.
$node = $this->drupalCreateNode(['type' => 'page', 'uid' => $this->blockingUser->id()]);
$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 . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
$client = \Drupal::httpClient();
$client->post($stats_path, ['headers' => $headers, 'body' => $post]);
// Configure and save the block.
$block = $this->drupalPlaceBlock('statistics_popular_block', [
'label' => 'Popular content',
'top_day_num' => 3,
'top_all_num' => 3,
'top_last_num' => 3,
]);
// Get some page and check if the block is displayed.
$this->drupalGet('user');
$this->assertText('Popular content', 'Found the popular content block.');
$this->assertText("Today's", "Found today's popular content.");
$this->assertText('All time', 'Found the all time popular content.');
$this->assertText('Last viewed', 'Found the last viewed popular content.');
$tags = Cache::mergeTags($node->getCacheTags(), $block->getCacheTags());
$tags = Cache::mergeTags($tags, $this->blockingUser->getCacheTags());
$tags = Cache::mergeTags($tags, ['block_view', 'config:block_list', 'node_list', 'rendered', 'user_view']);
$this->assertCacheTags($tags);
$contexts = Cache::mergeContexts($node->getCacheContexts(), $block->getCacheContexts());
$contexts = Cache::mergeContexts($contexts, ['url.query_args:_wrapper_format']);
$this->assertCacheContexts($contexts);
// Check if the node link is displayed.
$this->assertRaw(\Drupal::l($node->label(), $node->urlInfo('canonical')), 'Found link to visited node.');
}
}

View file

@ -32,7 +32,7 @@ class MigrateStatisticsConfigsTest extends MigrateDrupal6TestBase {
*/
public function testStatisticsSettings() {
$config = $this->config('statistics.settings');
$this->assertIdentical(0, $config->get('count_content_views'));
$this->assertSame(1, $config->get('count_content_views'));
$this->assertConfigSchema(\Drupal::service('config.typed'), 'statistics.settings', $config->get());
}