Remove calls to deprecated global $user in module as part of the meta #2047951: [META] Remove calls to deprecated global $user and $GLOBALS['user']

From #2163203-5: Remove calls to deprecated global $user wherever possible (outside bootstrap and authentication).

Let's expand the scope of this issue to convert all the trivial references outside bootstrap and session handling, and then work on those tougher issues separately.

Before RTBCing a patch for this issue, confirm that no more references to the global remain outside #2061953: Remove calls to deprecated global $user in core/lib/Drupal/Core/Authentication/AuthenticationManager.php , #2062771: #2062771: Remove calls to deprecated global $user in \Drupal\Core\Session\SessionManager, #2062069: Remove calls to deprecated global $user in core/includes/bootstrap.inc . Edit: Fixing TestBase and its child base classes might also merit a separate issue, but uses in normal test classes themselves should be (edit) either in this patch or in that patch, depending on which makes more sense. But not separately.

CommentFileSizeAuthor
#87 interdiff-2163203.txt988 byteslongwave
#87 2163203-global-user-87.patch22.11 KBlongwave
#7 2163203-7-global-user.patch24.58 KBjoelpittet
#10 2163203-7-global-user-part2.patch13 KBjoelpittet
#10 2163203-7-global-user-part1.patch11.55 KBjoelpittet
#12 2163203-7-global-user-part2-1.patch6.79 KBjoelpittet
#12 2163203-7-global-user-part2-2.patch6.21 KBjoelpittet
#15 2163203-15-global-user.patch23.98 KBjoelpittet
#18 2163203-17-global-user.patch24.05 KBjoelpittet
#21 2163203-21-global-user.patch23.47 KBjoelpittet
#23 2163203-7-global-user-part2-2-1.patch3.68 KBjoelpittet
#23 2163203-7-global-user-part2-2-2.patch3.11 KBjoelpittet
#25 2163203-25-global-user.patch23.71 KBjoelpittet
#28 2163203-27-global-user.patch23.14 KBjoelpittet
#30 2163203-30-global-user.patch21.45 KBjoelpittet
#33 2163203-33-global-user.patch22.63 KBjoelpittet
#34 2163203-34-global-user.patch22.13 KBjoelpittet
#37 2163203-37-global-user.patch24.26 KBjoelpittet
#38 interdiff.txt2.45 KBjoelpittet
#39 global-user-bootstrap.patch1.67 KBjoelpittet
#39 global-user-common.patch875 bytesjoelpittet
#39 global-user-session.patch4.84 KBjoelpittet
#39 global-user-authmanager.patch1019 bytesjoelpittet
#39 global-user-cookie.patch783 bytesjoelpittet
#39 global-user-EntityAccessController.patch588 bytesjoelpittet
#39 global-user-FormBuilder.patch521 bytesjoelpittet
#39 global-user-AuthenticationEnhancer.patch813 bytesjoelpittet
#39 global-user-EntityReferenceSelectionSortTest.patch831 bytesjoelpittet
#39 global-user-HistoryTimestampTest.patch640 bytesjoelpittet
#39 global-user-TestBase.patch2.6 KBjoelpittet
#39 global-user-WebTestBase.patch627 bytesjoelpittet
#39 global-user-FormatDateTest.patch1.92 KBjoelpittet
#39 global-user-DrupalDateTimeTest.patch1.77 KBjoelpittet
#39 global-user-EntityAccessTest.patch1.65 KBjoelpittet
#39 global-user-TokenReplaceTest.patch1.22 KBjoelpittet
#39 global-user-ArgumentDefaultTest.patch1.11 KBjoelpittet
#39 global-user-user.patch3.05 KBjoelpittet
#39 global-user-user.pages_.inc_.patch533 bytesjoelpittet
#39 global-user-generated7.patch853 bytesjoelpittet
#39 interdiff.txt27.9 KBjoelpittet
#39 2163203-38-global-user.patch51.41 KBjoelpittet
#61 2163203-61-global-user.patch30.17 KBjoelpittet
#61 interdiff.txt6.5 KBjoelpittet
#62 remaining.txt24.44 KBjoelpittet
#63 2163203-global-user-63.patch28.92 KBjoelpittet
#68 2163203-global-user-68-reroll.patch25.62 KBjoelpittet
#69 2163203-global-user-69.patch0 bytesjoelpittet
#70 2163203-global-user-69.patch24.81 KBjoelpittet
#76 2163203-global-user-76.patch24.85 KBjoelpittet
#80 2163203-global-user-80.patch24.84 KBsaki007ster
#1 2163203-remove-global-user-content_translation.patch1.41 KBjoelpittet
#85 2163203-global-user-85.patch22.08 KBlongwave
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joelpittet’s picture

Status: Active » Needs review
FileSize
1.41 KB
joelpittet’s picture

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

testbot activate?

joelpittet’s picture

Status: Reviewed & tested by the community » Needs review
xjm’s picture

Title: Remove calls to deprecated global $user in content_translation module » Remove calls to deprecated global $user wherever possible (outside bootstrap and authentication)
Status: Needs review » Needs work

Thanks for your work on this! See #2047951-40: [META] Remove calls to deprecated global $user and $GLOBALS['user']. Let's expand the scope of this issue to convert all the trivial references outside bootstrap and session handling, and then work on those tougher issues separately.

Before RTBCing a patch for this issue, confirm that no more references to the global remain outside #2061953: Remove calls to deprecated global $user in core/lib/Drupal/Core/Authentication/AuthenticationManager.php , #2062771: #2062771: Remove calls to deprecated global $user in \Drupal\Core\Session\SessionManager, #2062069: Remove calls to deprecated global $user in core/includes/bootstrap.inc . Edit: Fixing TestBase and its child base classes might also merit a separate issue, but uses in normal test classes themselves should be (edit) either in this patch or in that patch, depending on which makes more sense. But not separately.

plach’s picture

Component: content_translation.module » user system
Status: Needs work » Active

The attached patch above is a duplicate of #2078057: Remove references to global $user in Content Translation module so moving back to active.

joelpittet’s picture

Status: Active » Needs review
FileSize
24.58 KB

@xjm I'm not to sure on the approach to do them all at once. I know what you are trying to do but these are deceivingly tricky.
A lot of tests don't work upon conversion and doesn't seem to be an API for setting the current_user.

But here's an attempt to see what testbot says.
Didn't convert them where they needed current_user set and some tests.
Didn't convert session.inc, bootstrap.inc, AuthenticationManager, AuthenticationEnhancer, TestBase, WebTestBase, user module and a few tests.

xjm’s picture

@joelpittet Yep, it'll take a bit more work in this issue, but overall it will be less overhead. Also, if we try to remove all the "easier" stuff here, we'll do a better job of surfacing specific bugs or API insufficiencies that can be spun off as separate issues about a specific topic rather than a specific file.

joelpittet’s picture

Status: Needs work » Needs review
FileSize
13 KB
11.55 KB

@xjm ok, just wanted to make sure you're looped in to what's happening. Here is the same patch split in half to try to narrow down what is causing the failed install.

joelpittet’s picture

joelpittet’s picture

FileSize
23.98 KB

Ok think it's entity controller

diff --git a/core/lib/Drupal/Core/Entity/EntityAccessController.php b/core/lib/Drupal/Core/Entity/EntityAccessController.php
index f18cd2c..9285422 100644
--- a/core/lib/Drupal/Core/Entity/EntityAccessController.php
+++ b/core/lib/Drupal/Core/Entity/EntityAccessController.php
@@ -276,7 +276,7 @@ protected function checkCreateAccess(AccountInterface $account, array $context,
    */
   protected function prepareUser(AccountInterface $account = NULL) {
     if (!$account) {
-      $account = $GLOBALS['user'];
+      $account = \Drupal::currentUser();
     }
     return $account;
   }
joelpittet’s picture

Status: Needs work » Needs review
FileSize
24.05 KB

guessed wrong i guess

joelpittet’s picture

Status: Needs work » Needs review
FileSize
23.47 KB

ok both

joelpittet’s picture

Status: Needs work » Needs review
FileSize
3.68 KB
3.11 KB

hmm needs narrowing further

joelpittet’s picture

Status: Needs work » Needs review
FileSize
23.71 KB

cookie!

joelpittet’s picture

Status: Needs work » Needs review
FileSize
23.14 KB

cookie and entityaccesscontroller removed...

joelpittet’s picture

Status: Needs work » Needs review
FileSize
21.45 KB
joelpittet’s picture

Status: Needs work » Needs review
FileSize
22.63 KB

hmm must have buggered that patch.

joelpittet’s picture

FileSize
22.13 KB

Ok this should get rid of the password reset errors.

joelpittet’s picture

Ok green! That leaves the following out:

global $user:

core/includes/bootstrap.inc
core/includes/session.inc
core/lib/Drupal/Core/Authentication/AuthenticationManager.php
core/lib/Drupal/Core/Authentication/Provider/Cookie.php
core/lib/Drupal/Core/Form/FormBuilder.php
core/lib/Drupal/Core/Routing/Enhancer/AuthenticationEnhancer.php
core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
core/modules/system/lib/Drupal/system/Tests/Common/FormatDateTest.php
core/modules/system/lib/Drupal/system/Tests/Datetime/DrupalDateTimeTest.php
core/modules/system/lib/Drupal/system/Tests/Entity/EntityAccessTest.php
core/modules/system/lib/Drupal/system/Tests/System/TokenReplaceTest.php
core/modules/user/user.module
core/modules/user/user.pages.inc
core/modules/user/lib/Drupal/user/Tests/Views/ArgumentDefaultTest.php

$GLOBALS['user']

core/includes/common.inc
core/lib/Drupal/Core/Entity/EntityAccessController.php
core/lib/Drupal/Core/Routing/Enhancer/AuthenticationEnhancer.php
core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php:
core/modules/history/lib/Drupal/history/Tests/Views/HistoryTimestampTest.php
core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
core/scripts/generate-d7-content.sh

Most of which are needing a way to set the current user(which may need a container?).

joelpittet’s picture

FileSize
24.26 KB

Pushing the envelope a bit, a few more added from user.module that I think can be safely added.

joelpittet’s picture

FileSize
2.45 KB
joelpittet’s picture

Ok here's a patch for each of rest split by file. I made up a wild guess on how to set the current user...
\Drupal::getContainer()->set('current_user', drupal_anonymous_user());

This is an attempt to weed out any other low hanging fruit from the rest of them.

joelpittet’s picture

Did a little test and seems to show promise for this way of setting the user in tests at least:

Debug tests:

  $account = $this->drupalCreateUser();
    debug($account->id());
    debug('expect 1');
    debug($GLOBALS['user']->id());
    debug(\Drupal::currentUser()->id());
    debug($this->container->get('current_user')->id());

    \Drupal::getContainer()->set('current_user', drupal_anonymous_user());
    debug('expect 0');
    debug($GLOBALS['user']->id());
    debug(\Drupal::currentUser()->id());
    debug($this->container->get('current_user')->id());


    $this->container->set('current_user', $account);
    debug('expect 2');
    debug($GLOBALS['user']->id());
    debug(\Drupal::currentUser()->id());
    debug($this->container->get('current_user')->id());

Results:

'2'

	Debug	TokenReplaceTest.php	33	Drupal\system\Tests\System\TokenReplaceTest->testTokenReplacement()	Debug

'expect 1'

	Debug	TokenReplaceTest.php	34	Drupal\system\Tests\System\TokenReplaceTest->testTokenReplacement()	Debug

'1'

	Debug	TokenReplaceTest.php	35	Drupal\system\Tests\System\TokenReplaceTest->testTokenReplacement()	Debug

'1'

	Debug	TokenReplaceTest.php	36	Drupal\system\Tests\System\TokenReplaceTest->testTokenReplacement()	Debug

'1'

	Debug	TokenReplaceTest.php	37	Drupal\system\Tests\System\TokenReplaceTest->testTokenReplacement()	Debug

'expect 0'

	Debug	TokenReplaceTest.php	40	Drupal\system\Tests\System\TokenReplaceTest->testTokenReplacement()	Debug

'1'

	Debug	TokenReplaceTest.php	41	Drupal\system\Tests\System\TokenReplaceTest->testTokenReplacement()	Debug

0

	Debug	TokenReplaceTest.php	42	Drupal\system\Tests\System\TokenReplaceTest->testTokenReplacement()	Debug

0

	Debug	TokenReplaceTest.php	43	Drupal\system\Tests\System\TokenReplaceTest->testTokenReplacement()	Debug

'expect 2'

	Debug	TokenReplaceTest.php	47	Drupal\system\Tests\System\TokenReplaceTest->testTokenReplacement()	Debug

'1'

	Debug	TokenReplaceTest.php	48	Drupal\system\Tests\System\TokenReplaceTest->testTokenReplacement()	Debug

'2'

	Debug	TokenReplaceTest.php	49	Drupal\system\Tests\System\TokenReplaceTest->testTokenReplacement()	Debug

'2'
joelpittet’s picture

Status: Needs work » Needs review
FileSize
30.17 KB
6.5 KB

Rolled in some of the ones I think are safe.

joelpittet’s picture

FileSize
24.44 KB

These are the remaining files that didn't get patched (see remaining.txt) for interdiff between #38 and #61:

+++ b/core/includes/bootstrap.inc
+++ b/core/includes/session.inc
+++ b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php
+++ b/core/lib/Drupal/Core/Authentication/Provider/Cookie.php
+++ b/core/lib/Drupal/Core/Entity/EntityAccessController.php
+++ b/core/lib/Drupal/Core/Form/FormBuilder.php
+++ b/core/lib/Drupal/Core/Routing/Enhancer/AuthenticationEnhancer.php
+++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceSelectionSortTest.php
+++ b/core/modules/history/lib/Drupal/history/Tests/Views/HistoryTimestampTest.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Common/FormatDateTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Datetime/DrupalDateTimeTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityAccessTest.php
+++ b/core/modules/user/lib/Drupal/user/Tests/Views/ArgumentDefaultTest.php
+++ b/core/modules/user/user.module
+++ b/core/modules/user/user.pages.inc
+++  b/core/scripts/generate-d7-content.sh
joelpittet’s picture

FileSize
28.92 KB

Re-roll.

webchick’s picture

This is going to break everything everywhere, so tagging and will try to fit this in the week after beta.

valthebald’s picture

content_translation module does not contain any mention neither of 'global $user', nor $GLOBALS['user']. Wrong title?

joelpittet’s picture

@valthebald the title isn't wrong, @xjm repurposed this issue in #6 Just needs a new issue summary too.

joelpittet’s picture

Issue summary: View changes
joelpittet’s picture

Re-rolled.

joelpittet’s picture

joelpittet’s picture

FileSize
24.81 KB

Re-roll

The last submitted patch, 69: 2163203-global-user-69.patch, failed testing.

aspilicious’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

2163203-global-user-69_0.patch no longer applies.


xjm’s picture

Issue summary: View changes

grr.

xjm’s picture

Issue summary: View changes
joelpittet’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs reroll
FileSize
24.85 KB

back to rtbc with re-roll.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

2163203-global-user-76.patch no longer applies.

error: patch failed: core/modules/simpletest/lib/Drupal/simpletest/TestBase.php:923
error: core/modules/simpletest/lib/Drupal/simpletest/TestBase.php: patch does not apply

joelpittet’s picture

@alexpott let me know if you want another re-roll and when you plan to commit this, if ever. I'll work on other things in the meanwhile. Ping me on IRC.

saki007ster’s picture

Assigned: Unassigned » saki007ster
saki007ster’s picture

FileSize
24.84 KB

re-rolled

saki007ster’s picture

Status: Needs work » Needs review
saki007ster’s picture

Assigned: saki007ster » Unassigned
ianthomas_uk’s picture

#2107533: Remove {menu_router} removes the user_is_* functions.

ianthomas_uk’s picture

Status: Needs review » Needs work

#2107533: Remove {menu_router} has been committed so this will need a reroll.

ianthomas_uk’s picture

Status: Needs review » Needs work

This is a good reroll of #76 (which was RTBC), except:

  1. +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
    @@ -965,8 +965,10 @@ protected function beforePrepareEnvironment() {
    -    global $user;
     
    +    global $conf;
    

    global $conf has been removed from HEAD

  2. +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
    @@ -1145,7 +1147,8 @@ protected function tearDown() {
    -    global $user;
    +
    +    global $conf;
    

    again

There is still global $user in:
\Drupal\Core\Form\FormBuilder
\Drupal\system\Tests\Common
\Drupal\system\Tests\Datetime
\Drupal\system\Tests\Entity
\Drupal\user\Tests\Views

And $GLOBALS['user'] in:
\Drupal\Core\Cron
\Drupal\history\Tests\Views

Some of these may warrant their own issues or be classsed as bootstrap/authentication.

longwave’s picture

Status: Needs work » Needs review
FileSize
22.11 KB
988 bytes

Fixed points in #86.

The remaining globals were tested separately in #39, not sure it's worth trying to remove more in this patch or in a followup, if there will be some left over anyway.

ianthomas_uk’s picture

Status: Needs review » Reviewed & tested by the community

OK, let's leave those for now and check again once bootstrap and authentication have been done. The reroll is good.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 7abce70 and pushed to 8.x. Thanks!

joelpittet’s picture

So happy to see this one getting in! Going to open another for the remaining from #62 and others that have sneaked in since.

Status: Fixed » Closed (fixed)

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