While writing unit test through DrupalWebTestCase we discovered that the global $user variable is being replaced with the user object from UID 1.

The global user within the test contains
[uid] => 1 [name] => placeholder-for-uid-1
even without logging in.

It appears simpletest.module treats you as uid 1 even if you didn't log in yet through drupalLogin().
It seems more logic that it should load UID 0 (anonymous) if you don't log in within the test case.

EDIT:

  • changed the issue title to be more clear.
  • Moved other content to seperate issue.
CommentFileSizeAuthor
#3 use_uid0_to_setUp_test-2500399.patch683 bytesNovitsh
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Novitsh’s picture

Title: Testing setUp user variable overridden with uid 1 » setUp global user loses data due to login with uid 1
Issue summary: View changes
Novitsh’s picture

Title: setUp global user loses data due to login with uid 1 » setUp global user assumes you're UID 1 due to login
Issue summary: View changes
Novitsh’s picture

Created a patch to use UID 0 instead of UID 1.

Novitsh’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 3: use_uid0_to_setUp_test-2500399.patch, failed testing.

nico.knaepen’s picture

Good remark. And also the setup function seems to contain too much logic besides what's necessary for testing.

nico.knaepen’s picture

Tried the following without success:

<?php
$this->loggedInUser = user_load(0);
?>

The user object then still contains a username:
placeholder-for-uid-1

The following workaround seems to work for now:

<?php
    // Log on as anonymous user.
    $form_state['uid'] = 0;
    user_login_submit(array(), $form_state);
?>