Problem/Motivation

We want to deprecated procedural functions in file.inc and replace with methods on FileSystem or StreamWrapperManager.

file_default_scheme() just wraps \Drupal::config('system.file')->get('default_scheme'), and porting it over just creates another API we need to maintain.

Also, we are calling file_default_scheme() in a load of tests, where we really only need 'public://'

Proposed resolution

  1. Replace usages of file_default_scheme() with \Drupal::config('system.file')->get('default_scheme')
  2. Replace usages of file_default_scheme() in tests with 'public://'

Remaining tasks

None.

User interface changes

None.

API changes

file_default_scheme() is deprecated.

Data model changes

None.

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kim.pepper created an issue. See original summary.

kim.pepper’s picture

Status: Active » Needs review
FileSize
21.03 KB

Initial patch.

kim.pepper’s picture

Fixes copy/paste error.

Berdir’s picture

Status: Needs review » Needs work
+++ b/core/modules/editor/tests/src/Functional/EditorLoadingTest.php
@@ -114,7 +114,7 @@ public function testLoading() {
       'image_upload' => [
         'status' => FALSE,
-        'scheme' => file_default_scheme(),
+        'scheme' => $this->config('system.file')->get('default_scheme'),
         'directory' => 'inline-images',
         'max_size' => '',

As discussed in slack, I would actually suggest to update all usages in tests that are not specifically testing the configurable default to just hardcode public, we *know* that's the value it has and we have dozens if not hundreds of hardcoded public:// references already in tests. That will also better show how common it really is to use that configuration (and hopefully that it is indeed OK to just access the configuration).

And we also need a legacy test I think.

kim.pepper’s picture

Status: Needs work » Needs review
FileSize
10.45 KB
20.91 KB

Thanks for the review @Berdir.

  1. Replaces use of config in tests with 'public'
  2. Adds a legacy test
Berdir’s picture

  1. +++ b/core/modules/file/src/Tests/FileManagedTestBase.php
    @@ -189,7 +189,7 @@ public function createUri($filepath = NULL, $contents = NULL, $scheme = NULL) {
         if (!isset($scheme)) {
    -      $scheme = file_default_scheme();
    +      $scheme = 'public';
         }
    

    this is the old deprecated base test, not sure if we should touch that one at all..

  2. +++ b/core/modules/file/tests/src/Functional/FileManagedTestBase.php
    @@ -184,7 +184,7 @@ public function createUri($filepath = NULL, $contents = NULL, $scheme = NULL) {
         }
         if (!isset($scheme)) {
    -      $scheme = file_default_scheme();
    +      $scheme = 'public';
         }
    

    this *could* maybe be a problem as it is a base class, so subclasses might rely on it, we'll see.

  3. +++ b/core/modules/image/src/Entity/ImageStyle.php
    @@ -549,7 +549,7 @@ protected function fileUriTarget($uri) {
        */
       protected function fileDefaultScheme() {
    -    return file_default_scheme();
    +    return \Drupal::config('system.file')->get('default_scheme');
       }
    

    the docblock here still mentiones the function, with todos to convert it to a service.

    Also, we could actually deprecate this function instead of updating it, call \Drupal::config() directly and mock it through the container?

  4. +++ b/core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php
    @@ -92,12 +93,12 @@ public function testFileCheckDirectoryHandling() {
         // Remove .htaccess file to then test that it gets re-created.
    -    @drupal_unlink(file_default_scheme() . '://.htaccess');
    -    $this->assertFalse(is_file(file_default_scheme() . '://.htaccess'), 'Successfully removed the .htaccess file in the files directory.', 'File');
    +    @drupal_unlink($default_scheme . '://.htaccess');
    +    $this->assertFalse(is_file($default_scheme . '://.htaccess'), 'Successfully removed the .htaccess file in the files directory.', 'File');
    

    aww, this will conflict with the RTBC drupal_unlink() issue, so will hold back on RTBC until that one is in. Not sure if updating that already now will merge it easier to rebase or not.

  5. +++ b/core/tests/Drupal/KernelTests/Core/File/FileDeleteTest.php
    @@ -28,7 +28,7 @@ public function testNormal() {
         // Try to delete a non-existing file
    -    $this->assertTrue(\Drupal::service('file_system')->delete(file_default_scheme() . '/' . $this->randomMachineName()), 'Returns true when deleting a non-existent file.');
    +    $this->assertTrue(\Drupal::service('file_system')->delete('public/' . $this->randomMachineName()), 'Returns true when deleting a non-existent file.');
       }
    

    this looks completely bogus, which it was before as well, but I don't think this is what it should do.

kim.pepper’s picture

  1. Fixed
  2. Didn't trigger any test fails, but I've rolled back anyway
  3. I think this is pretty tricky to try and mock \Drupal::config() here. ImageStyles don't allow us to inject anything. I've updated the comment to remove references to file_default_scheme() and replacing it with a service.
  4. Yeah, that's fine.
  5. Not sure what else to do here except replace 'public/' with 'public://'
kim.pepper’s picture

kim.pepper’s picture

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

> I think this is pretty tricky to try and mock \Drupal::config() here.

Should be possible by providing a mock through a container like other unit tests, but yeah, this is out of scope here I think.

> Not sure what else to do here except replace 'public/' with 'public://'

Yeah, that's what I meant. It does test a non-existing file, but I think that's what it should have been and feels like a more realistic test than trying to delete DRUPAL_ROOT/public/...

Looks good to me I think, lets see what the committers think about this suggestion.

Maybe the issue summary could be improved a bit and e.g. point out that we removed the configurability from a lot of tests where we just know that it is public://. I quick search shows around 450 existing usages of "'public://" in tests, so these are already the exception.

kim.pepper’s picture

Issue summary: View changes

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 8: 3049021-8.patch, failed testing. View results

kim.pepper’s picture

Status: Needs work » Needs review
FileSize
20.57 KB

Not exactly sure what the fail was, so re-rolling #8.

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Random fail maybe, saw a few random build successful problems recently.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 13: 3049021-8.patch, failed testing. View results

kim.pepper’s picture

Status: Needs work » Reviewed & tested by the community

Random test fails.

larowlan’s picture

issue credits

larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed 31a5cba and pushed to 8.8.x. Thanks!

published change record

  • larowlan committed 31a5cba on 8.8.x
    Issue #3049021 by kim.pepper, Berdir: Deprecate file_default_scheme()...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Pancho’s picture

FileSize
869 bytes

Think we need a followup here:

  @trigger_error('file_default_scheme() is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Use \Drupal::config(\'system.file\')->get(\'default_scheme\') instead. See https://www.drupal.org/project/paragraphs/issues/3049030', E_USER_DEPRECATED);

It's supposed to be: https://www.drupal.org/node/3049030 instead.

kim.pepper’s picture

@Pancho Thanks for picking this up. However, this issue is closed. I created #3058839: Incorrect @see link in file_default_scheme() deprecation if you want to post your patch there.