You are here

public function RestfulEntityValidatorTestCase::testEntityValidator in RESTful 7.2

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

Test entity validator.

File

tests/RestfulEntityValidatorTestCase.test, line 35
Contains RestfulEntityValidatorTestCase.

Class

RestfulEntityValidatorTestCase

Code

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.');
}