3 namespace Drupal\taxonomy\Tests;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\user\RoleInterface;
7 use Drupal\file\Entity\File;
8 use Drupal\field\Entity\FieldStorageConfig;
11 * Tests access checks of private image fields.
15 class TaxonomyImageTest extends TaxonomyTestBase {
18 * Used taxonomy vocabulary.
20 * @var \Drupal\taxonomy\VocabularyInterface
22 protected $vocabulary;
29 public static $modules = ['image'];
31 protected function setUp() {
34 // Remove access content permission from registered users.
35 user_role_revoke_permissions(RoleInterface::AUTHENTICATED_ID, ['access content']);
37 $this->vocabulary = $this->createVocabulary();
38 // Add a field to the vocabulary.
39 $entity_type = 'taxonomy_term';
41 FieldStorageConfig::create([
42 'field_name' => $name,
43 'entity_type' => $entity_type,
46 'uri_scheme' => 'private',
50 'field_name' => $name,
51 'entity_type' => $entity_type,
52 'bundle' => $this->vocabulary->id(),
55 entity_get_display($entity_type, $this->vocabulary->id(), 'default')
56 ->setComponent($name, [
61 entity_get_form_display($entity_type, $this->vocabulary->id(), 'default')
62 ->setComponent($name, [
63 'type' => 'image_image',
69 public function testTaxonomyImageAccess() {
70 $user = $this->drupalCreateUser(['administer site configuration', 'administer taxonomy', 'access user profiles']);
71 $this->drupalLogin($user);
73 // Create a term and upload the image.
74 $files = $this->drupalGetTestFiles('image');
75 $image = array_pop($files);
76 $edit['name[0][value]'] = $this->randomMachineName();
77 $edit['files[field_test_0]'] = drupal_realpath($image->uri);
78 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save'));
79 $this->drupalPostForm(NULL, ['field_test[0][alt]' => $this->randomMachineName()], t('Save'));
80 $terms = entity_load_multiple_by_properties('taxonomy_term', ['name' => $edit['name[0][value]']]);
81 $term = reset($terms);
82 $this->assertText(t('Created new term @name.', ['@name' => $term->getName()]));
84 // Create a user that should have access to the file and one that doesn't.
85 $access_user = $this->drupalCreateUser(['access content']);
86 $no_access_user = $this->drupalCreateUser();
87 $image = File::load($term->field_test->target_id);
88 $this->drupalLogin($access_user);
89 $this->drupalGet(file_create_url($image->getFileUri()));
90 $this->assertResponse(200, 'Private image on term is accessible with right permission');
92 $this->drupalLogin($no_access_user);
93 $this->drupalGet(file_create_url($image->getFileUri()));
94 $this->assertResponse(403, 'Private image on term not accessible without right permission');