You are here

public function StreamWrapperTest::testPharFile in Drupal 7

Tests that only valid phar files can be used.

File

modules/simpletest/tests/file.test, line 3009
This provides SimpleTests for the core file handling functionality. These include FileValidateTest and FileSaveTest.

Class

StreamWrapperTest
Tests stream wrapper functions.

Code

public function testPharFile() {
  if (!in_array('phar', stream_get_wrappers(), TRUE)) {
    $this
      ->pass('There is no phar stream wrapper registered.');

    // Nothing else in this test is relevant when there's no phar stream
    // wrapper. testPharStreamWrapperRegistration() is sufficient for testing
    // the conditions of when the stream wrapper should or should not be
    // registered.
    return;
  }
  $base = dirname(dirname(__FILE__)) . '/files';

  // Ensure that file operations via the phar:// stream wrapper work for phar
  // files with the .phar extension.
  $this
    ->assertFalse(file_exists("phar://{$base}/phar-1.phar/no-such-file.php"));
  $this
    ->assertTrue(file_exists("phar://{$base}/phar-1.phar/index.php"));
  $file_contents = file_get_contents("phar://{$base}/phar-1.phar/index.php");
  $expected_hash = 'c7e7904ea573c5ebea3ef00bb08c1f86af1a45961fbfbeb1892ff4a98fd73ad5';
  $this
    ->assertIdentical($expected_hash, hash('sha256', $file_contents));

  // Ensure that file operations via the phar:// stream wrapper throw an
  // exception for files without the .phar extension.
  try {
    file_exists("phar://{$base}/image-2.jpg/index.php");
    $this
      ->fail('Expected exception failed to be thrown when accessing an invalid phar file.');
  } catch (Exception $e) {
    $this
      ->assertEqual(get_class($e), 'TYPO3\\PharStreamWrapper\\Exception', 'Expected exception thrown when accessing an invalid phar file.');
  }
}