Backup of db before drupal security update
[yaffs-website] / web / core / modules / taxonomy / src / Tests / TaxonomyImageTest.php
1 <?php
2
3 namespace Drupal\taxonomy\Tests;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\user\RoleInterface;
7 use Drupal\file\Entity\File;
8 use Drupal\field\Entity\FieldStorageConfig;
9
10 /**
11  * Tests access checks of private image fields.
12  *
13  * @group taxonomy
14  */
15 class TaxonomyImageTest extends TaxonomyTestBase {
16
17   /**
18    * Used taxonomy vocabulary.
19    *
20    * @var \Drupal\taxonomy\VocabularyInterface
21    */
22   protected $vocabulary;
23
24   /**
25    * Modules to enable.
26    *
27    * @var array
28    */
29   public static $modules = ['image'];
30
31   protected function setUp() {
32     parent::setUp();
33
34     // Remove access content permission from registered users.
35     user_role_revoke_permissions(RoleInterface::AUTHENTICATED_ID, ['access content']);
36
37     $this->vocabulary = $this->createVocabulary();
38     // Add a field to the vocabulary.
39     $entity_type = 'taxonomy_term';
40     $name = 'field_test';
41     FieldStorageConfig::create([
42       'field_name' => $name,
43       'entity_type' => $entity_type,
44       'type' => 'image',
45       'settings' => [
46         'uri_scheme' => 'private',
47       ],
48     ])->save();
49     FieldConfig::create([
50       'field_name' => $name,
51       'entity_type' => $entity_type,
52       'bundle' => $this->vocabulary->id(),
53       'settings' => [],
54     ])->save();
55     entity_get_display($entity_type, $this->vocabulary->id(), 'default')
56       ->setComponent($name, [
57         'type' => 'image',
58         'settings' => [],
59       ])
60       ->save();
61     entity_get_form_display($entity_type, $this->vocabulary->id(), 'default')
62       ->setComponent($name, [
63         'type' => 'image_image',
64         'settings' => [],
65       ])
66       ->save();
67   }
68
69   public function testTaxonomyImageAccess() {
70     $user = $this->drupalCreateUser(['administer site configuration', 'administer taxonomy', 'access user profiles']);
71     $this->drupalLogin($user);
72
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()]));
83
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');
91
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');
95   }
96
97 }