View source
<?php
namespace Drupal\Tests\stream_wrapper_example\Kernel;
use Drupal\Component\Utility\Html;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\stream_wrapper_example\Traits\MockSessionTrait;
class StreamWrapperTest extends KernelTestBase {
use MockSessionTrait;
public static $modules = [
'stream_wrapper_example',
'file',
'system',
];
public function setUp() {
parent::setUp();
$this->container
->set('request_stack', $this
->createSessionMock());
}
public function testSchemeRegistered() {
$have_session_scheme = $this->container
->get('stream_wrapper_manager')
->isValidScheme('session');
$this
->assertTrue($have_session_scheme, "System knows about our stream wrapper");
}
public function testReadWrite() {
$this
->resetStore();
$uri = 'session://drupal.txt';
$this
->assertFileNotExists($uri, "File {$uri} should not exist yet.");
$handle = fopen($uri, 'wb');
$this
->assertNotEmpty($handle, "Handle for {$uri} should be non-empty.");
$buffer = "Ain't seen nothin' yet!\n";
$old = error_reporting(E_ERROR);
$bytes_written = @fwrite($handle, $buffer);
error_reporting($old);
$this
->assertNotFalse($bytes_written, "Write to {$uri} succeeded.");
$result = fclose($handle);
$this
->assertNotFalse($result, "Closed {$uri}.");
$this
->assertFileExists($uri, "File {$uri} should now exist.");
$this
->assertDirectoryNotExists($uri, "{$uri} is not a directory.");
$this
->assertTrue(is_file($uri), "{$uri} is a file.");
$contents = file_get_contents($uri);
$contents = Html::decodeEntities($contents);
$this
->assertEquals($buffer, $contents, "Data for {$uri} should make the round trip.");
}
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.");
$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.');
$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.');
}
protected function getCurrentStore() {
$handle = $this
->getSessionHelper();
return $handle
->getPath('');
}
protected function resetStore() {
$handle = $this
->getSessionHelper();
$handle
->cleanUpStore();
}
}