phpunit test and $USER

phpunit test and $USER

by Itamar Tzadok -
Number of replies: 3

The default user id for $USER in the phpunit test is 0. I need it to be a privileged user such as admin so as to test certain privileged actions. In the test I've set $USER->id = 2 which seems to work. Is there a better/recommended way for achieving the same effect? smile 

Average of ratings: -
In reply to Itamar Tzadok

Re: phpunit test and $USER

by Petr Skoda -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers
Hi,

you can use $this->setAdminUser(), $this->setGuestUser() or general $this->setUser($user). If you need to create new user try $this->getDataGenerator()->create_user($userrecord).

Petr
Average of ratings: Useful (2)
In reply to Petr Skoda

Re: phpunit test and $USER

by Dan Poltawski -
Do we have any convenience methods for enrolling a user in a course?

I'm thinking of tests for a module:

* Create 5 users
* Enrol into test course
* Do tests in mod_mymod using those test users.