Skip to content
Snippets Groups Projects
Select Git revision
  • c4b5c0dce5efef4e5e9d78bfe22df75c57ec6bea
  • 11.x default protected
  • 11.2.x protected
  • 10.5.x protected
  • 10.6.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

TaxonomyImageTest.php

Blame
  • Lee Rowlands's avatar
    Issue #2870459 by naveenvalecha, Lendude, zahord, Jo Fitzgerald, dawehner,...
    Lee Rowlands authored
    Issue #2870459 by naveenvalecha, Lendude, zahord, Jo Fitzgerald, dawehner, larowlan, claudiu.cristea: Convert web tests to browser tests for taxonomy module
    c4b5c0dc
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    TaxonomyImageTest.php 3.25 KiB
    <?php
    
    namespace Drupal\Tests\taxonomy\Functional;
    
    use Drupal\field\Entity\FieldConfig;
    use Drupal\Tests\TestFileCreationTrait;
    use Drupal\user\RoleInterface;
    use Drupal\file\Entity\File;
    use Drupal\field\Entity\FieldStorageConfig;
    
    /**
     * Tests access checks of private image fields.
     *
     * @group taxonomy
     */
    class TaxonomyImageTest extends TaxonomyTestBase {
    
      use TestFileCreationTrait {
        getTestFiles as drupalGetTestFiles;
        compareFiles as drupalCompareFiles;
      }
    
      /**
       * Used taxonomy vocabulary.
       *
       * @var \Drupal\taxonomy\VocabularyInterface
       */
      protected $vocabulary;
    
      /**
       * Modules to enable.
       *
       * @var array
       */
      public static $modules = ['image'];
    
      protected function setUp() {
        parent::setUp();
    
        // Remove access content permission from registered users.
        user_role_revoke_permissions(RoleInterface::AUTHENTICATED_ID, ['access content']);
    
        $this->vocabulary = $this->createVocabulary();
        // Add a field to the vocabulary.
        $entity_type = 'taxonomy_term';
        $name = 'field_test';
        FieldStorageConfig::create([
          'field_name' => $name,
          'entity_type' => $entity_type,
          'type' => 'image',
          'settings' => [
            'uri_scheme' => 'private',
          ],
        ])->save();
        FieldConfig::create([
          'field_name' => $name,
          'entity_type' => $entity_type,
          'bundle' => $this->vocabulary->id(),
          'settings' => [],
        ])->save();
        entity_get_display($entity_type, $this->vocabulary->id(), 'default')
          ->setComponent($name, [
            'type' => 'image',
            'settings' => [],
          ])
          ->save();
        entity_get_form_display($entity_type, $this->vocabulary->id(), 'default')
          ->setComponent($name, [
            'type' => 'image_image',
            'settings' => [],
          ])
          ->save();
      }
    
      public function testTaxonomyImageAccess() {
        $user = $this->drupalCreateUser(['administer site configuration', 'administer taxonomy', 'access user profiles']);
        $this->drupalLogin($user);
    
        // Create a term and upload the image.
        $files = $this->drupalGetTestFiles('image');
        $image = array_pop($files);
        $edit['name[0][value]'] = $this->randomMachineName();
        $edit['files[field_test_0]'] = drupal_realpath($image->uri);
        $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save'));
        $this->drupalPostForm(NULL, ['field_test[0][alt]' => $this->randomMachineName()], t('Save'));
        $terms = entity_load_multiple_by_properties('taxonomy_term', ['name' => $edit['name[0][value]']]);
        $term = reset($terms);
        $this->assertText(t('Created new term @name.', ['@name' => $term->getName()]));
    
        // Create a user that should have access to the file and one that doesn't.
        $access_user = $this->drupalCreateUser(['access content']);
        $no_access_user = $this->drupalCreateUser();
        $image = File::load($term->field_test->target_id);
        $this->drupalLogin($access_user);
        $this->drupalGet(file_create_url($image->getFileUri()));
        $this->assertResponse(200, 'Private image on term is accessible with right permission');
    
        $this->drupalLogin($no_access_user);
        $this->drupalGet(file_create_url($image->getFileUri()));
        $this->assertResponse(403, 'Private image on term not accessible without right permission');
      }
    
    }