Tests can be run in a number of different contexts. By default, tests are run in a canonical context. This means the parser is initialized with an anonymous user and the content language. This is done to make the output of the test consistent across users and languages.
However, sometimes it is easier or necessary to run a test in the context of an existing user. For instance, because the template makes use of the MyVariables extension and the output of the template is dependent on who is viewing it. For this reason, tests can be run in a "user" context. This can achieved by using the "@context" annotation:
<testcase name="foo" group="bar" context="user"> ... </testcase>
When this annotation is present, the test is run as the current user. This means the parser is initialized with the User object of the current user.
Running a test as another user
mwunit-mock-userright and that the
$wgMWUnitAllowRunningTestAsOtherUseris set to
It is also possible to run a test as if another user is logged in. This can be achieved through the "@user" annotation:
<testcase name="foo" group="bar" context="user" user="Xxmarijnw"> ... </testcase>
The above snippet would run the test case as if the user
Xxmarijnw were running it in "user" context. When running a test as another user, the
$wgUser global is temporarily replaced, the RequestContext is altered and the Parser is initialized with the mocked user's User object.
mwunit-mock-userright since they can simply run those parser functions via a mocked test case.