Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set 404 exception if dataset ID isn't found. #4379

Open
wants to merge 8 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions modules/dkan_js_frontend/config/schema/dkan_js_frontend.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
dkan_js_frontend.config:
type: config_object
mapping:
css_folder:
type: string
label: 'css folder'
js_folder:
type: string
label: 'css folder'
routes:
type: sequence
label: 'Routes'
sequence:
type: label
minified:
type: boolean
label: 'Is minified'
preprocess:
type: boolean
label: 'Preprocess'
js:
type: mapping
label: 'js'
mapping:
attributes:
type: sequence
label: 'Attributes'
sequence:
type: label
50 changes: 50 additions & 0 deletions modules/dkan_js_frontend/src/Controller/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,66 @@
namespace Drupal\dkan_js_frontend\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\metastore\Exception\MissingObjectException;
use Drupal\metastore\MetastoreService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* The Page controller.
*/
class Page extends ControllerBase {

/**
* Metastore service.
*/
private MetastoreService $service;

/**
* The current path.
*
* @var \Drupal\Core\Path\CurrentPathStack
*/
protected CurrentPathStack $currentPath;

/**
* Inherited.
*
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('dkan.metastore.service'),
$container->get('path.current')
);
}

/**
* Constructor.
*/
public function __construct(MetastoreService $service, CurrentPathStack $current_path) {
$this->service = $service;
$this->currentPath = $current_path;
}

/**
* Returns a render-able array.
*/
public function content() {
// Path should always have leading slash.
// @see \Symfony\Component\HttpFoundation\Request::getPathInfo()
$path_parts = explode('/', $this->currentPath->getPath());

if (is_array($path_parts) && count($path_parts) === 3 && $path_parts[1] === 'dataset') {
try {
$this->service->get('dataset', $path_parts[2]);
}
catch (MissingObjectException $exception) {
throw new NotFoundHttpException();
}
}

return [
'#theme' => 'page__dkan_js_frontend',
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\dkan_js_frontend\Functional\Controller;

use Drupal\Tests\BrowserTestBase;

/**
* @covers \Drupal\dkan_js_frontend\Controller\Page
* @coversDefaultClass \Drupal\dkan_js_frontend\Controller\Page
*
* @group dkan
* @group dkan_js_frontend
* @group functional
*/
class PageTest extends BrowserTestBase {

protected $defaultTheme = 'stark';

protected static $modules = [
'dkan_js_frontend',
'metastore',
'node',
'field',
];

/**
* Test 200 for existing route, and 404 for non-existent datasets.
*/
public function test() {
$this->drupalGet('home');
$session = $this->getSession();
$this->assertEquals(200, $session->getStatusCode(), $session->getPage()->getHtml());

$this->drupalGet('dataset/123');
$session = $this->getSession();
$this->assertEquals(404, $session->getStatusCode(), $session->getPage()->getHtml());
}

}
87 changes: 87 additions & 0 deletions modules/dkan_js_frontend/tests/src/Kernel/Controller/PageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\dkan_js_frontend\Kernel;

use Drupal\Core\Path\CurrentPathStack;
use Drupal\dkan_js_frontend\Controller\Page;
use Drupal\KernelTests\KernelTestBase;
use Drupal\metastore\Exception\MissingObjectException;
use Drupal\metastore\MetastoreService;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* @covers \Drupal\dkan_js_frontend\Controller\Page
* @coversDefaultClass \Drupal\dkan_js_frontend\Controller\Page
*
* @group dkan
* @group dkan_js_frontend
* @group kernel
*/
class PageTest extends KernelTestBase {

/**
* @covers ::content
*/
public function test404OnBadPath() {
$metastore_service = $this->getMockBuilder(MetastoreService::class)
->disableOriginalConstructor()
->onlyMethods(['get'])
->getMock();
$metastore_service->expects($this->once())
->method('get')
->willThrowException(new MissingObjectException());

$this->container->set('dkan.metastore.service', $metastore_service);

$current_path = $this->getMockBuilder(CurrentPathStack::class)
->disableOriginalConstructor()
->onlyMethods(['getPath'])
->getMock();
$current_path->expects($this->once())
->method('getPath')
// Always use leading slash.
// @see \Symfony\Component\HttpFoundation\Request::getPathInfo()
->willReturn('/dataset/123');

$this->container->set('path.current', $current_path);

$page = Page::create($this->container);
$this->expectException(NotFoundHttpException::class);
$page->content();
}

/**
* @covers ::content
*/
public function testPathPresent() {
// Mock
$metastore_service = $this->getMockBuilder(MetastoreService::class)
->disableOriginalConstructor()
->getMock();

$this->container->set('dkan.metastore.service', $metastore_service);

$current_path = $this->getMockBuilder(CurrentPathStack::class)
->disableOriginalConstructor()
->onlyMethods(['getPath'])
->getMock();
$current_path->expects($this->once())
->method('getPath')
// Always use leading slash.
// @see \Symfony\Component\HttpFoundation\Request::getPathInfo()
->willReturn('/dataset/123');

$this->container->set('path.current', $current_path);

$page = Page::create($this->container);
$this->assertEquals(
[
'#theme' => 'page__dkan_js_frontend',
],
$page->content(),
);
}

}
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<directory>modules/datastore/tests/src/Unit</directory>
<directory>modules/datastore/modules/datastore_mysql_import/tests/src/Unit</directory>
<directory>modules/frontend/tests/src/Unit</directory>
<directory>modules/dkan_js_frontend/tests/src</directory>
<directory>modules/dkan_js_frontend/tests/src/Unit</directory>
<directory>modules/harvest/tests/src/Unit</directory>
<directory>modules/json_form_widget/tests/src/Unit</directory>
</testsuite>
Expand All @@ -46,7 +46,7 @@
<directory>modules/metastore/modules/metastore_search/tests/src/Functional</directory>
<directory>modules/metastore/modules/metastore_admin/tests/src/Functional</directory>
<directory>modules/datastore/tests/src/Functional</directory>
<directory>modules/dkan_js_frontend/tests/src</directory>
<directory>modules/dkan_js_frontend/tests/src/Functional</directory>
<directory>tests/src/Functional</directory>
</testsuite>
</testsuites>
Expand Down