You are here

function RestWSTestCase::testCRUD in RESTful Web Services 7

Same name and namespace in other branches
  1. 7.2 restws.test \RestWSTestCase::testCRUD()

CRUD tests for nodes.

File

./restws.test, line 25
RESTful web services tests.

Class

RestWSTestCase
@file RESTful web services tests.

Code

function testCRUD() {

  // Test Read.
  $title = $this
    ->randomName(8);
  $node = $this
    ->drupalCreateNode(array(
    'title' => $title,
  ));
  $account = $this
    ->drupalCreateUser(array(
    'access resource node',
  ));
  $this
    ->drupalLogin($account);
  $result = $this
    ->httpRequest('node/' . $node->nid . '.json', 'GET', $account);
  $node_array = drupal_json_decode($result);
  $this
    ->assertEqual($node->title, $node_array['title'], 'Node title was received correctly.');
  $this
    ->assertResponse('200', 'HTTP response code is correct.');
  $this
    ->assertEqual(curl_getinfo($this->curlHandle, CURLINFO_CONTENT_TYPE), 'application/json', 'HTTP content type is correct.');

  // Test Create.
  $account = $this
    ->drupalCreateUser(array(
    'access content',
    'bypass node access',
    'access resource node',
  ));
  $title = $this
    ->randomName(8);
  $new_node = array(
    'body' => array(
      LANGUAGE_NONE => array(
        array(),
      ),
    ),
    'title' => $title,
    'type' => 'page',
    'author' => $account->uid,
  );
  $json = drupal_json_encode($new_node);
  $result = $this
    ->httpRequest('node', 'PUT', $account, $json);
  $result_array = drupal_json_decode($result);
  $nid = $result_array['id'];
  $node = node_load($nid);
  $this
    ->assertEqual($title, $node->title, 'Node title in DB is equal to the new title.');
  $this
    ->assertResponse('201', 'HTTP response code is correct.');
  $this
    ->assertEqual(curl_getinfo($this->curlHandle, CURLINFO_CONTENT_TYPE), 'application/json', 'HTTP content type is correct.');

  // Test Update.
  $new_title = $this
    ->randomName(8);
  $json = drupal_json_encode(array(
    'title' => $new_title,
  ));
  $this
    ->httpRequest('node/' . $node->nid, 'POST', $account, $json);

  // Clear the static cache, otherwise we won't see the update.
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertEqual($new_title, $node->title, 'Node title in DB is equal to the updated title.');
  $this
    ->assertResponse('200', 'HTTP response code is correct.');
  $this
    ->assertEqual(curl_getinfo($this->curlHandle, CURLINFO_CONTENT_TYPE), 'application/json', 'HTTP content type is correct.');

  // Test delete.
  $this
    ->httpRequest('node/' . $node->nid, 'DELETE', $account);

  // Clear the static cache, otherwise we won't see the update.
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertFalse($node, 'Node is not in the DB anymore.');
  $this
    ->assertResponse('200', 'HTTP response code is correct.');
  $this
    ->assertEqual(curl_getinfo($this->curlHandle, CURLINFO_CONTENT_TYPE), 'application/json', 'HTTP content type is correct.');
}