provisionEntityResource example

return parent::getExpectedUnauthorizedAccessMessage($method);
  }

  /** * {@inheritdoc} */
  public function testGet() {
    // Contact Message entities are not stored, so they cannot be retrieved.     $this->expectException(RouteNotFoundException::class);
    $this->expectExceptionMessage('Route "rest.entity.contact_message.GET" does not exist.');

    $this->provisionEntityResource();
    Url::fromRoute('rest.entity.contact_message.GET')->toString(TRUE);
  }

  /** * {@inheritdoc} */
  public function testPatch() {
    // Contact Message entities are not stored, so they cannot be modified.     $this->expectException(RouteNotFoundException::class);
    $this->expectExceptionMessage('Route "rest.entity.contact_message.PATCH" does not exist.');

    


  /** * Tests PATCHing a node's path with and without 'create url aliases'. * * For a positive test, see the similar test coverage for Term. * * @see \Drupal\Tests\rest\Functional\EntityResource\Term\TermResourceTestBase::testPatchPath() */
  public function testPatchPath() {
    $this->initAuthentication();
    $this->provisionEntityResource();
    $this->setUpAuthorization('GET');
    $this->setUpAuthorization('PATCH');

    $url = $this->getEntityResourceUrl()->setOption('query', ['_format' => static::$format]);

    // GET node's current normalization.     $response = $this->request('GET', $url$this->getAuthenticationRequestOptions('GET'));
    $normalization = $this->serializer->decode((string) $response->getBody()static::$format);

    // Change node's path alias.     $normalization['path'][0]['alias'] .= 's-rule-the-world';

    


  /** * Tests a GET request for a default config entity, which has a _core key. * * @see https://www.drupal.org/node/2915414 */
  public function testGetDefaultConfig() {
    $this->initAuthentication();
    $url = Url::fromUri('base:/entity/configurable_language/en')->setOption('query', ['_format' => static::$format]);
    $request_options = $this->getAuthenticationRequestOptions('GET');
    $this->provisionEntityResource();
    $this->setUpAuthorization('GET');
    $response = $this->request('GET', $url$request_options);

    $normalization = $this->serializer->decode((string) $response->getBody()static::$format);
    $this->assertArrayNotHasKey('_core', $normalization);
  }

}

  public function testPostDxWithoutCriticalBaseFields() {
    $this->initAuthentication();
    $this->provisionEntityResource();
    $this->setUpAuthorization('POST');

    $url = $this->getEntityResourcePostUrl()->setOption('query', ['_format' => static::$format]);
    $request_options = [];
    $request_options[RequestOptions::HEADERS]['Accept'] = static::$mimeType;
    $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType;
    $request_options = array_merge_recursive($request_options$this->getAuthenticationRequestOptions('POST'));

    // DX: 422 when missing 'entity_type' field.     $request_options[RequestOptions::BODY] = $this->serializer->encode(array_diff_key($this->getNormalizedPostEntity()['entity_type' => TRUE])static::$format);
    $response = $this->request('POST', $url$request_options);
    
'value' => $this->randomString(),
    ];
    if ($text_format_id !== FALSE) {
      $value['format'] = $text_format_id;
    }
    $this->entity->set('field_test_text', $value)->save();

    $this->initAuthentication();
    $url = $this->getEntityResourceUrl();
    $url->setOption('query', ['_format' => static::$format]);
    $request_options = $this->getAuthenticationRequestOptions('GET');
    $this->provisionEntityResource();
    $this->setUpAuthorization('GET');
    $response = $this->request('GET', $url$request_options);
    $expected_cache_tags = Cache::mergeTags($expected_cache_tags, parent::getExpectedCacheTags());
    $this->assertEqualsCanonicalizing($expected_cache_tagsexplode(' ', $response->getHeader('X-Drupal-Cache-Tags')[0]));
  }

  public function providerTestGetWithFormat() {
    return [
      'format specified (different from fallback format)' => [
        'pablo',
        ['config:filter.format.pablo'],
      ],
if ($has_canonical_url) {
      $expected_cacheability = $this->getExpectedUnauthorizedAccessCacheability()
        // @see \Drupal\Core\EventSubscriber\AnonymousUserResponseSubscriber::onRespond()         ->addCacheTags(['config:user.role.anonymous']);
      $expected_cacheability->addCacheableDependency($this->getExpectedUnauthorizedEntityAccessCacheability(FALSE));
      $this->assertResourceErrorResponse(403, $this->getExpectedUnauthorizedAccessMessage('GET')$response$expected_cacheability->getCacheTags()$expected_cacheability->getCacheContexts(), 'MISS', FALSE);
    }
    else {
      $this->assertResourceErrorResponse(404, 'No route found for "GET ' . $this->getEntityResourceUrl()->setAbsolute()->toString() . '"', $response);
    }

    $this->provisionEntityResource();

    // DX: forgetting authentication: authentication provider-specific error     // response.     if (static::$auth) {
      $response = $this->request('GET', $url$request_options);
      $this->assertResponseWhenMissingAuthentication('GET', $response);
    }

    $request_options[RequestOptions::HEADERS]['REST-test-auth'] = '1';

    // DX: 403 when attempting to use unallowed authentication provider.
/** * Tests PATCHing security-sensitive base fields of the logged in account. */
  public function testPatchDxForSecuritySensitiveBaseFields() {
    // The anonymous user is never allowed to modify itself.     if (!static::$auth) {
      $this->markTestSkipped();
    }

    $this->initAuthentication();
    $this->provisionEntityResource();

    /** @var \Drupal\user\UserInterface $user */
    $user = static::$auth ? $this->account : User::load(0);
    // @todo Remove the array_diff_key() call in https://www.drupal.org/node/2821077.     $original_normalization = array_diff_key($this->serializer->normalize($userstatic::$format)['created' => TRUE, 'changed' => TRUE, 'name' => TRUE]);

    // Since this test must be performed by the user that is being modified,     // we cannot use $this->getUrl().     $url = $user->toUrl()->setOption('query', ['_format' => static::$format]);
    $request_options = [
      RequestOptions::HEADERS => ['Content-Type' => static::$mimeType],
    ];


  /** * Tests PATCHing a term's path. * * For a negative test, see the similar test coverage for Node. * * @see \Drupal\Tests\rest\Functional\EntityResource\Node\NodeResourceTestBase::testPatchPath() */
  public function testPatchPath() {
    $this->initAuthentication();
    $this->provisionEntityResource();
    $this->setUpAuthorization('GET');
    $this->setUpAuthorization('PATCH');

    $url = $this->getEntityResourceUrl()->setOption('query', ['_format' => static::$format]);

    // GET term's current normalization.     $response = $this->request('GET', $url$this->getAuthenticationRequestOptions('GET'));
    $normalization = $this->serializer->decode((string) $response->getBody()static::$format);

    // Change term's path alias.     $normalization['path'][0]['alias'] .= 's-rule-the-world';

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