You are here

public function RestfulHookMenuTestCase::testViewEntity in RESTful 7.2

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

Test viewing an entity (GET method).

File

tests/RestfulHookMenuTestCase.test, line 37
Contains \RestfulHookMenuTestCase.

Class

RestfulHookMenuTestCase

Code

public function testViewEntity() {
  $user1 = $this
    ->drupalCreateUser(array(
    'edit own article content',
  ));
  $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, RequestInterface::METHOD_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' => RequestInterface::METHOD_PATCH,
    'X-CSRF-Token' => drupal_get_token(\Drupal\restful\Plugin\authentication\Authentication::TOKEN_VALUE),
    'Content-Type' => 'application/json',
    'Authorization' => 'Basic ' . base64_encode($user1->name . ':' . $user1->pass_raw),
  );
  $body = array(
    'label' => 'new title',
  );
  $handler = restful()
    ->getResourceManager()
    ->getPlugin('articles:1.1');
  $header_bag = new \Drupal\restful\Http\HttpHeaderBag($headers);
  $handler
    ->setRequest(Request::create('api/v1.0/articles/' . $node1->nid, array(), RequestInterface::METHOD_POST, $header_bag, FALSE, NULL, array(), array(), array(), $body));
  $handler
    ->setPath($node1->nid);
  restful()
    ->getFormatterManager()
    ->format($handler
    ->process(), 'json');
  $node1 = node_load($node1->nid);
  $this
    ->assertEqual($node1->title, 'new title', 'HTTP method was overridden.');
}