getExpectedDocument example


        $psr_responses = $this->getResponses([static::getRelatedLink(static::toResourceIdentifier($entity)$public_field_name)]$request_options);
        $collected_responses[] = static::toCollectionResourceResponse(static::toResourceResponses($psr_responses), NULL, TRUE);
        $entity = $entity->{$field_name}->entity;
      }
      if (!empty($collected_responses)) {
        $data['responses'][$path] = static::toCollectionResourceResponse($collected_responses, NULL, TRUE);
      }
      return $data;
    }['responses' => [], 'already_checked' => []]);

    $individual_document = $this->getExpectedDocument();

    $expected_base_url = Url::fromRoute(sprintf('jsonapi.%s.individual', static::$resourceTypeName)['entity' => $this->entity->uuid()])->setAbsolute();
    $include_url = clone $expected_base_url;
    $query = ['include' => implode(',', $include_paths)];
    $include_url->setOption('query', $query);
    $individual_document['links']['self']['href'] = $include_url->toString();

    // The test entity reference field should always be present.     if (!isset($individual_document['data']['relationships']['field_jsonapi_test_entity_ref'])) {
      if (static::$resourceTypeIsVersionable) {
        assert($this->entity instanceof RevisionableInterface);
        
// DX: 403 when unauthorized, or 200 if the 'view label' operation is     // supported by the entity type.     $response = $this->request('GET', $url$request_options);
    if (!static::$anonymousUsersCanViewLabels) {
      $expected_403_cacheability = $this->getExpectedUnauthorizedAccessCacheability();
      $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.
// Modify the entity under test to use the provided parent terms.     $this->entity->set('parent', $parent_term_ids)->save();

    // @todo Remove line below in favor of commented line in https://www.drupal.org/project/drupal/issues/2878463.     $url = Url::fromRoute(sprintf('jsonapi.%s.individual', static::$resourceTypeName)['entity' => $this->entity->uuid()]);
    // $url = $this->entity->toUrl('jsonapi');     $request_options = [];
    $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
    $request_options = NestedArray::mergeDeep($request_options$this->getAuthenticationRequestOptions());
    $this->setUpAuthorization('GET');
    $response = $this->request('GET', $url$request_options);
    $this->assertSameDocument($this->getExpectedDocument(), Json::decode($response->getBody()));
  }

  /** * Data provider for ::testGetIndividualTermWithParent(). */
  public function providerTestGetIndividualTermWithParent() {
    return [
      'root parent: [0] (= no parent)' => [
        [0],
      ],
      'non-root parent: [2]' => [
        [
->enableLayoutBuilder()
      ->setOverridable()
      ->save();
    $this->assertCount(1, $entity->getThirdPartySetting('layout_builder', 'sections'));
    return $entity;
  }

  /** * {@inheritdoc} */
  protected function getExpectedDocument() {
    $document = parent::getExpectedDocument();
    array_unshift($document['data']['attributes']['dependencies']['module'], 'layout_builder');
    $document['data']['attributes']['hidden'][OverridesSectionStorage::FIELD_NAME] = TRUE;
    $document['data']['attributes']['third_party_settings']['layout_builder'] = [
      'enabled' => TRUE,
      'allow_custom' => TRUE,
    ];
    return $document;
  }

}
$this->setUpAuthorization('POST');

    // 404 when the field name is invalid.     $invalid_uri = Url::fromUri('base:' . static::$postUri . '_invalid');
    $response = $this->fileRequest($invalid_uri$this->testFileData);
    $this->assertResourceErrorResponse(404, 'Field "field_rest_file_test_invalid" does not exist.', $invalid_uri$response);

    // This request will have the default 'application/octet-stream' content     // type header.     $response = $this->fileRequest($uri$this->testFileData);
    $this->assertSame(201, $response->getStatusCode());
    $expected = $this->getExpectedDocument();
    $this->assertResponseData($expected$response);

    // Check the actual file data.     $this->assertSame($this->testFileData, file_get_contents('public://foobar/example.txt'));

    // Test the file again but using 'filename' in the Content-Disposition     // header with no 'file' prefix.     $response = $this->fileRequest($uri$this->testFileData, ['Content-Disposition' => 'filename="example.txt"']);
    $this->assertSame(201, $response->getStatusCode());
    $expected = $this->getExpectedDocument(2, 'example_0.txt');
    $this->assertResponseData($expected$response);

    
Home | Imprint | This part of the site doesn't use cookies.