Commit 3f7a3f4a authored by amateescu's avatar amateescu
Browse files

Fix and update the XML Sitemap engines module.

parent 21fc8408
......@@ -2,8 +2,9 @@
namespace Drupal\xmlsitemap\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Url;
use Drupal\simpletest\WebTestBase;
use Drupal\xmlsitemap\Entity\XmlSitemap;
/**
......@@ -354,7 +355,7 @@ abstract class XmlSitemapTestBase extends WebTestBase {
foreach ($messages as $message) {
$message->text = $this->formatWatchdogMessage($message);
if (in_array($message->severity, array(WATCHDOG_EMERGENCY, WATCHDOG_ALERT, WATCHDOG_CRITICAL, WATCHDOG_ERROR, WATCHDOG_WARNING))) {
if (in_array($message->severity, array(RfcLogLevel::EMERGENCY, RfcLogLevel::ALERT, RfcLogLevel::CRITICAL, RfcLogLevel::ERROR, RfcLogLevel::WARNING))) {
$this->fail($message->text);
}
$verbose[] = $message->text;
......@@ -383,7 +384,7 @@ abstract class XmlSitemapTestBase extends WebTestBase {
if (!isset($levels)) {
module_load_include('admin.inc', 'dblog');
$levels = watchdog_severity_levels();
$levels = RfcLogLevel::getLevels();
}
return t('@timestamp - @severity - @type - @message', array(
......
engines: {}
custom_urls: {}
custom_urls: ''
minimum_lifetime: 86400
# Schema for the xmlsitemap_engines configuration files of the XMLSitemap module.
xmlsitemap_engines.settings:
type: mapping
label: 'XMLSitemap engines settings'
mapping:
engines:
type: mapping
label: 'Engines that will receive the sitemap'
custom_urls:
type: mapping
label: 'Url engines'
minimum_lifetime:
type: integer
label: 'Minumum lifetime for sitemaps'
type: config_object
label: 'XMLSitemap engines settings'
mapping:
engines:
type: sequence
label: 'Engines that will receive the sitemap'
sequence:
type: string
label: 'Search engine'
custom_urls:
type: string
label: 'Url engines'
minimum_lifetime:
type: integer
label: 'Minumum lifetime for sitemaps'
......@@ -42,6 +42,7 @@ class XmlSitemapEnginesSettingsForm extends ConfigFormBase {
*/
public function __construct(ConfigFactoryInterface $config_factory, DateFormatter $date, StateInterface $state) {
parent::__construct($config_factory);
$this->date = $date;
$this->state = $state;
}
......@@ -51,7 +52,9 @@ class XmlSitemapEnginesSettingsForm extends ConfigFormBase {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'), $container->get('date.formatter'), $container->get('state')
$container->get('config.factory'),
$container->get('date.formatter'),
$container->get('state')
);
}
......@@ -85,7 +88,7 @@ class XmlSitemapEnginesSettingsForm extends ConfigFormBase {
$form['engines'] = array(
'#type' => 'checkboxes',
'#title' => t('Submit the sitemap to the following engines'),
'#title' => $this->t('Submit the sitemap to the following engines'),
'#default_value' => $this->config('xmlsitemap_engines.settings')->get('engines'),
'#options' => $engine_options,
);
......@@ -97,19 +100,19 @@ class XmlSitemapEnginesSettingsForm extends ConfigFormBase {
}
$form['minimum_lifetime'] = array(
'#type' => 'select',
'#title' => t('Do not submit more often than every'),
'#title' => $this->t('Do not submit more often than every'),
'#options' => $format_lifetimes,
'#default_value' => $this->config('xmlsitemap_engines.settings')->get('minimum_lifetime'),
);
$form['xmlsitemap_engines_submit_updated'] = array(
'#type' => 'checkbox',
'#title' => t('Only submit if the sitemap has been updated since the last submission.'),
'#title' => $this->t('Only submit if the sitemap has been updated since the last submission.'),
'#default_value' => $this->state->get('xmlsitemap_engines_submit_updated'),
);
$form['custom_urls'] = array(
'#type' => 'textarea',
'#title' => t('Custom submission URLs'),
'#description' => t('Enter one URL per line. The token [sitemap] will be replaced with the URL to your sitemap. For example: %example-before would become %example-after.', array('%example-before' => 'http://example.com/ping?[sitemap]', '%example-after' => xmlsitemap_engines_prepare_url('http://example.com/ping?[sitemap]', Url::fromRoute('xmlsitemap.sitemap_xml', [], ['absolute' => TRUE])))),
'#title' => $this->t('Custom submission URLs'),
'#description' => $this->t('Enter one URL per line. The token [sitemap] will be replaced with the URL to your sitemap. For example: %example-before would become %example-after.', array('%example-before' => 'http://example.com/ping?[sitemap]', '%example-after' => xmlsitemap_engines_prepare_url('http://example.com/ping?[sitemap]', Url::fromRoute('xmlsitemap.sitemap_xml', [], ['absolute' => TRUE])->toString()))),
'#default_value' => $this->config('xmlsitemap_engines.settings')->get('custom_urls'),
'#rows' => 2,
'#wysiwyg' => FALSE,
......@@ -130,7 +133,7 @@ class XmlSitemapEnginesSettingsForm extends ConfigFormBase {
foreach ($custom_urls as $custom_url) {
$url = xmlsitemap_engines_prepare_url($custom_url, '');
if (!UrlHelper::isValid($url, TRUE)) {
$form_state->setErrorByName($custom_url, t('Invalid URL %url.', array('%url' => $custom_url)));
$form_state->setErrorByName($custom_url, $this->t('Invalid URL %url.', array('%url' => $custom_url)));
}
}
$custom_urls = implode("\n", $custom_urls);
......
......@@ -2,6 +2,7 @@
namespace Drupal\xmlsitemap_engines\Tests;
use Drupal\xmlsitemap\Entity\XmlSitemap;
use Drupal\xmlsitemap\Tests\XmlSitemapTestBase;
use Drupal\Core\Url;
......@@ -22,7 +23,7 @@ class XmlSitemapEnginesFunctionalTest extends XmlSitemapTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['system', 'path', 'node', 'dblog', 'xmlsitemap_engines', 'xmlsitemap_engines_test'];
public static $modules = ['path', 'dblog', 'xmlsitemap_engines', 'xmlsitemap_engines_test'];
/**
* {@inheritdoc}
......@@ -35,7 +36,7 @@ class XmlSitemapEnginesFunctionalTest extends XmlSitemapTestBase {
// @todo For some reason the test client does not have clean URLs while
// the test runner does, so it causes mismatches in watchdog assertions
// later.
$this->submit_url = Url::fromUri('base://ping', array('absolute' => TRUE, 'query' => array('sitemap' => ''))) . '[sitemap]';
$this->submit_url = Url::fromUri('base://ping', array('absolute' => TRUE, 'query' => array('sitemap' => '')))->toString() . '[sitemap]';
}
/**
......@@ -46,9 +47,9 @@ class XmlSitemapEnginesFunctionalTest extends XmlSitemapTestBase {
'xmlsitemap_engines_submit_last' => REQUEST_TIME - 10000,
'xmlsitemap_generated_last' => REQUEST_TIME - 100,
));
\Drupal::config('xmlsitemap_engines.settings')->set('minimum_lifetime', 0)->save();
\Drupal::configFactory()->getEditable('xmlsitemap_engines.settings')->set('minimum_lifetime', 0)->save();
xmlsitemap_engines_cron();
$this->assertTrue($this->state->get('xmlsitemap_engines_submit_last') > (REQUEST_TIME - 100), 'Submitted the sitemaps to search engines. {}');
$this->assertTrue($this->state->get('xmlsitemap_engines_submit_last') > (REQUEST_TIME - 100), 'Submitted the sitemaps to search engines.');
}
/**
......@@ -120,7 +121,7 @@ class XmlSitemapEnginesFunctionalTest extends XmlSitemapTestBase {
$this->assertText('Invalid URL an-invalid-url.');
$this->assertNoText('The configuration options have been saved.');
$url = Url::fromUri('base://ping', array('absolute' => TRUE));
$url = Url::fromUri('base://ping', array('absolute' => TRUE))->toString();
$edit = array('custom_urls' => $url);
$this->drupalPostForm('admin/config/search/xmlsitemap/engines', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'));
......@@ -130,9 +131,9 @@ class XmlSitemapEnginesFunctionalTest extends XmlSitemapTestBase {
$this->assertText(t('The configuration options have been saved.'));
$this->submitEngines();
$url = xmlsitemap_engines_prepare_url($this->submit_url, Url::fromRoute('xmlsitemap.sitemap_xml', [], array('absolute' => TRUE)));
$this->assertWatchdogMessage(array('type' => 'xmlsitemap', 'message' => 'Submitted the sitemap to %url and received response @code.', 'variables' => array('%url' => $url, '@code' => '200')));
$this->assertWatchdogMessage(array('type' => 'xmlsitemap', 'message' => 'Recieved ping for @sitemap.', 'variables' => array('@sitemap' => Url::fromRoute('xmlsitemap.sitemap_xml', [], array('absolute' => TRUE)))));
$url = xmlsitemap_engines_prepare_url($this->submit_url, Url::fromRoute('xmlsitemap.sitemap_xml', [], array('absolute' => TRUE))->toString());
$this->assertWatchdogMessage(array('type' => 'xmlsitemap', 'message' => 'Submitted the sitemap to %url and received response @code.', 'variables' => array('%url' => $url, '@code' => 200)));
$this->assertWatchdogMessage(array('type' => 'xmlsitemap', 'message' => 'Recieved ping for @sitemap.', 'variables' => array('@sitemap' => Url::fromRoute('xmlsitemap.sitemap_xml', [], array('absolute' => TRUE))->toString())));
}
}
......@@ -24,14 +24,12 @@ class XmlSitemapEnginesTestController extends ControllerBase {
public function render() {
$query = \Drupal::request()->query->get('sitemap');
if (empty($query) || !UrlHelper::isValid($query)) {
$message = t('No valid sitemap parameter provided.');
\Drupal::logger('xmlsitemap')->debug($message);
\Drupal::logger('xmlsitemap')->debug('No valid sitemap parameter provided.');
// @todo Remove this? Causes an extra watchdog error to be handled.
throw new NotFoundHttpException();
}
else {
$message = t('Recieved ping for @sitemap.', array('@sitemap' => $query));
\Drupal::logger('xmlsitemap')->debug($message);
\Drupal::logger('xmlsitemap')->debug('Recieved ping for @sitemap.', array('@sitemap' => $query));
}
return new Response('', 200);
}
......
......@@ -19,20 +19,5 @@ function xmlsitemap_engines_test_xmlsitemap_engine_info() {
* Implements hook_xmlsitemap_engine_info_alter().
*/
function xmlsitemap_engines_test_xmlsitemap_engine_info_alter(&$engines) {
$engines['simpletest']['url'] = Url::fromUri('base://ping', array('absolute' => TRUE, 'query' => array('sitemap' => ''))) . '[sitemap]';
}
function xmlsitemap_engines_test_pinged() {
$query_sitemap = \Drupal::request()->query->get('sitemap');
//$query_sitemap = $query->get('sitemap');
if (empty($query_sitemap) || !UrlHelper::isValid($query_sitemap)) {
$message = t('No valid sitemap parameter provided.');
\Drupal::logger('xmlsitemap')->debug($message);
// @todo Remove this? Causes an extra watchdog error to be handled.
throw new NotFoundHttpException();
}
else {
$message = t('Recieved ping for @sitemap.', array('@sitemap' => $query_sitemap));
\Drupal::logger('xmlsitemap')->debug($message);
}
$engines['simpletest']['url'] = Url::fromUri('internal:/ping', array('absolute' => TRUE, 'query' => array('sitemap' => '')))->toString() . '[sitemap]';
}
xmlsitemap_engines_test.ping:
path: '/ping'
defaults:
_content: '\Drupal\xmlsitemap_engines_test\Controller\XmlSitemapEnginesTestController::render'
_controller: '\Drupal\xmlsitemap_engines_test\Controller\XmlSitemapEnginesTestController::render'
_title: 'Ping'
requirements:
_access: 'TRUE'
<?php
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Routing\RouteMatchInterface;
use GuzzleHttp\Exception\RequestException;
use Drupal\Core\Cache\Cache;
......@@ -80,7 +81,7 @@ function xmlsitemap_engines_submit_access() {
/**
* Submit the sitemaps to all the specified search engines.
*
* @param $smids
* @param array $smids
* An optional array of XML sitemap IDs. If not provided, it will load all
* existing XML sitemaps.
*/
......@@ -111,14 +112,15 @@ function xmlsitemap_engines_submit_engines(array $smids = array()) {
/**
* Submit the sitemaps to a specific URL.
*
* @param $url
* @param string $url
* The URL for sitemap submission.
* @param $sitemaps
* @param array $sitemaps
* An array of URLs of the sitemaps to submit.
*/
function xmlsitemap_engines_submit_sitemaps($url, array $sitemaps) {
foreach ($sitemaps as $sitemap) {
$sitemap_url = Url::fromUri($sitemap->uri['path'], $sitemap->uri['options']);
$sitemap_path = UrlHelper::isExternal($sitemap->uri['path']) ? $sitemap->uri['path'] : 'base://' . $sitemap->uri['path'];
$sitemap_url = Url::fromUri($sitemap_path, $sitemap->uri['options'])->toString();
$submit_url = xmlsitemap_engines_prepare_url($url, $sitemap_url);
try {
$request = \Drupal::httpClient()->get($submit_url);
......@@ -133,9 +135,10 @@ function xmlsitemap_engines_submit_sitemaps($url, array $sitemaps) {
/**
* Replace valid tokens in the URL with their appropriate values.
*
* @param $url
* @param string $url
* An un-tokenized URL.
* @return
*
* @return string
* A tokenized URL.
*/
function xmlsitemap_engines_prepare_url($url, $sitemap) {
......@@ -145,12 +148,12 @@ function xmlsitemap_engines_prepare_url($url, $sitemap) {
/**
* Returns information about supported search engines.
*
* @param $engine
* @param string $engine
* (optional) The engine to return information for. If omitted, information
* for all engines is returned.
* @param $reset
* (optional) Boolean whether to reset the static cache and do nothing. Only
* used for tests.
*
* @return string[]
* An array of supported search engines.
*
* @see hook_xmlsitemap_engines_info()
* @see hook_xmlsitemap_engines_info_alter()
......
......@@ -4,4 +4,4 @@ xmlsitemap_engines.settings:
_form: '\Drupal\xmlsitemap_engines\Form\XmlSitemapEnginesSettingsForm'
_title: 'Search Engines'
requirements:
_permission: 'administer xmlsitemap'
\ No newline at end of file
_permission: 'administer xmlsitemap'
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