View source  
  <?php
namespace Drupal\Tests\commerce_file\Kernel;
use Drupal\commerce_license\Entity\License;
use Drupal\commerce_product\Entity\Product;
class FileAccessTest extends CommerceFileKernelTestBase {
  
  public function testFileAccess() {
    
    $account = $this
      ->createUser([], [
      'view commerce_product',
      'view own commerce_license',
    ]);
    
    $product = Product::create([
      'type' => 'default',
      'title' => 'My license file',
      'variations' => [
        $this->variation,
      ],
      'stores' => [
        $this->store,
      ],
    ]);
    $product
      ->save();
    $this
      ->assertFalse($this->file
      ->access('view', $account));
    $this
      ->assertFalse($this->file
      ->access('download', $account));
    $another_user = $this
      ->createUser([], [
      'bypass license control',
      'view commerce_product',
    ]);
    $this
      ->assertTrue($this->file
      ->access('view', $another_user));
    $this
      ->assertTrue($this->file
      ->access('download', $another_user));
    $another_user = $this
      ->createUser([], [
      'administer commerce_license',
      'view commerce_product',
    ]);
    $this
      ->assertTrue($this->file
      ->access('view', $another_user));
    $this
      ->assertTrue($this->file
      ->access('download', $another_user));
    $file_access_handler = \Drupal::entityTypeManager()
      ->getAccessControlHandler('file');
    $file_access_handler
      ->resetCache();
    $license = License::create([
      'type' => 'commerce_file',
      'state' => 'active',
      'uid' => $account
        ->id(),
      'product_variation' => $this->variation,
      'file_download_limit' => 2,
      'expiration_type' => [
        'target_plugin_id' => 'unlimited',
        'target_plugin_configuration' => [],
      ],
    ]);
    $license
      ->save();
    $this
      ->assertTrue($this->file
      ->access('view', $account));
    $this
      ->assertTrue($this->file
      ->access('download', $account));
    
    $download_logger = $this->container
      ->get('commerce_file.download_logger');
    $download_logger
      ->log($license, $this->file);
    $file_access_handler
      ->resetCache();
    $this
      ->assertTrue($this->file
      ->access('view', $account));
    $this
      ->assertTrue($this->file
      ->access('download', $account));
    
    $download_logger
      ->log($license, $this->file);
    $file_access_handler
      ->resetCache();
    $this
      ->assertFalse($this->file
      ->access('view', $account));
    $this
      ->assertFalse($this->file
      ->access('download', $account));
  }
}