diff -u b/src/Context/FieldResolver.php b/src/Context/FieldResolver.php --- b/src/Context/FieldResolver.php +++ b/src/Context/FieldResolver.php @@ -90,11 +90,6 @@ if (empty($external_field_name)) { throw new BadRequestHttpException('No field name was provided for the filter.'); } - // Right now we are exposing all the fields with the name they have in - // the Drupal backend. But this may change in the future. - if (strpos($external_field_name, '.') === FALSE) { - return $resource_type->getInternalName($external_field_name); - } // Turns 'uid.categories.name' into // 'uid.entity.field_category.entity.name'. This may be too simple, but it // works for the time being. reverted: --- b/tests/src/Functional/comment/JsonApiFunctionalCommentTest.php +++ /dev/null @@ -1,30 +0,0 @@ - ['target_id' => $this->user->id()]]; - $term = $this->createTerm($this->vocabulary, $values); - $comment = $this->createComment($term, 'field_my_comment', 'This is the comment_term body'); - $output = Json::decode($this->drupalGet('/jsonapi/comment/comment_term/' . $comment->uuid(), ['query' => ['include' => 'entity_id,entity_id.field_my_user']])); - $this->assertEquals($term->uuid(), $output['included'][0]['attributes']['uuid']); - $this->assertEquals($this->user->uuid(), $output['included'][1]['attributes']['uuid']); - } - -} reverted: --- b/tests/src/Functional/comment/JsonApiFunctionalCommentTestBase.php +++ /dev/null @@ -1,178 +0,0 @@ -httpClient = $this->container->get('http_client_factory') - ->fromOptions(['base_uri' => $this->baseUrl]); - - $this->vocabulary = $this->createVocabulary(); - - $this->createEntityReferenceField( - 'taxonomy_term', - $this->vocabulary->id(), - 'field_my_user', - 'field_my_user', - 'user', - 'default', - [ - 'auto_create' => TRUE, - ], - FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED - ); - $commentTerm = CommentType::create([ - 'id' => 'comment_term', - 'label' => 'comment term', - 'description' => 'This is the comment bundle for taxonomy term.', - 'target_entity_type_id' => 'taxonomy_term', - ]); - $commentTerm->save(); - - // Create a comment field on tags. - $fieldStorageTagsComment = FieldStorageConfig::create([ - 'entity_type' => 'taxonomy_term', - 'field_name' => 'field_my_comment', - 'type' => 'comment', - 'settings' => [ - 'comment_type' => 'comment_term', - ], - ]); - $fieldStorageTagsComment->save(); - FieldConfig::create([ - 'field_storage' => $fieldStorageTagsComment, - 'label' => $this->randomMachineName(), - 'entity_type' => 'taxonomy_term', - 'bundle' => $this->vocabulary->id(), - ])->save(); - - // Create a user. - $this->user = $this->drupalCreateUser([]); - - $this->grantPermissions(Role::load(RoleInterface::ANONYMOUS_ID), [ - 'access user profiles', - 'administer taxonomy', - 'administer comments', - ]); - - drupal_flush_all_caches(); - } - - /** - * Performs a HTTP request. Wraps the Guzzle HTTP client. - * - * Why wrap the Guzzle HTTP client? Because any error response is returned via - * an exception, which would make the tests unnecessarily complex to read. - * - * @param string $method - * HTTP method. - * @param \Drupal\Core\Url $url - * URL to request. - * @param array $request_options - * Request options to apply. - * - * @see \GuzzleHttp\ClientInterface::request() - * - * @return \Psr\Http\Message\ResponseInterface - * Response - */ - protected function request($method, Url $url, array $request_options) { - try { - $response = $this->httpClient->request($method, $url->toString(), $request_options); - } - catch (ClientException $e) { - $response = $e->getResponse(); - } - catch (ServerException $e) { - $response = $e->getResponse(); - } - - return $response; - } - - /** - * Create a comment. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity where the comment create on. - * @param string $fieldName - * The comment field name. - * @param string $body - * The comment body. - * - * @return \Drupal\comment\Entity\Comment - * Comment entity. - */ - public function createComment(EntityInterface $entity, $fieldName, $body) { - $values = [ - 'entity_type' => 'taxonomy_term', - 'entity_id' => $entity->id(), - 'field_name' => $fieldName, - 'uid' => 0, - 'comment_type' => 'comment_term', - 'subject' => 'My Awesome Comment', - 'comment_body' => $body, - 'status' => 1, - ]; - $comment = Comment::create($values); - $comment->save(); - return $comment; - } - -} diff -u b/tests/src/Kernel/Context/FieldResolverTest.php b/tests/src/Kernel/Context/FieldResolverTest.php --- b/tests/src/Kernel/Context/FieldResolverTest.php +++ b/tests/src/Kernel/Context/FieldResolverTest.php @@ -29,118 +29,26 @@ $this->sut = \Drupal::service('jsonapi.field_resolver'); - EntityTestBundle::create([ - 'id' => 'bundle1', - ])->save(); - EntityTestBundle::create([ - 'id' => 'bundle2', - ])->save(); - EntityTestBundle::create([ - 'id' => 'bundle3', - ])->save(); - - FieldStorageConfig::create([ - 'field_name' => 'field_test1', - 'type' => 'string', - 'entity_type' => 'entity_test_with_bundle', - ])->save(); - FieldConfig::create([ - 'field_name' => 'field_test1', - 'entity_type' => 'entity_test_with_bundle', - 'bundle' => 'bundle1', - ])->save(); - - FieldStorageConfig::create([ - 'field_name' => 'field_test2', - 'type' => 'string', - 'entity_type' => 'entity_test_with_bundle', - ])->save(); - FieldConfig::create([ - 'field_name' => 'field_test2', - 'entity_type' => 'entity_test_with_bundle', - 'bundle' => 'bundle1', - ])->save(); - - FieldStorageConfig::create([ - 'field_name' => 'field_test3', - 'type' => 'string', - 'entity_type' => 'entity_test_with_bundle', - ])->save(); - FieldConfig::create([ - 'field_name' => 'field_test3', - 'entity_type' => 'entity_test_with_bundle', - 'bundle' => 'bundle2', - ])->save(); - FieldConfig::create([ - 'field_name' => 'field_test3', - 'entity_type' => 'entity_test_with_bundle', - 'bundle' => 'bundle3', - ])->save(); + $this->makeBundle('bundle1'); + $this->makeBundle('bundle2'); + $this->makeBundle('bundle3'); + + $this->makeField('string', 'field_test1', 'entity_test_with_bundle', ['bundle1']); + $this->makeField('string', 'field_test2', 'entity_test_with_bundle', ['bundle1']); + $this->makeField('string', 'field_test3', 'entity_test_with_bundle', ['bundle2', 'bundle3']); // Provides entity reference fields. - FieldStorageConfig::create([ - 'field_name' => 'field_test_ref1', - 'type' => 'entity_reference', - 'settings' => [ - 'target_type' => 'entity_test_with_bundle', - ], - 'entity_type' => 'entity_test_with_bundle', - ])->save(); - FieldConfig::create([ - 'field_name' => 'field_test_ref1', - 'entity_type' => 'entity_test_with_bundle', - 'bundle' => 'bundle1', - ])->save(); - - FieldStorageConfig::create([ - 'field_name' => 'field_test_ref2', - 'type' => 'entity_reference', - 'settings' => [ - 'target_type' => 'entity_test_with_bundle', + $settings = ['target_type' => 'entity_test_with_bundle']; + $this->makeField('entity_reference', 'field_test_ref1', 'entity_test_with_bundle', ['bundle1'], $settings, [ + 'handler_settings' => [ + 'target_bundles' => ['bundle2', 'bundle3'], ], - 'entity_type' => 'entity_test_with_bundle', - ])->save(); - FieldConfig::create([ - 'field_name' => 'field_test_ref2', - 'entity_type' => 'entity_test_with_bundle', - 'bundle' => 'bundle1', - ])->save(); - - FieldStorageConfig::create([ - 'field_name' => 'field_test_ref3', - 'type' => 'entity_reference', - 'settings' => [ - 'target_type' => 'entity_test_with_bundle', - ], - 'entity_type' => 'entity_test_with_bundle', - ])->save(); - FieldConfig::create([ - 'field_name' => 'field_test_ref3', - 'entity_type' => 'entity_test_with_bundle', - 'bundle' => 'bundle2', - ])->save(); - FieldConfig::create([ - 'field_name' => 'field_test_ref3', - 'entity_type' => 'entity_test_with_bundle', - 'bundle' => 'bundle3', - ])->save(); + ]); + $this->makeField('entity_reference', 'field_test_ref2', 'entity_test_with_bundle', ['bundle1'], $settings); + $this->makeField('entity_reference', 'field_test_ref3', 'entity_test_with_bundle', ['bundle2', 'bundle3'], $settings); // Add a field with multiple properties. - FieldStorageConfig::create([ - 'field_name' => 'field_test_text', - 'type' => 'text', - 'entity_type' => 'entity_test_with_bundle', - ])->save(); - FieldConfig::create([ - 'field_name' => 'field_test_text', - 'entity_type' => 'entity_test_with_bundle', - 'bundle' => 'bundle1', - ])->save(); - FieldConfig::create([ - 'field_name' => 'field_test_text', - 'entity_type' => 'entity_test_with_bundle', - 'bundle' => 'bundle2', - ])->save(); + $this->makeField('text', 'field_test_text', 'entity_test_with_bundle', ['bundle1', 'bundle2']); } /** @@ -150,39 +58,79 @@ $entity_type_id = 'entity_test_with_bundle'; $bundle = 'bundle1'; - $this->assertEquals('field_test1', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test1')); - $this->assertEquals('field_test2', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test2')); - - $this->assertEquals('field_test_ref1.entity.field_test1', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test_ref1.field_test1')); - $this->assertEquals('field_test_ref1.entity.field_test2', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test_ref1.field_test2')); - $this->assertEquals('field_test_ref2.entity.field_test1', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test_ref2.field_test1')); - $this->assertEquals('field_test_ref2.entity.field_test2', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test_ref2.field_test2')); - - $this->assertEquals('field_test_ref1.entity.field_test_text', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test_ref1.field_test_text')); - $this->assertEquals('field_test_ref1.entity.field_test_text.value', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test_ref1.field_test_text.value')); - $this->assertEquals('field_test_ref1.entity.field_test_text.format', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test_ref1.field_test_text.format')); - $this->assertEquals('field_test_ref2.entity.field_test_text', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test_ref2.field_test_text')); - $this->assertEquals('field_test_ref2.entity.field_test_text.value', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test_ref2.field_test_text.value')); - $this->assertEquals('field_test_ref2.entity.field_test_text.format', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test_ref2.field_test_text.format')); - - /*$this->assertEquals('field_test3', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test3')); - $this->assertEquals('field_test_ref3.entity.field_test1', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test_ref3.field_test1')); - $this->assertEquals('field_test_ref3.entity.field_test2', $this->sut->resolveInternal($entity_type_id, $bundle, 'field_test_ref3.field_test2')); - */ + $cases = [ + ['field_test1', 'field_test1'], + ['field_test2', 'field_test2'], + + ['field_test_ref2.entity.field_test1', 'field_test_ref2.field_test1'], + ['field_test_ref2.entity.field_test2', 'field_test_ref2.field_test2'], + + ['field_test_ref1.entity.field_test_text', 'field_test_ref1.field_test_text'], + ['field_test_ref1.entity.field_test_text.value', 'field_test_ref1.field_test_text.value'], + ['field_test_ref1.entity.field_test_text.format', 'field_test_ref1.field_test_text.format'], + ['field_test_ref2.entity.field_test_text', 'field_test_ref2.field_test_text'], + ['field_test_ref2.entity.field_test_text.value', 'field_test_ref2.field_test_text.value'], + ['field_test_ref2.entity.field_test_text.format', 'field_test_ref2.field_test_text.format'], + ]; + + foreach ($cases as list($expect, $external_path)) { + $this->assertEquals($expect, $this->sut->resolveInternal($entity_type_id, $bundle, $external_path)); + } } /** * Expects an error when an invalid field is provided. * * @covers ::resolveInternal + * @dataProvider resolveInternalErrorProvider * * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException */ - public function testResolveInternalError() { - $original = 'host.fail!!.deep'; - $not_expected = 'host.entity.fail!!.entity.deep'; + public function testResolveInternalError($entity_type, $bundle, $external_path) { + $this->sut->resolveInternal($entity_type, $bundle, $external_path); + } + + public function resolveInternalErrorProvider() { + return [ + // Should fail because none of these bundles have these fields. + ['entity_test_with_bundle', 'bundle1', 'host.fail!!.deep'], + ['entity_test_with_bundle', 'bundle2', 'field_test_ref2'], + ['entity_test_with_bundle', 'bundle1', 'field_test_ref3'], + // Should fail because the nested field don't exist on the targeted resources. + ['entity_test_with_bundle', 'bundle1', 'field_test_ref1.field_test1'], + ['entity_test_with_bundle', 'bundle1', 'field_test_ref1.field_test2'], + ['entity_test_with_bundle', 'bundle1', 'field_test_ref1.field_test_ref1'], + ['entity_test_with_bundle', 'bundle1', 'field_test_ref1.field_test_ref2'], + ]; + } + + /** + * Create a simple bundle. + */ + protected function makeBundle($name) { + EntityTestBundle::create([ + 'id' => $name, + ])->save(); + } - $this->assertEquals($not_expected, $this->sut->resolveInternal('entity_test_with_bundle', 'bundle1', $original)); + protected function makeField($type, $name, $entity_type, $bundles, $storage_settings = [], $config_settings = []) { + $storage_config = [ + 'field_name' => $name, + 'type' => $type, + 'entity_type' => $entity_type, + 'settings' => $storage_settings, + ]; + + FieldStorageConfig::create($storage_config)->save(); + + foreach ($bundles as $bundle) { + FieldConfig::create([ + 'field_name' => $name, + 'entity_type' => $entity_type, + 'bundle' => $bundle, + 'settings' => $config_settings, + ])->save(); + } } }