Commit graph

67 commits

Author SHA1 Message Date
Bartosz Dziewoński
079d302211 tests: Let FakeQqxMessageLocalizer handle numParam()
Follow-up to 279fd16bab.

Change-Id: I966e688cfaafdfe3af4b12ced1c3acf2062aa302
2024-06-25 17:23:24 +00:00
Bartosz Dziewoński
279fd16bab tests: Avoid deprecated Status / StatusValue methods
Use more specific and less fragile assertions whenever possible;
most tests don't need to examine the internals of the status so
closely. Use StatusValue::getMessages() when necessary.

Change-Id: Ie4b3ebc03abb0e352e82394ced6ab9e733c83fb4
2024-06-15 10:14:37 +02:00
Umherirrender
f27c2433bb tests: Use namespaced classes (2)
Changes to the use statements done automatically via script
Addition of missing use statement done manually

Change-Id: I4ff4d0c10820dc2a3b8419b4115fadf81a76f7a2
2024-06-13 23:21:02 +02:00
Hakan Özdemir
3dc17beafc MediaWikiTestCaseTrait: Replace StaticHookRegistry qualifier with import
Change-Id: Ib6de00d82740678a54b5270dfa7bc5bcc0ce3a5d
2024-05-05 12:17:56 +00:00
jenkins-bot
7e216c8f7d Merge "StatusValue: Document and deprecate broken ways to search/replace messages" 2024-05-03 22:15:23 +00:00
Amir Sarabadani
214674d6b1 namespace MWDebug
Bug: T353458
Change-Id: I99d728bd111ff882220cd175ff09d4da20b81eae
2024-05-03 22:59:47 +02:00
Bartosz Dziewoński
c66441d05c StatusValue: Document and deprecate broken ways to search/replace messages
StatusValue::replaceMessage() doesn't really work if given a
MessageSpecifier, and doesn't work at all if given a MessageValue.

StatusValue::hasMessage() and ::hasMessagesExcept() ignore the message
parameters when searching if given a MessageSpecifier or MessageValue.

Therefore, deprecate passing anything other than strings representing
message keys to these methods.

Change-Id: Icb4effc6c5319c12ba4ebeab447a0cef54f8e255
2024-05-03 17:04:27 +02:00
jenkins-bot
66ede48455 Merge "Streamline MediaWikiTestCaseTrait::getMockMessage implementation" 2024-04-15 09:33:30 +00:00
thiemowmde
85c9374aa8 Streamline MediaWikiTestCaseTrait::getMockMessage implementation
Lots of code duplication there. PHPUnit comes with neat helper
methods for that.

Depends-On: I30ab799ceab36b80c330d1233f3009814d7f6c98
Change-Id: I86ff2d61fb1c28cc6a285cce6dc2b6d4f5b7bca4
2024-04-12 06:45:45 +00:00
thiemowmde
021f2f6c32 Add __debugInfo to MediaWikiTestCaseTrait::createNoOpMock
I keep running into this whenever I use createNoOpMock. I think it's
XDebug that's calling this method, and then PHPUnit flooding the
console with extremely long stack traces.

We pretty much never do anything custom with this method:
https://codesearch.wmcloud.org/search/?q=__debugInfo&files=%5C.php%24

Change-Id: Ib2ab86fb243555f5e4449ed72cb032cb465e415d
2024-04-10 14:25:07 +02:00
Reedy
bec8eaf1f9 tests: Update @since tags for backports
Follows-Up: I1054f6f944d491b536949cada33e2ac670e026f8
Follows-Up: If6a4c7dbba39389b06ce64fb5cac08cd8d7dd9fc
Change-Id: I6f4f060d5f47db19d9e705d3fe2f60395ab195dc
2024-04-08 16:09:42 +01:00
James D. Forrester
785aef4a7e build: Upgrade phpunit to 9.6.16
Bug: T342110
Depends-On: I764fb9abb0c6318451a6a8b63206b4c29ac53f06
Depends-On: Ieb8c9cdf99a8973b2e68598143c74f8da152d47a
Change-Id: Id708a7716505ffce26c8eb438a98d74195682e91
2024-02-25 00:04:54 +00:00
Daimona Eaytoy
89f583625a tests: Add replacement for assertions deprecated in PHPUnit 9.6
expectWarning() and friends have been deprecated in PHPUnit 9.6, and
removed in PHPUnit 10. Unfortunately, there is no simple replacement
because PHPUnit no longer converts them to exceptions in the first
place. In fact, Sebastian Bergmann explicitly stated that he does not
consider the use case of

> a library developer to verify a code block warns its consumer when
> certain action is performed

worth supporting.

So, add an ad-hoc replacement for all the deprecated methods. This is
quite ugly, but it's simple enough given the low number of usages.

On the bright side, this new method does not halt the test when the
warning is triggered. This seems to align with the developers'
expectation, seen in a few existing tests, that any code following the
notice will be executed.

Bug: T342110
Change-Id: I214abfed4280834840c115777ed78eb0a5570da9
2024-02-23 22:09:45 +01:00
Umherirrender
1b9809b310 tests: Skip failing tests on php8.2 (and make pass)
- Mark test as skipped referring known task about improve of the tests
- Mark test as skipped testing for backward-compatibility on
  parser cache serialization, test data contains old properties,
  which are now unknown and created dynamically

Change-Id: If6a4c7dbba39389b06ce64fb5cac08cd8d7dd9fc
2023-12-10 22:57:48 +00:00
Derick Alangi
f9cc91bd34 phpunit: Remove reference of deprecated params in TestCaseTrait
These parameters are deprecated and not used, we should just kill
them all.

Change-Id: I73039c564298da36536e64eb5d39950317ace4af
2023-09-06 11:52:36 +00:00
Daimona Eaytoy
61354c9f40 phpunit: Add temporary overrides for assertObject(Not)HasAttribute
assertObjectHasAttribute and assertObjectNotHasAttribute are amongst the
assertion methods deprecated in PHPUnit 9.6. PHPUnit 10 introduced
replacement methods for them, assertObjectHasProperty and
assertObjectNotHasProperty respectively, but these weren't backported to
the 9.x branch.

Add temporary overrides for those methods in MediaWikiTestCaseTrait, so
that we can start migrating our code before the PHPUnit 10 update. The
signature, implementation, and error messages of these assertions are
copied from upstream, so the transition should be seamless. The only
tiny difference is that our version also prints "Failed asserting that
false is true" (and vice versa for NotHasAttribute) after the custom
message, but I don't think this is worth the complexity of adding a
custom Constraint.

Also migrate the few existing usages to the new methods.

Bug: T342110
Change-Id: If489d2c70fabbed6a068ef22d166f6e73f6adf6d
2023-08-20 00:22:24 +02:00
Timo Tijhof
12347418ae phpunit: Move assertArraySubmapSame() to MediaWikiTestCaseTrait
This isn't specific to integration tests.

Rename it to assertArrayContains() as the terms "submap" is uncommon.
The term "same" does correctly connotate strict equality, but seems
redundant/implied when operating on mutiple values, such as with
the built-in assertContains(), assertJsonStringEqualsJsonString(),
etc.

Change-Id: I9d8e7dca128a73d03b173a8ec3566f55edddde6a
2023-05-01 22:19:21 +00:00
Kevin Israel
0ae29bbb69 tests: Replace checkPHPExtension() with @requires annotations
PHPUnit 3.7.0 was the first version to support @requires extension. At
the time checkPHPExtension() was added in a7901801b4, MediaWiki
still supported PHPUnit 3.6.7.

MediaWiki now requires intl and xml, so I removed checks for those
extensions rather than converting them to annotations.

checkPHPExtension() is removed without deprecation; it does not appear
to have ever been used (and is not likely to be used) in MW extensions.
This is explicitly permitted under the stable interface policy. Even if
it were not, only tests are affected, and they are supposed to fail
anyway if hard deprecated code is used.

Change-Id: I45f9b4c0e120683103cead916f4d4ef58bd11530
2022-10-23 20:47:14 -04:00
Derick Alangi
98ba32dfdf Make IDE not complain on createNoOpMock() & createNoOpAbstractMock()
Since `createNoOpMock()` & `createNoOpAbstractMock()` returns a `MockObject`
type, this makes callers of this method complain in the IDE as if the right
type is not passed in.

Using a Psalm template in this patch fixes this warnings (got the idea
from PHPUnit TestCase.php). In addition, using normal @params so that
phan can be happy as well.

Change-Id: Ic0074d0d0d432c71473a48d79653e59ca7ad0085
2022-10-20 11:46:11 +01:00
Daimona Eaytoy
7a494d1fa4 tests: Upgrade PHPUnit from 8.5+ to 9.5+
* DeprecatedHooksTest: Don't use assertContains().
* Replace uses of deprecated asserts:
  - assertFileNotExists() -> assertFileDoesNotExist()
* Update hierarchy of MediaWikiPHPUnitResultPrinter, since ResultPrinter
  is an interface in PHPUnit 9.
* Remove temporary forward-compat methods.
* Remove directories that don't exist from tests/phpunit/suite.xml, since
  they now make PHPUnit exit:
   - tests/phpunit/skins, it used to have SideBarTest, then moved to
     tests/phpunit/includes/skins
   - tests/phpunit/documentation, it used to have ReleaseNotesTest, then
     moved to tests/phpunit/unit/documentation
* Update configuration with --migrate-configuration and reformat.
* Avoid redefining getMockBuilder() in
  ActionModuleBasedHandlerTestTrait, use a @method annotation instead.
* In RCCacheEntryFactoryTest, avoid using internal PHPUnit logic for
  HTML validation, and use native PHP methods instead. The code was
  copied from Xml::load (moved to \Xml\Loader::load in PHPUnit 9) and
  simplified for this use case.

Bug: T243600
Bug: T262076
Change-Id: I851b9158b73d0cfc315eed9d63b15c54b05895e3
2022-10-08 02:03:55 +02:00
Daimona Eaytoy
f6efe06b44 tests: Add forward-compat assertDoesNotMatchRegularExpression too
Follow-up to Ibce4c2479a533d132eb9f0641f8473fc6ca14fdb with the same
rationale.

Bug: T243600
Change-Id: I6bf543db387cbe934ef5ce353a8a978097f9a614
2022-10-07 14:35:18 +00:00
Daimona Eaytoy
bbb62e3273 tests: Add temporary forward-compat method for assertMatchesRegularExpression
assertRegExp was deprecated in PHPUnit 9 and its replacement,
assertMatchesRegularExpression, was also introduced in PHPUnit 9. Hence,
we cannot migrate callers before the upgrade, but we also can't upgrade
before callers are migrated due to deprecation warnings. This is
especially true in extensions.

So create a temporary method that extensions can use while we
transition.

Bug: T243600
Change-Id: Ibce4c2479a533d132eb9f0641f8473fc6ca14fdb
2022-10-07 14:53:12 +02:00
Thiemo Kreuz
61ae7504df Replace trivial usa of mock builder with createMock() shortcut
createMock() does the same, but is much easier to read.

A small difference is that some of the replacements made in this
patch didn't use disableOriginalConstructor() before. In case this
was relevant we should see the respective test fail. If not we can
save some CPU cycles and skip these constructors.

Change-Id: Ib98fb06e0fe753b7a53cb087a47e1159515a8ad5
2022-07-15 16:43:48 +00:00
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