You are here

function FileTestBase::assertFilePermissions in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/File/FileTestBase.php \Drupal\system\Tests\File\FileTestBase::assertFilePermissions()

Helper function to test the permissions of a file.

Parameters

$filepath: String file path.

$expected_mode: Octal integer like 0664 or 0777.

$message: Optional message.

4 calls to FileTestBase::assertFilePermissions()
UnmanagedCopyTest::testNormal in core/modules/system/src/Tests/File/UnmanagedCopyTest.php
Copy a normal file.
UnmanagedCopyTest::testOverwriteSelf in core/modules/system/src/Tests/File/UnmanagedCopyTest.php
Copy a file onto itself.
UnmanagedMoveTest::testNormal in core/modules/system/src/Tests/File/UnmanagedMoveTest.php
Move a normal file.
UnmanagedSaveDataTest::testFileSaveData in core/modules/system/src/Tests/File/UnmanagedSaveDataTest.php
Test the file_unmanaged_save_data() function.

File

core/modules/system/src/Tests/File/FileTestBase.php, line 59
Contains \Drupal\system\Tests\File\FileTestBase.

Class

FileTestBase
Base class for file tests that adds some additional file specific assertions and helper functions.

Namespace

Drupal\system\Tests\File

Code

function assertFilePermissions($filepath, $expected_mode, $message = NULL) {

  // Clear out PHP's file stat cache to be sure we see the current value.
  clearstatcache(TRUE, $filepath);

  // Mask out all but the last three octets.
  $actual_mode = fileperms($filepath) & 0777;

  // PHP on Windows has limited support for file permissions. Usually each of
  // "user", "group" and "other" use one octal digit (3 bits) to represent the
  // read/write/execute bits. On Windows, chmod() ignores the "group" and
  // "other" bits, and fileperms() returns the "user" bits in all three
  // positions. $expected_mode is updated to reflect this.
  if (substr(PHP_OS, 0, 3) == 'WIN') {

    // Reset the "group" and "other" bits.
    $expected_mode = $expected_mode & 0700;

    // Shift the "user" bits to the "group" and "other" positions also.
    $expected_mode = $expected_mode | $expected_mode >> 3 | $expected_mode >> 6;
  }
  if (!isset($message)) {
    $message = t('Expected file permission to be %expected, actually were %actual.', array(
      '%actual' => decoct($actual_mode),
      '%expected' => decoct($expected_mode),
    ));
  }
  $this
    ->assertEqual($actual_mode, $expected_mode, $message);
}