You are here

RestfulUpdateEntityTestCase.test in RESTful 7

Same filename and directory in other branches
  1. 7.2 tests/RestfulUpdateEntityTestCase.test

Contains RestfulUpdateEntityTestCase

File

tests/RestfulUpdateEntityTestCase.test
View source
<?php

/**
 * @file
 * Contains RestfulUpdateEntityTestCase
 */
class RestfulUpdateEntityTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Update entity',
      'description' => 'Test the updating of an entity using PUT and PATCH methods.',
      'group' => 'RESTful',
    );
  }
  function setUp() {
    parent::setUp('restful_test');
  }

  /**
   * Test update an entity (PUT method).
   */
  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);
  }

  /**
   * Test update an entity (PATCH method).
   */
  function testUpdateEntityAsPatch() {
    $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
      ->patch($id, $request);
    $expected_result = array(
      array(
        'id' => $id,
        'label' => $new_label,
        'self' => $handler
          ->versionedUrl($id),
        'body' => $text,
      ),
    );
    $result[0]['body'] = trim(strip_tags($result[0]['body']));
    $this
      ->assertEqual($result, $expected_result);

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

    // Setting new value for 'body'.
    $result = $handler
      ->patch($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(
      'body' => NULL,
    );
    $result = $handler
      ->patch($id, $request);
    $this
      ->assertEqual($result[0]['body'], NULL);
  }

}

Classes

Namesort descending Description
RestfulUpdateEntityTestCase @file Contains RestfulUpdateEntityTestCase