You are here

public function RestfulSubResourcesCreateEntityTestCase::testCreateAndUpdateEntity in RESTful 7.2

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

Test creating and updating an entity with sub-resources.

File

tests/RestfulSubResourcesCreateEntityTestCase.test, line 95
Contains RestfulSubResourcesCreateEntityTestCase.

Class

RestfulSubResourcesCreateEntityTestCase

Code

public function testCreateAndUpdateEntity() {
  $base_request = $request = array(
    'label' => 'parent',
    'body' => 'Drupal',
    'entity_reference_single' => array(
      'body' => array(
        'label' => 'child1',
        'body' => 'Drupal1',
      ),
      'request' => array(
        'method' => 'POST',
      ),
    ),
    'entity_reference_multiple' => array(
      array(
        'body' => array(
          'label' => 'child2',
          'body' => 'Drupal2',
        ),
        'request' => array(
          'method' => 'POST',
        ),
      ),
      array(
        'body' => array(
          'label' => 'child3',
          'body' => 'Drupal3',
        ),
        'request' => array(
          'method' => 'POST',
        ),
      ),
    ),
  );

  // POST parent.
  $this
    ->processRequests('post', '', $request);

  // PUT/ PATCH parent.
  $settings = array(
    'type' => 'article',
    'title' => 'title1',
  );
  $node1 = $this
    ->drupalCreateNode($settings);
  $request = $base_request;
  foreach (array(
    'put',
    'patch',
  ) as $method) {
    $this
      ->processRequests($method, $node1->nid, $request);
  }

  // POST parent, reference existing on single reference, multiple reference
  // empty.
  $node2 = $this
    ->drupalCreateNode($settings);
  $node3 = $this
    ->drupalCreateNode($settings);
  $node4 = $this
    ->drupalCreateNode($settings);
  $node5 = $this
    ->drupalCreateNode($settings);
  $request = $base_request;
  $request['entity_reference_single'] = $node1->nid;
  $request['entity_reference_multiple'] = array(
    $node2->nid,
    $node3->nid,
  );
  $this
    ->processRequests('post', '', $request);

  // POST parent, reference existing on multiple reference, POST a new one,
  // PATCH existing one, and PUT existing one.
  $request = $base_request;
  $request['entity_reference_single'] = $node2->nid;
  $request['entity_reference_multiple']['request'] = array(
    'method' => RequestInterface::METHOD_PATCH,
  );
  $request['entity_reference_multiple'] = array(
    array(
      // Set the $node3.
      'id' => $node3->nid,
    ),
    array(
      'body' => array(
        // Create a new node.
        'label' => 'POST new one',
        'body' => 'Drupal',
      ),
      'request' => array(
        'method' => 'POST',
      ),
    ),
    array(
      // Set $node4 and update some of the fields.
      'id' => $node4->nid,
      'body' => array(
        'label' => 'PATCH existing one',
        'body' => 'Drupal',
      ),
      'request' => array(
        'method' => 'PATCH',
      ),
    ),
    array(
      // Set $node5 and update some of the fields.
      'id' => $node5->nid,
      'body' => array(
        'label' => 'PATCH existing one',
        'body' => 'Drupal',
      ),
      'request' => array(
        'method' => 'POST',
      ),
    ),
  );
  $this
    ->processRequests('post', '', $request);

  // Test the version of the sub-resource.
  $public_fields = $this->handler
    ->getFieldDefinitions();
  $erm_resource = $public_fields
    ->get('entity_reference_multiple')
    ->getResource();
  $this
    ->assertEqual($erm_resource['majorVersion'], 1, 'Sub resource major version is correct.');
  $this
    ->assertEqual($erm_resource['minorVersion'], 2, 'Sub resource minor version is correct.');
}