DrupalWebTestCase::drupalLogout() makes to GET request:

    $this->drupalGet('user/logout');
    $this->drupalGet('user');

The second drupalGet() was introduced in #721086: Create tests for system actions, clean up token replacement, clean up triggers and also discussed in #363580: OpenID login fails when in maintenance mode, but it isn't really explained why it is necessary.

Also, the current code silently proceeds if the user is not logged in when drupalLogout() is called. I don't think such forgiving behaviour is helpful while testing. Or there may be some use case that I haven't thought of. Anyway, here is a patch awaiting your comments.

Comments

Status: Needs review » Needs work

The last submitted patch, user-logout-1.patch, failed testing.

c960657’s picture

Status: Needs work » Needs review
StatusFileSize
new2.75 KB

Two test classes needed some adjustments. I think they are reasonable adjustments (the two in menu.test were bug fixes IMO).

sun’s picture

Status: Needs review » Needs work

Excellent findings + patch!

+++ b/core/modules/simpletest/drupal_web_test_case.php
@@ -1265,8 +1265,8 @@ class DrupalWebTestCase extends DrupalTestCase {
+    $this->assertResponse(200, t('User was logged in.'));

s/in/out/

c960657’s picture

StatusFileSize
new2.75 KB

Direction changed :-)

sun’s picture

Status: Needs work » Reviewed & tested by the community

Awesome patch. Thanks a lot, @c960657!

dries’s picture

Status: Reviewed & tested by the community » Fixed

Looks good. Committed to 8.x. Thanks.

sun’s picture

Version: 8.x-dev » 7.x-dev
Status: Fixed » Patch (to be ported)
Issue tags: +Needs backport to D7

This can actually be backported. It's a (very) small, but still, a test performance improvement.

tim.plunkett’s picture

Version: 7.x-dev » 8.x-dev
Status: Patch (to be ported) » Reviewed & tested by the community

This did not get pushed yet, setting back.

tim.plunkett’s picture

Version: 8.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)
tim.plunkett’s picture

Version: 7.x-dev » 8.x-dev
Status: Patch (to be ported) » Needs review
StatusFileSize
new760 bytes

Not sure if I'm misunderstanding this, because I don't know how this test is passing.

The code in this patch directly follows this hunk:

+++ b/core/modules/system/tests/menu.testundefined
@@ -257,8 +256,8 @@ class MenuRouterTestCase extends DrupalWebTestCase {
   function testAuthUserUserLogin() {
-    $this->loggedInUser = $this->drupalCreateUser(array());
-    $this->drupalLogin($this->loggedInUser);
+    $web_user = $this->drupalCreateUser(array());
+    $this->drupalLogin($web_user);
 
     $this->drupalGet('user/login');
tim.plunkett’s picture

Version: 8.x-dev » 7.x-dev
StatusFileSize
new2.7 KB

Duh. I clearly did not read how drupalLogin worked. Reroll for D7.

Status: Needs review » Needs work

The last submitted patch, drupal-1545930-11.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review
StatusFileSize
new3.52 KB

There were some oddities in AccessDeniedTestCase that were cleaned up in #1373142: Use the Testing profile. Speed up testbot by 50%.

A chunk of the same code, including drupalLogout(), was called twice in a row.

Status: Needs review » Needs work

The last submitted patch, drupal-1545930-13.patch, failed testing.

tim.plunkett’s picture

It seems that testUserActionAssignmentExecution is logging in as a user, blocking the user it's logged in as, unblocking it while still theoretically logged in as the blocked user, creating a new user with the same variable name as the newly unblocked user, and trying to log in as that new user.

So I'm a little unsure how to resolve that.

c960657’s picture

You can reset $this->loggedInUser explicitly before calling drupalLogin() again. I did something similar in D8 patch.

tim.plunkett’s picture

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

Well this feels like cheating, but okay.

sun’s picture

Status: Needs review » Reviewed & tested by the community

Works for me. Trigger is a dead end either way. ;)

David_Rothstein’s picture

Status: Reviewed & tested by the community » Needs review

If this change broke several core tests, then how is it safe for D7 backport? Won't it break contrib tests too?

sun’s picture

Status: Needs review » Needs work
+++ b/modules/simpletest/drupal_web_test_case.php
@@ -1240,8 +1240,8 @@ class DrupalWebTestCase extends DrupalTestCase {
-    $this->drupalGet('user/logout');
-    $this->drupalGet('user');
+    $this->drupalGet('user/logout', array('query' => array('destination' => 'user')));
+    $this->assertResponse(200, t('User was logged out.'));

You're right. The assertResponse() cannot be backported, only the combination into a single drupalGet() can.

Let's make sure to have a patch that only contains that change, so we can see whether anything breaks with that only.

tim.plunkett’s picture

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

Fair point.

sun’s picture

StatusFileSize
new810 bytes

This needs to pass or this cannot be backported.

Status: Needs review » Needs work

The last submitted patch, drupal-1545930-21.no-adjustments.patch, failed testing.

sun’s picture

Version: 7.x-dev » 8.x-dev
Status: Needs work » Fixed
Issue tags: -Needs backport to D7

Alrighty.

Status: Fixed » Closed (fixed)

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

Umayal’s picture

Title: drupalLogout() should only do one request, not two » drupalLogout()

how to test user logout?
I am using following code to test the logout function.
how to get $this value or user value.
$this->drupalGet('user/logout', array('query' => array('destination' => 'user')));
$this->assertResponse(200, t('User was logged out.'));
I am new for testing.I don't how to test in drupal.

sun’s picture

Title: drupalLogout() » drupalLogout() should only do one request, not two

This issue is closed for a long time already, and is not a support ticket. See http://drupal.org/simpletest for the testing handbook.

Restoring issue title.