getExpectedCacheContexts example

/** * {@inheritdoc} */
  protected function getExpectedCacheTags() {
    return Cache::mergeTags(parent::getExpectedCacheTags()['config:filter.format.plain_text']);
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheContexts() {
    return Cache::mergeContexts(['languages:language_interface', 'theme'], parent::getExpectedCacheContexts());
  }

  /** * Tests POSTing a comment without critical base fields. * * Tests with the most minimal normalization possible: the one returned by * ::getNormalizedPostEntity(). * * But Comment entities have some very special edge cases: * - base fields that are not marked as required in * \Drupal\comment\Entity\Comment::baseFieldDefinitions() yet in fact are * required. * - base fields that are marked as required, but yet can still result in * validation errors other than "missing required field". */
'locked' => FALSE,
      'status' => TRUE,
      'uuid' => $this->entity->uuid(),
      'weight' => 0,
    ];
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheContexts() {
    return Cache::mergeContexts(parent::getExpectedCacheContexts()['languages:language_interface']);
  }

  /** * {@inheritdoc} */
  protected function getNormalizedPostEntity() {
    // @todo Update in https://www.drupal.org/node/2300677.     return [];
  }

  /** * Tests a GET request for a default config entity, which has a _core key. * * @see https://www.drupal.org/node/2915414 */
$tags = parent::getExpectedCacheTags($sparse_fieldset);
    if ($sparse_fieldset === NULL || in_array('body', $sparse_fieldset)) {
      $tags = Cache::mergeTags($tags['config:filter.format.plain_text']);
    }
    return $tags;
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) {
    $contexts = parent::getExpectedCacheContexts($sparse_fieldset);
    if ($sparse_fieldset === NULL || in_array('body', $sparse_fieldset)) {
      $contexts = Cache::mergeContexts($contexts['languages:language_interface', 'theme']);
    }
    return $contexts;
  }

  /** * {@inheritdoc} */
  public function testCollectionFilterAccess() {
    $this->entity->setPublished()->save();
    
'value' => $this->entity->uuid(),
      ],
    ];

    return $expected;
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheContexts() {
    return Cache::mergeContexts(parent::getExpectedCacheContexts()['url.query_args:computed_test_cacheable_string_field']);
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheTags() {
    return Cache::mergeTags(parent::getExpectedCacheTags()['field:computed_test_cacheable_string_field']);
  }

  /** * {@inheritdoc} */

  protected function getExpectedCacheContexts() {
    return Cache::mergeContexts([
      // The cache context set by the filter_test_cache_contexts filter.       'languages:' . LanguageInterface::TYPE_CONTENT,
      // The default cache contexts for Renderer.       'languages:' . LanguageInterface::TYPE_INTERFACE,
      'theme',
      // The cache tags set by the filter_test_cache_merge filter.       'user.permissions',
    ], parent::getExpectedCacheContexts());
  }

  /** * Tests GETting an entity with the test text field set to a specific format. * * @dataProvider providerTestGetWithFormat */
  public function testGetWithFormat($text_format_id, array $expected_cache_tags) {
    FilterFormat::create([
      'name' => 'Pablo Picasso',
      'format' => 'pablo',
      
'name' => 'Llama',
          'drupal_internal__uid' => 3,
        ],
      ],
    ];
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) {
    $cache_contexts = parent::getExpectedCacheContexts($sparse_fieldset);
    if ($sparse_fieldset === NULL || in_array('mail', $sparse_fieldset)) {
      $cache_contexts = Cache::mergeContexts($cache_contexts['user']);
    }
    return $cache_contexts;
  }

  /** * {@inheritdoc} */
  protected function getPostDocument() {
    return [
      

  protected function getPostDocument() {
    // @todo Update in https://www.drupal.org/node/2300677.     return [];
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) {
    return Cache::mergeContexts(parent::getExpectedCacheContexts()['languages:language_interface']);
  }

  /** * Tests a GET request for a default config entity, which has a _core key. * * @see https://www.drupal.org/project/drupal/issues/2915539 */
  public function testGetIndividualDefaultConfig() {
    // @todo Remove line below in favor of commented line in https://www.drupal.org/project/drupal/issues/2878463.     $url = Url::fromRoute('jsonapi.configurable_language--configurable_language.individual', ['entity' => ConfigurableLanguage::load('en')->uuid()]);
    /* $url = ConfigurableLanguage::load('en')->toUrl('jsonapi'); */

    

  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']);
    }

  protected function getPostDocument() {
    // @todo Update in https://www.drupal.org/node/2300677.     return [];
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) {
    return Cache::mergeContexts(parent::getExpectedCacheContexts()['languages:language_interface']);
  }

  /** * {@inheritdoc} */
  protected function createAnotherEntity($key) {
    NodeType::create([
      'name' => 'Llamaids',
      'type' => 'llamaids',
    ])->save();

    
$tags = parent::getExpectedCacheTags($sparse_fieldset);
    if ($sparse_fieldset === NULL || in_array('description', $sparse_fieldset)) {
      $tags = Cache::mergeTags($tags['config:filter.format.plain_text', 'config:filter.settings']);
    }
    return $tags;
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) {
    $contexts = parent::getExpectedCacheContexts($sparse_fieldset);
    if ($sparse_fieldset === NULL || in_array('description', $sparse_fieldset)) {
      $contexts = Cache::mergeContexts($contexts['languages:language_interface', 'theme']);
    }
    return $contexts;
  }

  /** * Tests GETting a term with a parent term other than the default <root> (0). * * @see ::getExpectedNormalizedEntity() * * @dataProvider providerTestGetIndividualTermWithParent */
$this->assertSame(static::$auth ? [] : ['MISS']$response->getHeader('X-Drupal-Cache'));
    // DX: 403 because unauthorized.     $url->setOption('query', ['_format' => static::$format]);
    $response = $this->request('GET', $url$request_options);
    $this->assertResourceErrorResponse(403, $this->getExpectedUnauthorizedAccessMessage('GET')$response$expected_403_cacheability->getCacheTags()$expected_403_cacheability->getCacheContexts()static::$auth ? FALSE : 'MISS', FALSE);
    $this->assertArrayNotHasKey('Link', $response->getHeaders());

    $this->setUpAuthorization('GET');

    // 200 for well-formed HEAD request.     $response = $this->request('HEAD', $url$request_options);
    $is_cacheable_by_dynamic_page_cache = empty(array_intersect(['user', 'session']$this->getExpectedCacheContexts()));
    $this->assertResourceResponse(200, '', $response$this->getExpectedCacheTags()$this->getExpectedCacheContexts()static::$auth ? FALSE : 'MISS', $is_cacheable_by_dynamic_page_cache ? 'MISS' : 'UNCACHEABLE');
    $head_headers = $response->getHeaders();

    // 200 for well-formed GET request. Page Cache hit because of HEAD request.     // Same for Dynamic Page Cache hit.     $response = $this->request('GET', $url$request_options);
    $this->assertResourceResponse(200, FALSE, $response$this->getExpectedCacheTags()$this->getExpectedCacheContexts()static::$auth ? FALSE : 'HIT', $is_cacheable_by_dynamic_page_cache ? (static::$auth ? 'HIT' : 'MISS') : 'UNCACHEABLE');
    // Assert that Dynamic Page Cache did not store a ResourceResponse object,     // which needs serialization after every cache hit. Instead, it should     // contain a flattened response. Otherwise performance suffers.     // @see \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::flattenResponse()

  protected function getPostDocument() {
    // @todo Update once https://www.drupal.org/node/2300677 is fixed.     return [];
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheContexts(array $sparse_fieldset = NULL) {
    // @see ::createEntity()     return array_values(array_diff(parent::getExpectedCacheContexts()['user.permissions']));
  }

  /** * {@inheritdoc} */
  protected function getExpectedCacheTags(array $sparse_fieldset = NULL) {
    // Because the 'user.permissions' cache context is missing, the cache tag     // for the anonymous user role is never added automatically.     return array_values(array_diff(parent::getExpectedCacheTags()['config:user.role.anonymous']));
  }

  
'href' => $expected_base_url->toString() . '/field_jsonapi_test_entity_ref' . $version_query_string,
          ],
          'self' => [
            'href' => $expected_base_url->toString() . '/relationships/field_jsonapi_test_entity_ref' . $version_query_string,
          ],
        ],
      ];
    }

    $basic_cacheability = (new CacheableMetadata())
      ->addCacheTags($this->getExpectedCacheTags())
      ->addCacheContexts($this->getExpectedCacheContexts());
    return static::decorateExpectedResponseForIncludedFields(new CacheableResourceResponse($individual_document)$resource_data['responses'])
      ->addCacheableDependency($basic_cacheability);
  }

  /** * Maps an array of PSR responses to JSON:API ResourceResponses. * * @param \Psr\Http\Message\ResponseInterface[] $responses * The PSR responses to be mapped. * * @return \Drupal\jsonapi\ResourceResponse[] * The ResourceResponses. */
$reason = $this->getExpectedUnauthorizedAccessMessage('GET');
      $message = trim("The current user is not allowed to GET the selected resource. $reason");
      $this->assertResourceErrorResponse(403, $message$url$response, '/data', $expected_403_cacheability->getCacheTags()$expected_403_cacheability->getCacheContexts(), FALSE, 'MISS');
      $this->assertArrayNotHasKey('Link', $response->getHeaders());
    }
    else {
      $expected_document = $this->getExpectedDocument();
      $label_field_name = $this->entity->getEntityType()->hasKey('label') ? $this->entity->getEntityType()->getKey('label') : static::$labelFieldName;
      $expected_document['data']['attributes'] = array_intersect_key($expected_document['data']['attributes'][$label_field_name => TRUE]);
      unset($expected_document['data']['relationships']);
      // MISS or UNCACHEABLE depends on data. It must not be HIT.       $dynamic_cache_label_only = !empty(array_intersect(['user', 'session']$this->getExpectedCacheContexts([$label_field_name]))) ? 'UNCACHEABLE' : 'MISS';
      $this->assertResourceResponse(200, $expected_document$response$this->getExpectedCacheTags()$this->getExpectedCacheContexts([$label_field_name]), FALSE, $dynamic_cache_label_only);
    }

    $this->setUpAuthorization('GET');

    // Set body despite that being nonsensical: should be ignored.     $request_options[RequestOptions::BODY] = Json::encode($this->getExpectedDocument());

    // 400 for GET request with reserved custom query parameter.     $url_reserved_custom_query_parameter = clone $url;
    $url_reserved_custom_query_parameter = $url_reserved_custom_query_parameter->setOption('query', ['foo' => 'bar']);
    
'url.query_args:resourceVersion', 'url.site', 'user.permissions'],
      FALSE,
      'MISS'
    );
    /* $this->assertResourceErrorResponse(403, 'The current user is not allowed to GET the selected resource.', $response, '/data'); */

    // 200 after granting permission.     $this->grantPermissionsToTestedRole(['view own unpublished content']);
    $response = $this->request('GET', $url$request_options);
    // The response varies by 'user', causing the 'user.permissions' cache     // context to be optimized away.     $expected_cache_contexts = Cache::mergeContexts($this->getExpectedCacheContexts()['user']);
    $expected_cache_contexts = array_diff($expected_cache_contexts['user.permissions']);
    $this->assertResourceResponse(200, FALSE, $response$this->getExpectedCacheTags()$expected_cache_contexts, FALSE, 'UNCACHEABLE');
  }

  /** * Asserts that normalizations are cached in an incremental way. * * @throws \Drupal\Core\Entity\EntityStorageException * * @internal */
  
Home | Imprint | This part of the site doesn't use cookies.