Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#3 | use_uid0_to_setUp_test-2500399.patch | 683 bytes | Novitsh |
Comments
Comment #1
Novitsh CreditAttribution: Novitsh as a volunteer commentedComment #2
Novitsh CreditAttribution: Novitsh as a volunteer commentedComment #3
Novitsh CreditAttribution: Novitsh as a volunteer commentedCreated a patch to use UID 0 instead of UID 1.
Comment #4
Novitsh CreditAttribution: Novitsh as a volunteer commentedComment #6
nico.knaepen CreditAttribution: nico.knaepen commentedGood remark. And also the setup function seems to contain too much logic besides what's necessary for testing.
Comment #7
nico.knaepen CreditAttribution: nico.knaepen commentedTried the following without success:
The user object then still contains a username:
placeholder-for-uid-1
The following workaround seems to work for now: