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