Skip to content
Snippets Groups Projects
Commit fd4e359f authored by Matt Glaman's avatar Matt Glaman Committed by Matt Glaman
Browse files

Issue #3094443 by mglaman: Do not build routes for disabled indexes

parent 74345c8a
Branches
Tags
No related merge requests found
......@@ -84,6 +84,9 @@ final class Routes implements ContainerInjectionInterface {
$indexes = $index_storage->loadMultiple();
foreach ($indexes as $index) {
assert($index instanceof IndexInterface);
if (!$index->status()) {
continue;
}
$resource_types = [];
foreach ($index->getDatasources() as $datasource) {
assert($datasource instanceof DatasourceInterface);
......@@ -113,11 +116,14 @@ final class Routes implements ContainerInjectionInterface {
$routes->add('jsonapi_search_api.index_' . $index->id(), $route);
}
// Prefix all routes with the JSON:API route prefix.
$routes->addPrefix('/%jsonapi%');
$routes->addRequirements([
'_access' => 'TRUE',
]);
if ($routes->count() > 0) {
$routes->addPrefix('/%jsonapi%');
$routes->addRequirements([
'_access' => 'TRUE',
]);
}
return $routes;
}
......
......@@ -29,12 +29,14 @@ final class RoutesTest extends UnitTestCase {
*
* @param \Drupal\search_api\IndexInterface $mocked_index
* The mocked index.
* @param bool $expect_disabled_index
* Boolean to check if the index is expected to be disabled.
* @param string[] $expected_entity_types
* The expected entity types from the index darasources.
* @param \Drupal\jsonapi\ResourceType\ResourceType[] $mocked_resource_types
* The mocked resource types.
*/
public function testRoutes(IndexInterface $mocked_index, array $expected_entity_types, array $mocked_resource_types) {
public function testRoutes(IndexInterface $mocked_index, bool $expect_disabled_index, array $expected_entity_types, array $mocked_resource_types) {
$entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
foreach ($expected_entity_types as $expected_entity_type) {
$entity_type_manager->hasDefinition($expected_entity_type)->willReturn(TRUE);
......@@ -64,17 +66,24 @@ final class RoutesTest extends UnitTestCase {
$route_collection = $routes->routes();
$sut = $route_collection->get('jsonapi_search_api.index_' . $mocked_index->id());
$this->assertNotNull($sut);
$this->assertEquals('/%jsonapi%/index/' . $mocked_index->id(), $sut->getPath());
$this->assertEquals([
'index' => ['type' => 'entity:search_api_index'],
], $sut->getOption('parameters'));
$this->assertEquals(IndexResource::class, $sut->getDefault('_jsonapi_resource'));
$this->assertEquals(array_map(static function (ResourceType $resource_type) {
return $resource_type->getTypeName();
}, $mocked_resource_types), $sut->getDefault('_jsonapi_resource_types'));
$this->assertEquals($mocked_index->uuid(), $sut->getDefault('index'));
$this->assertEquals('search_api_index--search_api_index', $sut->getDefault(JsonapiRoutes::RESOURCE_TYPE_KEY));
if ($expect_disabled_index) {
$this->assertFalse($mocked_index->status());
$this->assertNull($sut);
}
else {
$this->assertNotNull($sut);
$this->assertEquals('/%jsonapi%/index/' . $mocked_index->id(), $sut->getPath());
$this->assertEquals([
'index' => ['type' => 'entity:search_api_index'],
], $sut->getOption('parameters'));
$this->assertEquals(IndexResource::class, $sut->getDefault('_jsonapi_resource'));
$this->assertEquals(array_map(static function (ResourceType $resource_type) {
return $resource_type->getTypeName();
}, $mocked_resource_types), $sut->getDefault('_jsonapi_resource_types'));
$this->assertEquals($mocked_index->uuid(), $sut->getDefault('index'));
$this->assertEquals('search_api_index--search_api_index', $sut->getDefault(JsonapiRoutes::RESOURCE_TYPE_KEY));
}
}
/**
......@@ -92,6 +101,19 @@ final class RoutesTest extends UnitTestCase {
yield [
$index->reveal(),
FALSE,
['node'],
$this->getMockedResourceTypes('node', ['articles']),
];
$index = $this->getStubMockedIndex('test_index', FALSE);
$mocked_datasource = $this->prophesize(DatasourceInterface::class);
$mocked_datasource->getEntityTypeId()->willReturn('node');
$mocked_datasource->getBundles()->willReturn(['articles' => 'Articles']);
$index->getDatasources()->willReturn([$mocked_datasource->reveal()]);
yield [
$index->reveal(),
TRUE,
['node'],
$this->getMockedResourceTypes('node', ['articles']),
];
......@@ -107,6 +129,7 @@ final class RoutesTest extends UnitTestCase {
yield [
$index->reveal(),
FALSE,
['node'],
$this->getMockedResourceTypes('node', ['articles', 'pages']),
];
......@@ -115,18 +138,21 @@ final class RoutesTest extends UnitTestCase {
/**
* Get an initial stubbed mocked index.
*
* @param string|null $index_id
* @param string $index_id
* The index ID.
* @param bool $status
* The index status.
*
* @return \Prophecy\Prophecy\ObjectProphecy
* The stub mocked index.
*/
private function getStubMockedIndex(?string $index_id = 'test_index'): ProphecyInterface {
private function getStubMockedIndex(string $index_id = 'test_index', bool $status = TRUE): ProphecyInterface {
$index = $this->prophesize(IndexInterface::class);
$index->id()->willReturn($index_id);
$index->uuid()->willReturn((new Php())->generate());
$index->getEntityTypeId()->willReturn('search_api_index');
$index->bundle()->willReturn('search_api_index');
$index->status()->willReturn($status);
return $index;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment