diff --git a/file_example/file_example.services.yml b/file_example/file_example.services.yml index 34c5440..03532ec 100644 --- a/file_example/file_example.services.yml +++ b/file_example/file_example.services.yml @@ -13,6 +13,5 @@ services: file_example.stream_wrapper: class: Drupal\file_example\StreamWrapper\FileExampleSessionStreamWrapper - arguments: ['@request_stack'] tags: - { name: stream_wrapper, scheme: session } diff --git a/file_example/src/StreamWrapper/FileExampleSessionStreamWrapper.php b/file_example/src/StreamWrapper/FileExampleSessionStreamWrapper.php index a24f897..a172f53 100644 --- a/file_example/src/StreamWrapper/FileExampleSessionStreamWrapper.php +++ b/file_example/src/StreamWrapper/FileExampleSessionStreamWrapper.php @@ -14,11 +14,6 @@ use Drupal\Core\StreamWrapper\StreamWrapperInterface; use Drupal\Component\Utility\Html; use Drupal\Core\Routing\UrlGeneratorTrait; -// These classes are used to let us access the Session object. -use Drupal\Core\DependencyInjection\ContainerInjectionInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; - - /** * Example stream wrapper class to handle session:// streams. * @@ -75,7 +70,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * * @ingroup file_example */ -class FileExampleSessionStreamWrapper implements StreamWrapperInterface, ContainerInjectionInterface { +class FileExampleSessionStreamWrapper implements StreamWrapperInterface { // We use this trait in order to get nice system-style links // for files stored via our stream wrapper. @@ -157,20 +152,6 @@ class FileExampleSessionStreamWrapper implements StreamWrapperInterface, Contain } /** - * {@inheritdoc} - * - * Since we are putting our data inside of the Session object, we - * need a way to access it. The solution is to use dependency injection - * to get the RequestStack object. - * - * @see https://www.drupal.org/node/2380327 - */ - public static function create(ContainerInterface $container) { - $request_stack = $container->get('request_stack'); - return new static($request_stack); - } - - /** * Get wrapped session manipulators. */ public function getSessionWrapper() { diff --git a/file_example/src/Tests/FileExampleTest.php b/file_example/src/Tests/FileExampleTest.php index ee7c55f..ea16d9d 100644 --- a/file_example/src/Tests/FileExampleTest.php +++ b/file_example/src/Tests/FileExampleTest.php @@ -37,25 +37,6 @@ class FileExampleTest extends WebTestBase { */ public function setUp() { parent::setUp(array('file_example')); - $permissions = [ - 'use file example', - 'read private files', - 'read temporary files', - 'read session files', - ]; - $this->priviledgedUser = $this->drupalCreateUser($permissions); - $this->drupalLogin($this->priviledgedUser); - } - - /** - * {@inheritdoc} - * - * t() no longer returns a string, but is used heavily in this test in contexts where it - * is important that it really return a string, and not TranslatableMarkup. We substitute - * our own implementation that will hopefully be localizable, but will not have this problem. - */ - protected function t($string, $args = [], $options = []) { - return (string) t($string, $args, $options); } /** @@ -66,16 +47,27 @@ class FileExampleTest extends WebTestBase { */ public function testFileExampleBasic() { + // Our test user needs to access some non-standard file types, + // so we bless it accordingly. + $permissions = [ + 'use file example', + 'read private files', + 'read temporary files', + 'read session files', + ]; + $priviledged_user = $this->drupalCreateUser($permissions); + $this->drupalLogin($priviledged_user); + $expected_text = array( - $this->t('Write managed file') => $this->t('Saved managed file'), - $this->t('Write unmanaged file') => $this->t('Saved file as'), - $this->t('Unmanaged using PHP') => $this->t('Saved file as'), + 'Write managed file' => 'Saved managed file', + 'Write unmanaged file' => 'Saved file as', + 'Unmanaged using PHP' => 'Saved file as', ); // For each of the three buttons == three write types. $buttons = array( - $this->t('Write managed file'), - $this->t('Write unmanaged file'), - $this->t('Unmanaged using PHP'), + 'Write managed file', + 'Write unmanaged file', + 'Unmanaged using PHP', ); foreach ($buttons as $button) { // For each scheme supported by Drupal + the session:// wrapper. @@ -88,13 +80,13 @@ class FileExampleTest extends WebTestBase { $edit = array( 'directory_name' => $dirname, ); - $this->drupalPostForm('examples/file_example', $edit, $this->t('Check to see if directory exists')); + $this->drupalPostForm('examples/file_example', $edit, t('Check to see if directory exists')); $this->assertRaw(t('Directory %dirname does not exist', array('%dirname' => $dirname)), 'Verify that directory does not exist.'); - $this->drupalPostForm('examples/file_example', $edit, $this->t('Create directory')); + $this->drupalPostForm('examples/file_example', $edit, t('Create directory')); $this->assertRaw(t('Directory %dirname is ready for use', array('%dirname' => $dirname))); - $this->drupalPostForm('examples/file_example', $edit, $this->t('Check to see if directory exists')); + $this->drupalPostForm('examples/file_example', $edit, t('Check to see if directory exists')); $this->assertRaw(t('Directory %dirname exists', array('%dirname' => $dirname)), 'Verify that directory now does exist.'); // Create a file in the directory we created. @@ -105,11 +97,11 @@ class FileExampleTest extends WebTestBase { $edit = array( 'fileops_file' => $filename, ); - $this->drupalPostForm('examples/file_example', $edit, $this->t('Check to see if file exists')); + $this->drupalPostForm('examples/file_example', $edit, t('Check to see if file exists')); $this->assertRaw(t('The file %filename does not exist', array('%filename' => $filename)), 'Verify that file does not yet exist.'); debug( - $this->t('Processing button=%button, scheme=%scheme, dir=%dirname, file=%filename', + t('Processing button=%button, scheme=%scheme, dir=%dirname, file=%filename', array( '%button' => $button, '%scheme' => $scheme, @@ -152,7 +144,7 @@ class FileExampleTest extends WebTestBase { $edit = array( 'fileops_file' => $filename, ); - $this->drupalPostForm('examples/file_example', $edit, $this->t('Check to see if file exists')); + $this->drupalPostForm('examples/file_example', $edit, t('Check to see if file exists')); $this->assertRaw(t('The file %filename exists', array('%filename' => $filename)), 'Verify that file now exists.'); // Now read the file that got written above and verify that we can use @@ -160,7 +152,7 @@ class FileExampleTest extends WebTestBase { $edit = array( 'fileops_file' => $output_filename, ); - $this->drupalPostForm('examples/file_example', $edit, $this->t('Read the file and store it locally')); + $this->drupalPostForm('examples/file_example', $edit, t('Read the file and store it locally')); $this->assertText(t('The file was read and copied')); @@ -168,16 +160,16 @@ class FileExampleTest extends WebTestBase { 'fileops_file' => $filename, ); - $this->drupalPostForm('examples/file_example', $edit, $this->t('Delete file')); + $this->drupalPostForm('examples/file_example', $edit, t('Delete file')); $this->assertText(t('Successfully deleted')); - $this->drupalPostForm('examples/file_example', $edit, $this->t('Check to see if file exists')); + $this->drupalPostForm('examples/file_example', $edit, t('Check to see if file exists')); $this->assertRaw(t('The file %filename does not exist', array('%filename' => $filename)), 'Verify file has been deleted.'); $edit = array( 'directory_name' => $dirname, ); - $this->drupalPostForm('examples/file_example', $edit, $this->t('Delete directory')); - $this->drupalPostForm('examples/file_example', $edit, $this->t('Check to see if directory exists')); + $this->drupalPostForm('examples/file_example', $edit, t('Delete directory')); + $this->drupalPostForm('examples/file_example', $edit, t('Check to see if directory exists')); $this->assertRaw(t('Directory %dirname does not exist', array('%dirname' => $dirname)), 'Verify that directory does not exist after deletion.'); } }