Skip to content

Commit

Permalink
Use absolute URLs in /sitemap.xml (#3729)
Browse files Browse the repository at this point in the history
  • Loading branch information
Clayton Liddell authored Dec 20, 2021
1 parent d2c1666 commit 8a4c744
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
21 changes: 15 additions & 6 deletions modules/dkan_js_frontend/dkan_js_frontend.module
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,17 @@ function dkan_js_frontend_simple_sitemap_arbitrary_links_alter(array &$arbitrary
* Collection of DKAN routes.
*/
function _dkan_js_frontend_add_static_links(array &$arbitrary_links, RouteCollection $routes): void {
// Build request context from request.
$request = \Drupal::request();
$request_context = (new RequestContext())->fromRequest($request);
// Build route URL generator.
$url_generator = new UrlGenerator($routes, $request_context);
// Loop through routes and add to sitemap.
foreach ($routes as $route) {
foreach ($routes as $route_name => $route) {
// Add this link to the sitemap if it's not a dynamic route.
if (empty($route->compile()->getPathVariables())) {
$arbitrary_links[] = DKAN_JS_FRONTEND_DEFAULT_STATIC_LINK + [
'url' => $route->getPath(),
'url' => $url_generator->generate($route_name, [], UrlGeneratorInterface::ABSOLUTE_URL),
];
}
}
Expand All @@ -134,17 +139,21 @@ function _dkan_js_frontend_add_static_links(array &$arbitrary_links, RouteCollec
* Collection of DKAN routes.
*/
function _dkan_js_frontend_add_dataset_links(array &$arbitrary_links, Route $dataset_route): void {
// Build route collection.
$routes = new RouteCollection();
$routes->add('dataset', $dataset_route);
// Build request context from request.
$request = \Drupal::request();
$request_context = (new RequestContext())->fromRequest($request);
// Build route URL generator.
$route_collection = new RouteCollection();
$route_collection->add('dataset', $dataset_route);
$url_generator = new UrlGenerator($route_collection, new RequestContext());
$url_generator = new UrlGenerator($routes, $request_context);

// Fetch dataset UUIDs.
$dataset_uuids = \Drupal::service('dkan.metastore.service')->getRangeUuids('dataset');
// Add dataset routes using the fetched UUIDs.
foreach ($dataset_uuids as $uuid) {
$arbitrary_links[] = DKAN_JS_FRONTEND_DEFAULT_DATASET_LINK + [
'url' => $url_generator->generate('dataset', ['id' => $uuid], UrlGeneratorInterface::ABSOLUTE_PATH),
'url' => $url_generator->generate('dataset', ['id' => $uuid], UrlGeneratorInterface::ABSOLUTE_URL),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use MockChain\Chain;
use MockChain\Options;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

$module_path = substr(__DIR__, 0, strpos(__DIR__, '/dkan_js_frontend/')) . '/dkan_js_frontend';
require_once($module_path . '/dkan_js_frontend.module');
Expand All @@ -26,6 +28,13 @@
*/
class SimpleSitemapArbitraryLinksAlterTest extends TestCase {

/**
* Base URL to use for testing sitemap URL generation.
*
* @var string
*/
protected const BASE_URL = 'https://example.com';

/**
* Test sitemap generation of static links.
*/
Expand All @@ -40,9 +49,11 @@ public function testSitemapStaticLinks(): void {
->add('entity_type.repository', EntityTypeRepository::class)
->add('entity_type.manager', EntityTypeManagerInterface::class)
->add('logger.factory', LoggerChannelFactory::class)
->add('request_stack', RequestStack::class)
->index(0);
$container = (new Chain($this))
->add(Container::class, 'get', $containerOptions)
->add(RequestStack::class, 'getCurrentRequest', (Request::create(self::BASE_URL)))
->getMock();
\Drupal::setContainer($container);

Expand All @@ -53,8 +64,8 @@ public function testSitemapStaticLinks(): void {
dkan_js_frontend_simple_sitemap_arbitrary_links_alter($arbitrary_links, $simpleSitemap);

$this->assertEquals($arbitrary_links, [
DKAN_JS_FRONTEND_DEFAULT_STATIC_LINK + ['url' => '/home'],
DKAN_JS_FRONTEND_DEFAULT_STATIC_LINK + ['url' => '/about'],
DKAN_JS_FRONTEND_DEFAULT_STATIC_LINK + ['url' => self::BASE_URL . '/home'],
DKAN_JS_FRONTEND_DEFAULT_STATIC_LINK + ['url' => self::BASE_URL . '/about'],
]);
}

Expand All @@ -72,9 +83,11 @@ public function testSitemapDatasetLinks(): void {
->add('entity_type.repository', EntityTypeRepository::class)
->add('entity_type.manager', EntityTypeManagerInterface::class)
->add('dkan.metastore.service', Service::class)
->add('request_stack', RequestStack::class)
->index(0);
$container = (new Chain($this))
->add(Container::class, 'get', $containerOptions)
->add(RequestStack::class, 'getCurrentRequest', (Request::create(self::BASE_URL)))
->add(Service::class, 'getRangeUuids', [1, 2])
->getMock();
\Drupal::setContainer($container);
Expand All @@ -85,9 +98,10 @@ public function testSitemapDatasetLinks(): void {
->getMock();
dkan_js_frontend_simple_sitemap_arbitrary_links_alter($arbitrary_links, $simpleSitemap);

$host = \Drupal::request()->getSchemeAndHttpHost();
$this->assertEquals($arbitrary_links, [
DKAN_JS_FRONTEND_DEFAULT_DATASET_LINK + ['url' => '/dataset/1'],
DKAN_JS_FRONTEND_DEFAULT_DATASET_LINK + ['url' => '/dataset/2'],
DKAN_JS_FRONTEND_DEFAULT_DATASET_LINK + ['url' => self::BASE_URL . '/dataset/1'],
DKAN_JS_FRONTEND_DEFAULT_DATASET_LINK + ['url' => self::BASE_URL . '/dataset/2'],
]);
}

Expand All @@ -106,11 +120,13 @@ public function testSitemapErrorNoDatasetRouteFound(): void {
->add('entity_type.manager', EntityTypeManagerInterface::class)
->add('logger.factory', LoggerChannelFactory::class)
->add('dkan.metastore.service', Service::class)
->add('request_stack', RequestStack::class)
->index(0);
$containerChain = (new Chain($this))
->add(Container::class, 'get', $containerOptions)
->add(LoggerChannelFactory::class, 'get', LoggerChannelInterface::class)
->add(LoggerChannelInterface::class, 'error', NULL, 'error');
->add(LoggerChannelInterface::class, 'error', NULL, 'error')
->add(RequestStack::class, 'getCurrentRequest', (Request::create(self::BASE_URL)));
\Drupal::setContainer($containerChain->getMock());

$arbitrary_links = [];
Expand Down

0 comments on commit 8a4c744

Please sign in to comment.