You are here

RestfulEntityValidatorTestCase.test in RESTful 7.2

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

Contains RestfulEntityValidatorTestCase.

File

tests/RestfulEntityValidatorTestCase.test
View source
<?php

/**
 * @file
 * Contains RestfulEntityValidatorTestCase.
 */
use Drupal\restful\Exception\BadRequestException;
use Drupal\restful\Http\Request;
use Drupal\restful\Http\RequestInterface;
class RestfulEntityValidatorTestCase extends RestfulCurlBaseTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Entity validator',
      'description' => 'Test integration with entity validator module.',
      'group' => 'RESTful',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp('restful_test', 'entity_validator_example');
  }

  /**
   * Test entity validator.
   */
  public function testEntityValidator() {
    $user1 = $this
      ->drupalCreateUser(array(
      'create article content',
    ));
    $this
      ->drupalLogin($user1);
    $handler = restful()
      ->getResourceManager()
      ->getPlugin('test_articles:1.0');
    $handler
      ->setAccount($user1);
    $parsed_body = array(
      'title' => 'no',
      'body' => 'Text with Drupal',
    );
    try {
      $request = Request::create('', $parsed_body, RequestInterface::METHOD_POST, NULL, FALSE, NULL, array(), array(), array(), $parsed_body);
      $handler
        ->setRequest($request);
      $handler
        ->process();
      $this
        ->fail('Too short title did not cause a "Bad request" error.');
    } catch (BadRequestException $e) {
      $field_errors = $e
        ->getFieldErrors();
      $expected_result = array(
        'title' => array(
          'The title should be at least 3 characters long.',
        ),
      );
      $this
        ->assertEqual($field_errors, $expected_result, 'Correct error message passed in the JSON');
    }
    $parsed_body['title'] = 'yes';
    $request = Request::create('', $parsed_body, RequestInterface::METHOD_POST, NULL, FALSE, NULL, array(), array(), array(), $parsed_body);
    $handler
      ->setRequest($request);
    $result = restful()
      ->getFormatterManager()
      ->negotiateFormatter(NULL, 'json')
      ->prepare($handler
      ->process());
    $this
      ->assertTrue($result['data'][0]['id'], 'Entity with proper title length passed validation and was created.');
  }

}

Classes