Commit graph

125 commits

Author SHA1 Message Date
jenkins-bot
5752dcbe0b Merge "Reset fake timestamps for unit tests too" 2021-04-14 21:45:02 +00:00
DannyS712
438b3924b4 Reset fake timestamps for unit tests too
Discovered in another patch that while integration
tests automatically reset fake timestamps afterwards,
unit tests don't. Add a shared method to MediaWikiTestCaseTrait
to reset for both integration tests and unit tests

Do the same for TypeDefTestCase

Change-Id: I677aec4e60894053fc554f2e13b069fb599858f2
2021-04-14 21:01:33 +00:00
jenkins-bot
9c74a067c3 Merge "Factor out rollback logic from WikiPage" 2021-04-14 20:22:27 +00:00
Petr Pchelko
46db19ecdf Factor out rollback logic from WikiPage
Change-Id: I95da91875fcf2f53143c315560e35ccd5ffbf4b3
2021-04-14 11:49:07 -07:00
DannyS712
ede7dc8b68 Convert DiffHistoryBlobTest to pure unit test
No integration needed, though it did rely on
checkPHPExtension which was only available in
MediaWikiIntegrationTestCase, moved to
MediaWikiTestCaseTrait

Also put under /includes/historyblob/ to match covered class

Change-Id: I0bfe3e44bd45535c18f5ee2534430330586a1c4c
2021-04-14 02:09:51 +00:00
James D. Forrester
bdddfb92ba Drop wgContLang, deprecated in 1.32
Bug: T245940
Depends-On: Ib7fe7318100c0aadc3aa759416bf787913a9b788
Change-Id: I75c3b6715abd5eaf3619337cab8b1844e9a8349a
2021-03-08 13:27:28 -06:00
Umherirrender
a1de8b8700 Tests: Mark more more closures as static
Result of a new sniff I25a17fb22b6b669e817317a0f45051ae9c608208

Bug: T274036
Change-Id: I695873737167a75f0d94901fa40383a33984ca55
2021-02-09 02:55:57 +00:00
Reedy
729f20afc8 Tests: Mark more closures as static
Bug: T274036
Change-Id: I911d3041cebe417d162934223b46ea295c6d20e3
2021-02-07 13:26:56 +01:00
Cindy Cicalese
21699acebd Allow expectDeprecation() in multiple tests in the same test suite
Without this, expectDeprecation() will not work if the same
deprecation is emitted in more than one test in the same test suite.

Change-Id: I850a072cc61437d1ab33af082ee99534bc6deee6
2021-02-02 08:32:26 -06:00
Umherirrender
7691dbeca9 Add missing @param and @return to documentation in tests
Change-Id: Ic663e81cca0bf007804a70772250914a85f1fef4
2021-01-22 19:57:25 +01:00
C. Scott Ananian
665eae14ac ParserTestRunner: share more code w/ MediaWikiIntegrationTestCase
Refactor the database setup code to share more code between
ParserTestRunner and MediaWikiIntegrationTestCase.  Made
`::setupAllTestDBs` static so it can be reused from
ParserTestRunner.

Made ParserTestRunner::addArticle more like
MediaWikiIntegrationTestCase::addCoreDBData().  Some additional
refactoring work could be done here in the future to share more code.

After the refactoring the ParserTestTables hook is no longer necessary
and so has been (soft) deprecated.  MediaWikiIntegrationTestCase
clones all database tables, so ParserTestRunner no longer needs to ask
extensions for a list of specific tables it should clone.  Cleaning up
the handful of extensions which define this hook will be left to a
future patch set.

Change-Id: I5124789fac333a664b73b4b4a1e801ecc0a618ca
2021-01-07 23:31:12 -05:00
C. Scott Ananian
6328600cdd MediaWikiIntegrationTestCase: use static:: for ::getTestSysop()/::getTestUser()
Also fix two places where we were using `$this->` in a corner case.

Using `static::` lets subclasses override these method definitions,
at least in theory.

Change-Id: I76a6d10ce463a5bc79e14051a31f469668924494
2020-12-24 09:52:54 -05:00
Daimona Eaytoy
6f7132cbcf Remove MediaWikiIntegrationTestCase::assertType
Deprecated since 1.35, not compatible with PHPUnit 8. This could've
happened earlier, but here we go.

Unused in known repos (except for the sniff looking for this usage):
https://codesearch.wmcloud.org/search/?q=assertType%5C(&i=nope&files=php&repos=

Bug: T192167
Change-Id: I479cf71d0941c35096d21686077b4d1cbc4f898d
2020-12-18 19:23:14 +00:00
jenkins-bot
02342b9065 Merge "Don't access $wgRequest from User" 2020-12-16 05:06:16 +00:00
Tim Starling
6b2a52181f Don't access $wgRequest from User
Some User methods fail if they are called before $wgRequest is
set. But according to the Setup.php comment, it is only set for b/c.
The global request object can be lazy-initialised at any time.

This is sufficient to avoid T263911 (loss/obfuscation of the $wgServer
error message).

In tests, try to keep $wgRequest and RequestContext::$request in sync.
Introduce MediaWikiIntegrationTestCase::setRequest() which sets both at
once, and use that instead of setMwGlobals() or direct assignment.

BlockManagerTest was accidentally exploiting the fact that the global
context request and $wgRequest were separate objects. Making them the
same causes session cookies to appear in the response, breaking the
cookie counts. Use a new response for the test.

Bug: T263911
Bug: T245940
Change-Id: I2be99f7251a837bc6b62be0b152038157dec10f2
2020-12-16 12:21:00 +11:00
jenkins-bot
f49e8f4f56 Merge "Clean up temp files correctly after unit tests" 2020-12-12 10:33:19 +00:00
Umherirrender
2579ca623a build: Updating mediawiki/mediawiki-codesniffer to 34.0.0
Change-Id: I2fb18ddd4c144655a665792901e59f88bcd906dc
2020-12-07 14:55:24 +01:00
Umherirrender
98de078a4c Improve callable docs
Change-Id: I4cc9b2dbe285ad935a1dfd08effa673a39167483
2020-11-13 22:55:10 +01:00
Umherirrender
21536f9af0 Clean up temp files correctly after unit tests
Change-Id: Iba4e45d8c41d678d4ec2de442c105e03cf8d87e1
2020-10-31 01:07:52 +01:00
Thiemo Kreuz
1fc8d79ac6 Remove documentation that literally repeats the code
For example, documenting the method getUser() with "get the User
object" does not add any information that's not already there.
But I have to read the text first to understand that it doesn't
document anything that's not already obvious from the code.

Some of this is from a time when we had a PHPCS sniff that was
complaining when a line like `@param User $user` doesn't end
with some descriptive text. Some users started adding text like
`@param User $user The User` back then. Let's please remove
this.

Change-Id: I0ea8d051bc732466c73940de9259f87ffb86ce7a
2020-10-27 19:20:26 +00:00
daniel
70e6cd05b1 phpunit: allo WikiPage to be used with editPage().
The editPage() convenience function in MediaWikiIntegrationtestCase
should optionally accept a WikiPage instance as a parameter. This
allows edits to be made conveniently, without any WikiPage instance
held by the caller going out of sync.

Change-Id: I68ad6df5f6dc357658fe112957bea8b11cf2471e
2020-10-16 15:20:31 +02:00
Daniel Kinzler
aea8cb17bd Re-apply: "PHPUnit: prevent HTTP requests"
This reverts commit c17ddfd786.

This time, also: Trying to get mock http to work in parser test

Bug: T262443
Change-Id: Id708cdfa3d3c27f30543c6bf212df96e6b0a69e1
2020-10-09 16:19:31 +02:00
Kosta Harlan
c17ddfd786 Revert "PHPUnit: prevent HTTP requests"
This reverts commit b35ab283b2.

Bug: T265024
Bug: T262443
Change-Id: I65ddf30805ba8a2932af57ee28fc2c4700c3490f
2020-10-08 17:42:17 +00:00
daniel
b35ab283b2 PHPUnit: prevent HTTP requests
Tests should not make real HTTP requests. Mock out the
HTTPRequestFactory service to prevent this.

Bug: T262443
Depends-On: I63bfd54c3de55d678e8b862b85c0adfb5fc94d91
Change-Id: I1702c11928f8760bb41b41f4c7c04d7af03f62e2
2020-09-30 10:07:36 +02:00
C. Scott Ananian
33c506843b phpunit tests: Reset the SpecialPageFactory when Content Language changes
This is causing problems for Parsoid CI, as parser tests fail when
phpunit runs the tests at a different point than they are run in
core's CI due to the side-effects of content-language changes made in
other phpunit tests. (For example, phpunit runs all extension tests
after core tests, so the same parsertest can pass if included in core
and then fail when included in an extension.)

SpecialPageFactory::$aliases has a dependency on the current content
language, with no way to reset it other than to recreate the
SpecialPageFactory.

Change-Id: I278580ed5cf2c85403cbaf601f8af4753e14a9d0
2020-09-23 14:17:31 -04:00
DannyS712
6beacf6504 MediaWikiIntegrationTestCase docs: call parent::setUp()
The example should be a good one

Change-Id: I398f36eb427b52d8f9016d75bea90917c2dabee5
2020-09-17 01:10:45 +00:00
jenkins-bot
9f2abc7be1 Merge "MediaWikiIntegrationTestCase: Fix reference to method in documentation" 2020-09-13 10:34:07 +00:00
jenkins-bot
2ed425f39e Merge "PHPUnit integration tests: fix handling of null user in editPage()" 2020-09-13 09:24:18 +00:00
Ostrzyciel
81ac2fc852 PHPUnit integration tests: fix handling of null user in editPage()
The PHPDoc for the editPage method stated that if $user is null,
the test sysop user will be used instead. This is not the case in
practice, as the method simply passes null to the doEditContent
method.

As Ammarpad has pointed out in a comment, this confusion is due to
I5a10163 assuming the sysop would be used, which is incorrect.
It's also probably better for the default user to be a "regular"
user, not a sysop, so I changed it to getTestUser() and noted this
in the doc block.

Depends-On: I7a79e0eaa1617e4d87a8d615a5391723c0e30b6a
Change-Id: I9f77474f40e0f6901aa2c6f846e471b822636aa5
2020-09-13 09:42:52 +02:00
daniel
cd5ea0d5ef Unit tests: simplify setTemporaryHook()
Since we reset all services between test cases, no special effort is
needed to restore the state of the HookContainer.

Bug: T255056
Change-Id: I364f2943d56644f9c7c3acc053e6b585bd8f535c
2020-09-08 21:25:28 +02:00
daniel
aab5619d4d HookContainer: fix clear() and scopedRegister().
Previously, clear() and scopedRegister() would not disable hook handlers
registered using the new mechanism. Only old style hook handlers would
be suppressed.

The new implementation is based on tombstones: Instead of actually
removing registered handlers to override them, tombstone markers are
put into place to disable existing handlers until the tombstone is
removed again. This allows hook handlers to be disabled temporarily
in a consistent way.

This removes HookContainer::getOriginalHooksForTest(), which is
incompatible with the new logic.

Bug: T255056
Change-Id: I08c1824797ac60a5098a52b5781af8ac4dd38928
2020-09-08 21:07:59 +02:00
Ed Sanders
9e5ba1f4f8 Fix switch/case indentation per mediawiki coding conventions
See https://www.mediawiki.org/wiki/Manual:Coding_conventions#Indenting_and_alignment

Bug: T182546
Change-Id: Ia0dedba6f4676f3c703e3416858f445e4ade4ef6
2020-07-18 09:17:46 +00:00
Thalia
6d4fb2b1c7 MediaWikiIntegrationTestCase: Fix reference to method in documentation
Change-Id: Ibb279fd5bb9d80313ce5c8ab2303118a353aeb81
2020-07-13 17:26:02 +01:00
daniel
7572a152fc Annotate extensible classes.
This marks some base classes as stable for subclassing.

Bug: T247862
Change-Id: I7b77075849aa40960f2486b463e498a3ea8058e9
2020-07-09 11:07:38 +02:00
DannyS712
2f4b71fc6c Replace uses of Revision constants
Bug: T257010
Change-Id: Id63123e8b8becd31756d5b68ca11edb238ec8a59
2020-07-03 01:23:44 +00:00
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