Problem/Motivation

Deprecate the functions: drupal_valid_test_ua() and drupal_generate_test_ua(). As we want to kill all includes.

Proposed resolution

Deprecate the functions: drupal_valid_test_ua() and drupal_generate_test_ua(). Move the functionality to the class Drupal\Core\Test\UserAgent.

Remaining tasks

TBD

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

TBD

CommentFileSizeAuthor
#160 reroll_diff_153-160.txt20.99 KBadityasingh
#160 3038513-160.patch43.23 KBadityasingh
#153 3038513-153-do-not-commit.patch44.99 KBandypost
#153 interdiff.txt1.8 KBandypost
#150 3038513-150-do-not-commit.patch44.75 KBandypost
#147 3038513-147.patch45.56 KBandypost
#147 interdiff.txt2.16 KBandypost
#146 3038513-146.patch46.58 KBandypost
#146 interdiff.txt1.59 KBandypost
#145 3038513-145.patch45.77 KBandypost
#145 interdiff.txt10.6 KBandypost
#144 3038513-144.patch47.1 KBandypost
#144 interdiff.txt4.57 KBandypost
#142 interdiff-140-142.txt560 bytesjungle
#142 3038513-142.patch47.53 KBjungle
#140 interdiff_132-140.txt2.12 KBraunak.singh
#140 3038513-140.patch47.53 KBraunak.singh
#136 interdiff_132-136.txt777 bytesraunak.singh
#136 3038513-136.patch39.51 KBraunak.singh
#132 interdiff.3038513.126-132.txt763 byteslongwave
#132 3038513-132.patch47.3 KBlongwave
#129 interdiff-126-129.txt1.83 KBhardik_patel_12
#129 3038513-129.patch46.93 KBhardik_patel_12
#126 diff_120-126.txt4.44 KBdeepak goyal
#126 3038513-126.patch47.04 KBdeepak goyal
#122 diff_114-120.txt5.22 KBdeepak goyal
#122 3038513-120.patch47.44 KBdeepak goyal
#120 interdiff-114-120.txt4.75 KBnaresh_bavaskar
#120 3038513-120.patch47.21 KBnaresh_bavaskar
#115 interdiff_113-114.txt2 KBmohrerao
#115 3038513-114.patch45.99 KBmohrerao
#113 interdiff-3038513-111-113.txt1.24 KBvoleger
#113 3038513-113.patch45.74 KBvoleger
#111 interdiff_106-111.txt3.83 KBmohrerao
#111 3038513-111.patch45.6 KBmohrerao
#110 interdiff_106-110.txt11.6 KBmohrerao
#110 3038513-110.patch36.98 KBmohrerao
#106 interdiff_100-106.txt772 bytesmohrerao
#106 3038513-106.patch44.27 KBmohrerao
#105 interdiff_100-105.txt776 bytesmohrerao
#105 3038513-105.patch44.98 KBmohrerao
#100 3038513-100.patch44.38 KBvoleger
#98 interdiff_95-96.txt2.68 KBmeenakshig
#98 3038513-96.patch44.04 KBmeenakshig
#95 3038513-95.patch43.58 KBvoleger
#94 interdiff-3038513-90-94.txt693 bytesvoleger
#94 3038513-94.patch44.32 KBvoleger
#90 interdiff-3038513-89-90.txt658 bytesvoleger
#90 3038513-90.patch44.24 KBvoleger
#89 3038513-89.patch44.25 KBvoleger
#87 3038513-87.patch51.92 KBandypost
#85 3038513-85-interdiff.txt3.61 KBberdir
#85 3038513-85.patch52.61 KBberdir
#82 interdiff-3038513-80-82.txt5.2 KBvoleger
#82 3038513-82.patch51.2 KBvoleger
#80 3038513-80.patch50.74 KBandypost
#80 interdiff.txt3.88 KBandypost
#77 interdiff-3038513-74-77.txt3.95 KBvoleger
#77 3038513-77.patch50.9 KBvoleger
#77 3038513-77-JUST-REROLL.patch49.02 KBvoleger
#74 interdiff-3038513-72-74.txt26.95 KBvoleger
#74 3038513-74.patch49.64 KBvoleger
#72 interdiff-3038513-70-72.txt622 bytesvoleger
#72 3038513-72.patch45.57 KBvoleger
#70 interdiff-3038513-68-70.txt793 bytesvoleger
#70 3038513-70.patch45.78 KBvoleger
#68 interdiff-3038513-64-68.txt937 bytesvoleger
#68 3038513-68.patch45.74 KBvoleger
#67 interdiff-3038513-66-67.txt1.65 KBvoleger
#67 3038513-67.patch45.99 KBvoleger
#66 interdiff-3038513-65-66.txt22.32 KBvoleger
#66 3038513-66.patch44.92 KBvoleger
#65 interdiff-3038513-64-65.txt11.41 KBvoleger
#65 3038513-65.patch45.06 KBvoleger
#64 interdiff-3038513-56-64.txt1.32 KBvoleger
#64 3038513-64.patch44.67 KBvoleger
#56 3038513-57.patch44.52 KBomarlopesino
#55 3038513-55.patch44.52 KBomarlopesino
#55 interdiff-3038513-53-55.txt2.77 KBomarlopesino
#53 interdiff-3038513-50-53.txt5.2 KBvoleger
#53 3038513-53.patch44.41 KBvoleger
#53 3038513-53-just-reroll.patch44.58 KBvoleger
#50 3038513-50.patch44.55 KBvoleger
#50 interdiff-3038513-45-50.txt2.25 KBvoleger
#45 interdiff-3038513-40-45.txt4.41 KBvoleger
#45 3038513-45.patch44.73 KBvoleger
#41 interdiff-3038513-40-41.txt951 bytesvoleger
#41 3038513-41.patch43.52 KBvoleger
#40 interdiff-3038513-39-40.txt976 bytesvoleger
#40 3038513-40-test-only.patch43.15 KBvoleger
#39 3038513-39.patch43.48 KBvoleger
#38 interdiff-3038513-35-38.txt5.72 KBvoleger
#38 3038513-38.patch43.48 KBvoleger
#35 3038513-35-interdiff.txt1.92 KBberdir
#35 3038513-35.patch42.93 KBberdir
#31 interdiff-3038513-27-31.txt1.87 KBvoleger
#31 3038513-31.patch42.95 KBvoleger
#28 interdiff-3038513-27-28.txt1.44 KBvoleger
#28 3038513-28.patch43.4 KBvoleger
#27 interdif-3038513-22-27.txt41.51 KBvoleger
#27 3038513-27.patch42.92 KBvoleger
#22 interdiff-3038513-15-22.txt33.22 KBvoleger
#22 3038513-22.patch41.52 KBvoleger
#16 3038513-15-interdiff.txt1.7 KBjepster_
#16 3038513-15.patch29.14 KBjepster_
#14 interdiff-3038513-13-14.txt3.34 KBvoleger
#14 3038513-14.patch29.1 KBvoleger
#13 interdiff-3038513-12-13.txt395 bytesvoleger
#13 3038513-13.patch28.51 KBvoleger
#12 interdiff-3038513-9-12.txt21.73 KBvoleger
#12 3038513-12.patch28.43 KBvoleger
#9 3038513-6.patch13.93 KBjepster_
#8 3038513-5.patch5.75 KBjepster_
#5 3038513-4.patch5.1 KBjepster_
#4 3038513-3.patch5.46 KBjepster_
#3 3038513-bootstrap.inc_.patch3.46 MBjepster_
#2 3038513-bootstrap.inc_.patch4.87 KBjepster_

Issue fork drupal-3038513

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Peter Majmesku created an issue. See original summary.

jepster_’s picture

StatusFileSize
new4.87 KB

First patch version.

jepster_’s picture

StatusFileSize
new3.46 MB

Replaced another usage of drupal_valid_test_ua() for being able to generate test groups.

jepster_’s picture

StatusFileSize
new5.46 KB

Merged in current 8.7.x branch.

jepster_’s picture

StatusFileSize
new5.1 KB

Included the bootstrap file again, which has been previously removed for testing purpose.

jepster_’s picture

Title: Remove requiring of bootstrap.inc in \Drupal\Core\DrupalKernel::bootEnvironment » Replace drupal_valid_test_ua() with static method for Drupal 9
jepster_’s picture

Issue summary: View changes
jepster_’s picture

StatusFileSize
new5.75 KB

Added deprecation message.

jepster_’s picture

StatusFileSize
new13.93 KB

Replaced remaining usages of drupal_valid_test_ua(). Except in the files, where Drupal is not fully booted.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jepster_’s picture

Status: Active » Needs review
voleger’s picture

StatusFileSize
new28.43 KB
new21.73 KB

Added missed replacements in the code and comments.
Added trigger error.

voleger’s picture

Issue tags: +Kill includes
StatusFileSize
new28.51 KB
new395 bytes

Forgot to kill includes)

voleger’s picture

StatusFileSize
new29.1 KB
new3.34 KB

Missed dependency, fixed CS.

voleger’s picture

jepster_’s picture

StatusFileSize
new29.14 KB
new1.7 KB

I have updated the Drupal version in the deprecation message and changed the visibility of class method to private. Protected was unnecessary there. Should be protected, if it actually matters.

berdir’s picture

Looks like this breaks basically every web test and as a result of that testbot isn't even capable of showing the results, try running some of those locally to debug what the problem is.

berdir’s picture

See https://dispatcher.drupalci.org/job/drupal_patches/88173/artifact/jenkin... for example, somehow the page seems to be basically dead in the test and it is unable to log in.

jepster_’s picture

Status: Needs review » Needs work

Edited this comment, because Drupal bootstrap actually is required. Wondering how the DRUPAL_ROOT constant could have anything to do with the changes from this branch.

jepster_’s picture

Status: Needs work » Postponed (maintainer needs more info)

@Berdir: What is the navigation path to that output? I'm curious which output other patches get within the Jenkins pipeline.

berdir’s picture

Status: Postponed (maintainer needs more info) » Needs work

You can't just visit a path, it simply doesn't work yet as it should. Had a look, the problem is that we need to update drupal_generate_test_ua() at the same time, these two functions are a tandem and they need to be in the same file, as they rely on the filectime() and fileinode() of __FILE__ and currently that doesn't match anymore.

voleger’s picture

Title: Replace drupal_valid_test_ua() with static method for Drupal 9 » Replace drupal_valid_test_ua() and drupal_generate_test_ua() with static methods for Drupal 9
Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new41.52 KB
new33.22 KB

Added replacements for drupal_generate_test_ua()

berdir’s picture

I've been wondering about a) moving this to a dedicated utility class. and b) expanding "ua" to UserAgent, possibly TestUserAgent::validate()/generate() or so.

Also would be nice if we could switch to a request object instead of using globals, not sure if that's always possible. Hope so :) Maybe it can remain static but we pass in the request object?

But first lets get some real test results, so far results look promising, just a few fails, mostly passes on the browser tests.

Status: Needs review » Needs work

The last submitted patch, 22: 3038513-22.patch, failed testing. View results

mile23’s picture

Yay just found this issue. :-)

  1. +++ b/core/lib/Drupal/Core/CoreServiceProvider.php
    @@ -126,7 +126,7 @@ public function alter(ContainerBuilder $container) {
    +    if (!DrupalKernel::validTestUa()) {
    
    +++ b/core/lib/Drupal/Core/Session/SessionConfiguration.php
    @@ -139,15 +140,15 @@ protected function getCookieDomain(Request $request) {
    +  private function drupalValidTestUa() {
    

    Since, generally, our test layer shouldn't be available at runtime, we should have a version of CoreServiceProvider that is added by the kernel if $env == 'test'. So if you say new DrupalKernel('test') you get a kernel that uses CoreServiceProviderHotRoddedForTests instead of CoreServiceProvider. CoreServiceProviderHotRoddedForTests would then swap out session_configuration service with SessionConfigurationForTesting.

    SessionConfigurationForTesting then knows how to deal with test UA and so forth.

    I'm not sure how far out of scope that would be here, but if there's any time to do it, it's when we're trying to get rid of include files.

  2. +++ b/core/lib/Drupal/Core/DrupalKernel.php
    @@ -980,6 +1002,107 @@ protected function initializeContainer() {
    +  public static function validTestUa($new_prefix = NULL) {
    ...
    +  public static function generateTestUa($prefix) {
    

    Again, since these really shouldn't be reachable during non-test runtime, let's not put these on DrupalKernel, because they don't really need what the kernel encapsulates.

    So these should be on a helper class in Drupal\Core\Test, with a @todo to move it to Drupal\Test in #2750461: Remove Simpletest UI because we don't want to maintain a graphical test runner and/or the refactoring mentioned above.

    So +1 on #23.

berdir’s picture

> I'm not sure how far out of scope that would be here

That's definitely out of scope IMHO, the whole point of these methods is to actually figure out if it is a test run or not, they can't be depending on that.

That, I also proposed in my comment above to move them into a utility and not DrupalKernel, with better names.

voleger’s picture

Status: Needs work » Needs review
StatusFileSize
new42.92 KB
new41.51 KB

Addressed #23 and #25

voleger’s picture

StatusFileSize
new43.4 KB
new1.44 KB

Let's see can we kill more includes now.

Status: Needs review » Needs work

The last submitted patch, 28: 3038513-28.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

berdir’s picture

Looks like all update tests are broken?

voleger’s picture

Status: Needs work » Needs review
StatusFileSize
new42.95 KB
new1.87 KB

Let's return all removed includes and see will this help.

jepster_’s picture

Status: Needs review » Reviewed & tested by the community

@voleger: Looks like the correct fix. Since the recent fails have reported issues with the DRUPAL_ROOT constant. That's what has been corrected.

Thanks for your good work!

voleger’s picture

Status: Reviewed & tested by the community » Needs work
voleger’s picture

No idea what is the reason for failed Functional and FunctionalJavascript tests.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new42.93 KB
new1.92 KB

Yeah, "PHP 7.1 & MySQL 5.7 Build Successful" actually means *very unsuccessful* unless a project has no tests.

  1. +++ b/core/lib/Drupal/Core/Test/UserAgent.php
    @@ -0,0 +1,140 @@
    + *
    + * @todo Move this class in to the \Drupal\Test namespace. See https://www.drupal.org/project/drupal/issues/2750461.
    + *
    

    I'm not convinced this makes sense. This is something that we need to run at regular runtime as I wrote above, it is *not* a test-only thing, we don't want to register \Drupal\Tests at runtime.

  2. +++ b/core/lib/Drupal/Core/Test/UserAgent.php
    @@ -0,0 +1,140 @@
    +      $key_file = \Drupal::root() . '/' . $test_db->getTestSitePath() . '/.htkey';
    +      if (!is_readable($key_file)) {
    +        header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
    +        exit;
    +      }
    +      $private_key = file_get_contents($key_file);
    +      // The file properties add more entropy not easily accessible to others.
    +      $key = $private_key . filectime(__FILE__) . fileinode(__FILE__);
    +      $time_diff = \Drupal::time()->getRequestTime() - $time;
    +      $test_hmac = Crypt::hmacBase64($check_string, $key);
    

    This stuff is called before we have a container, you can't use \Drupal:: here.

    I went with $_SERVER['REQUEST_TIME'] for now, that then passes functional tests again.

    Something we could try is require that a request object is passed to this and then read all the server/cookie stuff from that, that should always exist. But lets get it green first with minimal changes.

    Quickstart is only half-passing with this, somehow error handling/display is still broken.

berdir’s picture

Status: Needs review » Needs work

The last submitted patch, 35: 3038513-35.patch, failed testing. View results

voleger’s picture

StatusFileSize
new43.48 KB
new5.72 KB

missed replacement,
reverted include file removal from update.php
tried to include bootstrap inc in the UserAgent methods

voleger’s picture

StatusFileSize
new43.48 KB

Reroll for core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php

voleger’s picture

StatusFileSize
new43.15 KB
new976 bytes

Looks everything replaced correctly. Let's try to decrease includes.

voleger’s picture

Status: Needs work » Needs review
StatusFileSize
new43.52 KB
new951 bytes

Looks like other bootstrap includes can't be replaced now, because of used DRUPAL_ROOT and REQUEST_TIME constants.
This one should be ready for review.

voleger’s picture

Nope, looks like #40 should be the patch for review.

berdir’s picture

#40 looks nice to me, I think what we need is a @group legacy test that tests the deprecation and that the old functions still work?

Status: Needs review » Needs work

The last submitted patch, 41: 3038513-41.patch, failed testing. View results

voleger’s picture

Issue tags: +Needs change record updates
StatusFileSize
new44.73 KB
new4.41 KB

Filled CR https://www.drupal.org/node/3044173
Added initial legacy test

jepster_’s picture

The test is failing, because no sites/simpletest/ENVIRONMENT_ID folder is being created. Can it be, that the test is fired too early?

voleger’s picture

Maybe defining DRUPAL_TEST_IN_CHILD_SITE will help with this test?

jepster_’s picture

Unfortunately I am getting still the same error with that setting.

  public function testDrupalTestUa() {
    define('DRUPAL_TEST_IN_CHILD_SITE', TRUE);
    $test_prefix = drupal_generate_test_ua(drupal_valid_test_ua());
    $this->assertNotEmpty($test_prefix);
    $this->assertNotFalse(drupal_valid_test_ua($test_prefix));
  }
mile23’s picture

+++ b/core/tests/Drupal/KernelTests/Core/Test/UserAgentLegacyTest.php
@@ -0,0 +1,35 @@
+class UserAgentLegacyTest extends KernelTestBase {
...
+    include_once DRUPAL_ROOT . "/core/includes/bootstrap.inc";

If you revert everything from this patch except this test, and then run it without including bootstrap.inc, it tells you that DRUPAL_TEST_IN_CHILD_SITE is undefined.

That's because kernel tests don't make requests, so that whole infrastructure shouldn't be expected to exist. There's no such thing as a kernel test that calls drupal_generate_test_ua().

Turn this test into a functional test, since that's what' we're testing.

The fact that this is ambiguous and brittle? ::points at #25.1:: There's a reason the symfony kernel has an $env variable on construction. I started on a patch to do this, but so many other things.

voleger’s picture

StatusFileSize
new2.25 KB
new44.55 KB

Converted test. Thanks @Mile23 for the explanation.

voleger’s picture

Status: Needs work » Needs review
berdir’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Session/SessionConfiguration.php
    @@ -139,15 +140,15 @@ protected function getCookieDomain(Request $request) {
     
       /**
    -   * Wraps drupal_valid_test_ua().
    +   * Wraps \Drupal\Core\Test\UserAgent::validate().
        *
        * @return string|false
    

    That kind of wrapping is almost always done for unit tests. I guess that's still necessary because it is a static call that does all kinds of things that would likely not work in a unit test.

    However, changing from protected to private is kind of a break as well. *If* we do that, we could possibly also rename the method to reflect the new name. If we don't do that, we also shouldn't change it to private.

  2. +++ b/core/tests/Drupal/FunctionalJavascriptTests/Core/Session/SessionTest.php
    @@ -39,9 +39,11 @@ protected function setUp() {
        *
    -   * Makes sure that drupal_valid_test_ua() works for multiple requests
    -   * performed by the Mink browser. The SIMPLETEST_USER_AGENT cookie must always
    -   * be valid.
    +   * Makes sure that UserAgent::validate() works for multiple
    +   * requests performed by the Mink browser. The SIMPLETEST_USER_AGENT cookie
    +   * must always be valid.
    +   *
    +   * @see \Drupal\Core\Test\UserAgent::validate()
        */
    

    Either this should use the full name, then we don't need the @see or we can revert the rewrapping because UserAgent::validate() is actually shorter than the function name and we can keep the text as it was before.

  3. +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php
    @@ -176,8 +176,9 @@ protected function setUp() {
    -      // from the site directory. To allow drupal_generate_test_ua() to write
    -      // a file containing the private key for drupal_valid_test_ua(), the site
    +      // from the site directory. To allow
    +      // \Drupal\Core\Test\UserAgent::generate() to write a file containing
    +      // the private key for \Drupal\Core\Test\UserAgent::validate(), the site
           // directory has to be writable.
           // BrowserTestBase::tearDown() will delete the entire test site directory.
    

    this is correct as it uses the full namespace, I think prefer that above too and then drop the @see.

  4. +++ b/core/tests/Drupal/Tests/UiHelperTrait.php
    @@ -424,13 +425,13 @@ protected function getAbsoluteUrl($path) {
        * Prepare for a request to testing site.
        *
        * The testing site is protected via a SIMPLETEST_USER_AGENT cookie that is
    -   * checked by drupal_valid_test_ua().
    +   * checked by DrupalKernel::validTestUa().
        *
    -   * @see drupal_valid_test_ua()
    +   * @see \Drupal\Core\Test\UserAgent::validate()
    

    this reference wasn't updated yet for the new class name, I'd also suggest to use the namespace and drop the @see.

voleger’s picture

Status: Needs work » Needs review
StatusFileSize
new44.58 KB
new44.41 KB
new5.2 KB

Rerolled.
Addressed #52
FIxed CS issues of deprecation messages.

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Test/UserAgent.php
    @@ -0,0 +1,141 @@
    +
    +/**
    + * Class UserAgent for testing environments.
    + *
    + * @todo Move this class in to the \Drupal\Test namespace. See
    + *   https://www.drupal.org/project/drupal/issues/2750461.
    + *
    

    We usually don't do Class X, that's literally repeating what it says below. We should just describe what it does: "Generates and validates test user agents"?

    Also, as mentioned in #35, I still think this @todo makes no sense. This has nothing to do with the graphical UI runner, this is about identifying if it is a test request, it's not possible to only load this when running a test as we need it to decide that.

  2. +++ b/core/lib/Drupal/Core/Test/UserAgent.php
    @@ -0,0 +1,141 @@
    +
    +    // A valid Simpletest request will contain a hashed and salted
    +    // authentication code. Check if this code is present in a cookie or custom
    +    // user agent string.
    +    $http_user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : NULL;
    

    Maybe this is where the @todo above is from? It's not limited to "Simpletest", we should replace this with "test request" or "browser test request" or something like that.

    And also as mentioned in #35, I would suggest we pass in the Request object here, so we can replace all those super-globals with $request.

  3. +++ b/core/lib/Drupal/Core/Test/UserAgent.php
    @@ -0,0 +1,141 @@
    +      if (!is_readable($key_file)) {
    +        header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
    +        exit;
    +      }
    

    also interesting. Are we far enough in the bootstrap process to do a throw an access denied exception?

  4. +++ b/core/lib/Drupal/Core/Test/UserAgent.php
    @@ -0,0 +1,141 @@
    +      if ($time_diff >= 0 && $time_diff <= 600 && Crypt::hashEquals($test_hmac, $hmac)) {
    +        static::$testPrefix = $prefix;
    +      }
    

    This will conflict with #3053956: Deprecate \Drupal\Component\Utility\Crypt::hashEquals() in favour of PHP's builtin hash_equals(), we'll need to update it to use hash_equals(), if it happens to be updated before that gets committed, we could already update now, will be easier to reroll then.

  5. +++ b/core/lib/Drupal/Core/Test/UserAgent.php
    @@ -0,0 +1,141 @@
    +      else {
    +        header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden (SIMPLETEST_USER_AGENT invalid)');
    +        exit;
    +      }
    

    same here.

  6. +++ b/core/lib/Drupal/Core/Test/UserAgent.php
    @@ -0,0 +1,141 @@
    +    return 'simple' . $check_string . ':' . Crypt::hmacBase64($check_string, static::$testKey);
    

    I guess simple is also a reference to simpletest, but not sure if there are any implications to changing how the hash is built exactly.

omarlopesino’s picture

Status: Needs work » Needs review
StatusFileSize
new2.77 KB
new44.52 KB

Adding new patch fixing 1 to 5.

I am not sure how, but in 6, adding 'simpletest' is necessary. It seems related to the table prefix created by simpletest, maybe someone can clarify?

omarlopesino’s picture

StatusFileSize
new44.52 KB

Reroll.

omarlopesino’s picture

Assigned: jepster_ » Unassigned
Issue tags: +drupaldevdays
mile23’s picture

Re: #54.1:

+ * Class UserAgent for testing environments.
+ *
+ * @todo Move this class in to the \Drupal\Test namespace. See
+ *   https://www.drupal.org/project/drupal/issues/2750461.
+ *

I'm pretty sure this is about not wanting to be able to autoload this class if there's no Simpletest UI. The linked issue is #2750461: Remove Simpletest UI because we don't want to maintain a graphical test runner

It'd be best if UserAgent weren't autoloadable out of a testing context. But since we need it for simpletest UI right now, and since it's a non-trivial refactor, it would have to happen when/if we remove that testing form. For instance, see this from install_begin_request() that shows us why we shouldn't be able to autoload it, and also why it's not easy to change:

  // If the hash salt leaks, it becomes possible to forge a valid testing user
  // agent, install a new copy of Drupal, and take over the original site.
  // The user agent header is used to pass a database prefix in the request when
  // running tests. However, for security reasons, it is imperative that no
  // installation be permitted using such a prefix.
  $user_agent = $request->cookies->get('SIMPLETEST_USER_AGENT') ?: $request->server->get('HTTP_USER_AGENT');
  if ($install_state['interactive'] && strpos($user_agent, 'simpletest') !== FALSE && !drupal_valid_test_ua()) {
    header($request->server->get('SERVER_PROTOCOL') . ' 403 Forbidden');
    exit;
  }

#54.6: If we want to have an initiative to remove the word 'simpletest' from core, then let's get on that. :-)

My main review:

+++ b/core/lib/Drupal/Core/Test/UserAgent.php
@@ -0,0 +1,147 @@
+  public static function validate($new_prefix = NULL) {
...
+    try {
+      $request = \Drupal::request();
+    }
+    catch (ContainerNotInitializedException $e) {
+      $request = Request::createFromGlobals();
+    }

So a couple things here...

1) We don't have any isolated tests of the UserAgent class. We just need a basic sanity check type test. It will have to be a kernel test so we have some of the dependencies such as a database.

2) If we update the signature of validate() to be validate($new_prefix = NULL, Symfony\Component\HttpFoundation\Request $request = NULL) then we can test the validation.

berdir’s picture

Status: Needs review » Needs work

> I'm pretty sure this is about not wanting to be able to autoload this class if there's no Simpletest UI. The linked issue is #2750461: Remove Simpletest UI because we don't want to maintain a graphical test runner

Yes, but that's my point, this code has nothing to do with the simpletest UI? It's to detect and validate that a request belongs to a certain test environment. It has to be loaded and used *before* we know that we are executing a test, which means it is impossible to have it in a test-only namespace or anything like that. This @todo is bogus, which is why I suggested to remove it :)

> #54.6: If we want to have an initiative to remove the word 'simpletest' from core, then let's get on that. :-)

The above is why I suggested to change this, having simpletest in there leads to those wrong conclusions that it as anything to do with simpletest.module, which is no longer true, not for phpunit tests. But I'm happy to leave that to another issue.

+1 on at least optionally allowing to pass in $request and I think we should make it required. The main check for this is very much in the critical path and is before we have a container *but* we have a request object.

mile23’s picture

Yes, but that's my point, this code has nothing to do with the simpletest UI?

Correct. It doesn't have anything directly to do with simpletest UI, but the UI allows you to run tests outside of a test-only context.

That's why drupal_valid_test_ua() is in bootstrap.inc, and we keep calling it in order to check whether or not we're in a test context. Core has a lot of places where it tries to figure out whether it's running under test or not, in order to work around simpletest.

We shouldn't be doing that, though. We should have an assumption that we're not running under tests. Over time we can remove complexity from core and from the testing system by decoupling them.

That's why we should eventually move UserAgent to Drupal\Tests\ so that it can't be autoloaded unless we're running tests. There's a test isolation reason for this, and there's also a security reason for this, see #58.

That's the explanation of my understanding of that @todo. It might make more sense to link to #2866082: [Plan] Roadmap for Simpletest instead. Or it might make more sense to just put UserAgent under Drupal\Tests\ now, if it's not too disruptive to the patch as it is.

berdir’s picture

But then how do you propose that we'd handle this instead?

We could change how in-test-requests are identified, but we will always need something like this to allow running tests without special hardcoded environments, being able to run tests in parallel and so on. It has nothing to do with simpletest, UI test runner or not. Not anymore.

I still believe that even if we would change this in the future, it has nothing to do with the removal of simpletest.module and there is no need for a @todo pointing to such an issue.

mile23’s picture

we will always need something like this to allow running tests without special hardcoded environments

The thing we're discussing *IS* the special hardcoded environment, and I'm proposing that we allow running tests without it. :-)

But, whatever. Just leave the todo out and then we'll eventually move it anyway.

So that leaves #58.1 and #58.2, and @Berdir at least agrees with 2.

kim.pepper’s picture

voleger’s picture

Assigned: Unassigned » voleger
StatusFileSize
new44.67 KB
new1.32 KB

Addressed 58.2

voleger’s picture

StatusFileSize
new45.06 KB
new11.41 KB

The first iteration of moving to Drupal\Tests namespace

voleger’s picture

StatusFileSize
new44.92 KB
new22.32 KB

Get rid of \Drupal\Core\Test\UserAgent in favor of \Drupal\Tests\UserAgent

voleger’s picture

Assigned: voleger » Unassigned
Status: Needs work » Needs review
StatusFileSize
new45.99 KB
new1.65 KB

Addressed #58.1 and #62

voleger’s picture

StatusFileSize
new45.74 KB
new937 bytes

Addressed #58.1 over the patch from #64

Status: Needs review » Needs work

The last submitted patch, 68: 3038513-68.patch, failed testing. View results

voleger’s picture

Status: Needs work » Needs review
StatusFileSize
new45.78 KB
new793 bytes

Fix constant definition check.

Status: Needs review » Needs work

The last submitted patch, 70: 3038513-70.patch, failed testing. View results

voleger’s picture

Status: Needs work » Needs review
StatusFileSize
new45.57 KB
new622 bytes

Removed ::generate method coverage. Not sure how to test it properly.

berdir’s picture

A bit confused about the last few patches, is this still work in progress?

As I wrote in the last chapter in #59, I think the request argument should be required and the caller should be responsible to provide it.

voleger’s picture

StatusFileSize
new49.64 KB
new26.95 KB

I tried to move the UserAgent class into Drupal\Tests. Looks like there needs more work to do it. So the last patch reverts that movement. Let's left this for follow-up issue.

Here the patch addressing #73

Status: Needs review » Needs work

The last submitted patch, 74: 3038513-74.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

berdir’s picture

  1. +++ b/core/includes/bootstrap.inc
    @@ -645,23 +649,48 @@
      */
    -function drupal_valid_test_ua($new_prefix = NULL) {
    -  @trigger_error('drupal_valid_test_ua() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Test\UserAgent::validate(). See https://www.drupal.org/node/3044173', E_USER_DEPRECATED);
    -  return UserAgent::validate($new_prefix);
    +function drupal_valid_test_ua($new_prefix = NULL, Request $request = NULL) {
    

    no need to change the areguments to the old function, just always use the fallback there.

Will need to review the other parts when I can look through the code, that looks much more complicated than what I was hoping for :)

voleger’s picture

StatusFileSize
new49.02 KB
new50.9 KB
new3.95 KB

Addressed #76
Also rerolled after recent changes related to Drupal Scaffold.

voleger’s picture

Status: Needs work » Needs review

Change status

Status: Needs review » Needs work

The last submitted patch, 77: 3038513-77.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new3.88 KB
new50.74 KB

Here's few fixes
- old implementation always using globals, so request should be taken not from \Drupal::request()
- fixed REQUEST_TIME should be taken from request too
- few docs improvements

Failed file upload tests indicates that more work needed to prevent using files when request is created from globals

1) Drupal\Tests\file\Functional\MultipleFileUploadTest::testMultipleFileFieldWithAllFileExtensions
Failed asserting that 'The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException</em>: The file &quot;/tmp/phpDoBlKa&quot; does not exist in <em class="placeholder">Symfony\Component\HttpFoundation\File\File-&gt;__construct()</em> (line <em class="placeholder">37</em> of <em class="placeholder">vendor/symfony/http-foundation/File/File.php</em>). <pre class="backtrace">Symfony\Component\HttpFoundation\File\UploadedFile-&gt;__construct(&#039;/tmp/phpDoBlKa&#039;, &#039;file1.wtf&#039;, &#039;&#039;, 32, 0) (Line: 87)\n
Symfony\Component\HttpFoundation\FileBag-&gt;convertFileInformation(Array)\n
array_map(Array, Array) (Line: 90)\n
Symfony\Component\HttpFoundation\FileBag-&gt;convertFileInformation(Array)\n
array_map(Array, Array) (Line: 90)\n
Symfony\Component\HttpFoundation\FileBag-&gt;convertFileInformation(Array) (Line: 52)\n
Symfony\Component\HttpFoundation\FileBag-&gt;set(&#039;files&#039;, Array) (Line: 61)\n
Symfony\Component\HttpFoundation\FileBag-&gt;add(Array) (Line: 40)\n
Symfony\Component\HttpFoundation\FileBag-&gt;replace(Array) (Line: 31)\n
Symfony\Component\HttpFoundation\FileBag-&gt;__construct(Array) (Line: 277)\n
Symfony\Component\HttpFoundation\Request-&gt;initialize(Array, Array, Array, Array, Array, Array, NULL) (Line: 255)\n
Symfony\Component\HttpFoundation\Request-&gt;__construct(Array, Array, Array, Array, Array, Array, NULL) (Line: 2089)\n
Symfony\Component\HttpFoundation\Request::createRequestFromFactory(Array, Array, Array, Array, Array, Array) (Line: 314)\n
Symfony\Component\HttpFoundation\Request::createFromGlobals() (Line: 199)\n
Drupal\Core\Extension\ExtensionDiscovery-&gt;scan(&#039;profile&#039;) (Line: 104)\n
Drupal\Core\Config\ExtensionInstallStorage-&gt;getAllFolders() (Line: 129)\n
Drupal\Core\Config\InstallStorage-&gt;listAll() (Line: 38)\n
Drupal\Core\Config\Schema\ConfigSchemaDiscovery-&gt;getDefinitions() (Line: 284)\n
Drupal\Core\Plugin\DefaultPluginManager-&gt;findDefinitions() (Line: 175)\n
Drupal\Core\Plugin\DefaultPluginManager-&gt;getDefinitions() (Line: 160)\n
Drupal\Core\Config\TypedConfigManager-&gt;getDefinitionWithReplacements(&#039;test_theme_settings.settings&#039;, Array, 1) (Line: 200)\n
Drupal\Core\Config\TypedConfigManager-&gt;getDefinition(&#039;test_theme_settings.settings&#039;) (Line: 361)\n
Drupal\Core\Config\TypedConfigManager-&gt;hasConfigSchema(&#039;test_theme_settings.settings&#039;) (Line: 208)\n
Drupal\Core\Config\Config-&gt;save() (Line: 504)\n
Drupal\system\Form\ThemeSettingsForm-&gt;submitForm(Array, Object)\n
call_user_func_array(Array, Array) (Line: 111)\n
Drupal\Core\Form\FormSubmitter-&gt;executeSubmitHandlers(Array, Object) (Line: 51)\n

Status: Needs review » Needs work

The last submitted patch, 80: 3038513-80.patch, failed testing. View results

voleger’s picture

Status: Needs work » Needs review
StatusFileSize
new51.2 KB
new5.2 KB

Reroll (simpletest.install file was updated)
Missed argument pass in one of the ::validate method call.
Also minor changes of the call replacements.

Status: Needs review » Needs work

The last submitted patch, 82: 3038513-82.patch, failed testing. View results

andypost’s picture

Looks all remaining failures are consequence of file upload handling by created request object

Files are processed from globals twice that's why second created request object throws exception - can't move already moved file

To solve it clearly the request object should be by-passed (not created each time) so it makes me think about other way to detect test ID

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new52.61 KB
new3.61 KB

I'm not sure what you mean with different way? The test ID *is* in the request data. The only decision we can make is whether or not we use the request object. The problem with not adding the argument now is that we can then never refactor it later on.

To make this work, we need to avoid Request::createFromGlobals() at least during regular requests, not just to make it work but also for performance reasons. The main one is \Drupal\Core\DrupalKernel::bootEnvironment(), and for the calls to that that matter, the request is available in the caller, so I started to add an argument there, we'll have to figure out BC for that.

I've also added some checks to ExtensionDiscovery so that we can use the request when available. Got one test working with that, lets see about the others.

Status: Needs review » Needs work

The last submitted patch, 85: 3038513-85.patch, failed testing. View results

andypost’s picture

StatusFileSize
new51.92 KB

Re-roll still getting issue with

1) Drupal\Tests\update\Functional\UpdateUploadTest::testUploadModule
Behat\Mink\Exception\ExpectationException: Current page is "/admin/modules/install", but "/core/authorize.php" expected.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

voleger’s picture

Title: Replace drupal_valid_test_ua() and drupal_generate_test_ua() with static methods for Drupal 9 » Replace drupal_valid_test_ua() and drupal_generate_test_ua() with static methods
Version: 8.9.x-dev » 9.1.x-dev
Status: Needs work » Needs review
StatusFileSize
new44.25 KB

Rerolled against 9.0.x

voleger’s picture

StatusFileSize
new44.24 KB
new658 bytes

Also had fixed the last issue with the patch. So the patch is finally ready for the review

The last submitted patch, 89: 3038513-89.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 90: 3038513-90.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

voleger’s picture

Status: Needs work » Needs review
StatusFileSize
new44.32 KB
new693 bytes

Added missed $defaultTheme in the legacy test.

voleger’s picture

StatusFileSize
new43.58 KB

Just reroll against 9.1.x
Ready for the review.

Status: Needs review » Needs work

The last submitted patch, 95: 3038513-95.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

voleger’s picture

Status: Needs work » Needs review
StatusFileSize
new44.38 KB
new1.46 KB

Forgot to add update.php into the diff result.

meenakshig’s picture

StatusFileSize
new44.04 KB
new2.68 KB
voleger’s picture

#98 looks like a crosspost
#97 is the patch for the review. I can't find the patch in Files below, so just hiding unrelated files from IS.

voleger’s picture

StatusFileSize
new44.38 KB

Reupload #97 patch. Unable set testing for the #97 patch.

Status: Needs review » Needs work

The last submitted patch, 100: 3038513-100.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

voleger’s picture

Status: Needs work » Needs review

Unrelated test fail

Status: Needs review » Needs work

The last submitted patch, 100: 3038513-100.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

daffie’s picture

Patch looks good. Some remarks:

  1. When I do a code base search for "drupal_valid_test_ua", I still find them in the wrong places, like: "core/tests/Drupal/Tests/Core/Database/DatabaseTest.php"
  2. +++ b/core/lib/Drupal/Core/Test/UserAgent.php
    @@ -0,0 +1,143 @@
    +      list(, $prefix, $time, $salt, $hmac) = $matches;
    

    This line can be changed to: [, $prefix, $time, $salt, $hmac] = $matches;

  3. +++ b/core/lib/Drupal/Core/Test/UserAgent.php
    @@ -0,0 +1,143 @@
    + * @package Drupal\Core\Test
    

    What does this line? And can it be removed?

mohrerao’s picture

StatusFileSize
new44.98 KB
new776 bytes

ignore this patch please

mohrerao’s picture

Status: Needs work » Needs review
StatusFileSize
new44.27 KB
new772 bytes
  1. I see that drupal_valid_test_ua is part of a comment in core/tests/Drupal/Tests/Core/Database/DatabaseTest.php and this can be removed if not needed here.
  2. Adding a patch for changes requested
  3. Not sure why its there. Can some one suggest if this can be removed?

Status: Needs review » Needs work

The last submitted patch, 106: 3038513-106.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

daffie’s picture

The points from comment #104.1 and #104.3 are still open.

mohrerao’s picture

Issue tags: +DIACWMay2020
mohrerao’s picture

Status: Needs work » Needs review
StatusFileSize
new36.98 KB
new11.6 KB

Fixed #104.1 and #104.3 .

mohrerao’s picture

StatusFileSize
new45.6 KB
new3.83 KB

Oops, patch missed adding UserAgent.php file.
Fixed #104.1 and #104.3 .

Status: Needs review » Needs work

The last submitted patch, 111: 3038513-111.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

voleger’s picture

StatusFileSize
new45.74 KB
new1.24 KB

Need to fix the last test fail. core/tests/Drupal/Tests/Core/Database/DatabaseTest.php setUp() method mocks container methods. So the argument 'request_stack' in 'has' method not expected.

mohrerao’s picture

Assigned: Unassigned » mohrerao

Working on the failed test.

mohrerao’s picture

Status: Needs work » Needs review
StatusFileSize
new45.99 KB
new2 KB

Removed mock container creation as this is now not needed.

mohrerao’s picture

Assigned: mohrerao » Unassigned
voleger’s picture

The last test fail fixed. +1 for rtbc

daffie’s picture

Status: Needs review » Needs work

Patch looks good, needs some minor improvements:

  1. +++ b/core/lib/Drupal/Core/DrupalKernel.php
    @@ -973,7 +974,7 @@ protected function initializeContainer() {
    -  public static function bootEnvironment($app_root = NULL) {
    +  public static function bootEnvironment($app_root = NULL, Request $request = NULL) {
    

    Missing the added parameter in the docblock.

  2. +++ b/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
    @@ -195,7 +196,7 @@ public function scan($type, $include_tests = NULL) {
    +      $include_tests = Settings::get('extension_discovery_scan_tests') || UserAgent::validate(\Drupal::hasService('request_stack') && \Drupal::request() ? \Drupal::request() : Request::createFromGlobals());
    

    Can we put the whole request stuff on a separate line. For an improvement in readability.

  3. +++ b/core/lib/Drupal/Core/Test/UserAgent.php
    @@ -0,0 +1,141 @@
    + * @see https://www.drupal.org/project/drupal/issues/2750461
    

    Why add link to an unrelated issue?

  4. +++ b/core/tests/Drupal/FunctionalTests/UserAgentLegacyTest.php
    @@ -0,0 +1,32 @@
    + * Test legacy UA functions.
    

    Can we change "UA" to "user agent".

  5. +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php
    @@ -263,11 +264,12 @@ protected function bootEnvironment() {
    +    // \Drupal\Core\Test\UserAgent::validate() can still be
         // safely executed. This primarily affects the (test) site directory
    

    Outlining to the 80 character maximum can be improved.

  6. When I do a code base search for "drupal_valid_test_ua(", I got one in "core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php"
deepak goyal’s picture

Assigned: Unassigned » deepak goyal
naresh_bavaskar’s picture

Status: Needs work » Needs review
StatusFileSize
new47.21 KB
new4.75 KB

@daffie Addressed the #118
Please review

naresh_bavaskar’s picture

Sorry @Deepak Goyal, I did not check that is assigned to you.

deepak goyal’s picture

StatusFileSize
new47.44 KB
new5.22 KB

I have also created patch for the same please review.

deepak goyal’s picture

Assigned: deepak goyal » Unassigned
daffie’s picture

Status: Needs review » Needs work

Review of the patch from comment #120.

  1. +++ b/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
    @@ -195,7 +196,9 @@ public function scan($type, $include_tests = NULL) {
    -      $include_tests = Settings::get('extension_discovery_scan_tests') || drupal_valid_test_ua();
    +      $include_tests = Settings::get('extension_discovery_scan_tests') ||
    +        UserAgent::validate(\Drupal::hasService('request_stack')
    +        && \Drupal::request() ? \Drupal::request() : Request::createFromGlobals());
    

    The change that I wanted was:

          $request = \Drupal::hasService('request_stack') && \Drupal::request() ? \Drupal::request() : Request::createFromGlobals();
          $include_tests = Settings::get('extension_discovery_scan_tests') || UserAgent::validate($request);
    
  2. +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php
    @@ -259,15 +260,17 @@ protected function bootEnvironment() {
    -    Database::addConnectionInfo('default', 'test-runner', $this->getDatabaseConnectionInfo()['default']);
    +    Database::addConnectionInfo('default', 'test-runner',
    +     $this->getDatabaseConnectionInfo()['default']);
    

    This change is not necessary. Out of scope for this issue.

  3. +++ b/core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php
    @@ -27,7 +27,7 @@ class UrlConversionTest extends UnitTestCase {
    +    // Mock the container so we don't need to mock \Drupal\Core\Test\UserAgent::validate().
    

    The comment goes over the 80 character limit.

  4. Point #118.5 is still open.
deepak goyal’s picture

Assigned: Unassigned » deepak goyal
deepak goyal’s picture

StatusFileSize
new47.04 KB
new4.44 KB

Hi @daffie
Updated patch please review.

deepak goyal’s picture

Assigned: deepak goyal » Unassigned
Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 126: 3038513-126.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

hardik_patel_12’s picture

Status: Needs work » Needs review
StatusFileSize
new46.93 KB
new1.83 KB

Solving test case , kindly review a new patch.

Status: Needs review » Needs work

The last submitted patch, 129: 3038513-129.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

longwave’s picture

@Hardik_Patel_12: thanks for the patches here and elsewhere, but your interdiffs seem to be diffs-of-diffs which are quite hard to read - please see https://www.drupal.org/documentation/git/interdiff for a number of ways of creating a more useful interdiff which lets reviewers see the changes between patches more easily.

longwave’s picture

Status: Needs work » Needs review
StatusFileSize
new47.3 KB
new763 bytes

Fixed the failing test in #126.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

Only #124.3 is still open.

daffie’s picture

Status: Reviewed & tested by the community » Needs work

Sorry, wrong status. Needs to be "needs work", because #124.3 is still open.

raunak.singh’s picture

Assigned: Unassigned » raunak.singh

I am working on this as per #133 comment

raunak.singh’s picture

Assigned: raunak.singh » Unassigned
Status: Needs work » Needs review
StatusFileSize
new39.51 KB
new777 bytes

Hi @daffie,
I have updated the patch and fix #124.3 issue. Please take a look.

daffie’s picture

Status: Needs review » Needs work

@Raunak.singh: I think your patch is missing some parts.

raunak.singh’s picture

Assigned: Unassigned » raunak.singh

Yes @daffie
I am Working on it.

raunak.singh’s picture

Assigned: raunak.singh » Unassigned

Due to internet issue patch file not added

raunak.singh’s picture

StatusFileSize
new47.53 KB
new2.12 KB

Resolve previous patch issue please take a look.

raunak.singh’s picture

Status: Needs work » Needs review
jungle’s picture

StatusFileSize
new47.53 KB
new560 bytes
+++ b/core/tests/Drupal/FunctionalTests/UserAgentLegacyTest.php
@@ -0,0 +1,32 @@
+  protected $defaultTheme = 'classy';

classy is not encouraged to use. even this is a test testing legacy code, but newly added. Changed to stark.

daffie’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs change record updates, -Needs issue summary update

All my remarks are addressed.
All code changes look good to me.
All usages of drupal_valid_test_ua() and drupal_generate_test_ua() have been replaced.
Both functions are deprecated and have deprecation message testing.
Updated the CR and IS.
For me it is RTBC.

andypost’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new4.57 KB
new47.1 KB

Bit more clean-up, while review I think we should split it more

- split validate() into set and get behaviors - validation and doing a lot with request are different functions
- while we changing update.php probably makes sense to move gc-* to \Drupal\Core\DrupalKernel::bootEnvironment() because this place should be called only once and we can use request object

+++ b/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php
@@ -195,7 +196,8 @@ public function scan($type, $include_tests = NULL) {
+      $request = \Drupal::hasService('request_stack') && \Drupal::request() ? \Drupal::request() : Request::createFromGlobals();

I think it should use \Drupal::hasRequest() instead

+++ b/core/lib/Drupal/Core/Session/SessionConfiguration.php
@@ -139,15 +140,18 @@ protected function getCookieDomain(Request $request) {
-  protected function drupalValidTestUa() {
-    return drupal_valid_test_ua();
+  protected function drupalValidTestUa(Request $request) {
+    return UserAgent::validate($request);

as we change signature, better to remove

andypost’s picture

StatusFileSize
new10.6 KB
new45.77 KB

Further clean-up - generate method works independently from request object, except a case when we need to test it.
Then the DRUPAL_TEST_IN_CHILD_SITE defined and verify should just make sure that setup already done

Patch also removes lots of new usages of the request

andypost’s picture

StatusFileSize
new1.59 KB
new46.58 KB

Extension discovery already using global request to find site path, so validation should use the same object

andypost’s picture

StatusFileSize
new2.16 KB
new45.56 KB

No reason to make kernel environment request dependent (only globals should be used here)

Patch removes this dependency but maybe we can move this detection to \Drupal\Core\DrupalKernel::initializeSettings() where request appears and site settings (testing ones are needs a cookie from request)
This method already protected and "testing knowledge" could be hidden there

The last submitted patch, 146: 3038513-146.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 147: 3038513-147.patch, failed testing. View results

andypost’s picture

StatusFileSize
new44.75 KB

Testing revert of interdiff from #146 (not clear why extension scan require \Drupal::request() for this tests)

POST request to: http://nginx/admin/config/regional/translate/import
The website encountered an unexpected error. Please try again later.

Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException: The file "/tmp/phpGdibLK" does not exist in Symfony\Component\HttpFoundation\File\File->__construct() (line 36 of vendor/symfony/http-foundation/File/File.php).
Symfony\Component\HttpFoundation\File\UploadedFile->__construct('/tmp/phpGdibLK', 'po_bnnEFa.po', '', 0, ) (Line: 87)
Symfony\Component\HttpFoundation\FileBag->convertFileInformation(Array)
array_map(Array, Array) (Line: 90)
Symfony\Component\HttpFoundation\FileBag->convertFileInformation(Array) (Line: 52)
Symfony\Component\HttpFoundation\FileBag->set('files', Array) (Line: 61)
Symfony\Component\HttpFoundation\FileBag->add(Array) (Line: 40)
Symfony\Component\HttpFoundation\FileBag->replace(Array) (Line: 31)
Symfony\Component\HttpFoundation\FileBag->__construct(Array) (Line: 269)
Symfony\Component\HttpFoundation\Request->initialize(Array, Array, Array, Array, Array, Array, NULL) (Line: 247)
Symfony\Component\HttpFoundation\Request->__construct(Array, Array, Array, Array, Array, Array, NULL) (Line: 1988)
Symfony\Component\HttpFoundation\Request::createRequestFromFactory(Array, Array, Array, Array, Array, Array) (Line: 293)
Symfony\Component\HttpFoundation\Request::createFromGlobals() (Line: 183)
Drupal\Core\Extension\ExtensionDiscovery->scan('profile') (Line: 96)
Drupal\Core\Config\ExtensionInstallStorage->getAllFolders() (Line: 129)
Drupal\Core\Config\InstallStorage->listAll() (Line: 107)
Drupal\locale\LocaleDefaultConfigStorage->listAll() (Line: 309)
Drupal\locale\LocaleConfigManager->getComponentNames(Array) (Line: 548)
locale_config_batch_update_components(Array, Array) (Line: 193)
Drupal\locale\Form\ImportForm->submitForm(Array, Object)
call_user_func_array(Array, Array) (Line: 113)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 592)
Drupal\Core\Form\FormBuilder->processForm('locale_translate_import_form', Array, Object) (Line: 320)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 573)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 158)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 706)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
alexpott’s picture

  1. +++ b/core/includes/bootstrap.inc
    @@ -353,92 +353,36 @@ function _drupal_exception_handler_additional($exception, $exception2) {
     function drupal_valid_test_ua($new_prefix = NULL) {
    -  static $test_prefix;
    ...
     function drupal_generate_test_ua($prefix) {
    -  static $key, $last_prefix;
    

    On the face of it this functions being functions is not really wrong. Moving them to a new class class only means more code to autoload on every request. This issue is alternatively fixed by #3151118: Include bootstrap.inc using composer with way less change.

  2. +++ b/core/lib/Drupal/Core/DrupalKernel.php
    @@ -689,7 +690,7 @@ public function terminate(Request $request, Response $response) {
    -    static::bootEnvironment();
    +    static::bootEnvironment(NULL);
    

    This change is not necessary.

andypost’s picture

Not all of this code is needed every request.
Generate and "resurrect from generated" are testing time "injections", which, in theory, should be bypassed for production, at least this ability nice to have.
Ideally it could be integrated into kernel (main one) and could use integration into update kernel (for GC instead of update.php)

andypost’s picture

StatusFileSize
new1.8 KB
new44.99 KB

Reroll after SA ceee045fa5f907f0e28d504f4a3e355a855c4e98

Also fixed #151.2 and added changes from SA to new class

hardik_patel_12’s picture

Status: Needs work » Needs review

alexpott’s picture

@andypost we're going to be loading the UserAgent class on every request I still think #3151118: Include bootstrap.inc using composer is the way to go and this change pushes unnecessary change and deprecations on contributed and custom code.

+++ b/core/lib/Drupal/Core/Test/UserAgent.php
@@ -0,0 +1,143 @@
+      $key_file = DRUPAL_ROOT . '/' . $test_db->getTestSitePath() . '/.htkey';

Moving this code away from where DRUPAL_ROOT is define doesn't improve the architecture. I'd argue it makes things a bit worse because at least DRUPAL_ROOT is defined in the same location as the methods currently are.

andypost’s picture

@alexpott I'm totally agree with cost of autoload (which I see in top 10 of the most of profilers last years)

Looks we need new issue policy/no-patch about further clean-up

On one hand loading of boostrap.php is cheaper and will be, even with PHP preload

OTOH this file still needs cleaning

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

daffie’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

I am sure that the patch needs a reroll now that #3151118: Include bootstrap.inc using composer has landed.

adityasingh’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new43.23 KB
new20.99 KB

Reroll the patch for 9.2

Status: Needs review » Needs work

The last submitted patch, 160: 3038513-160.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

daffie’s picture

+++ b/core/tests/Drupal/FunctionalTests/UserAgentLegacyTest.php
@@ -0,0 +1,32 @@
+   * @expectedDeprecation drupal_valid_test_ua() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Use \Drupal\Core\Test\UserAgent::validate(). See https://www.drupal.org/node/3044173
+   * @expectedDeprecation drupal_generate_test_ua() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Use \Drupal\Core\Test\UserAgent::generate(). See https://www.drupal.org/node/3044173

These 2 lines are old code. We are now using the method $this->expectDeprecation().

voleger’s picture

Status: Needs work » Needs review

Rebased MR-12
Updated versions in the deprecation messages.

alexpott’s picture

Status: Needs review » Needs work

As stated in #151 I don't think we should be moving drupal_valid_test_ua() is worth it. Now that bootstrap.inc is autoloaded there's no compelling reason to make that change.

drupal_generate_test_ua() should move to somewhere in the test system - maybe as a static method in \Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware.

voleger’s picture

Title: Replace drupal_valid_test_ua() and drupal_generate_test_ua() with static methods » Move drupal_generate_test_ua() into the test system

Reverted deprecation of drupal_valid_test_ua()

voleger’s picture

Status: Needs work » Needs review

Updated CR [#3044173]

daffie’s picture

Status: Needs review » Needs work

The testbot is not happy with the latest MR. See https://dispatcher.drupalci.org/job/drupal_patches/71311/

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

alexpott’s picture

Status: Needs work » Closed (works as designed)

Given the security implications and how drupal_valid_test_ua() and drupal_generate_test_ua() are related I'd prefer to leave these two functions inside bootstrap.inc and next to each other. I think the supposed benefits of having one less method in bootstrap.inc are not as great as the benefit of having these methods next to each other.

I'm going to close this issue as works as designed.