Commit 27d8fc6f authored by RenatoG's avatar RenatoG Committed by RenatoG

Issue #2906634 by RenatoG: Fix Drupal Coding Standards and Best Practices for XML sitemap Custom

parent e605f69d
...@@ -2,9 +2,12 @@ ...@@ -2,9 +2,12 @@
/** /**
* @file * @file
* Administrative page callbacks for the xmlsitemap_custom module. * Administrative page callbacks for the xmlsitemap_custom.
*/ */
/**
* List Links.
*/
function xmlsitemap_custom_list_links() { function xmlsitemap_custom_list_links() {
$header = array( $header = array(
'loc' => array('data' => t('Location'), 'field' => 'loc', 'sort' => 'asc'), 'loc' => array('data' => t('Location'), 'field' => 'loc', 'sort' => 'asc'),
...@@ -62,6 +65,9 @@ function xmlsitemap_custom_list_links() { ...@@ -62,6 +65,9 @@ function xmlsitemap_custom_list_links() {
return $build; return $build;
} }
/**
* Edit Link Form.
*/
function xmlsitemap_custom_edit_link_form($form, &$form_state, $link = array()) { function xmlsitemap_custom_edit_link_form($form, &$form_state, $link = array()) {
module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin'); module_load_include('inc', 'xmlsitemap', 'xmlsitemap.admin');
_xmlsitemap_set_breadcrumb('admin/config/search/xmlsitemap/custom'); _xmlsitemap_set_breadcrumb('admin/config/search/xmlsitemap/custom');
...@@ -116,7 +122,7 @@ function xmlsitemap_custom_edit_link_form($form, &$form_state, $link = array()) ...@@ -116,7 +122,7 @@ function xmlsitemap_custom_edit_link_form($form, &$form_state, $link = array())
); );
$form['actions'] = array( $form['actions'] = array(
'#type' => 'actions' '#type' => 'actions',
); );
$form['actions']['submit'] = array( $form['actions']['submit'] = array(
'#type' => 'submit', '#type' => 'submit',
...@@ -131,6 +137,9 @@ function xmlsitemap_custom_edit_link_form($form, &$form_state, $link = array()) ...@@ -131,6 +137,9 @@ function xmlsitemap_custom_edit_link_form($form, &$form_state, $link = array())
return $form; return $form;
} }
/**
* Edit Link Form Validate.
*/
function xmlsitemap_custom_edit_link_form_validate($form, &$form_state) { function xmlsitemap_custom_edit_link_form_validate($form, &$form_state) {
$link = &$form_state['values']; $link = &$form_state['values'];
...@@ -143,12 +152,16 @@ function xmlsitemap_custom_edit_link_form_validate($form, &$form_state) { ...@@ -143,12 +152,16 @@ function xmlsitemap_custom_edit_link_form_validate($form, &$form_state) {
$menu_item = menu_get_item($link['loc']); $menu_item = menu_get_item($link['loc']);
xmlsitemap_restore_user(); xmlsitemap_restore_user();
// Since the menu item access results are cached, manually check the current path. // Since the menu item access results are cached, manually check the current
// path.
if ($menu_item && strpos($link['loc'], 'admin/config/search/xmlsitemap/custom') === 0 && !user_access('administer xmlsitemap', drupal_anonymous_user())) { if ($menu_item && strpos($link['loc'], 'admin/config/search/xmlsitemap/custom') === 0 && !user_access('administer xmlsitemap', drupal_anonymous_user())) {
$menu_item['access'] = FALSE; $menu_item['access'] = FALSE;
} }
if (db_query_range("SELECT 1 FROM {xmlsitemap} WHERE type <> 'custom' AND loc = :loc AND status = 1 AND access = 1 AND language IN (:languages)", 0, 1, array(':loc' => $link['loc'], ':languages' => array(LANGUAGE_NONE, $link['language'])))->fetchField()) { if (db_query_range("SELECT 1 FROM {xmlsitemap} WHERE type <> 'custom' AND loc = :loc AND status = 1 AND access = 1 AND language IN (:languages)", 0, 1, array(
':loc' => $link['loc'],
':languages' => array(LANGUAGE_NONE, $link['language']),
))->fetchField()) {
form_set_error('loc', t('There is already an existing link in the sitemap with the path %link.', array('%link' => $link['loc']))); form_set_error('loc', t('There is already an existing link in the sitemap with the path %link.', array('%link' => $link['loc'])));
} }
elseif (empty($menu_item['access']) && !is_readable('./' . $link['loc'])) { elseif (empty($menu_item['access']) && !is_readable('./' . $link['loc'])) {
...@@ -158,6 +171,9 @@ function xmlsitemap_custom_edit_link_form_validate($form, &$form_state) { ...@@ -158,6 +171,9 @@ function xmlsitemap_custom_edit_link_form_validate($form, &$form_state) {
} }
} }
/**
* Edit Link Form Submit.
*/
function xmlsitemap_custom_edit_link_form_submit($form, &$form_state) { function xmlsitemap_custom_edit_link_form_submit($form, &$form_state) {
$link = $form_state['values']; $link = $form_state['values'];
xmlsitemap_link_save($link); xmlsitemap_link_save($link);
...@@ -165,6 +181,9 @@ function xmlsitemap_custom_edit_link_form_submit($form, &$form_state) { ...@@ -165,6 +181,9 @@ function xmlsitemap_custom_edit_link_form_submit($form, &$form_state) {
$form_state['redirect'] = 'admin/config/search/xmlsitemap/custom'; $form_state['redirect'] = 'admin/config/search/xmlsitemap/custom';
} }
/**
* Delete Link Form.
*/
function xmlsitemap_custom_delete_link_form($form, &$form_state, array $link) { function xmlsitemap_custom_delete_link_form($form, &$form_state, array $link) {
// @todo Remove when http://drupal.org/node/576290 is fixed. // @todo Remove when http://drupal.org/node/576290 is fixed.
_xmlsitemap_set_breadcrumb('admin/config/search/xmlsitemap/custom'); _xmlsitemap_set_breadcrumb('admin/config/search/xmlsitemap/custom');
...@@ -189,6 +208,9 @@ function xmlsitemap_custom_delete_link_form($form, &$form_state, array $link) { ...@@ -189,6 +208,9 @@ function xmlsitemap_custom_delete_link_form($form, &$form_state, array $link) {
); );
} }
/**
* Delete Link Form Submit.
*/
function xmlsitemap_custom_delete_link_form_submit($form, &$form_state) { function xmlsitemap_custom_delete_link_form_submit($form, &$form_state) {
$link = $form_state['values']['link']; $link = $form_state['values']['link'];
xmlsitemap_link_delete('custom', $link['id']); xmlsitemap_link_delete('custom', $link['id']);
......
...@@ -3,8 +3,5 @@ description = Adds user configurable links to the sitemap. ...@@ -3,8 +3,5 @@ description = Adds user configurable links to the sitemap.
package = XML sitemap package = XML sitemap
core = 7.x core = 7.x
dependencies[] = xmlsitemap dependencies[] = xmlsitemap
files[] = xmlsitemap_custom.module
files[] = xmlsitemap_custom.admin.inc
files[] = xmlsitemap_custom.install
files[] = xmlsitemap_custom.test files[] = xmlsitemap_custom.test
configure = admin/config/search/xmlsitemap/custom configure = admin/config/search/xmlsitemap/custom
<?php <?php
/**
* @file
* Main file for XML sitemap Custom.
*/
/** /**
* Implements hook_menu(). * Implements hook_menu().
*/ */
...@@ -42,7 +47,7 @@ function xmlsitemap_custom_menu() { ...@@ -42,7 +47,7 @@ function xmlsitemap_custom_menu() {
/** /**
* Menu load callback; load a custom sitemap link from the {xmlsitemap} table. * Menu load callback; load a custom sitemap link from the {xmlsitemap} table.
* *
* @param $id * @param string $id
* The sitemap link ID of the custom link to load. * The sitemap link ID of the custom link to load.
* *
* @see xmlsitemap_link_load() * @see xmlsitemap_link_load()
......
...@@ -2,10 +2,17 @@ ...@@ -2,10 +2,17 @@
/** /**
* @file * @file
* Unit tests for the xmlsitemap_custom module. * Unit tests for the xmlsitemap_custom.
*/ */
/**
* Class with Functional Test for XML Sitemap Custom.
*/
class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper { class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
/**
* Get Info.
*/
public static function getInfo() { public static function getInfo() {
return array( return array(
'name' => 'XML sitemap custom interface tests', 'name' => 'XML sitemap custom interface tests',
...@@ -14,7 +21,10 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper { ...@@ -14,7 +21,10 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
); );
} }
function setUp($modules = array()) { /**
* Setup.
*/
public function setUp($modules = array()) {
$modules[] = 'xmlsitemap_custom'; $modules[] = 'xmlsitemap_custom';
$modules[] = 'path'; $modules[] = 'path';
parent::setUp($modules); parent::setUp($modules);
...@@ -23,7 +33,10 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper { ...@@ -23,7 +33,10 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
$this->drupalLogin($this->admin_user); $this->drupalLogin($this->admin_user);
} }
function testCustomLinks() { /**
* Test Custom Links.
*/
public function testCustomLinks() {
// Set a path alias for the node page. // Set a path alias for the node page.
$alias = array('source' => 'system/files', 'alias' => 'public-files'); $alias = array('source' => 'system/files', 'alias' => 'public-files');
path_save($alias); path_save($alias);
...@@ -57,7 +70,12 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper { ...@@ -57,7 +70,12 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
$links = xmlsitemap_link_load_multiple(array('type' => 'custom', 'loc' => 'system/files')); $links = xmlsitemap_link_load_multiple(array('type' => 'custom', 'loc' => 'system/files'));
$this->assertEqual(count($links), 1, t('Custom link saved in the database.')); $this->assertEqual(count($links), 1, t('Custom link saved in the database.'));
$link = reset($links); $link = reset($links);
$this->assertSitemapLinkValues('custom', $link['id'], array('priority' => 0.5, 'changefreq' => 0, 'access' => 1, 'status' => 1)); $this->assertSitemapLinkValues('custom', $link['id'], array(
'priority' => 0.5,
'changefreq' => 0,
'access' => 1,
'status' => 1,
));
$this->clickLink('Edit'); $this->clickLink('Edit');
$edit = array( $edit = array(
...@@ -66,7 +84,12 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper { ...@@ -66,7 +84,12 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
); );
$this->drupalPost(NULL, $edit, t('Save')); $this->drupalPost(NULL, $edit, t('Save'));
$this->assertText('The custom link for system/files was saved'); $this->assertText('The custom link for system/files was saved');
$this->assertSitemapLinkValues('custom', $link['id'], array('priority' => 0.1, 'changefreq' => XMLSITEMAP_FREQUENCY_ALWAYS, 'access' => 1, 'status' => 1)); $this->assertSitemapLinkValues('custom', $link['id'], array(
'priority' => 0.1,
'changefreq' => XMLSITEMAP_FREQUENCY_ALWAYS,
'access' => 1,
'status' => 1,
));
$this->clickLink('Delete'); $this->clickLink('Delete');
$this->drupalPost(NULL, array(), t('Delete')); $this->drupalPost(NULL, array(), t('Delete'));
...@@ -77,7 +100,7 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper { ...@@ -77,7 +100,7 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
/** /**
* Test adding files as custom links. * Test adding files as custom links.
*/ */
function testCustomFileLinks() { public function testCustomFileLinks() {
// Test an invalid file. // Test an invalid file.
$edit['loc'] = $this->randomName(); $edit['loc'] = $this->randomName();
$this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save')); $this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
...@@ -85,11 +108,12 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper { ...@@ -85,11 +108,12 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
$this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc'])); $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));
// Test an unaccessible file . // Test an unaccessible file .
//$edit['loc'] = '.htaccess'; // @codingStandardsIgnoreStart
//$this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save')); // $edit['loc'] = '.htaccess';
//$this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc']))); // $this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
//$this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc'])); // $this->assertText(t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $edit['loc'])));
// $this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));
// @codingStandardsIgnoreEnd
// Test a valid file. // Test a valid file.
$edit['loc'] = 'misc/drupal.js'; $edit['loc'] = 'misc/drupal.js';
$this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save')); $this->drupalPost('admin/config/search/xmlsitemap/custom/add', $edit, t('Save'));
...@@ -104,4 +128,5 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper { ...@@ -104,4 +128,5 @@ class XMLSitemapCustomFunctionalTest extends XMLSitemapTestHelper {
$links = xmlsitemap_link_load_multiple(array('type' => 'custom', 'loc' => $edit['loc'])); $links = xmlsitemap_link_load_multiple(array('type' => 'custom', 'loc' => $edit['loc']));
$this->assertEqual(count($links), 1, t('Custom link saved in the database.')); $this->assertEqual(count($links), 1, t('Custom link saved in the database.'));
} }
} }
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