Commit graph

90 commits

Author SHA1 Message Date
Tim Starling
68c433bd23 Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.

General principles:
* Use DI if it is already used. We're not changing the way state is
  managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
  is a service, it's a more generic interface, it is the only
  thing that provides isRegistered() which is needed in some cases,
  and a HookRunner can be efficiently constructed from it
  (confirmed by benchmark). Because HookContainer is needed
  for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
  SpecialPage and ApiBase have getHookContainer() and getHookRunner()
  methods in the base class, and classes that extend that base class
  are not expected to know or care where the base class gets its
  HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
  getHookRunner() methods, getting them from the global service
  container. The point of this is to ease migration to DI by ensuring
  that call sites ask their local friendly base class rather than
  getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
  methods did not seem warranted, there is a private HookRunner property
  which is accessed directly. Very rarely (two cases), there is a
  protected property, for consistency with code that conventionally
  assumes protected=private, but in cases where the class might actually
  be overridden, a protected accessor is preferred over a protected
  property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
  global code. In a few cases it was used for objects with broken
  construction schemes, out of horror or laziness.

Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore

Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router

setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine

Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-05-30 14:23:28 +00:00
Reedy
229b2c15e8 Fix a plethora of class and function call case mismatches
Bug: T231412
Change-Id: I597a25de3294a6673424f30475760280ef209a8a
2020-05-26 14:14:46 +01:00
jenkins-bot
d9da0268db Merge "Update setTemporaryHook() to use scopedRegister()" 2020-05-11 04:55:18 +00:00
Nikki Nikkhoui
8d378e977c Update setTemporaryHook() to use scopedRegister()
The new HookContainer.php introduces a scopedRegister() method for
temporarily setting hooks. Let's use that in MediaWikiUnitTestCase
and MediaWikiIntegrationTestCase instead of directly accessing
global $wgHooks to do so.

Also introduces setTemporaryHook() and removeTemporaryHook()
methods in MWIntegrationTestCase for easily adding/removing of
temporary hooks.

Bug: T250300
Change-Id: I8cefd41b66f882c53646b76de76c51f0d8730f72
2020-05-11 14:12:00 +10:00
jenkins-bot
c7ca79509d Merge "MediaWikiIntegrationTestCase remove temporary handler" 2020-05-11 03:28:14 +00:00
Reedy
ead6b1e53f Fix some PSR12.Properties.ConstantVisibility.NotFound in tests/phpunit/
Change-Id: I0f678049dc274f0cd29f543bb293c33da51d8529
2020-05-09 23:55:09 +00:00
Timo Tijhof
355a6cd4c3 phpunit: Add error_reporting/AtEase check to MediaWikiUnitTestCase
This existed on MediaWikiIntegrationTestCase, but not on
MediaWikiUnitTestCase. As a result of that, I spent about four
days tracking down a dangling AtEase::suppressWarnings with
missing AtEase::restoreWarnings (as part of Ib6758d724c).

Move it to the common MediaWikiTestCaseTrait instead so that we
get it on unit/ as well.

Example:

> There was 1 failure:
>
> 1) Pbkdf2PasswordTest::testCryptThrows
> PHP error_reporting setting found dirty.
> Did you forget AtEase::restoreWarnings?

Change-Id: I7dc3fe90385c8066b89a5e06c55f5455edfbb4ca
2020-05-09 01:48:45 +00:00
Nikki Nikkhoui
0194507035 MediaWikiIntegrationTestCase remove temporary handler
Add the ability to remove a temporary hook handler. This function
was initially part of https://gerrit.wikimedia.org/r/c/mediawiki/core/+/589097
but needed to be split up because it was part of a circular dependency of
patchsets, where
https://gerrit.wikimedia.org/r/c/mediawiki/extensions/CirrusSearch/+/595222
was dependent on it.

Bug: T250300

Change-Id: I0c41f50a6416fd2fe5607278e31c0a07f28dd3d8
2020-05-08 14:19:27 -07:00
jenkins-bot
98b3bef602 Merge "[WIP] Workaround PHP 7.4>= memory leak in tests" 2020-05-08 10:41:02 +00:00
jenkins-bot
3cfaa194ed Merge "Introduce UserOptionsManager and DefaultOptionsManager" 2020-05-01 20:22:56 +00:00
jenkins-bot
8495a356ea Merge "MediaWikiIntegrationTestCase: Remove use of Revision objects" 2020-05-01 18:13:37 +00:00
Petr Pchelko
788331c48a Introduce UserOptionsManager and DefaultOptionsManager
This converts user options management to a separate
service for use in DI context.

User options are accessed quite early on in installation
process and full-on options management depends on the
database. Prior we have protected from accessing the DB
by setting a hacky $wgUser with 0 id, and relying on the
implementation that it doesn't go into the database to
get the default user options. Now we can't really do that
since DBLoadBalancer is required to instantiate the options
manager. Instead, we redefine the options manager with
a DefaultOptionsManager, that only provides access to
default options and doesn't require DB access.

UserOptionsManager uses PreferencesFactory, however
injecting it will produce a cyclic dependency. The problem
is that we separate options to different kinds, which are
inferred from the PreferencesFactory declaration for those
options (e.g. if it's a radio button in the UI declaration,
the option is of multiselect kind). This is plain wrong,
the dependency should be wise versa. This will be addressed
separately, since it's requires larger refactoring. For now
the PreferencesFactory is obtained on demand. This will be
addressed in a followup.

Bug: T248527
Change-Id: I74917c5eaec184d188911a319895b941ed55ee87
2020-04-28 15:42:43 -07:00
Máté Szabó
239672b98a [WIP] Workaround PHP 7.4>= memory leak in tests
On PHP 7.4 and above, a memory leak can be observed in
ServiceContainer::loadWiringFiles when running PHPUnit tests. This patch aims to
work around the leak by changing
MediaWikiIntegrationTestCase::installMockMwServices to cache and reuse the
original service wirings if no config overrides were supplied. This
significantly reduces the amount of times that the wiring closures need to be
included; on PHP 8, it caused memory usage to drop to 894.25 MB from 4+ GB when
running the PHPUnit tests for MediaWiki core.

However, it seems to be causing some test failures and so it is currently a WIP.

Bug: T247990
Change-Id: I24971221b8accf78d61479d286e907c1111212a0
2020-04-28 23:44:31 +02:00
Tim Starling
df05164822 More flexible deprecation warnings
* Split MWDebug::sendRawDeprecated() from MWDebug::deprecated(). The new
  function can be used to send arbitrary messages to the deprecation
  log, rather than being constrained by the fixed format of
  MWDebug::deprecated().
* Split formatCallerDescription() from sendMessage() to allow the caller
  of sendRawDeprecated() to do its own caller formatting.
* Use the new function in MWLBFactory::logDeprecation()
* In tests, replace the ugly implementation of hideDeprecated() with one
  that works by setting a list of regexes to filter. hideDeprecated()
  now filters deprecation warnings that are a string match to the
  supplied function. filterDeprecated() can be used to filter a regex,
  and is intended to be used to filter warnings sent via
  sendRawDeprecated(). The filter list is reset at the start of each
  test, instead of leaking across tests as before.

Change-Id: I0d0df86db2e61cdd1769426bfa7bad4c2ae5e977
2020-04-22 12:37:22 +10:00
DannyS712
6b2a4dd1fe MediaWikiIntegrationTestCase: Remove use of Revision objects
Only support passing RevisionRecord or int, remove use of
Revision::newFromId

Bug: T249021
Bug: T249561
Change-Id: Id4a8f64f239d0664865056887fe0a11c7e468d5f
2020-04-21 23:20:25 +00:00
Aaron Schulz
cbc700e186 rbms: optimize and rename truncateTable() to truncate()
Allow truncation of multiple tables. This also provides for
a way to avoid risky keywords like CASCADE for Postgres.

For Postgres, use RESTART IDENTITY, which has been supported
since Postgres 8.4.

Avoid TRUNCATE/DELETE queries for empty temp tables, which is
useful for integrations tests that frequently call this method.

Reorganize and tweak the regexes in Database::getTempWrites().
It now recognizes multi-table DROP/TRUNCATE (Postgres-style).

Change-Id: Idd49f118b20ea5a0f7a3e8c00369aabcd45dd44e
2020-04-21 01:26:18 -07:00
Tim Starling
2a83a3aed8 Disable mail in mediaWikiSetUp() instead of TestSetup
To disable mail in tests, use setTemporaryHook after the
service container setup, instead of the soon-to-be deprecated
Hooks::register().

Change-Id: I84ea69feb0ebf81855b745c14fdba50d4e121de8
2020-04-09 11:25:11 +10:00
Timo Tijhof
e0ed6df864 phpunit: Add setNullLogger() and make tests default to LegacySpi
== Motivation

Mute a log channel, for which the Logger object is injected by
service wiring, for a service that is overridden by default,
such as 'DBLoadBalancerFactory'. For that, calling setLogger()
mid-test would be too late.

== Changes

* Add a test-only method to LegacyLogger that makes it possible
  to change its `minimumLevel` attribute, thus making it turn
  itself into a NullLogger if raised to infinity. This is the
  same principle we use already for disabled log channels when
  using MediaWiki normally (see LegacyLogger::__construct).

* Previously, the developer's LocalSettings.php was loaded
  which includes the Spi configuration. This meant other Spi's
  could be configured which means we might not be dealing with
  a LegacyLogger object.

  Similar to what we do with ObjectCache and JobQueue already,
  make the default Spi in tests the same as the normal MW default.

* Add setNullLogger() which makes use of these two.

Bug: T248195
Change-Id: Ieade3585812de47342259afa765e230fff06f526
2020-04-07 22:00:13 +00:00
DannyS712
cf2c2a0778 Tests: Replace ::doDeleteArticle with WikiPage::doDeleteArticleReal
Not including WikiPageDbTestBase, which is testing the method

Bug: T248000
Change-Id: I1848a79be9e25759654053e88ef956269342f588
2020-03-25 11:34:17 -07:00
Aaron Schulz
e1a8c4e2a0 rdbms: add IMaintainableDatabase::truncateTable()
Use it in MediaWikiIntegrationTestCase for resetting tables.

Also create Database::resetSequencesForTable() helper method from
the resetSequenceForTable() methods in the SQLite/Postgres classes.

Change-Id: I20945e20590e69340b1ce75f6bb2f6972375b00c
2020-03-25 01:36:06 +00:00
Aaron Schulz
1238043eff Add MediaWikiIntegrationTestCase::assertArraySubmapSame()
Also fix PHPUnit 9 warning in PNGMetadataExtractorTest about $delta.

This should fix all of the integration test warning spam.

Bug: T244095
Change-Id: I0e2a76d5df2685ae5ad1498864e0b5f9db60c0cc
2020-03-19 15:53:59 -07:00
James D. Forrester
5ad77b9a2b MediaWikiIntegrationTestCase: Hard-deprecate assertType()
Already soft-deprecated in 726f10b, which also removed the
unused assertTypeOrValue() method without deprecation.

Change-Id: I0d417bf79f03e899dccfa469f639c04d4cf999cf
2020-01-20 12:39:15 +00:00
James D. Forrester
4f2d1efdda Coding style: Auto-fix MediaWiki.Classes.UnsortedUseStatements.UnsortedUse
Change-Id: I94a0ae83c65e8ee419bbd1ae1e86ab21ed4d8210
2020-01-10 09:32:25 -08:00
James D. Forrester
5e9fca47b9 Coding style: Auto-fix MediaWiki.Usage.PHPUnit*
Change-Id: I86fc55a4fc8ceafe368692173211bbcd6d8581d7
2020-01-10 10:17:12 +00:00
Gergő Tisza
035db6bb59
Share assertArrayEquals with MediaWikiUnitTestCase
assertArrayEquals is a generic assertion that does not have anything
to do with integration testing, and is quite useful for isolated
testing of classes which return unordered sets.

Change-Id: I45af77402a1bc922579aff14b21874ec8680e765
2020-01-07 16:05:40 -08:00
Daimona Eaytoy
726f10bf5d Deprecate and stop using assertType(OrValue)
*assertType is marked as deprecated, and should ideally be removed soon
(i.e. no hard deprecation to follow)
*Most usages of assertType in core were autofixed by using I8ef556b630812aeea77c5606713f53d9af609f1b
*assertTypeOrValue was removed because only used in SiteTest
(codesearch: https://codesearch.wmflabs.org/search/?q=assertTypeOrValue&i=nope&files=&repos=)
*SiteTest::assertTypeOrFalse was removed because unused

Bug: T192167
Change-Id: Icb3014b8fe7d1c43e64a37e0bdaaffec18bb482f
2019-12-15 00:07:59 +00:00
DannyS712
18647a56cd Fix MediaWikiIntegrationTestCase MWException text for using pages
Should say "When testing /with/ pages", rather than "which"

Change-Id: Id2c1409358349f548fc72ac815812ed0bd2b5c47
2019-12-14 00:27:50 +00:00
jenkins-bot
f6d7b482b3 Merge "Use nullable type in function signature" 2019-11-25 17:50:46 +00:00
jenkins-bot
9b7812f8b8 Merge "phpunit: Remove 6And8Compat trait from base classes" 2019-11-21 18:30:05 +00:00
Daimona Eaytoy
3228629f34 phpunit: Remove 6And8Compat trait from base classes
We've decided to switch to PHPUnit 7 to make things easier. This also
means that we shouldn't suppress PHPUnit 8 warnings, otherwise we cannot
know if tests are already compatible with PHPUnit 8.

Note that the trait is not deleted, because it may still be necessary
for single tests -- we're just not including it by default. However, it
is now deprecated, and its use is discouraged.

Bug: T192167
Change-Id: If90e6b6ffe03dfb117ec2b4b13c79983fe1bd0f2
2019-11-21 17:42:39 +00:00
Florian
d7c72bcbe5 Fix broken running tests locally in IDE
The class PHPMaintClass is meant to be loaded when running phpunit.php
which is only done when running tests with it. When running tests by
using the bootstrap file only, e.g. in an IDE, this class will not be
available. Relying on it in other classes will therefore break them.

Moving the required parts to another outside class and add it to the
test autoloader. My feeling says, that adding the PHPMaintClass to the
autoloader says "NOOOO", that's why I added a new one.

There also seems to be some CI builds failing because of that:
https://integration.wikimedia.org/ci/hob/quibble-vendor-mysql-php72-docker/30642/console

Bug: T151101
Change-Id: I33e27009657a951173694fc847973560a1ce967b
2019-11-21 16:28:12 +00:00
Umherirrender
a1f0be1c21 Use nullable type in function signature
Enable the sniff

Change-Id: I9fff860706b0ffb99bb9e78ff26d15bc7c87bdb4
2019-11-20 19:20:38 +01:00
Max Semenik
830548509d Add PHPUnit6And8Compat trait
This is moved out of the main PHPUnit upgrade commit to allow
extensions to silence their warnings beforehand, to avoid breaking
their tests.

Bug: T192167
Change-Id: I60379a933a3a1b018d9f5ef6b51019002389dda3
2019-11-19 00:48:57 -08:00
jenkins-bot
a677dedf53 Merge "phpunit: Add typehints for PHPUnit 8" 2019-11-18 22:18:19 +00:00
jenkins-bot
2f53343fc3 Merge "phpunit: delete MediaWikiTestResult and TestRunner" 2019-11-18 22:17:49 +00:00
Daimona Eaytoy
1c20e1e032 phpunit: Add typehints for PHPUnit 8
Split from Ic14f5debc53e55d67146dc96279d26dfd52b4000.

Bug: T192167
Change-Id: Iec3b9f6f3702578ac76806e73ff0263aa15ac458
2019-11-17 15:15:24 +01:00
Daimona Eaytoy
a184478c14 phpunit: delete MediaWikiTestResult and TestRunner
These were made final in PHPUnit8. Since there seems to be no easy way
to work around that, partially revert
Ibcaf9ca81c8dc63cce6dc6f6fb1fffee19f8804e and start using static
properties again (cfr. T192167#5550034).

Split from Ic14f5debc53e55d67146dc96279d26dfd52b4000.

Bug: T192167
Change-Id: I3fe163738662ae41eb275a0327cb33187290a70f
2019-11-17 15:02:54 +01:00
Umherirrender
97ed35e2e8 Add missing @param and @return to documentation
Change-Id: I48ee838a7ebf4f25a4883d4c7886b39c2d3916af
2019-11-16 13:45:55 +01:00
Zoranzoki21
5aa721c08b Remove prepareServices function from MediaWikiIntegrationTestCase.php as it is deprecated since 1.32
Change-Id: I985d9bdbf69f7c15ab4175bfdf1ff69045974cfa
2019-11-15 04:41:34 +00:00
Daimona Eaytoy
e8664ff7a7 Remove PHPUnit4And6Compat from Test classes
No repo should be using PHPUnit4 features anymore. Removing this trait
from the test classes is the first step towards the complete removal,
see I84694e32c06f1f0f9fb8398b1f2b92d3df599ddb.

Bug: T192167
Change-Id: I60e66259ea920e84573ae99293b005306876b325
2019-11-10 14:08:09 +01:00
Timo Tijhof
0da1ecfd89 profiler: Remove ProfilerOutputDb and profileinfo.php entry point
Bug: T231366
Change-Id: I211c8192200d9af00914f9847608a6daf2898f91
2019-11-06 15:28:00 -05:00
Max Semenik
48a323f702 tests: Add explicit return type void to setUp() and tearDown()
Bug: T192167
Depends-On: I581e54278ac5da3f4e399e33f2c7ad468bae6b43
Change-Id: I3a21fb55db76bac51afdd399cf40ed0760e4f343
2019-10-30 14:31:22 -07:00
jenkins-bot
3bef9f4e82 Merge "Drop MediaWikiServices::resetLanguageServices()" 2019-10-30 12:14:25 +00:00
jenkins-bot
a1cc2a6e12 Merge "Hard-deprecate LockManagerGroup::getDefault/getAny" 2019-10-30 12:02:39 +00:00
Aryeh Gregor
27a8523cad Drop MediaWikiServices::resetLanguageServices()
There is no reason this should be necessary. Resetting services should
do the trick, together with Language::$mLangObjCache while it exists.
Resetting only a subset of services is fragile and shouldn't be done,
because one of the reset services might later be injected into a service
that's not reset, resulting in bugs.

Change-Id: I8c72b466ba7337649f9256ee6c078c76163f9785
2019-10-29 15:28:18 +02:00
Aryeh Gregor
a5b6268761 Reset services in setService()
Otherwise bugs can be caused by services that hold stale service
references. These are very annoying to track down.

Change-Id: Id9e5a8a0fa3493af0855f4bf1816175ecff6136a
2019-10-29 15:26:34 +02:00
Aryeh Gregor
b0795a2550 Hard-deprecate LockManagerGroup::getDefault/getAny
These appear to be unused. Moreover, their behavior doesn't match their
documentation. getDefault() claims to return NullLockManager if no lock
manager could be found, but really returns it if there's no lock manager
named 'default'. getAny() claims to throw if no lock manager can be
found, but actually throws if no lock manager is named 'default' or
'fsLockManager'. The behavior can be easily replicated by just using
get() yourself and catching any exception.

Bug: T234227
Change-Id: Iad083847f45d6e017a3f7dbfece1f9c155c5efd6
2019-10-29 14:48:44 +02:00
Lucas Werkmeister
cd7e60a535 tests: Reset $wgContLang consistently in PHPUnit tests (take 2)
This a second attempt at 09407afd74, which was reverted due
to breaking Wikibase integration tests.

The original commit had two separate typos that made it not work at all.
Additionally, some things were apparently expecting $wgContLang to be an
actual Language and not a StubObject. To solve that, we just set it
directly and don't use a StubObject. Initialization should be cheap, and
it would probably have been unstubbed on every test anyway.

Bug: T231799
Change-Id: I6d68a22e53e17458d0a63c13bd97c19baff4d1c7
2019-10-29 13:31:47 +02:00
jenkins-bot
979465a0e1 Merge "Convert FileBackendGroup to service" 2019-10-29 09:46:26 +00:00
Aryeh Gregor
0de9c47b50 Remove Language::factory and getParentLanguage use
Change-Id: I11f8801ef47ec1a1f63d840116e69667e6f3ae3c
2019-10-27 12:34:28 +02:00