3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\Tests\TestFileCreationTrait;
7 use Drupal\user\RoleInterface;
8 use Drupal\file\Entity\File;
9 use Drupal\field\Entity\FieldStorageConfig;
12 * Tests access checks of private image fields.
16 class TaxonomyImageTest extends TaxonomyTestBase {
18 use TestFileCreationTrait {
19 getTestFiles as drupalGetTestFiles;
20 compareFiles as drupalCompareFiles;
24 * Used taxonomy vocabulary.
26 * @var \Drupal\taxonomy\VocabularyInterface
28 protected $vocabulary;
35 public static $modules = ['image'];
37 protected function setUp() {
40 // Remove access content permission from registered users.
41 user_role_revoke_permissions(RoleInterface::AUTHENTICATED_ID, ['access content']);
43 $this->vocabulary = $this->createVocabulary();
44 // Add a field to the vocabulary.
45 $entity_type = 'taxonomy_term';
47 FieldStorageConfig::create([
48 'field_name' => $name,
49 'entity_type' => $entity_type,
52 'uri_scheme' => 'private',
56 'field_name' => $name,
57 'entity_type' => $entity_type,
58 'bundle' => $this->vocabulary->id(),
61 entity_get_display($entity_type, $this->vocabulary->id(), 'default')
62 ->setComponent($name, [
67 entity_get_form_display($entity_type, $this->vocabulary->id(), 'default')
68 ->setComponent($name, [
69 'type' => 'image_image',
75 public function testTaxonomyImageAccess() {
76 $user = $this->drupalCreateUser(['administer site configuration', 'administer taxonomy', 'access user profiles']);
77 $this->drupalLogin($user);
79 // Create a term and upload the image.
80 $files = $this->drupalGetTestFiles('image');
81 $image = array_pop($files);
82 $edit['name[0][value]'] = $this->randomMachineName();
83 $edit['files[field_test_0]'] = drupal_realpath($image->uri);
84 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save'));
85 $this->drupalPostForm(NULL, ['field_test[0][alt]' => $this->randomMachineName()], t('Save'));
86 $terms = entity_load_multiple_by_properties('taxonomy_term', ['name' => $edit['name[0][value]']]);
87 $term = reset($terms);
88 $this->assertText(t('Created new term @name.', ['@name' => $term->getName()]));
90 // Create a user that should have access to the file and one that doesn't.
91 $access_user = $this->drupalCreateUser(['access content']);
92 $no_access_user = $this->drupalCreateUser();
93 $image = File::load($term->field_test->target_id);
94 $this->drupalLogin($access_user);
95 $this->drupalGet(file_create_url($image->getFileUri()));
96 $this->assertResponse(200, 'Private image on term is accessible with right permission');
98 $this->drupalLogin($no_access_user);
99 $this->drupalGet(file_create_url($image->getFileUri()));
100 $this->assertResponse(403, 'Private image on term not accessible without right permission');