You are here

public function RestfulCreateEntityTestCase::testFileUploadAccess in RESTful 7.2

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

Test access for file upload.

File

tests/RestfulCreateEntityTestCase.test, line 215
Contains RestfulCreateEntityTestCase

Class

RestfulCreateEntityTestCase
Class RestfulCreateEntityTestCase.

Code

public function testFileUploadAccess() {
  variable_set('restful_file_upload', TRUE);
  variable_set('restful_file_upload_allow_anonymous_user', TRUE);

  // Make sure the user is logged out even when using the UI tests.
  $this
    ->drupalLogout();

  // Test access for anonymous user (allowed).
  $handler = $this
    ->fileResource();
  $this
    ->assertTrue($handler
    ->access(), 'File upload is allowed to anonymous users.');
  variable_set('restful_file_upload_allow_anonymous_user', FALSE);

  // Now that we have a successfully uploaded file, make sure it's the same
  // file that was uploaded.
  $response = drupal_json_decode(restful()
    ->getFormatterManager()
    ->format($handler
    ->process()));
  $original = hash_file('md5', $this->imagePath);
  $file = file_load($response['data'][0][0]['id']);
  $uploaded = hash_file('md5', file_create_url($file->uri));
  $this
    ->assertEqual($original, $uploaded, 'Original and uploaded file are identical.');

  // Test access for anonymous user (denied).
  $handler = $this
    ->fileResource();
  $this
    ->assertFalse($handler
    ->access(), 'File upload is denied to anonymous users.');
  $this
    ->drupalLogin($this->account);

  // Test access for authenticated users (allowed).
  $handler = $this
    ->fileResource();
  $this
    ->assertTrue($handler
    ->access(), 'File upload is allowed to authenticated users.');

  // Test access for authenticated users (denied).
  variable_set('restful_file_upload', FALSE);
  try {
    $this
      ->fileResource();
    $this
      ->fail('The file upload endpoint is not disalbed.');
  } catch (\Drupal\restful\Exception\ServiceUnavailableException $e) {
    $this
      ->pass('The file upload endpoint is disalbed.');
  }
}