Extension:MWUnit/Contexts

From mediawiki.org

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.

There is a multitude of ways to get information about the current user. Therefore it is possible some extensions will still output user-specific information even when running in a canonical context.

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[edit]

This requires the user running the test to have the mwunit-mock-user right and that the $wgMWUnitAllowRunningTestAsOtherUser is set to true

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.

Any parser function run in the test will also be run as if the mocked user were logged in, with the rights of that user. Any state changing parser functions will be executed as if the mocked user performed them. This means that parser functions that are restricted to certain user groups will also be available to users with the mwunit-mock-user right since they can simply run those parser functions via a mocked test case.

See also[edit]