Chris@0: vocabulary = $this->createVocabulary(); Chris@0: // Add a field to the vocabulary. Chris@0: $entity_type = 'taxonomy_term'; Chris@0: $name = 'field_test'; Chris@0: FieldStorageConfig::create([ Chris@0: 'field_name' => $name, Chris@0: 'entity_type' => $entity_type, Chris@0: 'type' => 'image', Chris@0: 'settings' => [ Chris@0: 'uri_scheme' => 'private', Chris@0: ], Chris@0: ])->save(); Chris@0: FieldConfig::create([ Chris@0: 'field_name' => $name, Chris@0: 'entity_type' => $entity_type, Chris@0: 'bundle' => $this->vocabulary->id(), Chris@0: 'settings' => [], Chris@0: ])->save(); Chris@0: entity_get_display($entity_type, $this->vocabulary->id(), 'default') Chris@0: ->setComponent($name, [ Chris@0: 'type' => 'image', Chris@0: 'settings' => [], Chris@0: ]) Chris@0: ->save(); Chris@0: entity_get_form_display($entity_type, $this->vocabulary->id(), 'default') Chris@0: ->setComponent($name, [ Chris@0: 'type' => 'image_image', Chris@0: 'settings' => [], Chris@0: ]) Chris@0: ->save(); Chris@0: } Chris@0: Chris@0: public function testTaxonomyImageAccess() { Chris@0: $user = $this->drupalCreateUser(['administer site configuration', 'administer taxonomy', 'access user profiles']); Chris@0: $this->drupalLogin($user); Chris@0: Chris@0: // Create a term and upload the image. Chris@0: $files = $this->drupalGetTestFiles('image'); Chris@0: $image = array_pop($files); Chris@0: $edit['name[0][value]'] = $this->randomMachineName(); Chris@14: $edit['files[field_test_0]'] = \Drupal::service('file_system')->realpath($image->uri); Chris@0: $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save')); Chris@0: $this->drupalPostForm(NULL, ['field_test[0][alt]' => $this->randomMachineName()], t('Save')); Chris@0: $terms = entity_load_multiple_by_properties('taxonomy_term', ['name' => $edit['name[0][value]']]); Chris@0: $term = reset($terms); Chris@0: $this->assertText(t('Created new term @name.', ['@name' => $term->getName()])); Chris@0: Chris@0: // Create a user that should have access to the file and one that doesn't. Chris@0: $access_user = $this->drupalCreateUser(['access content']); Chris@0: $no_access_user = $this->drupalCreateUser(); Chris@0: $image = File::load($term->field_test->target_id); Chris@0: $this->drupalLogin($access_user); Chris@0: $this->drupalGet(file_create_url($image->getFileUri())); Chris@0: $this->assertResponse(200, 'Private image on term is accessible with right permission'); Chris@0: Chris@0: $this->drupalLogin($no_access_user); Chris@0: $this->drupalGet(file_create_url($image->getFileUri())); Chris@0: $this->assertResponse(403, 'Private image on term not accessible without right permission'); Chris@0: } Chris@0: Chris@0: }