ReadOnlyStreamWrapperTest.php 4.34 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\system\Tests\File;

/**
6 7 8
 * Tests the read-only stream wrapper write functions.
 *
 * @group File
9 10 11 12
 */
class ReadOnlyStreamWrapperTest extends FileTestBase {

  /**
13
   * A stream wrapper scheme to register for the test.
14
   *
15
   * @var string
16 17
   */
  protected $scheme = 'dummy-readonly';
18 19 20 21 22 23

  /**
   * A fully-qualified stream wrapper class name to register for the test.
   *
   * @var string
   */
24
  protected $classname = 'Drupal\file_test\StreamWrapper\DummyReadOnlyStreamWrapper';
25 26

  /**
27
   * Test read-only specific behavior.
28
   */
29
  function testReadOnlyBehavior() {
30
    // Generate a test file
31
    $filename = $this->randomMachineName();
32 33
    $site_path = $this->container->get('site.path');
    $filepath = $site_path . '/files/' . $filename;
34 35 36 37
    file_put_contents($filepath, $filename);

    // Generate a read-only stream wrapper instance
    $uri = $this->scheme . '://' . $filename;
38
    \Drupal::service('stream_wrapper_manager')->getViaScheme($this->scheme);
39

40 41 42 43 44 45 46 47 48 49 50
    // Attempt to open a file in read/write mode
    $handle = @fopen($uri, 'r+');
    $this->assertFalse($handle, 'Unable to open a file for reading and writing with the read-only stream wrapper.');
    // Attempt to open a file in binary read mode
    $handle = fopen($uri, 'rb');
    $this->assertTrue($handle, 'Able to open a file for reading in binary mode with the read-only stream wrapper.');
    $this->assertTrue(fclose($handle), 'Able to close file opened in binary mode using the read_only stream wrapper.');
    // Attempt to open a file in text read mode
    $handle = fopen($uri, 'rt');
    $this->assertTrue($handle, 'Able to open a file for reading in text mode with the read-only stream wrapper.');
    $this->assertTrue(fclose($handle), 'Able to close file opened in text mode using the read_only stream wrapper.');
51 52 53 54
    // Attempt to open a file in read mode
    $handle = fopen($uri, 'r');
    $this->assertTrue($handle, 'Able to open a file for reading with the read-only stream wrapper.');
    // Attempt to change file permissions
55
    $this->assertFalse(@chmod($uri, 0777), 'Unable to change file permissions when using read-only stream wrapper.');
56 57 58 59 60 61
    // Attempt to acquire an exclusive lock for writing
    $this->assertFalse(@flock($handle, LOCK_EX | LOCK_NB), 'Unable to acquire an exclusive lock using the read-only stream wrapper.');
    // Attempt to obtain a shared lock
    $this->assertTrue(flock($handle, LOCK_SH | LOCK_NB), 'Able to acquire a shared lock using the read-only stream wrapper.');
    // Attempt to release a shared lock
    $this->assertTrue(flock($handle, LOCK_UN | LOCK_NB), 'Able to release a shared lock using the read-only stream wrapper.');
62 63
    // Attempt to truncate the file
    $this->assertFalse(@ftruncate($handle, 0), 'Unable to truncate using the read-only stream wrapper.');
64
    // Attempt to write to the file
65
    $this->assertFalse(@fwrite($handle, $this->randomMachineName()), 'Unable to write to file using the read-only stream wrapper.');
66 67 68
    // Attempt to flush output to the file
    $this->assertFalse(@fflush($handle), 'Unable to flush output to file using the read-only stream wrapper.');
    // Attempt to close the stream.  (Suppress errors, as fclose triggers fflush.)
69
    $this->assertTrue(fclose($handle), 'Able to close file using the read_only stream wrapper.');
70 71 72 73 74 75 76
    // Test the rename() function
    $this->assertFalse(@rename($uri, $this->scheme . '://newname.txt'), 'Unable to rename files using the read-only stream wrapper.');
    // Test the unlink() function
    $this->assertTrue(@drupal_unlink($uri), 'Able to unlink file using read-only stream wrapper.');
    $this->assertTrue(file_exists($filepath), 'Unlink File was not actually deleted.');

    // Test the mkdir() function by attempting to create a directory.
77
    $dirname = $this->randomMachineName();
78
    $dir = $site_path . '/files/' . $dirname;
79 80 81 82 83 84 85 86 87
    $readonlydir = $this->scheme . '://' . $dirname;
    $this->assertFalse(@drupal_mkdir($readonlydir, 0775, 0), 'Unable to create directory with read-only stream wrapper.');
    // Create a temporary directory for testing purposes
    $this->assertTrue(drupal_mkdir($dir), 'Test directory created.');
    // Test the rmdir() function by attempting to remove the directory.
    $this->assertFalse(@drupal_rmdir($readonlydir), 'Unable to delete directory with read-only stream wrapper.');
    // Remove the temporary directory.
    drupal_rmdir($dir);
  }
88

89
}