function TaxonomyPrivateFileTestCase::testTaxonomyImageAccess

Tests access to a private file on a taxonomy term entity.

File

modules/taxonomy/taxonomy.test, line 2306

Class

TaxonomyPrivateFileTestCase
Tests appropriate access control to private file fields on a term.

Code

public function testTaxonomyImageAccess() {
  $user = $this->drupalCreateUser(array(
    'administer site configuration',
    'administer taxonomy',
    'access user profiles',
  ));
  $this->drupalLogin($user);
  // Create a term and upload the image.
  $term = $this->createTerm($this->vocabulary);
  $files = $this->drupalGetTestFiles('image');
  $image = array_pop($files);
  $edit['files[field_test_' . LANGUAGE_NONE . '_0]'] = drupal_realpath($image->uri);
  $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save'));
  $term = taxonomy_term_load($term->tid);
  $this->assertText(t('Updated term @name.', array(
    '@name' => $term->name,
  )));
  // Create a user that should have access to the file and one that doesn't.
  $access_user = $this->drupalCreateUser(array(
    'access content',
  ));
  $no_access_user = $this->drupalCreateUser();
  $image = file_load($term->field_test[LANGUAGE_NONE][0]['fid']);
  $image_url = file_create_url($image->uri);
  $this->drupalLogin($access_user);
  $this->drupalGet($image_url);
  $this->assertResponse(200, 'Private image on term is accessible with right permission');
  $this->drupalLogin($no_access_user);
  $this->drupalGet($image_url);
  $this->assertResponse(403, 'Private image on term not accessible without right permission');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.