You are here

function RestfulUpdateEntityTestCase::testUpdateEntityAsPut in RESTful 7

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

Test update an entity (PUT method).

File

tests/RestfulUpdateEntityTestCase.test, line 25
Contains RestfulUpdateEntityTestCase

Class

RestfulUpdateEntityTestCase
@file Contains RestfulUpdateEntityTestCase

Code

function testUpdateEntityAsPut() {
  $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;
  $handler = restful_get_restful_handler('test_articles');
  $request = array(
    'label' => $new_label,
  );
  $result = $handler
    ->put($id, $request);
  $expected_result = array(
    array(
      'id' => $id,
      'label' => $new_label,
      'self' => $handler
        ->versionedUrl($id),
      'body' => NULL,
    ),
  );
  $this
    ->assertEqual($result, $expected_result);

  // Update an entity with invalid property name.
  $request['invalid'] = 'wrong';
  try {
    $handler
      ->put($id, $request);
    $this
      ->fail('User can update using PUT method an entity with invalid property name.');
  } catch (Exception $e) {
    $this
      ->pass('User cannot update using PUT method an entity with invalid property name.');
  }
  $new_label = $this
    ->randomName();
  $new_body = $this
    ->randomName();
  $request = array(
    'label' => $new_label,
    'body' => $new_body,
  );

  // Setting new value for 'body'.
  $result = $handler
    ->put($id, $request);

  // Making sure the new body has been set.
  $this
    ->assertEqual(trim(strip_tags($result[0]['body'])), $new_body);

  // Removing the body value.
  $request = array(
    'label' => $new_label,
    'body' => NULL,
  );
  $result = $handler
    ->put($id, $request);
  $this
    ->assertEqual($result[0]['body'], NULL);
}