You are here

protected function RestfulSubResourcesCreateEntityTestCase::processRequests in RESTful 7.2

Same name and namespace in other branches
  1. 7 tests/RestfulSubResourcesCreateEntityTestCase.test \RestfulSubResourcesCreateEntityTestCase::processRequests()

Assert valid and invalid requests.

Parameters

string $method: The method name.

string $path: The path.

array $request: The request array.

1 call to RestfulSubResourcesCreateEntityTestCase::processRequests()
RestfulSubResourcesCreateEntityTestCase::testCreateAndUpdateEntity in tests/RestfulSubResourcesCreateEntityTestCase.test
Test creating and updating an entity with sub-resources.

File

tests/RestfulSubResourcesCreateEntityTestCase.test, line 221
Contains RestfulSubResourcesCreateEntityTestCase.

Class

RestfulSubResourcesCreateEntityTestCase

Code

protected function processRequests($method = 'post', $path = '', array $request = array()) {
  $method = strtoupper($method);
  $query = $parsed_body = array();
  if (Request::isWriteMethod($method)) {
    $parsed_body = $request;
  }
  else {
    $query = $request;
  }
  $this->handler
    ->setRequest(Request::create($path, $query, $method, NULL, FALSE, NULL, array(), array(), array(), $parsed_body));
  $this->handler
    ->setPath($path);
  $result = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($this->handler
    ->process(), 'json'));
  $result = $result['data'][0];
  $this
    ->assertTrue($result['id'], 'Parent entity created.');
  $this
    ->assertTrue($result['entity_reference_single']['id'], 'Single sub-resource created or updated.');
  $this
    ->assertTrue($result['entity_reference_multiple'][0]['id'] && $result['entity_reference_multiple'][1]['id'], 'Multiple sub-resource created or updated.');

  // Validation fail on the parent.
  $request['label'] = 'no';
  $this
    ->assertInvalidRequest($method, $path, $request);

  // Validation fail on the single resource.
  $request['label'] = 'parent';
  if (is_array($request['entity_reference_single'])) {
    $request['entity_reference_single']['label'] = 'no';
    $this
      ->assertInvalidRequest($method, $path, $request);
    $request['entity_reference_single']['label'] = 'child1';
  }

  // Validation fail on the multiple resource.
  if (!empty($request['entity_reference_multiple'][0]['label']) && is_array($request['entity_reference_multiple'][0]['label'])) {
    $request['entity_reference_multiple'][0]['label'] = 'no';
    $this
      ->assertInvalidRequest($method, $path, $request);
  }
}