Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / file / tests / src / Kernel / AccessTest.php
index cfd8e56838b9ef4ed22e6102822baad5c466d9f6..d875c1f6640bd466715fdb7f4657cedab29ce11f 100644 (file)
@@ -93,7 +93,7 @@ class AccessTest extends KernelTestBase {
     \Drupal::currentUser()->setAccount($this->user1);
     /** @var \Drupal\file\FileInterface $file */
     $file = File::create([
-      'uri' => 'public://test.png'
+      'uri' => 'public://test.png',
     ]);
     // While creating a file entity access will be allowed for create-only
     // fields.
@@ -123,4 +123,28 @@ class AccessTest extends KernelTestBase {
     $this->assertFalse($this->file->access('create'));
   }
 
+  /**
+   * Tests cacheability metadata.
+   */
+  public function testFileCacheability() {
+    $file = File::create([
+      'filename' => 'green-scarf',
+      'uri' => 'private://green-scarf',
+      'filemime' => 'text/plain',
+      'status' => FILE_STATUS_PERMANENT,
+    ]);
+    $file->save();
+    \Drupal::service('session')->set('anonymous_allowed_file_ids', [$file->id() => $file->id()]);
+
+    $account = User::getAnonymousUser();
+    $file->setOwnerId($account->id())->save();
+    $this->assertSame(['session', 'user'], $file->access('view', $account, TRUE)->getCacheContexts());
+    $this->assertSame(['session', 'user'], $file->access('download', $account, TRUE)->getCacheContexts());
+
+    $account = $this->user1;
+    $file->setOwnerId($account->id())->save();
+    $this->assertSame(['user'], $file->access('view', $account, TRUE)->getCacheContexts());
+    $this->assertSame(['user'], $file->access('download', $account, TRUE)->getCacheContexts());
+  }
+
 }