Commit 0ad514cb authored by RenatoG's avatar RenatoG Committed by RenatoG

Issue #2906602 by RenatoG: Fix Drupal Code Sniffer for XML sitemap node

parent d51b2d6e
...@@ -3,6 +3,4 @@ description = Adds content links to the sitemap. ...@@ -3,6 +3,4 @@ description = Adds content links to the sitemap.
package = XML sitemap package = XML sitemap
core = 7.x core = 7.x
dependencies[] = xmlsitemap dependencies[] = xmlsitemap
files[] = xmlsitemap_node.module
files[] = xmlsitemap_node.install
files[] = xmlsitemap_node.test files[] = xmlsitemap_node.test
<?php <?php
/**
* @file
* Default file for XML sitemap node.
*/
/** /**
* Implements hook_entity_info_alter(). * Implements hook_entity_info_alter().
*/ */
...@@ -33,7 +38,7 @@ function xmlsitemap_node_xmlsitemap_index_links($limit) { ...@@ -33,7 +38,7 @@ function xmlsitemap_node_xmlsitemap_index_links($limit) {
/** /**
* Process node sitemap links. * Process node sitemap links.
* *
* @param $nids * @param array $nids
* An array of node IDs. * An array of node IDs.
*/ */
function xmlsitemap_node_xmlsitemap_process_node_links(array $nids) { function xmlsitemap_node_xmlsitemap_process_node_links(array $nids) {
...@@ -124,6 +129,7 @@ function xmlsitemap_node_form_node_type_form_alter(array &$form, array $form_sta ...@@ -124,6 +129,7 @@ function xmlsitemap_node_form_node_type_form_alter(array &$form, array $form_sta
} }
/** /**
* @codingStandardsIgnoreLine
* Implements hook_form_alter(). * Implements hook_form_alter().
* *
* Add the XML sitemap individual link options for a node. * Add the XML sitemap individual link options for a node.
...@@ -140,9 +146,10 @@ function xmlsitemap_node_form_node_form_alter(array &$form, array &$form_state) ...@@ -140,9 +146,10 @@ function xmlsitemap_node_form_node_form_alter(array &$form, array &$form_state)
/** /**
* Fetch all the timestamps for when a node was changed. * Fetch all the timestamps for when a node was changed.
* *
* @param $node * @param object $node
* A node object. * A node object.
* @return *
* @return array
* An array of UNIX timestamp integers. * An array of UNIX timestamp integers.
*/ */
function xmlsitemap_node_get_timestamps(stdClass $node) { function xmlsitemap_node_get_timestamps(stdClass $node) {
...@@ -164,7 +171,7 @@ function xmlsitemap_node_get_timestamps(stdClass $node) { ...@@ -164,7 +171,7 @@ function xmlsitemap_node_get_timestamps(stdClass $node) {
* *
* The link will be saved as $node->xmlsitemap. * The link will be saved as $node->xmlsitemap.
* *
* @param $node * @param object $node
* A node object. * A node object.
*/ */
function xmlsitemap_node_create_link(stdClass $node) { function xmlsitemap_node_create_link(stdClass $node) {
...@@ -195,11 +202,11 @@ function xmlsitemap_node_create_link(stdClass $node) { ...@@ -195,11 +202,11 @@ function xmlsitemap_node_create_link(stdClass $node) {
$node->xmlsitemap['changefreq'] = $node->nid ? xmlsitemap_calculate_changefreq($timestamps) : 0; $node->xmlsitemap['changefreq'] = $node->nid ? xmlsitemap_calculate_changefreq($timestamps) : 0;
$node->xmlsitemap['changecount'] = $node->nid ? count($timestamps) - 1 : 0; $node->xmlsitemap['changecount'] = $node->nid ? count($timestamps) - 1 : 0;
// Node access must be reset since it a user may have changed published status, etc. // Node access must be reset since it a user may have changed published
//$access = &drupal_static('node_access'); // status, etc.
//unset($access[0][$node->nid]); // $access = &drupal_static('node_access');
//node_access_acquire_grants($node); // unset($access[0][$node->nid]);
// node_access_acquire_grants($node);
// The following values must always be checked because they are volatile. // The following values must always be checked because they are volatile.
$node->xmlsitemap['loc'] = $uri['path']; $node->xmlsitemap['loc'] = $uri['path'];
$node->xmlsitemap['lastmod'] = count($timestamps) ? max($timestamps) : 0; $node->xmlsitemap['lastmod'] = count($timestamps) ? max($timestamps) : 0;
...@@ -212,16 +219,18 @@ function xmlsitemap_node_create_link(stdClass $node) { ...@@ -212,16 +219,18 @@ function xmlsitemap_node_create_link(stdClass $node) {
/** /**
* Determine whether a user may view the specified node. * Determine whether a user may view the specified node.
* *
* @param $node * @param object $node
* The node object on which the operation is to be performed, or node type * The node object on which the operation is to be performed, or node type
* (e.g. 'forum') for "create" operation. * (e.g. 'forum') for "create" operation.
* @param $account * @param object $account
* Optional, a user object representing the user for whom the operation is to * Optional, a user object representing the user for whom the operation is to
* be performed. Determines access for a user other than the current user. * be performed. Determines access for a user other than the current user.
* @return *
* @return bool
* TRUE if the operation may be performed, FALSE otherwise. * TRUE if the operation may be performed, FALSE otherwise.
* *
* This is for all intesive purposes a copy of Drupal 7's node_access() function. * This is for all intesive purposes a copy of Drupal 7's node_access()
* function.
*/ */
function xmlsitemap_node_view_access($node, $account = NULL) { function xmlsitemap_node_view_access($node, $account = NULL) {
global $user; global $user;
...@@ -241,8 +250,7 @@ function xmlsitemap_node_view_access($node, $account = NULL) { ...@@ -241,8 +250,7 @@ function xmlsitemap_node_view_access($node, $account = NULL) {
// $node may be either an object or a node type. Since node types cannot be // $node may be either an object or a node type. Since node types cannot be
// an integer, use either nid or type as the static cache id. // an integer, use either nid or type as the static cache id.
//$cid = is_object($node) ? $node->nid : $node; // $cid = is_object($node) ? $node->nid : $node;
// If we've already checked access for this node, user and op, return from // If we've already checked access for this node, user and op, return from
// cache. // cache.
if (isset($rights[$account->uid][$node->nid])) { if (isset($rights[$account->uid][$node->nid])) {
...@@ -294,7 +302,8 @@ function xmlsitemap_node_view_access($node, $account = NULL) { ...@@ -294,7 +302,8 @@ function xmlsitemap_node_view_access($node, $account = NULL) {
$query->condition($nids); $query->condition($nids);
$query->range(0, 1); $query->range(0, 1);
// Fetch the node grants and allow other modules to alter them (D7 backport). // Fetch the node grants and allow other modules to alter them
// (D7 backport).
$grants = &drupal_static(__FUNCTION__ . ':grants', array()); $grants = &drupal_static(__FUNCTION__ . ':grants', array());
if (!isset($grants[$account->uid][$op])) { if (!isset($grants[$account->uid][$op])) {
// Indicate that this is our special function in the grants. // Indicate that this is our special function in the grants.
......
...@@ -5,10 +5,30 @@ ...@@ -5,10 +5,30 @@
* Unit tests for the xmlsitemap_node module. * Unit tests for the xmlsitemap_node module.
*/ */
/**
* Node Functional Test.
*/
class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper { class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
/**
* Normal User.
*
* @var string
*
* @codingStandardsIgnoreStart
*/
protected $normal_user; protected $normal_user;
/**
* Nodes.
*
* @var array
*/
protected $nodes = array(); protected $nodes = array();
/**
* Get Info.
*/
public static function getInfo() { public static function getInfo() {
return array( return array(
'name' => 'XML sitemap node', 'name' => 'XML sitemap node',
...@@ -17,21 +37,43 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper { ...@@ -17,21 +37,43 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
); );
} }
function setUp($modules = array()) { /**
* Setup.
*/
public function setUp($modules = array()) {
$modules[] = 'xmlsitemap_node'; $modules[] = 'xmlsitemap_node';
$modules[] = 'comment'; $modules[] = 'comment';
parent::setUp($modules); parent::setUp($modules);
$this->admin_user = $this->drupalCreateUser(array('administer nodes', 'bypass node access', 'administer content types', 'administer xmlsitemap')); $this->admin_user = $this->drupalCreateUser(array(
$this->normal_user = $this->drupalCreateUser(array('create page content', 'edit any page content', 'access content', 'view own unpublished content')); 'administer nodes',
'bypass node access',
'administer content types',
'administer xmlsitemap',
));
$this->normal_user = $this->drupalCreateUser(array(
'create page content',
'edit any page content',
'access content',
'view own unpublished content',
));
xmlsitemap_link_bundle_settings_save('node', 'page', array('status' => 1, 'priority' => 0.5)); xmlsitemap_link_bundle_settings_save('node', 'page', array('status' => 1, 'priority' => 0.5));
} }
function testNodeSettings() { /**
* Node Settings.
*/
public function testNodeSettings() {
$body_field = 'body[' . LANGUAGE_NONE . '][0][value]'; $body_field = 'body[' . LANGUAGE_NONE . '][0][value]';
$node = $this->drupalCreateNode(array('status' => FALSE, 'uid' => $this->normal_user->uid)); $node = $this->drupalCreateNode(array('status' => FALSE, 'uid' => $this->normal_user->uid));
$this->assertSitemapLinkValues('node', $node->nid, array('access' => 0, 'status' => 1, 'priority' => 0.5, 'status_override' => 0, 'priority_override' => 0)); $this->assertSitemapLinkValues('node', $node->nid, array(
'access' => 0,
'status' => 1,
'priority' => 0.5,
'status_override' => 0,
'priority_override' => 0,
));
$this->drupalLogin($this->normal_user); $this->drupalLogin($this->normal_user);
$this->drupalGet('node/' . $node->nid . '/edit'); $this->drupalGet('node/' . $node->nid . '/edit');
...@@ -44,7 +86,13 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper { ...@@ -44,7 +86,13 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
); );
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$this->assertText('Basic page Test node title has been updated.'); $this->assertText('Basic page Test node title has been updated.');
$this->assertSitemapLinkValues('node', $node->nid, array('access' => 0, 'status' => 1, 'priority' => 0.5, 'status_override' => 0, 'priority_override' => 0)); $this->assertSitemapLinkValues('node', $node->nid, array(
'access' => 0,
'status' => 1,
'priority' => 0.5,
'status_override' => 0,
'priority_override' => 0,
));
$this->drupalLogin($this->admin_user); $this->drupalLogin($this->admin_user);
$this->drupalGet('node/' . $node->nid . '/edit'); $this->drupalGet('node/' . $node->nid . '/edit');
...@@ -58,7 +106,13 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper { ...@@ -58,7 +106,13 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
); );
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$this->assertText('Basic page Test node title has been updated.'); $this->assertText('Basic page Test node title has been updated.');
$this->assertSitemapLinkValues('node', $node->nid, array('access' => 1, 'status' => 0, 'priority' => 0.9, 'status_override' => 1, 'priority_override' => 1)); $this->assertSitemapLinkValues('node', $node->nid, array(
'access' => 1,
'status' => 0,
'priority' => 0.9,
'status_override' => 1,
'priority_override' => 1,
));
$edit = array( $edit = array(
'xmlsitemap[status]' => 'default', 'xmlsitemap[status]' => 'default',
...@@ -67,13 +121,19 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper { ...@@ -67,13 +121,19 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
); );
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$this->assertText('Basic page Test node title has been updated.'); $this->assertText('Basic page Test node title has been updated.');
$this->assertSitemapLinkValues('node', $node->nid, array('access' => 0, 'status' => 1, 'priority' => 0.5, 'status_override' => 0, 'priority_override' => 0)); $this->assertSitemapLinkValues('node', $node->nid, array(
'access' => 0,
'status' => 1,
'priority' => 0.5,
'status_override' => 0,
'priority_override' => 0,
));
} }
/** /**
* Test the content type settings. * Test the content type settings.
*/ */
function testTypeSettings() { public function testTypeSettings() {
$this->drupalLogin($this->admin_user); $this->drupalLogin($this->admin_user);
$node_old = $this->drupalCreateNode(); $node_old = $this->drupalCreateNode();
...@@ -99,8 +159,16 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper { ...@@ -99,8 +159,16 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
$this->assertText('Changed the content type of 2 posts from page to page2.'); $this->assertText('Changed the content type of 2 posts from page to page2.');
$this->assertText('The content type Basic page has been updated.'); $this->assertText('The content type Basic page has been updated.');
$this->assertSitemapLinkValues('node', $node->nid, array('subtype' => 'page2', 'status' => 1, 'priority' => 0.5)); $this->assertSitemapLinkValues('node', $node->nid, array(
$this->assertSitemapLinkValues('node', $node_old->nid, array('subtype' => 'page2', 'status' => 1, 'priority' => 0.5)); 'subtype' => 'page2',
'status' => 1,
'priority' => 0.5,
));
$this->assertSitemapLinkValues('node', $node_old->nid, array(
'subtype' => 'page2',
'status' => 1,
'priority' => 0.5,
));
$this->assertEqual(count(xmlsitemap_link_load_multiple(array('type' => 'node', 'subtype' => 'page'))), 0); $this->assertEqual(count(xmlsitemap_link_load_multiple(array('type' => 'node', 'subtype' => 'page'))), 0);
$this->assertEqual(count(xmlsitemap_link_load_multiple(array('type' => 'node', 'subtype' => 'page2'))), 2); $this->assertEqual(count(xmlsitemap_link_load_multiple(array('type' => 'node', 'subtype' => 'page2'))), 2);
...@@ -112,7 +180,7 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper { ...@@ -112,7 +180,7 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
/** /**
* Test the import of old nodes via cron. * Test the import of old nodes via cron.
*/ */
function testCron() { public function testCron() {
$limit = 5; $limit = 5;
variable_set('xmlsitemap_batch_limit', $limit); variable_set('xmlsitemap_batch_limit', $limit);
...@@ -127,8 +195,8 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper { ...@@ -127,8 +195,8 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
// Clear all the node link data so we can emulate 'old' nodes. // Clear all the node link data so we can emulate 'old' nodes.
db_delete('xmlsitemap') db_delete('xmlsitemap')
->condition('type', 'node') ->condition('type', 'node')
->execute(); ->execute();
// Run cron to import old nodes. // Run cron to import old nodes.
xmlsitemap_node_cron(); xmlsitemap_node_cron();
...@@ -137,7 +205,11 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper { ...@@ -137,7 +205,11 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
$node = array_pop($nodes); $node = array_pop($nodes);
if ($i <= $limit) { if ($i <= $limit) {
// The first $limit nodes should be inserted. // The first $limit nodes should be inserted.
$this->assertSitemapLinkValues('node', $node->nid, array('access' => 1, 'status' => 1, 'lastmod' => $node->changed)); $this->assertSitemapLinkValues('node', $node->nid, array(
'access' => 1,
'status' => 1,
'lastmod' => $node->changed,
));
} }
else { else {
// Any beyond $limit should not be in the sitemap. // Any beyond $limit should not be in the sitemap.
...@@ -145,4 +217,5 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper { ...@@ -145,4 +217,5 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
} }
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment