Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 295019

Unnecessary entity creation in EntityResourceTestBase

$
0
0

This is a slightly obscure and minor issue that cropped up during #2350135: Duplicate path aliases can be added.

Around line 795 in EntityResourceTestBase::testPost(), we send an "invalid" POST request with multiple values for a single-value field.

    $request_options[RequestOptions::BODY] = $parseable_invalid_request_body;
    $response = $this->request('POST', $url, $request_options);

    // DX: 422 when invalid entity: multiple values sent for single-value field.
    if ($label_field = $this->entity->getEntityType()->hasKey('label') ? $this->entity->getEntityType()->getKey('label') : static::$labelFieldName) {
      $label_field_capitalized = $this->entity->getFieldDefinition($label_field)->getLabel();
      $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\n$label_field: $label_field_capitalized: this field cannot hold more than 1 values.\n", $response);
    }

As shown, the request is always sent - but the error is only expected if the entity actually has a label field.

For entities that don't, this means that the "invalid" request goes through and actually creates an entity. There's no indication that the test expects this to happen. Therefore it seems correct that the "invalid" request is only sent if the test actually expects an error:

    // DX: 422 when invalid entity: multiple values sent for single-value field.
    if ($label_field = $this->entity->getEntityType()->hasKey('label') ? $this->entity->getEntityType()->getKey('label') : static::$labelFieldName) {
      $request_options[RequestOptions::BODY] = $parseable_invalid_request_body;
      $response = $this->request('POST', $url, $request_options);
      $label_field_capitalized = $this->entity->getFieldDefinition($label_field)->getLabel();
      $this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\n$label_field: $label_field_capitalized: this field cannot hold more than 1 values.\n", $response);
    }

Viewing all articles
Browse latest Browse all 295019

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>