You are here

public function StreamWrapperTest::testDirectories in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 stream_wrapper_example/tests/src/Kernel/StreamWrapperTest.php \Drupal\Tests\stream_wrapper_example\Kernel\StreamWrapperTest::testDirectories()

Directory creation.

File

modules/stream_wrapper_example/tests/src/Kernel/StreamWrapperTest.php, line 92

Class

StreamWrapperTest
Test of the Session Stream Wrapper Class.

Namespace

Drupal\Tests\stream_wrapper_example\Kernel

Code

public function testDirectories() {
  $this
    ->resetStore();
  $dir_uri = 'session://directory1/directory2';
  $sample_file = 'file.txt';
  $content = "Wrote this as a file?\n";
  $dir = dirname($dir_uri);
  $this
    ->assertFileNotExists($dir, "The outer dir {$dir} should not exist yet.");

  // We don't care about mode, since we don't support it.
  $worked = mkdir($dir);
  $this
    ->assertDirectoryExists($dir, "Directory {$dir} was created.");
  $first_file_content = 'This one is in the first directory.';
  $uri = $dir . "/" . $sample_file;
  $bytes = file_put_contents($uri, $first_file_content);
  $this
    ->assertNotFalse($bytes, "Wrote to {$uri}.\n");
  $this
    ->assertFileExists($uri, "File {$uri} actually exists.");
  $got_back = file_get_contents($uri);
  $got_back = Html::decodeEntities($got_back);
  $this
    ->assertSame($first_file_content, $got_back, 'Data in subdir made round trip.');

  // Now try down down nested.
  $result = mkdir($dir_uri);
  $this
    ->assertTrue($result, 'Nested dir got created.');
  $file_in_sub = $dir_uri . "/" . $sample_file;
  $bytes = file_put_contents($file_in_sub, $content);
  $this
    ->assertNotFalse($bytes, 'File in nested dirs got written to.');
  $got_back = file_get_contents($file_in_sub);
  $got_back = Html::decodeEntities($got_back);
  $this
    ->assertSame($content, $got_back, 'Data in subdir made round trip.');
  $worked = unlink($file_in_sub);
  $this
    ->assertTrue($worked, 'Deleted file in subdir.');
  $this
    ->assertFileNotExists($file_in_sub, 'File in subdir should not exist.');
}