Commit 30347dd2 authored by amateescu's avatar amateescu
Browse files

Fix XmlSitemapCustomFunctionalTest.

parent 01cfb78d
......@@ -4,8 +4,8 @@ namespace Drupal\xmlsitemap_custom\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Http\ClientFactory;
use Drupal\Core\Language\LanguageInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
......@@ -40,6 +40,13 @@ class XmlSitemapCustomAddForm extends FormBase {
*/
protected $aliasManager;
/**
* The HTTP client to fetch the feed data with.
*
* @var \Drupal\Core\Http\ClientFactory
*/
protected $httpClientFactory;
/**
* The xmlsitemap link storage handler.
*
......@@ -56,13 +63,16 @@ class XmlSitemapCustomAddForm extends FormBase {
* The language manager service.
* @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
* The path alias manager service.
* @param \Drupal\Core\Http\ClientFactory $http_client_factory
* A Guzzle client object.
* @param \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface $link_storage
* The xmlsitemap link storage service.
*/
public function __construct(Connection $connection, LanguageManagerInterface $language_manager, AliasManagerInterface $alias_manager, XmlSitemapLinkStorageInterface $link_storage) {
public function __construct(Connection $connection, LanguageManagerInterface $language_manager, AliasManagerInterface $alias_manager, ClientFactory $http_client_factory, XmlSitemapLinkStorageInterface $link_storage) {
$this->connection = $connection;
$this->languageManager = $language_manager;
$this->aliasManager = $alias_manager;
$this->httpClientFactory = $http_client_factory;
$this->linkStorage = $link_storage;
}
......@@ -74,6 +84,7 @@ class XmlSitemapCustomAddForm extends FormBase {
$container->get('database'),
$container->get('language_manager'),
$container->get('path.alias_manager'),
$container->get('http_client_factory'),
$container->get('xmlsitemap.link_storage')
);
}
......@@ -205,8 +216,8 @@ class XmlSitemapCustomAddForm extends FormBase {
$form_state->setErrorByName('loc', $this->t('There is already an existing link in the sitemap with the path %link.', array('%link' => $link['loc'])));
}
try {
$client = new Client();
$client->get(Url::fromRoute('<front>', [], array('absolute' => TRUE))->toString() . $link['loc']);
$client = $this->httpClientFactory->fromOptions(['config/curl', array(CURLOPT_FOLLOWLOCATION => FALSE)]);
$client->get(Url::fromUserInput('/' . ltrim($link['loc'], '/'), ['absolute' => TRUE])->toString());
}
catch (ClientException $e) {
$form_state->setErrorByName('loc', $this->t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $link['loc'])));
......
......@@ -3,8 +3,8 @@
namespace Drupal\xmlsitemap_custom\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Http\ClientFactory;
use Drupal\Core\Language\LanguageInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
......@@ -39,6 +39,13 @@ class XmlSitemapCustomEditForm extends FormBase {
*/
protected $aliasManager;
/**
* The HTTP client to fetch the feed data with.
*
* @var \Drupal\Core\Http\ClientFactory
*/
protected $httpClientFactory;
/**
* The xmlsitemap link storage handler.
*
......@@ -53,12 +60,15 @@ class XmlSitemapCustomEditForm extends FormBase {
* The language manager service.
* @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
* The path alias manager service.
* @param \Drupal\Core\Http\ClientFactory $http_client_factory
* A Guzzle client object.
* @param \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface $link_storage
* The xmlsitemap link storage service.
*/
public function __construct(LanguageManagerInterface $language_manager, AliasManagerInterface $alias_manager, XmlSitemapLinkStorageInterface $link_storage) {
public function __construct(LanguageManagerInterface $language_manager, AliasManagerInterface $alias_manager, ClientFactory $http_client_factory, XmlSitemapLinkStorageInterface $link_storage) {
$this->languageManager = $language_manager;
$this->aliasManager = $alias_manager;
$this->httpClientFactory = $http_client_factory;
$this->linkStorage = $link_storage;
}
......@@ -69,6 +79,7 @@ class XmlSitemapCustomEditForm extends FormBase {
return new static(
$container->get('language_manager'),
$container->get('path.alias_manager'),
$container->get('http_client_factory'),
$container->get('xmlsitemap.link_storage')
);
}
......@@ -161,8 +172,8 @@ class XmlSitemapCustomEditForm extends FormBase {
$link['loc'] = $this->aliasManager->getPathByAlias($link['loc'], $link['language']);
$form_state->setValue('loc', $link['loc']);
try {
$client = new Client();
$client->get(Url::fromRoute('<front>', [], array('absolute' => TRUE))->toString() . $link['loc']);
$client = $this->httpClientFactory->fromOptions(['config/curl', array(CURLOPT_FOLLOWLOCATION => FALSE)]);
$client->get(Url::fromUserInput('/' . ltrim($link['loc'], '/'), ['absolute' => TRUE])->toString());
}
catch (ClientException $e) {
$form_state->setErrorByName('loc', $this->t('The custom link @link is either invalid or it cannot be accessed by anonymous users.', array('@link' => $link['loc'])));
......
......@@ -60,7 +60,7 @@ class XmlSitemapCustomFunctionalTest extends XmlSitemapTestBase {
$this->assertNoSitemapLink(array('type' => 'custom', 'loc' => $edit['loc']));
// Test a path not accessible to anonymous user.
$edit['loc'] = 'admin/people/people';
$edit['loc'] = 'admin/people';
$this->drupalPostForm(NULL, $edit, t('Save'));
$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']));
......
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