Unverified Commit 5ff249f5 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2997123 by jibran, Wim Leers, bbrala, gabesullice, quietone,...

Issue #2997123 by jibran, Wim Leers, bbrala, gabesullice, quietone, axle_foley00, KapilV, acbramley, dpi, mglaman, tstoeckler, e0ipso, Sam152: Cacheability of normalized computed fields' properties is not captured during serialization
parent 117168a8
Loading
Loading
Loading
Loading
+187 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\jsonapi\Functional;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTestComputedField;
use Drupal\user\Entity\User;

/**
 * JSON:API integration test for the "EntityTestComputedField" content entity type.
 *
 * @group jsonapi
 */
class EntityTestComputedFieldTest extends ResourceTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['entity_test'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected static $entityTypeId = 'entity_test_computed_field';

  /**
   * {@inheritdoc}
   */
  protected static $resourceTypeName = 'entity_test_computed_field--entity_test_computed_field';

  /**
   * {@inheritdoc}
   */
  protected static $patchProtectedFieldNames = [];

  /**
   * {@inheritdoc}
   *
   * @var \Drupal\entity_test\Entity\EntityTestComputedField
   */
  protected $entity;

  /**
   * {@inheritdoc}
   */
  protected function setUpAuthorization($method) {
    $this->grantPermissionsToTestedRole(['administer entity_test content']);

    switch ($method) {
      case 'GET':
        $this->grantPermissionsToTestedRole(['view test entity']);
        break;

      case 'POST':
        $this->grantPermissionsToTestedRole(['create entity_test entity_test_with_bundle entities']);
        break;

      case 'PATCH':
      case 'DELETE':
        $this->grantPermissionsToTestedRole(['administer entity_test content']);
        break;
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function createEntity() {
    $entity_test = EntityTestComputedField::create([
      'name' => 'Llama',
      'type' => 'entity_test_computed_field',
    ]);

    $entity_test->setOwnerId(0);
    $entity_test->save();

    return $entity_test;
  }

  /**
   * {@inheritdoc}
   */
  protected function getExpectedDocument() {
    $self_url = Url::fromUri('base:/jsonapi/entity_test_computed_field/entity_test_computed_field/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
    $author = User::load(0);
    return [
      'jsonapi' => [
        'meta' => [
          'links' => [
            'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
          ],
        ],
        'version' => '1.0',
      ],
      'links' => [
        'self' => ['href' => $self_url],
      ],
      'data' => [
        'id' => $this->entity->uuid(),
        'type' => 'entity_test_computed_field--entity_test_computed_field',
        'links' => [
          'self' => ['href' => $self_url],
        ],
        'attributes' => [
          'created' => (new \DateTime())->setTimestamp($this->entity->get('created')->value)->setTimezone(new \DateTimeZone('UTC'))->format(\DateTime::RFC3339),
          'name' => 'Llama',
          'drupal_internal__id' => 1,
          'computed_string_field' => NULL,
          'computed_test_cacheable_string_field' => 'computed test cacheable string field',
        ],
        'relationships' => [
          'computed_reference_field' => [
            'data' => NULL,
            'links' => [
              'related' => ['href' => $self_url . '/computed_reference_field'],
              'self' => ['href' => $self_url . '/relationships/computed_reference_field'],
            ],
          ],
          'user_id' => [
            'data' => [
              'id' => $author->uuid(),
              'meta' => [
                'drupal_internal__target_id' => (int) $author->id(),
              ],
              'type' => 'user--user',
            ],
            'links' => [
              'related' => ['href' => $self_url . '/user_id'],
              'self' => ['href' => $self_url . '/relationships/user_id'],
            ],
          ],
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function getPostDocument() {
    return [
      'data' => [
        'type' => 'entity_test_computed_field--entity_test_computed_field',
        'attributes' => [
          'name' => 'Dramallama',
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function getSparseFieldSets() {
    // EntityTest's owner field name is `user_id`, not `uid`, which breaks
    // nested sparse fieldset tests.
    return array_diff_key(parent::getSparseFieldSets(), array_flip([
      'nested_empty_fieldset',
      'nested_fieldset_with_owner_fieldset',
    ]));
  }

  protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) {
    $cache_contexts = parent::getExpectedCacheContexts($sparse_fieldset);
    if ($sparse_fieldset === NULL || in_array('computed_test_cacheable_string_field', $sparse_fieldset)) {
      $cache_contexts = Cache::mergeContexts($cache_contexts, ['url.query_args:computed_test_cacheable_string_field']);
    }

    return $cache_contexts;
  }

  protected function getExpectedCacheTags(array $sparse_fieldset = NULL) {
    $expected_cache_tags = parent::getExpectedCacheTags($sparse_fieldset);
    if ($sparse_fieldset === NULL || in_array('computed_test_cacheable_string_field', $sparse_fieldset)) {
      $expected_cache_tags = Cache::mergeTags($expected_cache_tags, ['field:computed_test_cacheable_string_field']);
    }

    return $expected_cache_tags;
  }

}
+3 −0
Original line number Diff line number Diff line
@@ -21,6 +21,9 @@ class PrimitiveDataNormalizer extends NormalizerBase {
   * {@inheritdoc}
   */
  public function normalize($object, $format = NULL, array $context = []) {
    // Add cacheability if applicable.
    $this->addCacheableDependency($context, $object);

    $parent = $object->getParent();
    if ($parent instanceof FieldItemInterface && $object->getValue()) {
      $serialized_property_names = $this->getCustomSerializedPropertyNames($parent);
+19 −0
Original line number Diff line number Diff line
@@ -2,11 +2,15 @@

namespace Drupal\Tests\serialization\Kernel;

use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\entity_test\Entity\EntityTestComputedField;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Datetime\DateTimePlus;
use Drupal\entity_test\Entity\EntitySerializedField;
use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\filter\Entity\FilterFormat;
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;

/**
 * Tests that entities can be serialized to supported core formats.
@@ -365,4 +369,19 @@ public function testDenormalizeStringValue() {
    ], EntitySerializedField::class);
  }

  /**
   * Tests normalizing cacheable computed field.
   */
  public function testCacheableComputedField() {
    $context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY] = new CacheableMetadata();
    $entity = EntityTestComputedField::create();
    $normalized = $this->serializer->normalize($entity, NULL, $context);
    $this->assertEquals('computed test cacheable string field', $normalized['computed_test_cacheable_string_field'][0]['value']);
    $this->assertInstanceOf(CacheableDependencyInterface::class, $context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
    // See \Drupal\entity_test\Plugin\Field\ComputedTestCacheableStringItemList::computeValue().
    $this->assertEquals($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->getCacheContexts(), ['url.query_args:computed_test_cacheable_string_field']);
    $this->assertEquals($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->getCacheTags(), ['field:computed_test_cacheable_string_field']);
    $this->assertEquals($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->getCacheMaxAge(), 800);
  }

}
+8 −0
Original line number Diff line number Diff line
@@ -102,5 +102,13 @@ entity.entity_test_view_builder.canonical:
  requirements:
    _access: 'TRUE'

entity.entity_test_computed_field.canonical:
  path: '/entity_test_computed_field/{entity_test_computed_field}'
  defaults:
    _entity_view: 'entity_test_computed_field.full'
    _title: 'Test full view mode'
  requirements:
    _entity_access: 'entity_test_computed_field.view'

route_callbacks:
  - '\Drupal\entity_test\Routing\EntityTestRoutes::routes'
+11 −1
Original line number Diff line number Diff line
@@ -4,7 +4,9 @@

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity_test\Plugin\Field\ComputedReferenceTestFieldItemList;
use Drupal\entity_test\Plugin\Field\ComputedTestCacheableStringItemList;
use Drupal\entity_test\Plugin\Field\ComputedTestFieldItemList;

/**
@@ -19,11 +21,12 @@
 *   },
 *   entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid",
 *     "label" = "name",
 *   },
 *   admin_permission = "administer entity_test content",
 *   links = {
 *     "add-form" = "/entity_test_computed_field/add",
 *     "canonical" = "/entity_test_computed_field/{entity_test_computed_field}",
 *   },
 * )
 */
@@ -46,6 +49,13 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
      ->setSetting('target_type', 'entity_test')
      ->setClass(ComputedReferenceTestFieldItemList::class);

    $fields['computed_test_cacheable_string_field'] = BaseFieldDefinition::create('computed_test_cacheable_string_item')
      ->setLabel(new TranslatableMarkup('Computed Cacheable String Field Test'))
      ->setComputed(TRUE)
      ->setClass(ComputedTestCacheableStringItemList::class)
      ->setReadOnly(FALSE)
      ->setInternal(FALSE);

    return $fields;
  }

Loading