You are here

function RestfulHookMenuTestCase::testViewEntity in RESTful 7

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

Test viewing an entity (GET method).

File

tests/RestfulHookMenuTestCase.test, line 28
Contains RestfulHookMenuTestCase

Class

RestfulHookMenuTestCase
@file Contains RestfulHookMenuTestCase

Code

function testViewEntity() {
  $user1 = $this
    ->drupalCreateUser();
  $title = $this
    ->randomName();
  $settings = array(
    'type' => 'article',
    'title' => $title,
    'uid' => $user1->uid,
  );
  $node1 = $this
    ->drupalCreateNode($settings);

  // Test version 1.0
  $result = $this
    ->httpRequest('api/v1.0/articles/' . $node1->nid);
  $expected_result = array(
    'data' => array(
      array(
        'id' => $node1->nid,
        'label' => $node1->title,
        'self' => url('api/v1.0/articles/' . $node1->nid, array(
          'absolute' => TRUE,
        )),
      ),
    ),
    'self' => array(
      'title' => 'Self',
      'href' => url('api/v1.0/articles/' . $node1->nid, array(
        'absolute' => TRUE,
      )),
    ),
  );
  $this
    ->assertEqual($result['body'], json_encode($expected_result));

  // Test version 1.1
  $result = $this
    ->httpRequest('api/v1.1/articles/' . $node1->nid, \RestfulInterface::GET);
  $expected_result['self']['href'] = url('api/v1.1/articles/' . $node1->nid, array(
    'absolute' => TRUE,
  ));
  unset($expected_result['data'][0]['self']);
  $this
    ->assertEqual($result['body'], json_encode($expected_result));

  // Test method override.
  $headers = array(
    'X-HTTP-Method-Override' => \RestfulInterface::PATCH,
  );
  $body = array(
    'label' => 'new title',
  );
  $this
    ->httpRequest('api/v1.0/articles/' . $node1->nid, \RestfulInterface::POST, $body, $headers);
  $node1 = node_load($node1->nid);
  $this
    ->assertEqual($node1->title, 'new title', 'HTTP method was overriden.');

  // Try to override with an invalid method.
  $headers = array(
    'X-HTTP-Method-Override' => 'MALICIOUS',
  );
  $body = array(
    'label' => 'new title',
  );
  $result = $this
    ->httpRequest('api/v1.0/articles/' . $node1->nid, \RestfulInterface::POST, $body, $headers);
  $this
    ->assertTrue($result['code'] > 399, 'Bad overridden method is caught.');
}