You are here

RestfulViewModeAndFormatterTestCase.test in RESTful 7.2

Same filename and directory in other branches
  1. 7 tests/RestfulViewModeAndFormatterTestCase.test

Contains RestfulViewModeFormatterTestCase

File

tests/RestfulViewModeAndFormatterTestCase.test
View source
<?php

/**
 * @file
 * Contains RestfulViewModeFormatterTestCase
 */
use Drupal\restful\Http\Request;
class RestfulViewModeAndFormatterTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'View mode and formatter',
      'description' => 'Test the integration with entity view mode and field API formatters.',
      'group' => 'RESTful',
    );
  }
  function setUp() {
    parent::setUp('restful_example', 'restful_test');
  }

  /**
   * Test the view mode integration.
   */
  public function testViewModeIntegration() {
    $resource_manager = restful()
      ->getResourceManager();
    $handler = $resource_manager
      ->getPlugin('articles:1.7');
    $nodes[] = restful_test_create_node_with_tags();
    $nodes[] = restful_test_create_node_with_tags();

    // Make sure to get more than one node.
    $handler
      ->setRequest(Request::create('api/articles/v1.7/' . $nodes[0]->nid . ',' . $nodes[1]->nid));
    $handler
      ->setPath($nodes[0]->nid . ',' . $nodes[1]->nid);
    $result = drupal_json_decode(restful()
      ->getFormatterManager()
      ->format($handler
      ->process(), 'json'));
    $result = $result['data'];

    // Make sure that all the fields were mapped.
    $this
      ->assertNotNull($result[0]['body'], 'Body field is populated.');
    $this
      ->assertTrue($result[0]['tags'], 'Tags field is populated.');
    $this
      ->assertNotNull($result[0]['image'], 'Image field is not NULL.');
  }

  /**
   * Test the field API formatter integration.
   */
  public function testFormatterIntegration() {
    $resource_manager = restful()
      ->getResourceManager();
    $handler = $resource_manager
      ->getPlugin('articles:1.5');

    // Create node.
    $text = 'Some body with long text';
    $settings = array(
      'type' => 'article',
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => $text,
          ),
        ),
      ),
    );
    $node = $this
      ->drupalCreateNode($settings);

    // Field with no formatter.
    $request = Request::create('api/articles/v1.5/' . $node->nid);
    $handler
      ->setRequest($request);
    $handler
      ->setPath($node->nid);
    $result = drupal_json_decode(restful()
      ->getFormatterManager()
      ->format($handler
      ->process(), 'json'));
    $result = $result['data'];
    $this
      ->assertEqual(trim(strip_tags($result[0]['body'])), $text, 'Raw value passed without a formatter.');

    // Add formatter settings.
    $field_definitions = $handler
      ->getFieldDefinitions();
    $display = array(
      'type' => 'text_summary_or_trimmed',
      'settings' => array(
        'trim_length' => 10,
      ),
    );

    /* @var \Drupal\restful\Plugin\resource\Field\ResourceFieldEntityText $body */
    $body = $field_definitions
      ->get('body');
    $body
      ->setFormatter($display);
    $field_definitions
      ->set('body', $body);
    $handler
      ->setFieldDefinitions($field_definitions);
    $resource_manager
      ->clearPluginCache('articles:1.5');
    $handler
      ->setRequest($request);
    $handler
      ->setPath($node->nid);
    $result = drupal_json_decode(restful()
      ->getFormatterManager()
      ->format($handler
      ->process(), 'json'));
    $result = $result['data'];

    // Core's trim formatter also inclues the opening <p> tag in the calculation
    // of number of chars.
    $this
      ->assertEqual($result[0]['body'], '<p>Some bo</p>', 'Value trimmed by formatter.');
  }

}