Verified Commit 2adeb979 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3048434 by claudiu.cristea, Lendude: Convert FileManagedAccessTest into a Kernel test

parent 9a11de65
Loading
Loading
Loading
Loading
+0 −83
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\file\Functional;

use Drupal\file\Entity\File;
use Drupal\user\Entity\Role;

/**
 * Tests access to managed files.
 *
 * @group file
 */
class FileManagedAccessTest extends FileManagedTestBase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Give anonymous users permission to access content, so they can view and
    // download public files.
    $anonymous_role = Role::load(Role::ANONYMOUS_ID);
    $anonymous_role->grantPermission('access content');
    $anonymous_role->save();
  }

  /**
   * Tests if public file is always accessible.
   */
  public function testFileAccess() {
    // Create a new file entity.
    $file = File::create([
      'uid' => 1,
      'filename' => 'drupal.txt',
      'uri' => 'public://drupal.txt',
      'filemime' => 'text/plain',
      'status' => FILE_STATUS_PERMANENT,
    ]);
    file_put_contents($file->getFileUri(), 'hello world');

    // Save it, inserting a new record.
    $file->save();

    // Create authenticated user to check file access.
    $account = $this->createUser(['access site reports', 'access content']);

    $this->assertTrue($file->access('view', $account), 'Public file is viewable to authenticated user');
    $this->assertTrue($file->access('download', $account), 'Public file is downloadable to authenticated user');

    // Create anonymous user to check file access.
    $account = $this->createUser()->getAnonymousUser();

    $this->assertTrue($file->access('view', $account), 'Public file is viewable to anonymous user');
    $this->assertTrue($file->access('download', $account), 'Public file is downloadable to anonymous user');

    // Create a new file entity.
    $file = File::create([
      'uid' => 1,
      'filename' => 'drupal.txt',
      'uri' => 'private://drupal.txt',
      'filemime' => 'text/plain',
      'status' => FILE_STATUS_PERMANENT,
    ]);
    file_put_contents($file->getFileUri(), 'hello world');

    // Save it, inserting a new record.
    $file->save();

    // Create authenticated user to check file access.
    $account = $this->createUser(['access site reports', 'access content']);

    $this->assertFalse($file->access('view', $account), 'Private file is not viewable to authenticated user');
    $this->assertFalse($file->access('download', $account), 'Private file is not downloadable to authenticated user');

    // Create anonymous user to check file access.
    $account = $this->createUser()->getAnonymousUser();

    $this->assertFalse($file->access('view', $account), 'Private file is not viewable to anonymous user');
    $this->assertFalse($file->access('download', $account), 'Private file is not downloadable to anonymous user');
  }

}
+77 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\file\Kernel;

use Drupal\Core\Session\AccountInterface;
use Drupal\file\Entity\File;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Entity\User;

/**
 * Tests access to managed files.
 *
 * @group file
 */
class FileManagedAccessTest extends KernelTestBase {

  use UserCreationTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'file',
    'system',
    'user',
  ];

  /**
   * Tests if public file is always accessible.
   */
  public function testFileAccess() {
    $this->installSchema('system', ['sequences']);
    $this->installEntitySchema('user');
    $this->installEntitySchema('file');
    $this->installSchema('file', ['file_usage']);
    $this->installConfig('user');

    $anonymous = User::create(['uid' => 0, 'name' => '']);
    $anonymous->save();
    user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, ['access content']);

    // Create an authenticated user to check file access.
    $account = $this->createUser(['access site reports', 'access content'], NULL, FALSE, ['uid' => 2]);

    // Create a new file entity in the public:// stream wrapper.
    $file_public = File::create([
      'uid' => 1,
      'filename' => 'drupal.txt',
      'uri' => 'public://drupal.txt',
      'status' => FILE_STATUS_PERMANENT,
    ]);
    $file_public->save();

    $this->assertTrue($file_public->access('view', $account));
    $this->assertTrue($file_public->access('download', $account));

    $this->assertTrue($file_public->access('view', $anonymous));
    $this->assertTrue($file_public->access('download', $anonymous));

    // Create a new file entity in the private:// stream wrapper.
    $file_private = File::create([
      'uid' => 1,
      'filename' => 'drupal.txt',
      'uri' => 'private://drupal.txt',
      'status' => FILE_STATUS_PERMANENT,
    ]);
    $file_private->save();

    $this->assertFalse($file_private->access('view', $account));
    $this->assertFalse($file_private->access('download', $account));

    $this->assertFalse($file_private->access('view', $anonymous));
    $this->assertFalse($file_private->access('download', $anonymous));
  }

}