Commit graph

44 commits

Author SHA1 Message Date
mainframe98
3fd374e974 tests: Move assertHTMLEquals to the test case trait
This allows accessing it in both integration and unit tests.

Bug: T310514
Change-Id: I5c97364cbdc81fe67f156cffb04b252af7ccf6ff
2022-06-13 15:32:57 +00:00
daniel
874ab55de8 assertStatusOK() should not report warnings.
The output generated by assertStatusOK() when the status is not OK
should not include warnings, only errors. isOK() will return true even
if there are warnings, so the presense of warnings is not relevant to
the test failure, and should not be reported.

Change-Id: Ida32210e62636960671282883eba86ffe820edd6
2022-06-03 13:44:11 +02:00
Aryeh Gregor
472a914c63 Refactor URL-parsing global functions to class
The new class, UrlUtils, is usable standalone or as a service. Using it
as a service will just automatically load a few settings from site
configuration.

In addition to just making our code cleaner, this will enable making
some of Setup.php's dynamic configuration more sane.

Test coverage is all lines except invalid URLs -- I couldn't find any.

Bug: T305093
Change-Id: I706ef8a50aafb518e13222719575d274c3583b90
2022-04-12 15:14:35 +03:00
daniel
e239b02a5e Add convenience methods for asserting status.
This ensures that assertions work in a uniform way,
and provides meaningful messages in cause of failure.

Change-Id: Ic01715b9a55444d3df6b5d4097e78cb8ac082b3e
2022-03-16 22:44:25 +01:00
Alexander Vorwerk
9b09bf3112 Use updated ObjectFactory namespace
Depends-On: I99c5e5664d2401c36a9890f148eba7c25e6e8324
Depends-On: I48ab818b2965da14af15ef370aa83ad9455badd9
Depends-On: I018371e4b77911e56152ca7b2df734afc73f58a5
Change-Id: I04ebdb52102f6191d49a9cc70b1f98308299e72f
2022-03-09 23:04:51 +00:00
Kosta Harlan
0c2cc804e1 phpunit: Use is_file/is_dir instead of file_exists
Yes, it's a micro-optimization. See https://bugs.php.net/bug.php?id=78285
and https://thephp.cc/articles/caching-makes-everything-faster-right
for more info.

Change-Id: Ib8e8e9794e15066476f35cdb1236df8b983274d6
2022-01-03 21:47:56 +01:00
Roman Stolar
a68e641f9d Move Content::getParserOutput & AbstractContent::fillParserOutput to ContentHandler
Update/Create override classes of ContentHandler.
Soft-deprecate and remove method from Content and classes that override them.

Bug: T287158
Change-Id: Idfcfbfe1a196cd69a04ca357281d08bb3d097ce2
2021-09-29 13:10:51 +03:00
Timo Tijhof
5abebcedf8 phpunit: Fix phpErrorFilter check in TestCaseTrait to avoid PHP bug
```
function check($txt) {
    echo sprintf("er=%+5s, ini=%+5s # $txt\n",
      error_reporting(), intval( ini_get( 'error_reporting' ) )
    );
}
function turningItOffAndOnAgain() { // have you tried...
  check('... enter');
  $old = error_reporting(42);
  check('... off');
  error_reporting($old);
  check('... on');
}

error_reporting(2);
check('start');           # er=2, ini=2
turningItOffAndOnAgain();
check('toggled');         # er=2, ini=2
@turningItOffAndOnAgain();
check('silence-toggled'); # er=2, ini=0
```

PHP correctly reflects the silenced state during the silenced state,
the same as our AtEase library.

PHP also correctly ignores changse to error_reporting while being
silenced. That is, it always restores it back to how it was regardless
of what a confused nested function may have done.

Where it fails is that it doesn't seem to sync the INI setting, which
caused this PHPUnit check to cause test failures.

While this an upstream bug, it was also a mistake on my part to
write the PHPUnit check based on the ini_get value. That's not the
idiomatic way to check the current state and apart from these two
lines of code, it seems nothing in any of our Codesearch-indexed
code bases, including third party, do this.

Bug: T291278
Change-Id: Ic3e66cb2e5b2b44b9997d323abbc87b7f8fe3c41
2021-09-17 17:35:50 +00:00
DannyS712
4b2bb69b99 Remove dependency on pimple external library
Dependency was added in T254053 so that we
could use a pimple ServiceLocator in
MediaWikiTestCaseTrait::createSimpleObjectFactory.
But, since the ServiceLocator being created is using
an empty container, its not actually used for anything,
and can easily be replaced with a mock that says
it has no services and throws if trying to retrieve any.

Per the PSR11 spec, the container interface should
throw an exception that implements
NotFoundExceptionInterface - use the
NoSuchServiceException from the Services library
that we use for MediaWikiServices

Change-Id: I721e7417036a8b0fa9084e9d13329262253ae0f6
2021-06-24 07:18:23 +00:00
jenkins-bot
c32f07ed6a Merge "tests: ensure __destruct is never doubled with anythingBut" 2021-05-22 19:54:27 +00:00
Petr Pchelko
ccd4b6d98b Give WikiAwareEntityTrait it's own unit tests.
Change-Id: I7f3b9e58a3d0ff4aeb1315c16d3cf91b79530095
2021-04-27 20:31:14 -07:00
Daimona Eaytoy
f43942b80e tests: ensure __destruct is never doubled with anythingBut
See task. Existing callers are not being updated because explicit is
better than implicit.

Bug: T280780
Change-Id: If6e9569dd3e98a14f8412fd245f6ed5141ed8d0d
2021-04-21 16:57:51 +02:00
Daimona Eaytoy
76fc5e9cde phpunit: Fix implementation of anythingBut
As I wrote in Ie9d5d7e53faee09b7fe63f9387f72dc87411c24d,
Assert::matches:
- Only accepts strings (and not arbitrary values)
- And it does so because the comparison uses format strings (à la
  printf), which is not what we want here.

This currently works for method names like the docs suggest, but it
fails with basically anything that is not a string without '%' chars.

Instead, use identicalTo() which performs === comparisons. Note that
using equalTo() (which uses `==`) would perhaps be more consistent
with the PHPUnit API, but then we'd have the usual problems with weak
comparisons. A separate implementation (or method parameter) which uses
== can be added later if someone really finds it necessary.

Change-Id: I8444129198182f5eccc0a7282abd609b2f763299
2021-04-18 16:32:56 +02:00
Daimona Eaytoy
535d7abf59 phpunit: Mass-replace setMethods with onlyMethods and adjust
Ended up using
  grep -Prl '\->setMethods\(' . | xargs sed -r -i 's/setMethods\(/onlyMethods\(/g'

special-casing setMethods( null ) -> onlyMethods( [] )

and then manual fix of failing test (from PS2 onwards).

Bug: T278010
Change-Id: I012dca7ae774bb430c1c44d50991ba0b633353f1
2021-04-16 20:15:00 +02: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
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
5c83c8952c tests: Avoid problematic language in comments and internal code
We can't avoid this entirely where we're testing upstream features
that are yet to be renamed, but this is a start.

Bug:T277986
Change-Id: I5c1ae717cc008ca147ea31fe35c44b19d1fe5d98
2021-03-28 21:21:28 -07:00
jenkins-bot
b5bec69f6e Merge "Add mock for numParams to MediaWikiTestCaseTrait" 2021-02-17 22:37:07 +00:00
Maggie Epps
7466d08aa1 Add mock for numParams to MediaWikiTestCaseTrait
Change-Id: I774b68d74e2fb89e7d8cbfd42b7588ec4c856176
2021-02-17 16:29:48 -05:00
Reedy
729f20afc8 Tests: Mark more closures as static
Bug: T274036
Change-Id: I911d3041cebe417d162934223b46ea295c6d20e3
2021-02-07 13:26:56 +01:00
Petr Pchelko
a37dfdd2b8 MediaWikiTestCaseTrait: move MWDebug::clearLog() to separate @after
With @after the tear down methods are separated by purpose,
and MWDebug::clearLog had nothing to do with phpErrorFilterTearDown.

Change-Id: I46a894737102fdbb74b24225dcf7617234259e31
2021-02-05 15:46:52 -06: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
Ammarpad
bf75f31779 Improve assertArrayEquals() logic in test trait
The previous logic obsfucates the issue about these remaining args
and the fact that they are all deprecated.

This patch makes it clear what's deprecated and the remaining
acceptable args count and position.

Change-Id: Ia7cef161fb990fac700fd2360c170d6ff1c4a8d0
2021-01-05 04:36:10 +01:00
jenkins-bot
0edfdb4010 Merge "Add $allow parameter for MediaWikiTestCaseTrait::createNoOpAbstractMock" 2020-12-03 17:08:13 +00:00
Gergő Tisza
926d78446b
Add $allow parameter for MediaWikiTestCaseTrait::createNoOpAbstractMock
This makes it consistent with createNoOpMock, and in general a mock with
a restricted set of methods is needed way more often than a mock with no
methods.

Change-Id: I787586fea2c3d9c3082e9d1007ecd2d552da6e4d
2020-12-03 00:24:21 -08:00
Tim Starling
c8f51d1fb7 Use the old HookContainer to set up the post-reset services
HookContainer::salvage() expects to be called immediately after the
object is constructed, and I enforced this expectation by asserting that
$this->handlersByName is empty.

However, MediaWikiServices::resetGlobalInstance() calls
HookContainer::run() in between construction and salvage(). The effect
is that the assertion is hit if any hooks were executed using the old
HookContainer.

So, call onMediaWikiServices() using the old pre-reset HookContainer,
instead of using a partially-constructed HookContainer for this purpose.
Pass the new MediaWikiServices instance as the argument, since that is
the thing that the hook is configuring.

Bug: T263925
Change-Id: I2b462d82c82314b11563a5ba1f02c6fb6d29e608
2020-12-01 13:04:12 +11:00
Umherirrender
49efbfe2fb Improve documentation about object to be stdClass or specific type
Some classes still needs exclude for the sniff

Change-Id: I9536c2ee84f6fb4c83cf862a152cf6b00344cb97
2020-11-20 14:47:56 +00:00
daniel
6eea7d7ed5 Add test infra for ParserCache serialization/deserialization
Based on Daniel's work at Ia6e70179b7ee5ce4e93888585ccc30d92da165c3
however was changed enough to move into a separate changeset.

More acceptance tests and data will be added in a followup commit.

Bug: T264397
Change-Id: I135187e83cbfa02b97c5656f0752f8bf1ceb58d0
2020-10-09 08:14:57 -06:00
Thiemo Kreuz
6aa6d10e86 Replace all call_user_func(_array) in all tests
There is native support for all of this now in PHP, thanks to changes
and additions that have been made in later versions. There should be no
need any more to ever use call_user_func() or call_user_func_array().

Reviewing this should be fairly easy: Because this patch touches
exclusivly tests, but no production code, there is no such thing as
"insufficent test coverage". As long as CI goes green, this should be
fine.

Change-Id: Ib9690103687734bb5a85d3dab0e5642a07087bbc
2020-06-06 18:41:20 +02:00
Kosta Harlan
82cf3a3cfd Add getMockMessage to MediaWikiTestCaseTrait
This patch adds two more commonly used methods for params/rawParams to
getMockMessage and relocates the functionality to the MediaWikiTestCaseTrait.

Follows-Up: Ia94521b786

Change-Id: If9a6ccf1885ba11fbf3fb1b586006118462c1410
2020-05-20 21:52:11 +02:00
Tim Starling
b3d762e148 Add HookRegistry
Add a HookRegistry interface and two concrete implementations,
representing HookContainer's view of its environment. This simplifies
creation of a HookContainer for testing.

Add MediaWikiTestCaseTrait::createHookContainer() which can be used
in most of the places that were previously creating mock hook
containers. It can also replace setTemporaryHook() in some cases.

Change-Id: I9ce15591dc40b3d717c203fa973141aa45a2500c
2020-05-13 11:51:02 +10: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
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
daniel
9884139d65 SearchHandler: add unit test
Change-Id: I963a2f31afddd37ada615f12d6d5725c20a18631
2020-03-02 20:52:46 +01:00
Gergő Tisza
8a08f2a806
Use dependency injection for AuthManager and allow it for providers
Change-Id: Icf3345bec993f8cb103f065e76b04fe1f3521639
2020-02-19 01:18:16 -08:00
Gergő Tisza
02bbdd3d81
Improve phpdoc in MediaWikiTestCaseTrait
- add MockObject as a return type for createNoOpMock. Technically
  object and object|MockObject are the same but this is enough to
  prevent some IDE warnings about incompatible types (and to guide
  IDE autocomplete although there is probably not much use for that
  in a no-op mock).
- fix a legacy classname alias which modern PHPUnit does not provide
  anymore

Change-Id: I6445bce2dd7a4bf97ad2374ab2dac566b3d73546
2020-02-11 14:13:25 -08: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
Umherirrender
97ed35e2e8 Add missing @param and @return to documentation
Change-Id: I48ee838a7ebf4f25a4883d4c7886b39c2d3916af
2019-11-16 13:45:55 +01: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
David Causse
d29adfe276 MediaWikiTestCaseTrait use $this instead of static calls
for consistency.

Change-Id: Ie769751cf6d08335932dd66c56acb1d3f43f1dab
2019-09-20 18:53:28 +02:00
David Causse
b3b95c3927 Add assertFileContains to MediaWikiTestCaseTrait
Used by CirrusSearch unit tests.

Change-Id: I1b67a9cddfca7fa06fb0b916981d1efde0764018
2019-09-17 17:24:33 +02:00
mainframe98
4941f2de91 Permit destructors in MediaWikiTestCaseTrait::createNoOpMock
709773ab57 introduced this method, but its implementation
does not permit calls to __destruct. PHP documentation on destructors
says that they'll be called unconditionally, which makes using the
mock created with this method unusable for classes that implement
__destruct.

Bug: T231656
Change-Id: Icb4f978c78e726401d75627128c9c76f6f9afc87
2019-08-30 15:27:38 +00:00
Aryeh Gregor
709773ab57 createNoOpMock() method for PHPUnit tests
This is just a shortcut for a commonly-used pattern, when you want to
create a mock that's never supposed to have methods called.

Change-Id: Ia7267e3d3108c1ff94485f7e44bf409808a762be
2019-08-29 18:00:37 +03:00
Aryeh Gregor
5e2199c5b0 BadFileLookup to replace wfIsBadImage
I think this probably shouldn't be directly in the MediaWiki namespace,
but I don't know where is a better place to put it.

In order to avoid gratuitous use of TitleFormatter, I changed the cache
format -- the old implementation used getPrefixedDBkey() and I switched
to an ns/dbkey pair. I also changed the cache keys to use SHA1 instead
of MD5, by Daniel's request.

The previous implementation cached the parsed blacklist for one minute
without invalidation, so it could return slightly stale results, but it
didn't retrieve the bad image list message on a cache hit. The new
implementation unconditionally retrieves the bad image list message, but
uses a hash of it in the cache key and caches for one day. The new
behavior happens to be more cleanly implementable in a service.

Bug: T200882
Bug: T139216
Change-Id: I69fed1b1f3cfc1aa149e0739780e67f6de01609d
2019-08-21 20:45:37 +03:00