You are here

function RestfulUpdateEntityCurlTestCase::testUpdateEntityAsPutPatch in RESTful 7.2

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

Test update an entity (PUT & PATCH methods).

File

tests/RestfulUpdateEntityCurlTestCase.test, line 44
Contains RestfulUpdateEntityCurlTestCase

Class

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.2/test_articles/' . $id, RequestInterface::METHOD_PUT, $request, array(
    'Content-Type' => 'application/x-www-form-urlencoded',
  ));
  $expected_result = array(
    'data' => array(
      array(
        'id' => '1',
        'label' => $new_label,
        'self' => url('api/v1.2/test_articles/' . $id, array(
          'absolute' => TRUE,
        )),
        'body' => NULL,
      ),
    ),
    'self' => array(
      'href' => url('api/v1.2/test_articles/' . $id, array(
        'absolute' => TRUE,
      )),
      'title' => 'Self',
    ),
  );
  $result = drupal_json_decode($result['body']);
  $this
    ->assertEqual($expected_result, $result);

  // Update an entity with invalid property name.
  $request['invalid'] = 'wrong';
  $result = $this
    ->httpRequest('api/v1.2/test_articles/' . $id, RequestInterface::METHOD_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.2/test_articles/{$id}", RequestInterface::METHOD_PATCH, $request, array(
    'Content-Type' => 'application/x-www-form-urlencoded',
  ));
  $expected_result = array(
    'data' => array(
      array(
        'id' => '2',
        'label' => $new_label,
        'self' => url('api/v1.2/test_articles/' . $id, array(
          'absolute' => TRUE,
        )),
        'body' => $text,
      ),
    ),
    'self' => array(
      'title' => 'Self',
      'href' => url('api/v1.2/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.2/test_articles/{$id}", RequestInterface::METHOD_PATCH, $request);
  $this
    ->assertEqual($result['code'], 400, 'User cannot update using PATCH method an entity with invalid property name.');
}