You are here

function RestfulUpdateEntityCurlTestCase::testUpdateEntityAsPutPatch in RESTful 7

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

Test update an entity (PUT & PATCH methods).

File

tests/RestfulUpdateEntityCurlTestCase.test, line 42
Contains RestfulUpdateEntityCurlTestCase

Class

RestfulUpdateEntityCurlTestCase
@file Contains RestfulUpdateEntityCurlTestCase

Code

function testUpdateEntityAsPutPatch() {

  // Test update an entity (PUT method).
  $label = $this
    ->randomName();
  $new_label = $this
    ->randomName();
  $text = $this
    ->randomName();
  $settings = array(
    'type' => 'article',
    'title' => $label,
  );
  $settings['body'][LANGUAGE_NONE][0]['value'] = $text;
  $node = $this
    ->drupalCreateNode($settings);
  $id = $node->nid;
  $request = array(
    'label' => $new_label,
  );
  $result = $this
    ->httpRequest('api/v1.0/test_articles/' . $id, \RestfulInterface::PUT, $request);
  $expected_result = array(
    'data' => array(
      array(
        'id' => $id,
        'label' => $new_label,
        'self' => url('api/v1.0/test_articles/' . $id, array(
          'absolute' => TRUE,
        )),
        'body' => NULL,
      ),
    ),
    'self' => array(
      'title' => 'Self',
      'href' => url('api/v1.0/test_articles/' . $id, array(
        'absolute' => TRUE,
      )),
    ),
  );
  $this
    ->assertEqual($result['body'], drupal_json_encode($expected_result));

  // Update an entity with invalid property name.
  $request['invalid'] = 'wrong';
  $result = $this
    ->httpRequest('api/v1.0/test_articles/' . $id, \RestfulInterface::PUT, $request);
  $this
    ->assertEqual($result['code'], 400, 'User cannot update using PUT method an entity with invalid property name.');

  // Test update an entity (PATCH method).
  $label = $this
    ->randomName();
  $new_label = $this
    ->randomName();
  $text = $this
    ->randomName();
  $settings = array(
    'type' => 'article',
    'title' => $label,
  );
  $settings['body'][LANGUAGE_NONE][0]['value'] = $text;
  $node = $this
    ->drupalCreateNode($settings);
  $id = $node->nid;
  $request = array(
    'label' => $new_label,
  );
  $result = $this
    ->httpRequest("api/v1.0/test_articles/{$id}", \RestfulInterface::PATCH, $request);
  $expected_result = array(
    'data' => array(
      array(
        'id' => $id,
        'label' => $new_label,
        'self' => url('api/v1.0/test_articles/' . $id, array(
          'absolute' => TRUE,
        )),
        'body' => $text,
      ),
    ),
    'self' => array(
      'title' => 'Self',
      'href' => url('api/v1.0/test_articles/' . $id, array(
        'absolute' => TRUE,
      )),
    ),
  );
  $result = drupal_json_decode($result['body']);
  $result['data'][0]['body'] = trim(strip_tags($result['data'][0]['body']));
  ksort($result);
  ksort($expected_result);
  $this
    ->assertEqual($result, $expected_result);

  // Update an entity with invalid property name.
  $request['invalid'] = 'wrong';
  $result = $this
    ->httpRequest("api/v1.0/test_articles/{$id}", \RestfulInterface::PATCH, $request);
  $this
    ->assertEqual($result['code'], 400, 'User cannot update using PATCH method an entity with invalid property name.');
}