Commit graph

205 commits

Author SHA1 Message Date
Func
4d1da6f7f8 phpunit: Update @covers annotations for namespaced classes
We renamed many classes to be namespaced, but the `@covers` and
`@coversDefaultClass` annotations weren't updated properly.

PHPUnit didn't support short cover annotations with `use` statements,
these didn't trigger any errors yet, because they are class alias.

This patch is populated by a modified version of PhpunitAnnotationsSniff.

Change-Id: I6c602290a30099239b17d2dc0d67b1488b4eaeeb
2023-05-27 17:43:12 +08:00
Umherirrender
e8bf1a18db title: Remove protection related deprecated Title functions
- Title::areCascadeProtectionSourcesLoaded()
- Title::areRestrictionsCascading()
- Title::areRestrictionsLoaded()
- Title::getAllRestrictions()
- Title::getCascadeProtectionSources()
- Title::getFilteredRestrictionTypes()
- Title::getRestrictionExpiry()
- Title::getRestrictionTypes()
- Title::getRestrictions()
- Title::isCascadeProtected()
- Title::isProtected()
- Title::isSemiProtected()
- Title::loadRestrictionsFromRows()

Bug: T306131
Change-Id: Id6c3a648453db51c464faeef2ecac4ffaa0676c1
2023-05-10 00:04:20 +02:00
Bartosz Dziewoński
6ba47296d9 Fix Phan suppressions related to Title::castFrom*() and friends
There is no way to express that Title::castFromPageIdentity(),
Title::castFromPageReference() and Title::castFromLinkTarget()
can only return null when the parameter is null. We need to add
Phan suppressions or explicit types almost everywhere that these
methods are used with parameters that are known to not be null.

Instead, introduce new methods Title::newFromPageIdentity() and
Title::newFromPageReference() (Title::newFromLinkTarget() already
exists), without the null-coalescing behavior, and use them when
the parameter is not null. This lets static analysis tools, and
humans, easily understand where nulls can't appear.

Do the same with the corresponding TitleFactory methods.

Change the obvious uses of castFrom*() to newFrom*() (if there is
a Phan suppression, a type check, or a method call on the result).

Change-Id: Ida4da75953cf3bca372a40dc88022443109ca0cb
2023-04-22 16:45:09 +02:00
Tim Starling
5e30a927bc tests: Make some PHPUnit data providers static
Just methods where adding "static" to the declaration was enough, I
didn't do anything with providers that used $this.

Initially by search and replace. There were many mistakes which I
found mostly by running the PHPStorm inspection which searches for
$this usage in a static method. Later I used the PHPStorm "make static"
action which avoids the more obvious mistakes.

Bug: T332865
Change-Id: I47ed6692945607dfa5c139d42edbd934fa4f3a36
2023-03-24 02:53:57 +00:00
Umherirrender
5e8665f67e title: Use Title::makeName instead of ::getFullText in ::isValid
In getFulltext the namespace number is converted to text, for NS_USER
this trigger the gender cache. Use canonical namespace instead.

When Title::canExist is called to fill a LinkBatch (Like done for
Special:Log/block via BlockLogFormatter::getPreloadTitles),
the effect of the batch lookup is not the best, because GenderCache
filled with single database queries instead.

This is similar to the usage of makeName in Title::makeTitleSafe

Handle invalid namespace numbers in Title::makeName the same as in
Title::prefix

Change-Id: Ibbdc7e0762568bb29ae59764c3666f275d201c6c
2023-03-23 04:34:27 +00:00
James D. Forrester
ad06527fb4 Reorg: Namespace the Title class
This is moderately messy.

Process was principally:

* xargs rg --files-with-matches '^use Title;' | grep 'php$' | \
  xargs -P 1 -n 1 sed -i -z 's/use Title;/use MediaWiki\\Title\\Title;/1'
* rg --files-without-match 'MediaWiki\\Title\\Title;' . | grep 'php$' | \
  xargs rg --files-with-matches 'Title\b' | \
  xargs -P 1 -n 1 sed -i -z 's/\nuse /\nuse MediaWiki\\Title\\Title;\nuse /1'
* composer fix

Then manual fix-ups for a few files that don't have any use statements.

Bug: T166010
Follows-Up: Ia5d8cb759dc3bc9e9bbe217d0fb109e2f8c4101a
Change-Id: If8fc9d0d95fc1a114021e282a706fc3e7da3524b
2023-03-02 08:46:53 -05:00
Amir Sarabadani
523ab7cff8 Reorg: Move RawMessage to under language/
To follow Message. This is approved as part of RFC T166010.

Also namespace it but doing it properly with PSR-4 would require
namespacing every class under language/ and that will take some time.

Bug: T321882
Change-Id: I195cf4c67bd51410556c2dd1e33cc9c1033d5d18
2022-12-16 11:30:19 +01:00
Umherirrender
e3af1aeed1 Hard-deprecate protection related deprecated Title function
Bug: T306131
Change-Id: I513efa22c42dd9194a385bae408af38cae9f1a6a
2022-12-16 00:36:53 +00:00
Reedy
0cb2c3c106 Fix casing of class and function name usages
Bug: T253628
Change-Id: I5c64f436d3cf757390b751ce3e34bfc7872bc176
2022-12-04 19:09:30 +00:00
Umherirrender
c6fecd5ab0 tests: Replace assertEmpty with assertSame
assertSame avoids use of loose comparisons and
allows to check the expected type

Change-Id: If821d1be2bc1ff9f8b70968a339f33c7a8e8880a
2022-11-25 00:42:53 +01:00
Derick Alangi
506ca39387 Rest: Allow fetching content of pages with system messages
NOTE: some phan suppressions removed, no longer needed now.

Bug: T317017
Change-Id: Iccbffb502eeace841f86ddb1987e77be50003973
2022-11-23 17:00:00 +01:00
Tim Starling
43a93d9782 Use the null coalescing assignment operator
Available since PHP 7.4.

Automated search, manual replacement.

Change-Id: Ibb163141526e799bff08cfeb4037b52144bb39fa
2022-10-21 13:26:49 +11:00
Thiemo Kreuz
0092d4581b Replace complicated setup with createNoOpMock() in TitleTest
Change-Id: Ib984a8e995c0285ed4cc29bdff248b4e06d53ff0
2022-08-06 09:19:48 +00:00
Brian Wolff
44203ab7cc Do not test giving a null fragment to Title::makeTitle
Method is documented as only taking strings. PHP8.1 gives
warning for nulls. We should not test invalid values unless
we are expecting an exception.

This fixes a failing test TitleTest::testMakeTitle_invalid on php8.1

Bug:  T313663
Change-Id: I51fe4c54832914055a624d94ad1a1c2720b23ec9
2022-07-29 22:01:38 -07:00
Derick Alangi
a3a2ed268a tests: Use overrideConfig(Value|Values) where needed
This patch covers: tests/phpunit/includes/actions/ and
tests/phpunit/includes/api/.

Change-Id: I60d8fb7b8a63b423606db935fa0d24cf859b2512
2022-07-12 22:24:50 +00:00
Matěj Suchánek
8096e6a6e3 Remove Title::isNamespaceProtected
It has been deprecated since 1.34 and it is unused.

Change-Id: I3f375a8d3aceb3e31c27532c28b190c41e628edf
2022-06-16 19:08:34 +02:00
Amir Sarabadani
30aa4679c0 Remove references to legacy page_restrictions field in page table
Bug: T218446
Change-Id: I4d45de0890b96e00d4b24fc96ceffbf336928bc2
2022-05-09 11:20:53 +02:00
Kunal Mehta
5498663e0e SECURITY: Add recursion guard to Title::newMainPage()
If the on-wiki "mainpage" message is a bare local interwiki
(`localiw:`), MediaWiki will try to normalize it to the main page,
causing an infinite recursive loop. Once in this state, there's no way
to fix it short of editing the database (or applying this patch).

The easiest way out of this is to add a recursion guard to fall back to
the hardcoded "Main Page" default.

Bug: T297571
Change-Id: I91316ecbdbe7c4ad1b7594643aef29a8e1f8fb26
2022-03-31 22:58:38 +01:00
daniel
00c489cd18 title: Restore boolean false Title::getTouched() for nonexistent page
Feeding the return value of the DB query through timstamp conversion
made it so that false was turned into the current timestamp, causing
confusion and cache churn.

Bug: T298520
Change-Id: I6e10b21f6b0e40ce7e3403ffc9a41a307e945354
2022-03-09 14:56:23 +00:00
Timo Tijhof
128debb64b tests: Change use of AtEase to at operator
Follows-up I361fde0de7f4406bce6ed075ed397effa5be3359.

Per T253461, not mass-changing source code, but the use of the native
error silencing operator (@) is especially useful in tests because:

1. It requires any/all statements to be explicitly marked. The
   suppressWarnings/restoreWarnings sections encourage developers to
   be "lazy" and thus encapsulate more than needed if there are multiple
   ones near each other, which would ignore potentially important
   warnings in a test case, which is generally exactly the time when
   it is really useful to get warnings etc.

2. It avoids leaking state, for example in LBFactoryTest the
   assertFalse call would throw a PHPUnit assertion error (not meant
   to be caught by the local catch), and thus won't reach
   AtEase::restoreWarnings. This then causes later code to end up
   in a mismatching state and creates a confusing error_reporting
   state.

See .phpcs.xml, where the at operator is allowed for all test code.

Change-Id: I68d1725d685e0a7586468bc9de6dc29ceea31b8a
2022-02-24 21:29:51 +00:00
Func
7f74a2e50c Clean up tests that misused the parameters of assertSame/Equals
Expected value is the first parameter to assertSame() or assertEquals().
And turn to use assertCount() for some assertions aginst count of array.

Based on code search `assert(?:Same|Equals)\(.+,.+expected` and I look
through files roughly, so some assertions that don't contains 'expected'
are also fixed. In the meantime, some assertions that I am not clear
about are not touched.

Change-Id: I75798b60d29fd19b33f4fdf34ed3c788db420d01
2022-02-08 07:21:10 +00:00
Alexander Vorwerk
decbaf4f38 phpunit: use ->getServiceContainer() in integration tests
Change-Id: I38299cb65eeaadfdc0eb05db4e8c0b0119cfb37d
2022-01-27 22:04:16 +01:00
Kunal Mehta
5ad1190f67 Remove unreachable parts of getTitleInvalidRegex() in PHP and JS
In a3a2744d03 (then r28968), the illegal title character regex was
expanded to block XML/HTML character references. However even back then
the two parts of the regex that aimed to block character references with
hashes in them were unreachable, because the fragment split happened one
step before illegal characters were checked. Any character reference
with a # in it would be split across the dbkey and fragment, and the
regex is only run against the dbkey.

This was noted in the JavaScript port of mediawiki.Title's tests, but
still left in. Those commented out test cases have now been removed as
they're technically valid titles with fragments.

Bug: T297578
Change-Id: I079f91a87fd7587ed70e65916fdd5efa01735efe
2021-12-12 22:07:21 -08:00
Amir Sarabadani
ede308ed55 Drop pr_user from page_restrictions
It's not used for 13 years, safe to say it won't be used anytime soon

Bug: T199377
Change-Id: Iecf4fb6046699a1758ad2d1dc55a3ee8eb4b0389
2021-12-06 14:15:39 +01:00
jenkins-bot
0aef7a462b Merge "Hard deprecate TitleFactory::newFromIDs and Title::newFromIDs" 2021-11-29 21:28:11 +00:00
TChin
8371b19662 Hard deprecate TitleFactory::newFromIDs and Title::newFromIDs
Bug: T291288
Change-Id: I1789bccc276ec7c01928775f9bcb8ff301af54ff
2021-11-29 14:23:26 -05:00
daniel
de63ad823a PageStore: avoid getPageById, since it bypasses caching.
Calling getPageById when the page ID is known was supposed to improve
query performance, but since it bypasses LinkCache, it ended up causing a
spike in database queries.

This optimization can be re-introduced once we also cache PageRecords by ID.

Bug: T296063
Change-Id: Ia4ee75b7b5a71d7d858f818d6467793bc642697b
2021-11-22 17:24:13 +01:00
Reedy
6e29611642 Remove or replace usages of "sanity"
Still some more to go...

Bug: T254646
Change-Id: Ia117f01e443c35b4765f3275cab4f2707e1be96f
2021-11-21 16:42:31 +00:00
daniel
fd3a695230 Title: use PageStore instead of LinkCache
This causes Title to no longer look up fields in the database
individually, but use LinkCache instead to load an entire row from the
page table at once.

This patch also causes Title to use in-process caching for some
getters that did not use caching before, such as isNewPage()
and getTouched(). These methods do not appear to be used on critical
code paths that involve database updates.

Note that getTouched() used to take an options $db parametr. This
appears to be unused, and has been deprecated in favor of a $flags
parameter, for consistency with other getters on the class.

DEPLOY: Risky! This re-implements the internal caching logic of Title
and slightly modifies caching semantics in some cases. This may have
unforeseen consequences.

Bug: T285389
Depends-On: I103b9e1d2bf594bfc1b0ea12b980dd20bb911c3a
Change-Id: I2df81df7186025e001520f24fd498623c7184772
2021-11-02 23:12:47 +01:00
vladshapik
1ae8e89eb5 Hard deprecate LinkCache::addGoodLinkObj
LinkCache::addGoodLinkObj was hard deprecated.
Use addGoodLinkObjFromRow() instead. PHPUnit tests
must use LinkCacheTestTrait::addGoodLinkObject().

Bug: T284955
Change-Id: I0c22be2f00e6d6d4ba6dd1511c5385cad55e8ff7
2021-10-26 18:21:32 +03:00
Cindy Cicalese
b0c0a934e4 Remove hard deprecated Watchlist methods in User, Title, and WatchAction
Bug: T292464
Change-Id: Ie9cb3036a69810344971c49c424a76c7fa1cba07
2021-10-04 15:14:24 -04:00
Derick Alangi
c296685f4f tests: Avoid trying to access the DB in data providers
NOTE: some test cases where removed as they're exactly doing the
same thing as the ones above.

Bug: T291268
Change-Id: I8fa8bafc892dbc84f2aab2549d453d6f39835d33
2021-09-20 10:32:45 +01:00
jenkins-bot
7ece0e7afd Merge "Title: Make use of BacklinkCacheFactory service" 2021-09-10 14:31:23 +00:00
jenkins-bot
1efeefe017 Merge "PageStore: Use LinkCache" 2021-09-09 20:04:47 +00:00
jenkins-bot
150a589ecf Merge "Title: restore behavior of getCascadeStrictionSources" 2021-09-09 19:41:48 +00:00
Derick Alangi
db43511e6a Title: Make use of BacklinkCacheFactory service
Change-Id: I48161585de6f329ec4037156234e0b07b3b837e6
2021-09-09 14:04:02 +01:00
Derick Alangi
7d12da5595 tests: Extract pure unit tests from TitleTest integration test
-> Fixed TODOs such as adding test cases for namespaces that are
   not in `$wgNamespacesWithSubpages` in `testGetSubpageText()`.

-> Re-arranged provider methods to come before the test case as
   already in the files for consistency.

Change-Id: Ice2b3a07ea2ddd20417cbe87da93065e1812619e
2021-09-08 17:59:59 +01:00
Derick Alangi
043c2d5589 tests: Merge test cases of TitleMethodsTest into TitleTest
Change-Id: I31bfd91f393f7f69335687a4e7ca830f4831e2f3
2021-09-08 17:59:59 +01:00
daniel
abc2179820 Title: restore behavior of getCascadeStrictionSources
The behavior of getCascadeStrictionSources was changed by Ia73ea587586cb69eb5
to conform to the documented behavior: it would always return false as the
first element of the return value if there were no cascading restriction sources,
as specified in the documentation.

Hopwever, the previous behavior was to return an empty array in that
case, unless the $getPages parameter was false. That behavior seems more
senible, and there is existing code that relies on it.

This patch restores the previous behavior and updates the documentation
instead.

Bug: T218395
Needed-By: I31ca0a8987f9694bc3b312a48c2c111ceda6fa3e
Change-Id: I1f24703b80566220ac6fe8ee500e838ed7fd29af
2021-09-08 17:22:17 +02:00
Derick A
7ebf05256a tests: Cover more methods in the Title::class (increase code coverage)
The methods that have been covered in this patch are listed below:
~ Title::hasFragment()
~ Title::getFragment()
~ Title::setFragment()
~ Title::isMainPage()
~ Title::equals()
~ Title::getPrefixedURL()
~ Title::prefix()
~ Title::__toString()
~ Title::getFullText()

Bug: T241406
Change-Id: I51ffadb52e7eef822e9e619e5132513dc1501f9b
2021-09-07 13:58:38 +00:00
Derick A
18bfc1e0c3 tests: Cover more methods in the Title class
The methods covered are listed below:
~ Title::getNsText()
~ Title::getSubjectNsText()
~ Title::getTalkNsText()
~ Title::isSpecial()
~ Title::isTalkPage()
~ Title::getBacklinkCache()
~ Title::isSubpageOf()
~ Title::isSubpage()
~ Title::getEditURL()

Change-Id: Iae73d0837134964b82dddfc0f941c6abea8a1e1f
2021-09-02 19:24:18 +01:00
daniel
3b6345ca16 PageStore: Use LinkCache
This makes the data stored by LinkCache compatible with PageStoreRecord,
so we can use LinkCache inside PageStore.

This causes PageStore to make use of local caching as well as WANObjectCache.

Note that getPageById() does not yet benefit from cache, but does
populate the cache.

Bug: T278940
Change-Id: Icc27a0d9299a3e4ce45521daef87ad06ec06f064
2021-09-01 08:24:34 +00:00
Aryeh Gregor
cf818256fb New RestrictionStore service
This allows checking restrictions without a dependency on Title, based
only on a PageIdentity.

Additional fixes along the way:

* Correctly return false instead of 'infinity' for
  getRestrictionExpiry( 'create' ) on an existing page
* Correctly handle non-special pages that can't exist (like media pages)
  in listApplicableRestrictionTypes() (return empty array instead of
  'create')
* Improve readability of isProtected()

The expectation change in TitleTest::testIsProtected() is because the
test was formerly broken, since it set mRestrictions without setting
mRestrictionsLoaded. (Which illustrates how this approach to testing is
essentially broken.)

Co-authored-by: Vedmaka <god.vedmaka@gmail.com>
Bug: T218395
Change-Id: Ia73ea587586cb69eb53265b2f8f7a296a2573dd0
2021-08-28 16:01:06 +00:00
Umherirrender
134045b9aa tests: Swap assertion order in unit tests
Changes from patch set Icb93c79f4843b59dae80d3eda1a880457a1a68f2
Also some swaps from assertEquals to assertSame/True/False/Null

Change-Id: Ife497ae6cb1888b77eb25e85b76df72adc65641a
2021-08-04 20:21:01 +02:00
Aryeh Gregor
b19db60c70 Hard-deprecate Title::isNamespaceProtected
No callers, deprecated in 1.34.

Change-Id: If2e1c762f91e3c8255fbb58b7c9f01e8929a7fa2
2021-07-29 17:15:33 +03:00
libraryupgrader
5357695270 build: Updating dependencies
composer:
* mediawiki/mediawiki-codesniffer: 36.0.0 → 37.0.0
  The following sniffs now pass and were enabled:
  * Generic.ControlStructures.InlineControlStructure
  * MediaWiki.PHPUnit.AssertCount.NotUsed

npm:
* svgo: 2.3.0 → 2.3.1
  * https://npmjs.com/advisories/1754 (CVE-2021-33587)

Change-Id: I2a9bbee2fecbf7259876d335f565ece4b3622426
2021-07-22 03:36:05 +00:00
DannyS712
7b556a5761 Move some Title tests to unit tests
Tests for the following methods don't need integration:
- legalChars()
- convertByteClassToUnicodeClass()
- newFromTitleValue()
- newFromLinkTarget()
- castFromLinkTarget()

Change-Id: I2ec6505088c74a90da9d0b8305ce94d81cc0562a
2021-07-09 23:18:57 +00:00
DannyS712
809798bd6d TitleTest::provideCastFromPageReference() remove unused $fake
Change-Id: I2d5b51a291cdfefcaaf8cf07de155a890e72a677
2021-07-05 07:14:29 +00:00
DannyS712
b45ddb2ab3 Use WikiPage::doUserEditContent() instead of ::doEditContent()
Results in passing a user where previously the fallback
to $wgUser was being used, mostly in tests.

Bug: T255507
Change-Id: Iabe24315b23c0ad1272353186425e71974528d23
2021-06-28 00:11:30 -07:00
Petr Pchelko
695e61ce14 Clean up hard deprecated Title methods
Depends-On: If27bb6b5142d09bc9293d080e97b6642462219f4
Change-Id: I37f012e53d1c881292a19aac87eb107636c6bfbe
2021-05-11 07:41:18 -07:00