You are here

function RestfulSubResourcesCreateEntityTestCase::testCreateAndUpdateEntity in RESTful 7

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

Test creating and updating an entity with sub-resources.

File

tests/RestfulSubResourcesCreateEntityTestCase.test, line 80
Contains RestfulSubResourcesCreateEntityTestCase.

Class

RestfulSubResourcesCreateEntityTestCase
@file Contains RestfulSubResourcesCreateEntityTestCase.

Code

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

  // 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'] = array(
    $node3->nid,
    array(
      'label' => 'POST new one',
      'body' => 'Drupal',
    ),
    array(
      'id' => $node4->nid,
      'label' => 'PATCH existing one',
      'body' => 'Drupal',
    ),
    array(
      // Explicitly set the method.
      '__application' => array(
        'method' => 'put',
      ),
      'id' => $node5->nid,
      'label' => 'PUT existing one',
      'body' => 'Drupal',
    ),
  );
  $this
    ->processRequests($method, $node1->nid, $request);

  // Test the version of the sub-resource.
  $public_fields = $this->handler
    ->getPublicFields();
  $this
    ->assertEqual($public_fields['entity_reference_multiple']['resource']['article']['major_version'], 1, 'Sub resource major version is correct.');
  $this
    ->assertEqual($public_fields['entity_reference_multiple']['resource']['article']['minor_version'], 2, 'Sub resource minor version is correct.');
}