Comments

damiankloip’s picture

Status: Active » Needs review
StatusFileSize
new4.5 KB

Status: Needs review » Needs work

The last submitted patch, 1: 2263339.patch, failed testing.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new4.91 KB
new1.16 KB

Need an actual account proxy object! I was certain I already did this change in the current user issue, maybe it got lost somewhere along the way.

Status: Needs review » Needs work

The last submitted patch, 3: 2263339-3.patch, failed testing.

damiankloip’s picture

We either wait on https://drupal.org/node/2263329 or merge it here.

blueminds’s picture

Status: Needs work » Needs review
StatusFileSize
new4.91 KB

reroll

Status: Needs review » Needs work

The last submitted patch, 6: 2263339-6.patch, failed testing.

damiankloip’s picture

The patch in #3 still applied fine?

blueminds’s picture

Yes, without any problem.

damiankloip’s picture

So what's the 'reroll' for? :)

blueminds’s picture

I get your point :)

damiankloip’s picture

damiankloip’s picture

6: 2263339-6.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 6: 2263339-6.patch, failed testing.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new6.26 KB
new1.35 KB

Fixed those failures. Adding current_user service to the setup in Drupal\system\Tests\KeyValueStore\StorageTestBase.

dawehner’s picture

+++ b/core/modules/system/lib/Drupal/system/Tests/Form/FormCacheTest.php
index 396f028..4a599c3 100644
--- a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php

+++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php
@@ -63,6 +67,10 @@ protected function setUp() {
+
+    $account_proxy = new AccountProxy(new AuthenticationManager(), new Request());
+    $account_proxy->setAccount(new AnonymousUserSession());
+    $this->container->set('current_user', $account_proxy);

Is there a simple reason why we have to construct the object for yourself and can't just use $this->container->get('current_user')->setAccount...?

damiankloip’s picture

Simple reason is this test extends from UnitTestBase, not DrupalUnitTestBase. So there is no container ready like normal.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Oh right.

znerol’s picture

+++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
@@ -1089,9 +1090,14 @@ private function prepareEnvironment() {
+    $account_proxy = new AccountProxy(new AuthenticationManager(), $request);
+
     // Run all tests as a anonymous user by default, web tests will replace that
     // during the test set up.
-    $this->container->set('current_user', new AnonymousUserSession());
+    $account_proxy->setAccount(new AnonymousUserSession());
+
+    $this->container->set('current_user', $account_proxy);
+

Is it really necessary to replace the current_user service here? Wouldn't it be enough to set the account on the proxy, in the same way like in restoreEnvironment?

damiankloip’s picture

But just above prepareEnvironment a new container is created:

    // Reset and create a new service container.
    $this->container = new ContainerBuilder();

So not sure how that would already have an account proxy object set on it?

znerol’s picture

I see.

+++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
@@ -1229,7 +1235,9 @@ private function restoreEnvironment() {
-    $this->container->set('current_user', $this->originalUser);
+    $current_user = $this->container->get('current_user');
+    $current_user->setAccount($this->originalUser);
+

What happens if this is removed entirely? That seems to affect the original container which is not supposed to be modified during the test.

damiankloip’s picture

Sorry but that is not a problem of this issue IMO. This is just converting the instances. That hunk is just a straight swap. If we think restoreEnvironment is doing something wrong. I think we should talk about that in a new concentrated issue?

znerol’s picture

Rereading the patch I still do not like these changes. Most consumers of the current_user just need the AccountInterface part of it. The AccountProxyInterface part is only important for those services which are supposed to swap out the user instances (i.e. authentication).

damiankloip’s picture

Well, also, e.g. the Cron class uses this. Just because tests do not rely on this now does not mean they wont in the future. If we usually have an AccountProxy in the container, we should also have this in the container for tests too. Otherwise, first implementation that needs it in there will have to make this change anyway. You want as few discrepancies in the test env as possible, no? You could encounter subtle issues that tests may not catch if the real life implementation is actually using the proxy and not just a straight up account.

dawehner’s picture

I really doubt that it is worth to fight here ...

znerol’s picture

Everything which calls AccountProxyInterface::setAccount() is potentially very dangerous. Only those services which really need to change the user should declare a dependency on AccountProxyInterface. All the others should stick with AccountInterface, therefore it is not clear to me why it is desirable to change the mocks in CommentLockTest, LocaleLookupTest and ViewUIObjectTest.

Perhaps it makes sense to have an AccountProxy in DUTB. But even there I think it would be legit to delegate the responsibility of setting up a real AccountProxy to the tests which cannot get by with a simple account but genuinely require the proxy.

dries’s picture

Would love to hear other people's thoughts on znerol's feedback.

@znerol: do you think this needs to be resolved in this patch or is this something that could be done in a follow-up?

xjm’s picture

effulgentsia’s picture

Component: markup » simpletest.module
Status: Reviewed & tested by the community » Needs review

I agree with #22 regarding TestBase, and with #26 regarding the specific test changes as out of scope for this issue unless there's a reason they're needed.

znerol’s picture

damiankloip’s picture

If you are mocking the container you should mock the services that would actually be in the container. If you are unit testing and just passing parameters into a constructor, use whatever you like to satisfy the interface.

But as mentioned in irc before, if zenerol feels that strongly about these few tests, do whatever gets it in. This was meant to be a quick issue tbh.

znerol’s picture

damiankloip’s picture

StatusFileSize
new3.63 KB
new2.25 KB

@znerol, so if we revert those parts, this is good in your opinion now?

znerol’s picture

I prefer getting in #2196241: Remove string translation services from TestBase container which will remove all services from TestBase. I do not see a benefit in changing FormCacheTest and StorageTestBase because of the reasons stated in #26.

damiankloip’s picture

Status: Needs review » Postponed
mgifford’s picture

Status: Postponed » Needs review
Related issues: +#2196241: Remove string translation services from TestBase container

@znerol it's fixed now.

almaudoh’s picture

StatusFileSize
new971 bytes

Reroll. Nothing much left in it. Related: #287292: Add functionality to impersonate a user

znerol’s picture

Oh, indeed. In my point of view #37 should be incorporated into #287292: Add functionality to impersonate a user or in a followup thereof.

znerol’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
mgifford’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new989 bytes

Let's try this again. A very simple re-roll.

damiankloip’s picture

Status: Needs review » Reviewed & tested by the community

Yep, looks like this is the only usage now that is not in a unit test.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 40: fix_all_current_user-2263339-40.patch, failed testing.

mitrpaka’s picture

Status: Needs work » Needs review
StatusFileSize
new985 bytes

Re-roll once again.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Used to be RTBC already.

webchick’s picture

Priority: Major » Normal
Status: Reviewed & tested by the community » Fixed

Ok, I believe znerol's feedback has been incorporated via other issues, so this should be safe to commit. Also, this seems like just a code clean-up, so no need for it to be major. It does make the code more robust, however, by switching to the user switching API, so seems legit as far as https://www.drupal.org/contribute/core/beta-changes goes.

Committed and pushed to 8.0.x. Thanks!

  • webchick committed b422ee7 on 8.0.x
    Issue #2263339 by damiankloip, blueminds, mgifford, xjm, almaudoh,...
webchick’s picture

Status: Fixed » Needs work

Oops. Based on dawehner's feedback in IRC, this should actually be using the new account_switcher service instead.

Rolled back for now. Should be an easy-ish fix tho.

  • webchick committed 0a4d2b5 on 8.0.x
    Revert "Issue #2263339 by damiankloip, blueminds, mgifford, xjm,...
mitrpaka’s picture

Status: Needs work » Needs review
StatusFileSize
new946 bytes
new711 bytes

Updated patch with account_switcher service.

The last submitted patch, 43: fix_all_current_user-2263339-43.patch, failed testing.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

+1

almaudoh’s picture

@@ -88,7 +89,7 @@ function testCacheToken() {
    * Tests the form cache without a logged-in user.
    */
   function testNoCacheToken() {
-    $this->container->set('current_user', new UserSession(array('uid' => 0)));
+    \Drupal::service('account_switcher')->switchTo(new AnonymousUserSession());

It's always a good idea to end calls to ::switchTo() with a corresponding ::switchBack() after the code block that was executed as another user. Though that would not affect this test, because the entire container is rebuilt anyway for the next test, it's good to demonstrate good coding practice in core.

webchick’s picture

Status: Reviewed & tested by the community » Needs work

That sounds like a needs work.

mitrpaka’s picture

Status: Needs work » Needs review
StatusFileSize
new1.39 KB
new1.09 KB

Patch updated based on #53. Thanks.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Fair

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Great, thanks.

Committed and pushed to 8.0.x (for real this time :)). Thanks!

  • webchick committed 9a504cb on 8.0.x
    Issue #2263339 by damiankloip, mitrpaka, blueminds, almaudoh, mgifford,...

Status: Fixed » Closed (fixed)

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