Page MenuHomePhabricator

Allow subclasses of ApiBase to be used in phpunit tests without MediaWiki being initialized
Closed, InvalidPublic

Description

Background:
MediaWiki defines two kinds of phpunit tests: "integration" tests that require MediaWiki to be initialized, and "unit" tests that doe not require any global state.

Such "unit" tests can set up any global state (such as global variables) that the code under test relies on. However, this is not easily possible when MediaWikiServices::getInstance() is used, see T243515.

Problem:
Currently, action api modules (subclasses of ApiBase) cannot be used in a "unit" test, because they rely on global state. This forces all tests for api modules, as well as any test that needs ApiMain, to be an "integration" test, which slows them, down a lot.

Solution:

  • Option 1: allow unit tests to set up mock services. See T246907 for some discussion on this.
  • Option 2: convert ApiBase and ApiMain to use dependency injection, to the point where they no longer fail due to missing global state when being instantiated without MediaWiki being initialized.

Event Timeline

This should already be possible with current DI

Change 631818 had a related patch set uploaded (by DannyS712; owner: DannyS712):
[mediawiki/core@master] Add pure unit tests for ApiUnblock

https://gerrit.wikimedia.org/r/631818

The attached patch for a unit test on ApiUnblock looked good, but did require a fair amount of mocking in the test. Is there a way we could simplify this for future tests? Or given that not all API modules require specific services (per ApiMain::$Modules) maybe other unit tests would require less mocking?

Change 631818 merged by jenkins-bot:
[mediawiki/core@master] Add pure unit tests for ApiUnblock

https://gerrit.wikimedia.org/r/631818

Change 642087 had a related patch set uploaded (by DannyS712; owner: DannyS712):
[mediawiki/core@master] Inject UserCache into ApiUnblock

https://gerrit.wikimedia.org/r/642087

Change 642087 merged by jenkins-bot:
[mediawiki/core@master] Inject UserCache into ApiUnblock

https://gerrit.wikimedia.org/r/642087

I no longer think this is necessary.