Verified Commit 4c116a3f authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3213752 by Spokje, bradjones1, quietone, _pratik_, ravi.shankar,...

Issue #3213752 by Spokje, bradjones1, quietone, _pratik_, ravi.shankar, rpayanm, bbrala, alexpott, catch: Remove dead code from JsonApiDocumentTopLevelNormalizerTest

(cherry picked from commit 40963387)
parent ee3fedcd
Loading
Loading
Loading
Loading
+13 −96
Original line number Diff line number Diff line
@@ -24,8 +24,6 @@
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

@@ -195,7 +193,7 @@ protected function setUp(): void {
    $this->node = Node::create([
      'title' => 'dummy_title',
      'type' => 'article',
      'uid' => 1,
      'uid' => $this->user,
      'body' => [
        'format' => 'plain_text',
        'value' => $this->randomStringValidate(42),
@@ -259,7 +257,7 @@ public function tearDown(): void {
   * @covers ::normalize
   */
  public function testNormalize() {
    [$request, $resource_type] = $this->generateProphecies('node', 'article');
    $resource_type = $this->container->get('jsonapi.resource_type.repository')->get('node', 'article');

    $resource_object = ResourceObject::createFromEntity($resource_type, $this->node);
    $includes = $this->includeResolver->resolve($resource_object, 'uid,field_tags,field_image');
@@ -356,61 +354,16 @@ public function testNormalize() {
    );
  }

  /**
   * @covers ::normalize
   */
  public function testNormalizeRelated() {
    $this->markTestIncomplete('This fails and should be fixed by https://www.drupal.org/project/drupal/issues/3213752');

    [$request, $resource_type] = $this->generateProphecies('node', 'article');
    $request->query = new ParameterBag([
      'fields' => [
        'user--user' => 'name,roles',
      ],
      'include' => 'roles',
    ]);
    $document_wrapper = $this->prophesize(JsonApiDocumentTopLevel::class);
    $author = $this->node->get('uid')->entity;
    $document_wrapper->getData()->willReturn($author);

    $jsonapi_doc_object = $this
      ->getNormalizer()
      ->normalize(
        $document_wrapper->reveal(),
        'api_json',
        [
          'resource_type' => $resource_type,
          'account' => NULL,
        ]
      );
    $normalized = $jsonapi_doc_object->getNormalization();
    $this->assertSame($normalized['data']['attributes']['name'], 'user1');
    $this->assertEquals($normalized['data']['id'], User::load(1)->uuid());
    $this->assertEquals('user--user', $normalized['data']['type']);
    // Make sure that the cache tags for the includes and the requested entities
    // are bubbling as expected.
    $this->assertSame(['user:1'], $jsonapi_doc_object->getCacheTags());
    $this->assertSame(Cache::PERMANENT, $jsonapi_doc_object->getCacheMaxAge());
  }

  /**
   * @covers ::normalize
   */
  public function testNormalizeUuid() {
    [$request, $resource_type] = $this->generateProphecies('node', 'article');
    $resource_type = $this->container->get('jsonapi.resource_type.repository')->get('node', 'article');
    $resource_object = ResourceObject::createFromEntity($resource_type, $this->node);
    $include_param = 'uid,field_tags';
    $includes = $this->includeResolver->resolve($resource_object, $include_param);
    $document_wrapper = new JsonApiDocumentTopLevel(new ResourceObjectData([$resource_object], 1), $includes, new LinkCollection([]));

    $request->query = new ParameterBag([
      'fields' => [
        'node--article' => 'title,node_type,uid,field_tags',
        'user--user' => 'name',
      ],
      'include' => $include_param,
    ]);

    $jsonapi_doc_object = $this
      ->getNormalizer()
      ->normalize(
@@ -487,11 +440,12 @@ public function testAliasFieldRouteException() {
    $this->assertSame('user_roles', $this->resourceTypeRepository->getByTypeName('user--user')->getPublicName('roles'));

    // Create the request to fetch the articles and fetch included user.
    [$request, $resource_type] = $this->generateProphecies('node', 'article');
    $resource_type = $this->container->get('jsonapi.resource_type.repository')->get('node', 'article');
    $user = User::load($this->node->getOwnerId());

    $resource_object = ResourceObject::createFromEntity($resource_type, $this->node);
    [$request, $user_resource_type] = $this->generateProphecies('user', 'user');
    $user_resource_type = $this->container->get('jsonapi.resource_type.repository')->get('user', 'user');

    $resource_object_user = LabelOnlyResourceObject::createFromEntity($user_resource_type, $user);
    $includes = $this->includeResolver->resolve($resource_object_user, 'user_roles');

@@ -534,7 +488,7 @@ public function testAliasFieldRouteException() {
   * @covers ::normalize
   */
  public function testNormalizeConfig() {
    [$request, $resource_type] = $this->generateProphecies('node_type', 'node_type');
    $resource_type = $this->container->get('jsonapi.resource_type.repository')->get('node_type', 'node_type');
    $resource_object = ResourceObject::createFromEntity($resource_type, $this->nodeType);
    $document_wrapper = new JsonApiDocumentTopLevel(new ResourceObjectData([$resource_object], 1), new NullIncludedData(), new LinkCollection([]));

@@ -567,7 +521,7 @@ public function testNormalizeConfig() {
  public function testDenormalize() {
    $payload = '{"data":{"type":"article","attributes":{"title":"Testing article"}}}';

    [$request, $resource_type] = $this->generateProphecies('node', 'article');
    $resource_type = $this->container->get('jsonapi.resource_type.repository')->get('node', 'article');
    $node = $this
      ->getNormalizer()
      ->denormalize(Json::decode($payload), NULL, 'api_json', [
@@ -636,8 +590,7 @@ public function testDenormalizeUuid() {
      [$payload_data, $expected] = $this->denormalizeUuidProviderBuilder($configuration);
      $payload = Json::encode($payload_data);

      [$request, $resource_type] = $this->generateProphecies('node', 'article');
      $this->container->get('request_stack')->push($request);
      $resource_type = $this->container->get('jsonapi.resource_type.repository')->get('node', 'article');
      try {
        $node = $this
          ->getNormalizer()
@@ -706,8 +659,7 @@ public function testDenormalizeInvalidTypeAndNoType() {

    // Test relationship member with invalid type.
    $payload = Json::encode($payload_data);
    [$request, $resource_type] = $this->generateProphecies('node', 'article');
    $this->container->get('request_stack')->push($request);
    $resource_type = $this->container->get('jsonapi.resource_type.repository')->get('node', 'article');
    try {
      $this
        ->getNormalizer()
@@ -725,8 +677,7 @@ public function testDenormalizeInvalidTypeAndNoType() {
    unset($payload_data['data']['relationships']['field_tags']['data'][0]['type']);

    $payload = Json::encode($payload_data);
    [$request, $resource_type] = $this->generateProphecies('node', 'article');
    $this->container->get('request_stack')->push($request);
    $resource_type = $this->container->get('jsonapi.resource_type.repository')->get('node', 'article');
    try {
      $this->container->get('jsonapi_test_normalizers_kernel.jsonapi_document_toplevel')
        ->denormalize(Json::decode($payload), NULL, 'api_json', [
@@ -800,15 +751,11 @@ protected function denormalizeUuidProviderBuilder(array $options) {
   *
   * @param \Drupal\Core\Cache\CacheableMetadata $expected_metadata
   *   The expected cacheable metadata.
   * @param array|null $fields
   *   Fields to include in the response, keyed by resource type.
   * @param array|null $includes
   *   Resources paths to include in the response.
   *
   * @dataProvider testCacheableMetadataProvider
   */
  public function testCacheableMetadata(CacheableMetadata $expected_metadata, $fields = NULL, $includes = NULL) {
    [$request, $resource_type] = $this->generateProphecies('node', 'article');
  public function testCacheableMetadata(CacheableMetadata $expected_metadata) {
    $resource_type = $this->container->get('jsonapi.resource_type.repository')->get('node', 'article');
    $resource_object = ResourceObject::createFromEntity($resource_type, $this->node);
    $context = [
      'resource_type' => $resource_type,
@@ -840,17 +787,6 @@ public function testCacheableMetadataProvider() {
    ];
  }

  /**
   * Decorates a request with sparse fieldsets and includes.
   */
  protected function decorateRequest(Request $request, array $fields = NULL, array $includes = NULL) {
    $parameters = new ParameterBag();
    $parameters->add($fields ? ['fields' => $fields] : []);
    $parameters->add($includes ? ['include' => $includes] : []);
    $request->query = $parameters;
    return $request;
  }

  /**
   * Helper to load the normalizer.
   */
@@ -862,23 +798,4 @@ protected function getNormalizer() {
    return $normalizer_service;
  }

  /**
   * Generates the prophecies for the mocked entity request.
   *
   * @param string $entity_type_id
   *   The ID of the entity type. Ex: node.
   * @param string $bundle
   *   The bundle. Ex: article.
   *
   * @return array
   *   A numeric array containing the request and the ResourceType.
   *
   * @throws \Exception
   */
  protected function generateProphecies($entity_type_id, $bundle) {
    $resource_type = $this->container->get('jsonapi.resource_type.repository')->get($entity_type_id, $bundle);

    return [new Request(), $resource_type];
  }

}