src/Annotation/JsonApiResource.php | 7 ---- src/Configuration/ResourceConfig.php | 28 -------------- src/Configuration/ResourceConfigInterface.php | 18 --------- src/Configuration/ResourceManager.php | 13 +------ src/Configuration/ResourceManagerInterface.php | 5 +-- src/Normalizer/RelationshipItemNormalizer.php | 2 +- src/Normalizer/RelationshipNormalizer.php | 5 +-- src/Plugin/JsonApiResourceManager.php | 1 - src/RelationshipItem.php | 7 ---- src/RelationshipItemInterface.php | 8 ---- src/Routing/Routes.php | 3 -- tests/modules/jsonapi_test/jsonapi_test.info.yml | 7 ---- tests/modules/jsonapi_test/jsonapi_test.module | 16 -------- tests/src/Functional/JsonApiFunctionalTest.php | 4 -- .../Kernel/Configuration/ResourceManagerTest.php | 15 ++------ .../src/Unit/Configuration/ResourceConfigTest.php | 2 - .../Unit/Plugin/Deriver/ResourceDeriverTest.php | 2 - tests/src/Unit/Routing/RoutesTest.php | 44 ++++++++++++++++++---- 18 files changed, 45 insertions(+), 142 deletions(-) diff --git a/src/Annotation/JsonApiResource.php b/src/Annotation/JsonApiResource.php index 277de71..3c69525 100644 --- a/src/Annotation/JsonApiResource.php +++ b/src/Annotation/JsonApiResource.php @@ -51,11 +51,4 @@ class JsonApiResource extends Plugin { */ public $data; - /** - * TRUE if the plugin is enabled. - * - * @var bool - */ - public $enabled; - } diff --git a/src/Configuration/ResourceConfig.php b/src/Configuration/ResourceConfig.php index 5a5fc62..46a3515 100644 --- a/src/Configuration/ResourceConfig.php +++ b/src/Configuration/ResourceConfig.php @@ -66,13 +66,6 @@ class ResourceConfig implements ResourceConfigInterface { protected $deserializationTargetClass; /** - * Is enabled? - * - * @var bool - */ - protected $isEnabled = TRUE; - - /** * {@inheritdoc} */ public function getGlobalConfig() { @@ -153,27 +146,6 @@ class ResourceConfig implements ResourceConfigInterface { } /** - * {@inheritdoc} - */ - public function isEnabled() { - return $this->isEnabled; - } - - /** - * {@inheritdoc} - */ - public function enable() { - $this->isEnabled = TRUE; - } - - /** - * {@inheritdoc} - */ - public function disable() { - $this->isEnabled = FALSE; - } - - /** * Instantiates a ResourceConfig object. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory diff --git a/src/Configuration/ResourceConfigInterface.php b/src/Configuration/ResourceConfigInterface.php index 401374c..eee8147 100644 --- a/src/Configuration/ResourceConfigInterface.php +++ b/src/Configuration/ResourceConfigInterface.php @@ -98,22 +98,4 @@ interface ResourceConfigInterface { */ public function getDeserializationTargetClass(); - /** - * Is the resource enabled? - * - * @return bool - * TRUE if the resource is enabled. FALSE otherwise. - */ - public function isEnabled(); - - /** - * Enable the resource. - */ - public function enable(); - - /** - * Disable the resource. - */ - public function disable(); - } diff --git a/src/Configuration/ResourceManager.php b/src/Configuration/ResourceManager.php index 106efc1..4ac7a3e 100644 --- a/src/Configuration/ResourceManager.php +++ b/src/Configuration/ResourceManager.php @@ -72,7 +72,7 @@ class ResourceManager implements ResourceManagerInterface { /** * {@inheritdoc} */ - public function all($include_disabled = FALSE) { + public function all() { if (!$this->all) { $entity_type_ids = array_keys($this->entityTypeManager->getDefinitions()); foreach ($entity_type_ids as $entity_type_id) { @@ -86,17 +86,8 @@ class ResourceManager implements ResourceManagerInterface { return $resource_config; }, array_keys($this->bundleManager->getBundleInfo($entity_type_id)))); } - // Allow altering the resource configuration. This is used, among other, to - // disable resources. - $this->moduleHandler->alter('jsonapi_resources', $this->all); } - // Filter out the disabled resources if necessary. - return $include_disabled ? - $this->all : - array_filter($this->all, function ($resource) { - /* @var \Drupal\jsonapi\Configuration\ResourceConfigInterface $resource */ - return $resource->isEnabled(); - }); + return $this->all; } /** diff --git a/src/Configuration/ResourceManagerInterface.php b/src/Configuration/ResourceManagerInterface.php index 74d1737..60dad9a 100644 --- a/src/Configuration/ResourceManagerInterface.php +++ b/src/Configuration/ResourceManagerInterface.php @@ -13,13 +13,10 @@ interface ResourceManagerInterface { /** * Get all the resource configuration objects. * - * @param bool $include_disabled - * TRUE to return disabled resources as well. - * * @return ResourceConfigInterface[] * The list of resource configs representing JSON API types. */ - public function all($include_disabled = FALSE); + public function all(); /** * Finds a resource config. diff --git a/src/Normalizer/RelationshipItemNormalizer.php b/src/Normalizer/RelationshipItemNormalizer.php index 4a14a69..72264f5 100644 --- a/src/Normalizer/RelationshipItemNormalizer.php +++ b/src/Normalizer/RelationshipItemNormalizer.php @@ -73,7 +73,7 @@ class RelationshipItemNormalizer extends FieldItemNormalizer implements UuidRefe ); $host_field_name = $relationship_item->getParent()->getPropertyName(); - // TODO Only include if the target entity type has the resource enabled. + // TODO Only include if the target entity is accessible. if (!empty($context['include']) && in_array($host_field_name, $context['include'])) { $context = $this->buildSubContext($context, $target_entity, $host_field_name); $included_normalizer_value = $this->jsonapiDocumentToplevelNormalizer->buildNormalizerValue($target_entity, $format, $context); diff --git a/src/Normalizer/RelationshipNormalizer.php b/src/Normalizer/RelationshipNormalizer.php index 6dc4220..32b327e 100644 --- a/src/Normalizer/RelationshipNormalizer.php +++ b/src/Normalizer/RelationshipNormalizer.php @@ -75,10 +75,7 @@ class RelationshipNormalizer extends NormalizerBase { /* @var \Drupal\jsonapi\RelationshipInterface $relationship */ $normalizer_items = array(); foreach ($relationship->getItems() as $relationship_item) { - /* @var \Drupal\jsonapi\RelationshipItemInterface $relationship_item */ - if (!$relationship_item->resourceIsEnabled()) { - continue; - } + // @todo Ignore inaccessible relationship items? $normalizer_items[] = $this->serializer->normalize($relationship_item, $format, $context); } $cardinality = $relationship->getCardinality(); diff --git a/src/Plugin/JsonApiResourceManager.php b/src/Plugin/JsonApiResourceManager.php index a6c982e..91af620 100644 --- a/src/Plugin/JsonApiResourceManager.php +++ b/src/Plugin/JsonApiResourceManager.php @@ -20,7 +20,6 @@ class JsonApiResourceManager extends DefaultPluginManager { protected $defaults = [ 'permission' => 'access content', 'controller' => Routes::FRONT_CONTROLLER, - 'enabled' => TRUE, ]; /** diff --git a/src/RelationshipItem.php b/src/RelationshipItem.php index 4f179cd..898fb39 100644 --- a/src/RelationshipItem.php +++ b/src/RelationshipItem.php @@ -85,11 +85,4 @@ class RelationshipItem implements RelationshipItemInterface { return $this->parent; } - /** - * {@inheritdoc} - */ - public function resourceIsEnabled() { - return $this->getTargetResourceConfig()->isEnabled(); - } - } diff --git a/src/RelationshipItemInterface.php b/src/RelationshipItemInterface.php index 2f87206..c996e01 100644 --- a/src/RelationshipItemInterface.php +++ b/src/RelationshipItemInterface.php @@ -34,12 +34,4 @@ interface RelationshipItemInterface { */ public function getParent(); - /** - * Is the target resource enabled? - * - * @return bool - * TRUE if the resource is enabled. FALSE otherwise. - */ - public function resourceIsEnabled(); - } diff --git a/src/Routing/Routes.php b/src/Routing/Routes.php index 3f16231..8a3876c 100644 --- a/src/Routing/Routes.php +++ b/src/Routing/Routes.php @@ -78,9 +78,6 @@ class Routes implements ContainerInjectionInterface { public function routes() { $collection = new RouteCollection(); foreach ($this->resourcePluginManager->getDefinitions() as $plugin_id => $plugin_definition) { - if (empty($plugin_definition['enabled'])) { - continue; - } $entity_type = $plugin_definition['entityType']; // For the entity type resources the bundle is NULL. $bundle = $plugin_definition['bundle']; diff --git a/tests/modules/jsonapi_test/jsonapi_test.info.yml b/tests/modules/jsonapi_test/jsonapi_test.info.yml deleted file mode 100644 index 302490c..0000000 --- a/tests/modules/jsonapi_test/jsonapi_test.info.yml +++ /dev/null @@ -1,7 +0,0 @@ -name: JSON API Test -type: module -core: 8.x -package: Web services -hidden: true -dependencies: - - jsonapi diff --git a/tests/modules/jsonapi_test/jsonapi_test.module b/tests/modules/jsonapi_test/jsonapi_test.module deleted file mode 100644 index f8cda7f..0000000 --- a/tests/modules/jsonapi_test/jsonapi_test.module +++ /dev/null @@ -1,16 +0,0 @@ -getEntityTypeId() == 'menu' && $resource->getBundleId() == 'menu') { - $resource->disable(); - } - } -} diff --git a/tests/src/Functional/JsonApiFunctionalTest.php b/tests/src/Functional/JsonApiFunctionalTest.php index f29743e..44c6214 100644 --- a/tests/src/Functional/JsonApiFunctionalTest.php +++ b/tests/src/Functional/JsonApiFunctionalTest.php @@ -30,7 +30,6 @@ class JsonApiFunctionalTest extends BrowserTestBase { public static $modules = [ 'basic_auth', 'jsonapi', - 'jsonapi_test', 'serialization', 'node', 'image', @@ -355,9 +354,6 @@ class JsonApiFunctionalTest extends BrowserTestBase { ])); $this->assertSession()->statusCodeEquals(200); $this->assertGreaterThanOrEqual(2, count($single_output['included'])); - // 17. Test filtering on the same field. - Json::decode($this->drupalGet('/jsonapi/menu/menu')); - $this->assertSession()->statusCodeEquals(404); // 17. Single user (check fields lacking 'view' access). $user_url = Url::fromRoute('jsonapi.user--user.individual', [ 'user' => $this->user->uuid(), diff --git a/tests/src/Kernel/Configuration/ResourceManagerTest.php b/tests/src/Kernel/Configuration/ResourceManagerTest.php index 0d3e1ee..b92e8aa 100644 --- a/tests/src/Kernel/Configuration/ResourceManagerTest.php +++ b/tests/src/Kernel/Configuration/ResourceManagerTest.php @@ -24,7 +24,6 @@ class ResourceManagerTest extends KernelTestBase { public static $modules = [ 'node', 'jsonapi', - 'jsonapi_test', 'serialization', 'system', 'user', @@ -77,18 +76,13 @@ class ResourceManagerTest extends KernelTestBase { } $this->assertNotEmpty($path); }); - // Make sure that the menu--menu resource is not present. - $disabled_resource = array_filter($all, function ($resource) { - return $resource->getEntityTypeId() == 'menu' && $resource->getBundleId() == 'menu'; - }); - $this->assertEmpty($disabled_resource); } /** * @covers ::get * @dataProvider getProvider */ - public function testGet($entity_type_id, $bundle_id, $entity_class, $enabled) { + public function testGet($entity_type_id, $bundle_id, $entity_class) { // Make sure that there are resources being created. $resource_config = $this->resourceManager->get($entity_type_id, $bundle_id); $this->assertInstanceOf(ResourceConfigInterface::class, $resource_config); @@ -98,7 +92,6 @@ class ResourceManagerTest extends KernelTestBase { $this->assertNotEmpty($resource_config->getGlobalConfig()); $this->assertSame('/' . $entity_type_id . '/' . $bundle_id, $resource_config->getPath()); $this->assertSame($entity_type_id . '--' . $bundle_id, $resource_config->getTypeName()); - $this->assertSame($enabled, $resource_config->isEnabled()); } /** @@ -109,9 +102,9 @@ class ResourceManagerTest extends KernelTestBase { */ public function getProvider() { return [ - ['node', 'article', 'Drupal\node\Entity\Node', TRUE], - ['node_type', 'node_type', 'Drupal\node\Entity\NodeType', TRUE], - ['menu', 'menu', 'Drupal\system\Entity\Menu', FALSE], + ['node', 'article', 'Drupal\node\Entity\Node'], + ['node_type', 'node_type', 'Drupal\node\Entity\NodeType'], + ['menu', 'menu', 'Drupal\system\Entity\Menu'], ]; } diff --git a/tests/src/Unit/Configuration/ResourceConfigTest.php b/tests/src/Unit/Configuration/ResourceConfigTest.php index dfccec8..8f16e86 100644 --- a/tests/src/Unit/Configuration/ResourceConfigTest.php +++ b/tests/src/Unit/Configuration/ResourceConfigTest.php @@ -50,8 +50,6 @@ class ResourceConfigTest extends UnitTestCase { ['setTypeName', 'getTypeName', $this->getRandomGenerator()->name()], ['setPath', 'getPath', $this->getRandomGenerator()->name()], ['setBundleId', 'getBundleId', $this->getRandomGenerator()->name()], - ['disable', 'isEnabled', FALSE], - ['enable', 'isEnabled', TRUE], ]; } diff --git a/tests/src/Unit/Plugin/Deriver/ResourceDeriverTest.php b/tests/src/Unit/Plugin/Deriver/ResourceDeriverTest.php index 8442698..da08dcf 100644 --- a/tests/src/Unit/Plugin/Deriver/ResourceDeriverTest.php +++ b/tests/src/Unit/Plugin/Deriver/ResourceDeriverTest.php @@ -70,12 +70,10 @@ class ResourceDeriverTest extends UnitTestCase { ], 'permission' => 'access content', 'controller' => '\\Drupal\\jsonapi\\RequestHandler::handle', - 'enabled' => TRUE, ]]; $actual = $this->deriver->getDerivativeDefinitions([ 'permission' => 'access content', 'controller' => '\Drupal\jsonapi\RequestHandler::handle', - 'enabled' => TRUE, ]); $this->assertArrayEquals($expected, $actual); } diff --git a/tests/src/Unit/Routing/RoutesTest.php b/tests/src/Unit/Routing/RoutesTest.php index 203b88c..3d55b0a 100644 --- a/tests/src/Unit/Routing/RoutesTest.php +++ b/tests/src/Unit/Routing/RoutesTest.php @@ -46,7 +46,6 @@ class RoutesTest extends UnitTestCase { ], 'controller' => 'MyCustomController', 'permission' => 'access content', - 'enabled' => TRUE, ], 'bundle:jsonapi.resource_type_2' => [ 'id' => 'bundle:jsonapi.resource_type_2', @@ -59,7 +58,6 @@ class RoutesTest extends UnitTestCase { ], 'controller' => 'MyCustomController', 'permission' => 'access content', - 'enabled' => FALSE, ], ]); $container = $this->prophesize(ContainerInterface::class); @@ -82,8 +80,8 @@ class RoutesTest extends UnitTestCase { // Get the route collection and start making assertions. $routes = $this->routes['ok']->routes(); - // Make sure that there are 4 routes for each resource. - $this->assertEquals(4, $routes->count()); + // Make sure that there are 4 routes for each resource, so 8 total. + $this->assertEquals(8, $routes->count()); $iterator = $routes->getIterator(); // Check the collection route. @@ -96,7 +94,14 @@ class RoutesTest extends UnitTestCase { $this->assertEquals(['GET', 'POST'], $route->getMethods()); $this->assertSame('MyCustomController', $route->getDefault(RouteObjectInterface::CONTROLLER_NAME)); $this->assertSame('Drupal\jsonapi\Resource\JsonApiDocumentTopLevel', $route->getOption('serialization_class')); - $this->assertFalse($iterator->offsetExists('jsonapi.resource_type_2.collection')); + $route = $iterator->offsetGet('jsonapi.resource_type_2.collection'); + $this->assertSame('/jsonapi/entity_type_2/bundle_path_2', $route->getPath()); + $this->assertSame('entity_type_2', $route->getRequirement('_entity_type')); + $this->assertSame('bundle_2_2', $route->getRequirement('_bundle')); + $this->assertSame(['lorem', 'ipsum'], $route->getOption('_auth')); + $this->assertEquals(['GET', 'POST'], $route->getMethods()); + $this->assertSame('MyCustomController', $route->getDefault(RouteObjectInterface::CONTROLLER_NAME)); + $this->assertSame('Drupal\jsonapi\Resource\JsonApiDocumentTopLevel', $route->getOption('serialization_class')); } /** @@ -117,7 +122,15 @@ class RoutesTest extends UnitTestCase { $this->assertSame('Drupal\jsonapi\Resource\JsonApiDocumentTopLevel', $route->getOption('serialization_class')); $this->assertSame(['lorem', 'ipsum'], $route->getOption('_auth')); $this->assertEquals(['entity_type_1' => ['type' => 'entity:entity_type_1']], $route->getOption('parameters')); - $this->assertFalse($iterator->offsetExists('jsonapi.resource_type_2.individual')); + $route = $iterator->offsetGet('jsonapi.resource_type_2.individual'); + $this->assertSame('/jsonapi/entity_type_2/bundle_path_2/{entity_type_2}', $route->getPath()); + $this->assertSame('entity_type_2', $route->getRequirement('_entity_type')); + $this->assertSame('bundle_2_2', $route->getRequirement('_bundle')); + $this->assertEquals(['GET', 'PATCH', 'DELETE'], $route->getMethods()); + $this->assertSame('MyCustomController', $route->getDefault(RouteObjectInterface::CONTROLLER_NAME)); + $this->assertSame('Drupal\jsonapi\Resource\JsonApiDocumentTopLevel', $route->getOption('serialization_class')); + $this->assertSame(['lorem', 'ipsum'], $route->getOption('_auth')); + $this->assertEquals(['entity_type_2' => ['type' => 'entity:entity_type_2']], $route->getOption('parameters')); } /** @@ -137,7 +150,14 @@ class RoutesTest extends UnitTestCase { $this->assertSame('MyCustomController', $route->getDefault(RouteObjectInterface::CONTROLLER_NAME)); $this->assertSame(['lorem', 'ipsum'], $route->getOption('_auth')); $this->assertEquals(['entity_type_1' => ['type' => 'entity:entity_type_1']], $route->getOption('parameters')); - $this->assertFalse($iterator->offsetExists('jsonapi.resource_type_2.related')); + $route = $iterator->offsetGet('jsonapi.resource_type_2.related'); + $this->assertSame('/jsonapi/entity_type_2/bundle_path_2/{entity_type_2}/{related}', $route->getPath()); + $this->assertSame('entity_type_2', $route->getRequirement('_entity_type')); + $this->assertSame('bundle_2_2', $route->getRequirement('_bundle')); + $this->assertEquals(['GET'], $route->getMethods()); + $this->assertSame('MyCustomController', $route->getDefault(RouteObjectInterface::CONTROLLER_NAME)); + $this->assertSame(['lorem', 'ipsum'], $route->getOption('_auth')); + $this->assertEquals(['entity_type_2' => ['type' => 'entity:entity_type_2']], $route->getOption('parameters')); } /** @@ -158,7 +178,15 @@ class RoutesTest extends UnitTestCase { $this->assertSame(['lorem', 'ipsum'], $route->getOption('_auth')); $this->assertEquals(['entity_type_1' => ['type' => 'entity:entity_type_1']], $route->getOption('parameters')); $this->assertSame('Drupal\Core\Field\EntityReferenceFieldItemList', $route->getOption('serialization_class')); - $this->assertFalse($iterator->offsetExists('jsonapi.resource_type_2.relationship')); + $route = $iterator->offsetGet('jsonapi.resource_type_2.relationship'); + $this->assertSame('/jsonapi/entity_type_2/bundle_path_2/{entity_type_2}/relationships/{related}', $route->getPath()); + $this->assertSame('entity_type_2', $route->getRequirement('_entity_type')); + $this->assertSame('bundle_2_2', $route->getRequirement('_bundle')); + $this->assertEquals(['GET', 'POST', 'PATCH', 'DELETE'], $route->getMethods()); + $this->assertSame('MyCustomController', $route->getDefault(RouteObjectInterface::CONTROLLER_NAME)); + $this->assertSame(['lorem', 'ipsum'], $route->getOption('_auth')); + $this->assertEquals(['entity_type_2' => ['type' => 'entity:entity_type_2']], $route->getOption('parameters')); + $this->assertSame('Drupal\Core\Field\EntityReferenceFieldItemList', $route->getOption('serialization_class')); } }