You are here

RestfulSimpleJsonTestCase.test in RESTful 7

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

Contains RestfulSimpleJsonTestCase

File

tests/RestfulSimpleJsonTestCase.test
View source
<?php

/**
 * @file
 * Contains RestfulSimpleJsonTestCase
 */
class RestfulSimpleJsonTestCase extends RestfulCurlBaseTestCase {
  public static function getInfo() {
    return array(
      'name' => 'View HAL+JSON',
      'description' => 'Test the viewing of an entity in HAL+JSON format.',
      'group' => 'RESTful',
    );
  }
  function setUp() {
    parent::setUp('restful_example', 'restful_test', 'entityreference');
    restful_test_add_fields();
  }

  /**
   * Test the Simple JSON formatter.
   */
  public function testSimpleJson() {
    $node = restful_test_create_node_with_tags();
    $handler = restful_get_restful_handler('articles', 1, 5);

    // Use the simple JSON formatter.
    $handler
      ->setPluginKey('formatter', 'json');
    $response = $handler
      ->get($node->nid);
    $formatter = \RestfulManager::outputFormat($handler);
    $result = $formatter
      ->format($response);
    if ($decoded_json = drupal_json_decode($result)) {
      $this
        ->pass('Valid JSON output generated.');
    }
    else {
      $this
        ->fail('Invalid JSON output generated.');
    }
    $this
      ->assertNotNull($decoded_json['data'], 'The "data" wrapper was created successfully.');

    // Assert the embedded tags.
    foreach ($decoded_json['data'][0]['tags'] as $index => $tag_info) {
      $this
        ->assertNotNull($tag_info['self'], 'The "self" property was populated for the tags.');
      $this
        ->assertNotNull($tag_info['id'], 'The "id" property was populated.');
      $this
        ->assertEqual($tag_info['label'], $response[0]['tags'][$index]['label'], 'The "label" property was populated correctly.');
    }

    // Assert the HATEOAS.
    // Create another node for pagination.
    restful_test_create_node_with_tags();
    $handler
      ->setRange(1);
    $response = $handler
      ->get();
    $result = $formatter
      ->format($response);
    $decoded_json = drupal_json_decode($result);
    $this
      ->assertNotNull($decoded_json['self'], '"Self" property added.');
    $this
      ->assertEqual($decoded_json['count'], 2, 'Count was populated correctly.');
    $this
      ->assertEqual(count($decoded_json['data']), 1, 'The correct number of items was listed.');
    $this
      ->assertNotNull($decoded_json['next'], '"Next" property added.');
  }

}

Classes

Namesort descending Description
RestfulSimpleJsonTestCase @file Contains RestfulSimpleJsonTestCase