PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/src/Tests/File/ReadOnlyStreamWrapperTest.php

https://gitlab.com/geeta7/drupal
PHP | 93 lines | 42 code | 9 blank | 42 comment | 0 complexity | abd986208548b1c3627a14d7b52314c2 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\system\Tests\File\ReadOnlyStreamWrapperTest.
  5. */
  6. namespace Drupal\system\Tests\File;
  7. /**
  8. * Tests the read-only stream wrapper write functions.
  9. *
  10. * @group File
  11. */
  12. class ReadOnlyStreamWrapperTest extends FileTestBase {
  13. /**
  14. * A stream wrapper scheme to register for the test.
  15. *
  16. * @var string
  17. */
  18. protected $scheme = 'dummy-readonly';
  19. /**
  20. * A fully-qualified stream wrapper class name to register for the test.
  21. *
  22. * @var string
  23. */
  24. protected $classname = 'Drupal\file_test\StreamWrapper\DummyReadOnlyStreamWrapper';
  25. /**
  26. * Test read-only specific behavior.
  27. */
  28. function testReadOnlyBehavior() {
  29. // Generate a test file
  30. $filename = $this->randomMachineName();
  31. $site_path = $this->container->get('site.path');
  32. $filepath = $site_path . '/files/' . $filename;
  33. file_put_contents($filepath, $filename);
  34. // Generate a read-only stream wrapper instance
  35. $uri = $this->scheme . '://' . $filename;
  36. \Drupal::service('stream_wrapper_manager')->getViaScheme($this->scheme);
  37. // Attempt to open a file in read/write mode
  38. $handle = @fopen($uri, 'r+');
  39. $this->assertFalse($handle, 'Unable to open a file for reading and writing with the read-only stream wrapper.');
  40. // Attempt to open a file in binary read mode
  41. $handle = fopen($uri, 'rb');
  42. $this->assertTrue($handle, 'Able to open a file for reading in binary mode with the read-only stream wrapper.');
  43. $this->assertTrue(fclose($handle), 'Able to close file opened in binary mode using the read_only stream wrapper.');
  44. // Attempt to open a file in text read mode
  45. $handle = fopen($uri, 'rt');
  46. $this->assertTrue($handle, 'Able to open a file for reading in text mode with the read-only stream wrapper.');
  47. $this->assertTrue(fclose($handle), 'Able to close file opened in text mode using the read_only stream wrapper.');
  48. // Attempt to open a file in read mode
  49. $handle = fopen($uri, 'r');
  50. $this->assertTrue($handle, 'Able to open a file for reading with the read-only stream wrapper.');
  51. // Attempt to change file permissions
  52. $this->assertFalse(@chmod($uri, 0777), 'Unable to change file permissions when using read-only stream wrapper.');
  53. // Attempt to acquire an exclusive lock for writing
  54. $this->assertFalse(@flock($handle, LOCK_EX | LOCK_NB), 'Unable to acquire an exclusive lock using the read-only stream wrapper.');
  55. // Attempt to obtain a shared lock
  56. $this->assertTrue(flock($handle, LOCK_SH | LOCK_NB), 'Able to acquire a shared lock using the read-only stream wrapper.');
  57. // Attempt to release a shared lock
  58. $this->assertTrue(flock($handle, LOCK_UN | LOCK_NB), 'Able to release a shared lock using the read-only stream wrapper.');
  59. // Attempt to truncate the file
  60. $this->assertFalse(@ftruncate($handle, 0), 'Unable to truncate using the read-only stream wrapper.');
  61. // Attempt to write to the file
  62. $this->assertFalse(@fwrite($handle, $this->randomMachineName()), 'Unable to write to file using the read-only stream wrapper.');
  63. // Attempt to flush output to the file
  64. $this->assertFalse(@fflush($handle), 'Unable to flush output to file using the read-only stream wrapper.');
  65. // Attempt to close the stream. (Suppress errors, as fclose triggers fflush.)
  66. $this->assertTrue(fclose($handle), 'Able to close file using the read_only stream wrapper.');
  67. // Test the rename() function
  68. $this->assertFalse(@rename($uri, $this->scheme . '://newname.txt'), 'Unable to rename files using the read-only stream wrapper.');
  69. // Test the unlink() function
  70. $this->assertTrue(@drupal_unlink($uri), 'Able to unlink file using read-only stream wrapper.');
  71. $this->assertTrue(file_exists($filepath), 'Unlink File was not actually deleted.');
  72. // Test the mkdir() function by attempting to create a directory.
  73. $dirname = $this->randomMachineName();
  74. $dir = $site_path . '/files/' . $dirname;
  75. $readonlydir = $this->scheme . '://' . $dirname;
  76. $this->assertFalse(@drupal_mkdir($readonlydir, 0775, 0), 'Unable to create directory with read-only stream wrapper.');
  77. // Create a temporary directory for testing purposes
  78. $this->assertTrue(drupal_mkdir($dir), 'Test directory created.');
  79. // Test the rmdir() function by attempting to remove the directory.
  80. $this->assertFalse(@drupal_rmdir($readonlydir), 'Unable to delete directory with read-only stream wrapper.');
  81. // Remove the temporary directory.
  82. drupal_rmdir($dir);
  83. }
  84. }