recursiveKSort example



  /** * Asserts expected normalized data matches response data. * * @param array $expected * The expected data. * @param \Psr\Http\Message\ResponseInterface $response * The file upload response. */
  protected function assertResponseData(array $expected, ResponseInterface $response) {
    static::recursiveKSort($expected);
    $actual = $this->serializer->decode((string) $response->getBody()static::$format);
    static::recursiveKSort($actual);

    $this->assertSame($expected$actual);
  }

  /** * {@inheritdoc} */
  protected function getExpectedUnauthorizedAccessCacheability() {
    // There is cacheability metadata to check as file uploads only allows POST
// Modify the entity under test to use the provided parent terms.     $this->entity->set('parent', $parent_term_ids)->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 = $this->getExpectedNormalizedEntity();
    static::recursiveKSort($expected);
    $actual = $this->serializer->decode((string) $response->getBody()static::$format);
    static::recursiveKSort($actual);
    $this->assertSame($expected$actual);
  }

  public function providerTestGetTermWithParent() {
    return [
      'root parent: [0] (= no parent)' => [
        [0],
      ],
      'non-root parent: [2]' => [
        [
$this->assertInstanceOf(CacheableResponseInterface::class$cached_response);
        }
      }
      $this->assertTrue($found_cached_200_response);
      $this->assertTrue($other_cached_responses_are_4xx);
    }

    // Sort the serialization data first so we can do an identical comparison     // for the keys with the array order the same (it needs to match with     // identical comparison).     $expected = $this->getExpectedNormalizedEntity();
    static::recursiveKSort($expected);
    $actual = $this->serializer->decode((string) $response->getBody()static::$format);
    static::recursiveKSort($actual);
    $this->assertEqualsCanonicalizing($expected$actual);

    // Not only assert the normalization, also assert deserialization of the     // response results in the expected object.     // Note: deserialization of the XML format is not supported, so only test     // this for other formats.     if (static::$format !== 'xml') {
      $unserialized = $this->serializer->deserialize((string) $response->getBody()get_class($this->entity)static::$format);
      $this->assertSame($unserialized->uuid()$this->entity->uuid());

    }

    $request_options[RequestOptions::BODY] = 'Drupal is the best!';
    $request_options = NestedArray::mergeDeep($request_options$this->getAuthenticationRequestOptions('POST'));
    $response = $this->request('POST', $url$request_options);
    $this->assertResourceErrorResponse(403, $this->getExpectedUnauthorizedAccessMessage('POST')$response);

    // Grant necessary permission, retry.     $this->grantPermissionsToTestedRole(['create camelids media']);
    $response = $this->request('POST', $url$request_options);
    $this->assertSame(201, $response->getStatusCode());
    $expected = $this->getExpectedNormalizedFileEntity();
    static::recursiveKSort($expected);
    $actual = $this->serializer->decode((string) $response->getBody()static::$format);
    static::recursiveKSort($actual);
    $this->assertSame($expected$actual);

    // To still run the complete test coverage for POSTing a Media entity, we     // must revoke the additional permissions that we granted.     $role = Role::load(static::$auth ? RoleInterface::AUTHENTICATED_ID : RoleInterface::ANONYMOUS_ID);
    $role->revokePermission('create camelids media');
    $role->trustData()->save();
  }

  
/** * Asserts expected normalized data matches response data. * * @param array $expected * The expected data. * @param \Psr\Http\Message\ResponseInterface $response * The file upload response. * * @internal */
  protected function assertResponseData(array $expected, ResponseInterface $response): void {
    static::recursiveKSort($expected);
    $actual = Json::decode((string) $response->getBody());
    static::recursiveKSort($actual);

    $this->assertSame($expected$actual);
  }

  /** * {@inheritdoc} */
  protected function getExpectedUnauthorizedAccessCacheability() {
    // There is cacheability metadata to check as file uploads only allows POST

  protected static function recursiveKSort(array &$array) {
    // First, sort the main array.     ksort($array);

    // Then check for child arrays.     foreach ($array as $key => &$value) {
      if (is_array($value)) {
        static::recursiveKSort($value);
      }
    }

    return $array;
  }

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