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.
package = XML sitemap
core = 7.x
dependencies[] = xmlsitemap
files[] = xmlsitemap_node.module
files[] = xmlsitemap_node.install
files[] = xmlsitemap_node.test
<?php
/**
* @file
* Default file for XML sitemap node.
*/
/**
* Implements hook_entity_info_alter().
*/
......@@ -33,7 +38,7 @@ function xmlsitemap_node_xmlsitemap_index_links($limit) {
/**
* Process node sitemap links.
*
* @param $nids
* @param array $nids
* An array of node IDs.
*/
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
}
/**
* @codingStandardsIgnoreLine
* Implements hook_form_alter().
*
* 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)
/**
* Fetch all the timestamps for when a node was changed.
*
* @param $node
* @param object $node
* A node object.
* @return
*
* @return array
* An array of UNIX timestamp integers.
*/
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.
*
* @param $node
* @param object $node
* A node object.
*/
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['changecount'] = $node->nid ? count($timestamps) - 1 : 0;
// Node access must be reset since it a user may have changed published status, etc.
//$access = &drupal_static('node_access');
//unset($access[0][$node->nid]);
//node_access_acquire_grants($node);
// Node access must be reset since it a user may have changed published
// status, etc.
// $access = &drupal_static('node_access');
// unset($access[0][$node->nid]);
// node_access_acquire_grants($node);
// The following values must always be checked because they are volatile.
$node->xmlsitemap['loc'] = $uri['path'];
$node->xmlsitemap['lastmod'] = count($timestamps) ? max($timestamps) : 0;
......@@ -212,16 +219,18 @@ function xmlsitemap_node_create_link(stdClass $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
* (e.g. 'forum') for "create" operation.
* @param $account
* @param object $account
* 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.
* @return
*
* @return bool
* 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) {
global $user;
......@@ -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
// 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
// cache.
if (isset($rights[$account->uid][$node->nid])) {
......@@ -294,7 +302,8 @@ function xmlsitemap_node_view_access($node, $account = NULL) {
$query->condition($nids);
$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());
if (!isset($grants[$account->uid][$op])) {
// Indicate that this is our special function in the grants.
......
......@@ -5,10 +5,30 @@
* Unit tests for the xmlsitemap_node module.
*/
/**
* Node Functional Test.
*/
class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
/**
* Normal User.
*
* @var string
*
* @codingStandardsIgnoreStart
*/
protected $normal_user;
/**
* Nodes.
*
* @var array
*/
protected $nodes = array();
/**
* Get Info.
*/
public static function getInfo() {
return array(
'name' => 'XML sitemap node',
......@@ -17,21 +37,43 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
);
}
function setUp($modules = array()) {
/**
* Setup.
*/
public function setUp($modules = array()) {
$modules[] = 'xmlsitemap_node';
$modules[] = 'comment';
parent::setUp($modules);
$this->admin_user = $this->drupalCreateUser(array('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'));
$this->admin_user = $this->drupalCreateUser(array(
'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));
}
function testNodeSettings() {
/**
* Node Settings.
*/
public function testNodeSettings() {
$body_field = 'body[' . LANGUAGE_NONE . '][0][value]';
$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->drupalGet('node/' . $node->nid . '/edit');
......@@ -44,7 +86,13 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
);
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$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->drupalGet('node/' . $node->nid . '/edit');
......@@ -58,7 +106,13 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
);
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$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(
'xmlsitemap[status]' => 'default',
......@@ -67,13 +121,19 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
);
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$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.
*/
function testTypeSettings() {
public function testTypeSettings() {
$this->drupalLogin($this->admin_user);
$node_old = $this->drupalCreateNode();
......@@ -99,8 +159,16 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
$this->assertText('Changed the content type of 2 posts from page to page2.');
$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_old->nid, array('subtype' => 'page2', 'status' => 1, 'priority' => 0.5));
$this->assertSitemapLinkValues('node', $node->nid, array(
'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' => 'page2'))), 2);
......@@ -112,7 +180,7 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
/**
* Test the import of old nodes via cron.
*/
function testCron() {
public function testCron() {
$limit = 5;
variable_set('xmlsitemap_batch_limit', $limit);
......@@ -137,7 +205,11 @@ class XMLSitemapNodeFunctionalTest extends XMLSitemapTestHelper {
$node = array_pop($nodes);
if ($i <= $limit) {
// 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 {
// Any beyond $limit should not be in the sitemap.
......@@ -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