Commit graph

64 commits

Author SHA1 Message Date
Umherirrender
16a436ce6c tests: Remove cleanup of blocks, done by ChangedTablesTracker
In DatabaseBlockTest changed from addDBDataOnce() to addDBData() as
ChangedTablesTracker cannot reset tables changed by addDBDataOnce()
between each test run.

Remove also User::clearInstanceCache done together with the deletion as
the TestUser class is calling this already between tests.

Change-Id: Ibd5e544138a2a9b554abc2dea54a5db38f9a828f
2024-01-20 11:41:15 +01:00
James D. Forrester
4ed5ca48b1 Follow-up 71ff05267: Stop writing to tablesUsed in tests, now unnecessary
Bug: T342301
Change-Id: I5ea01f7ee103570165261bde0965c5b65e04c369
2023-11-21 09:02:48 -05:00
Tim Starling
cc40f0a12d Migrate callers of DatabaseBlock methods moved to DatabaseBlockStore
* Migrate callers of DatabaseBlock methods newListFromTarget,
  newFromID, newFromTarget, doAutoblock.
* Deprecate DatabaseBlock methods newFromID and getAutoblockExpiry.
  These are the methods with no extension callers in code search.

Bug: T255433
Change-Id: If0358459f53d32e7fe984c2fb8b61e0088f28922
2023-11-06 12:03:51 +11:00
Tim Starling
c638dac3f2 Add tests for new DatabaseBlockStore and BlockManager methods
Adapted from DannyS712's tests of DatabaseBlockFactory
Ie26b589c2ba5ec2715446944ec8cf761676be026

Co-Authored-by: DannyS712 <dannys712.wiki@gmail.com>
Change-Id: Ic4fa2ceea13658853eae48d754c4b84d6f8be3f8
2023-11-03 17:03:33 +11:00
Tim Starling
dfc691bfc5 Migrate callers of deprecated method BlockManager::getUserBlock()
Bug: T345683
Depends-On: I2fff3ff961f788ca2fa84eba54293edf9145d738
Change-Id: I6d00af72cdda2a949b861dba402cf2f0c70d1b49
2023-10-27 17:42:29 +00:00
Tim Starling
a588e3969d Add PermissionManager::$blockManager
PermissionManager has an extensive need for block information, and it
gets it from the global state with User::getBlock(). In the future it
should get block information from BlockManager.

However, this is complicated because BlockManager already depends on
PermissionManager, solely for ipblock-exempt rights. For now, have
BlockManager use the service container for this information.

To simplify review, the changes which use
PermissionManager::$blockManager will be in a subsequent commit.

Bug: T345683
Change-Id: I7a92eed142dcf2595cc3e2c5fa85a362c2a10d99
2023-10-19 10:15:20 +11:00
Tim Starling
03a9bf2794 Add Block::toArray()
There are 7 callers of CompositeBlock::getOriginalBlocks() in code
search, and all are following a pattern like

  $blocks = $block instanceof CompositeBlock
    ? $block->getOriginalBlock() : [ $block ]

or can be reorganised to follow such a pattern, which suggests the need
for a public interface along these lines.

So, add Block::toArray() which returns the list of blocks in a
CompositeBlock, or wraps the single block in an array if the block is
not a CompositeBlock.

Also, remove the need for a phan-var override by moving the instanceof
DatabaseBlock check from shouldTrackBlockWithCookie() to its caller.

Bug: T345683
Change-Id: If769db9a831f63a79b57fb3bc9c7cece511c70c3
2023-09-21 10:27:21 +10:00
jenkins-bot
512502fcc9 Merge "Blocks cleanup" 2023-09-20 05:43:55 +00:00
Tim Starling
cdbd39bfdf Blocks cleanup
* Make BlockManager internal methods private, since nothing calls them
  anymore.
* In AbstractBlock and DatabaseBlock, remove deprecated public
  properties mExpiry, mHideName, mTimestamp, mAuto and mParentBlockId.
* In BlockRestrictionStore, remove all the "instanceof Restriction"
  checks. If someone passes in something that's not a Restriction, we
  should throw, not ignore it, because we don't know the caller's
  intention. Add a type declaration to $hasher in equals() so that it
  will throw.
* Remove the "m" prefix from all private and protected properties.
  AbstractBlock is not stable to override so this is not a stable
  interface break.
* In BlockRestrictionStore::restrictionsToRemove(), use an O(N)
  algorithm.
* In BlockRestrictionStore::rowToRestriction(), use a switch instead of
  a type map, so that the calls are statically analyzable.
* In BlockUser::__construct(), fix the initialisation order issue by
  inlining the relevant logic.
* Rename variable $actionRestriction.
* In Special:Block, fix call to deprecated method getTargetAndType(),
  and hard deprecate it. @deprecated has the effect of deprecating a
  method for both internal and external callers, there's no such thing
  as an external-only deprecation. So it's necessary to rename it if you
  want to keep it as a private method.

Bug: T345683
Change-Id: If4a4a18d7b5fec825417de81302266119c215fd3
2023-09-20 09:40:52 +10:00
James D. Forrester
1d0b7ae1e2 Namespace User under \MediaWiki\User
Bug: T166010
Change-Id: I7257302b485588af31384d4f7fc8e30551f161f1
2023-09-19 19:18:16 +00:00
Amir Sarabadani
5bd33d46ef Reorg: Move WebRequest to includes\Request
This has been approved as part of RFC T166010

Bug: T321882
Change-Id: I6bbdbbe6ea48cc1f50bc568bb8780fc7c5361a6f
2023-09-11 21:44:34 +01:00
jenkins-bot
eafcbe2578 Merge "Ensure temporary users are blocked by configured IP blocks" 2023-08-09 15:55:28 +00:00
Thalia
13f4d6bc83 Ensure temporary users are blocked by configured IP blocks
Temporary users are blocked if their IP address is listed in
$wgDnsBlacklistUrls or $wgSoftBlockRanges.

Bug: T343704
Change-Id: Ia3383bd10c3c6c35c586550b68f4af5f4659e815
2023-08-07 14:14:54 +01:00
Thalia
9a647b34b0 BlockManagerTest: Add tests that cover untested service options
Bug: T342885
Change-Id: I21575eda13f9468da8c54e0b1347c4078c3a3af4
2023-08-07 10:52:15 +01:00
Thalia
3184678f10 BlockManagerTest: Use class @covers instead of per-method annotations
Change-Id: I643286fe2eeaf361f4c55cac787d933ba9786162
2023-07-26 16:27:53 +01:00
Amir Sarabadani
bc1af1a53a Reorg: Move WebResponse and FauxResponse to Request/
Bug: T321882
Change-Id: Iae8b9e0170c67c1adf861a6d0da45178a7d3cca7
2023-03-01 18:18:11 +01:00
Umherirrender
abf2ea44e8 tests: Handle dynamic created properties on test classes
- Declare missing properties
- Remove unused or avoid set of properties
- Add AllowDynamicProperties

Bug: T314099
Change-Id: Ie631894f2ffbb616f6dd09851405d681f3eb8fcd
2023-01-13 21:03:42 +00:00
Amir Sarabadani
bbe704b5c1 Reorg: Move some of request related classes to MediaWiki/Request
Redoing I5ea70120d74 but without moving WebRequest that caused issues
with phan-taint-plugin.

Moving:
 - DerivativeRequest
 - FauxRequest
 - FauxRequestUpload
 - PathRouter
 - WebRequestUpload

Bug: T321882
Change-Id: I832b133aaf61ee9f6190b0227d2f3de99bd1717b
2022-10-28 10:15:31 +00:00
Zabe
f6b9381d7f Revert "Reorg: Move some of request related classes to MediaWiki/Request"
This reverts commit 2bdc0b2b72.

Reason for revert: T166010#8349431

Bug: T166010
Change-Id: Idcd3025647aec99532f5d69b9c1718c531761283
2022-10-27 13:14:16 +00:00
Amir Sarabadani
2bdc0b2b72 Reorg: Move some of request related classes to MediaWiki/Request
Moving:
 - DerivativeRequest
 - FauxRequest
 - FauxRequestUpload
 - PathRouter
 - WebRequest
 - WebRequestUpload

Bug: T166010
Change-Id: I5ea70120d745f2876ae31d039f3f8a51e49e9ad8
2022-10-26 16:49:10 +02:00
Derick Alangi
86d4a253e7 tests: Migrate setMwGlobals() to overrideConfigValue(s)()
Directories covered are: /api/, /auth/ and /block/, more to come.

Change-Id: I9dfb0773cbfff36cba7389ed2d9b3c7b1b22612e
2022-07-19 15:42:15 +01:00
Reedy
41c42d5435 Tests: Cleanup some unnecessary nested function calls
Replace ->will( ->return with ->willReturn(

Change-Id: Ia2dfafa03cac8169d86d6fa5a30b73bfad1fe9fa
2022-06-06 01:02:34 +01:00
Alexander Vorwerk
decbaf4f38 phpunit: use ->getServiceContainer() in integration tests
Change-Id: I38299cb65eeaadfdc0eb05db4e8c0b0119cfb37d
2022-01-27 22:04:16 +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
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
Roman Stolar
67cc77312f Update DatabaseBlock construct option 'by' and 'byText' usage to use User Identity only
Bug: T283641
Change-Id: Ic6d4a6e10bda0115c87a85d8a9ddfd4098cd1373
2021-06-02 17:01:32 +03:00
Petr Pchelko
47b1065bb8 Make BlockManager::getUserBlock take UserIdentity
Change-Id: Ifb405196fbe8e4809f05049bf4ab806d9ef29498
2021-05-26 19:29:47 -07:00
DannyS712
1d5bac66c4 Reduce mocking LoggerInterface
Use TestLogger when we want to ensure specific
logged messages, or NullLogger when we don't care

Change-Id: Ifebc770933d4f5313d5b8b43a52437dbe1e24432
2021-04-23 22:35:43 +00: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
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
Paladox
ffe96ff3fb Fix fetching ipblock-exempt within BlockManager::getUserBlock
Bug: T271551
Bug: T270145
Change-Id: I458b7b6b3f79c5961c01c35677b7b25021e316a0
2021-01-25 13:29:39 +00:00
DannyS712
bb1126a825 Add a test for ipblock-exempt granted via hook
Bug: T271551
Change-Id: Ie793d0e2cdfcf98da649f2788d3cacb7bac6ae15
2021-01-09 04:48:19 +00:00
Thiemo Kreuz
7166284835 Add helpful @var tags to BlockManagerTest
Without these IDEs mark method calls on the wrapped objects
as errors.

Change-Id: I4d7ee0d7475174c20a18df99ebda7ef1a3afbd56
2021-01-08 17:13:13 +01:00
James D. Forrester
abdc94a3da Swap out uses of User->isLoggedIn() with ->isRegistered()
Bug: T270450
Change-Id: I90ff94a553ebae9a4ef97c25c333f067f19c327d
2020-12-22 03:13:37 +00:00
Paladox
0b4c4402e8 Fix infinite recursion with wgAutopromote when using getBlock within checkCondition
The problem was that when using $user->getBlock
within checkCondition (APCOND_BLOCKED) caused a infinite
recursion as calling getBlock eventually leads back to checkCondition.

The fix here is to stop using userHasRight as that is the cause of the recursion.

This also fixes testGetUserAutopromoteBlockedDoesNotRecurse test so it
shouldn't fail.

Bug: T270145
Change-Id: Ic751eba70730de286bbd55772bd7562459fb30b4
2020-12-17 19:57:10 +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
Thalia
addb098c39 Deprecate DatabaseBlock methods moved to DatabaseBlockStore
Following 23c3c70d7f, soft deprecate the static methods on
DatabaseBlock that have been moved to DatabaseBlockStore:
* ::insert
* ::delete
* ::update
* ::purgeExpired

Update calls to the deprecated methods from core.

Change-Id: I1272eb978594fd4f386bda12cbc24131ad7d882f
2020-09-13 22:17:27 +00:00
addshore
959bc315f2 MediaWikiTestCase to MediaWikiIntegrationTestCase
The name change happened some time ago, and I think its
about time to start using the name name!
(Done with a find and replace)

My personal motivation for doing this is that I have started
trying out vscode as an IDE for mediawiki development, and
right now it doesn't appear to handle php aliases very well
or at all.

Change-Id: I412235d91ae26e4c1c6a62e0dbb7e7cf3c5ed4a6
2020-06-30 17:02:22 +01: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
Thiemo Kreuz
e1dd371e11 Make use of PHPUnit's assertCount feature where possible
… and avoid assertEmpty() on arrays, in favor of a much more strict
assertSame( [] ).

Change-Id: I20266b0b1fc38a3a87666ba1b0793cb2b37d94a9
2020-03-02 15:58:41 +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
Daimona Eaytoy
7b946ffb4b Fixes for PHPUnit 8 compat in DB suite
Once again, this fixes all issues aside from assertArraySubset.

Bug: T192167
Change-Id: I45c91dc1cf23f04140576dc66233558bb6021324
2019-12-15 00:27:54 +00:00
Thalia
7299ffbb7b Throw deprecation warnings for block cookie methods
DatabaseBlock methods for handling block cookies are deprecated, so
stop using these methods in tests and throw warnings.

Change-Id: I2b5cfd579aa14bbfc7a292587a288ee5032eb5ab
2019-11-18 19:22:37 +00: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
Thalia
a883b6b91b Add tests to ensure that block cookies are kept and removed correctly
Change-Id: I1ff885921b3f159ce41cb8cbd640b90ecd985d41
2019-10-23 14:47:37 +00:00
Thalia
235cade5a3 Clear block cookie when tracking block, not when checking block
Before this commit, clearing a block cookie happens as a
side-effect of checking for a block cookie. After, a block cookie
is cleared if trackBlockWithCookie finds that there should be no
block cookie, but there is one.

Bug: T233595
Change-Id: Id5777361f95c60d2849cacba82f2ed9add647abf
2019-10-09 14:09:34 +01:00
James D. Forrester
40c35286cb Services: Convert BlockManager's static to a const now HHVM is gone
Change-Id: I01d6e18fc30bd61ba7ea5ce1c7c646524579c4ba
2019-10-08 11:24:22 -07:00
Timo Tijhof
0e1e4ee5de
block: Allow cookie-block tracking from any uncached web request
This was previously hardcoded from three places: 1) Upon viewing EditPage,
2) Upon viewing SpecialCreateAccount, 3) For any url if the user is
logged-in (User::loadFromSession/isLoggedIn).

== User::loadFromSession

Performing cookie blocks from here created a circular dependency because
Block may need the user language for localisation, which is determined by
asking the User object. This was previously worked around by using a
DeferredUpdate (T180050, T226777). Moving this logic explicitly to the
end of the pre-send cycle in MediaWiki::preOutputCommit breaks the cycle.
This is also where other request-specific handling resides already.

== Limited effect on unregistered users

When an unregistered user performs an edit, and gets blocked,
the cookie block is not applied until they open built-in editor
or CreateAccount page. This makes it more likely for a user's
IP to change meanwhile. Either intentionally, or simply due to
IPs varying naturally (e.g. between mobile locations, or when
going on/off WiFi). By applying it throughout sessioned page
views for unregistered users, it is more likely to get set.
Similar to what was already done for logged-in users.

This commit also makes the intent of not caching EditPage and
SpecialCreateAccount explicit. This was previously implicit
through nothing having called setCdnMaxage() and/or due to
Session::persist being checked for by OutputPage::sendCacheControl.

Bug: T233594
Change-Id: Icf5a00f9b41d31bb6d4742c049feca0039d0c9d9
2019-10-01 13:52:58 -04:00
Martin Urbanec
c64d6c4379 Log DNS blacklist matches with info level, non-matches with debug level
Bug: T230822
Change-Id: I036b38ac322181fdba4e9e6c1ff539f79b5bb79c
2019-09-14 18:49:30 +02:00
Thalia
5fffa5c07a Pass the user and request into BlockManager::getUserBlock
Blocks are checked from the User object. Specifically,
User::getBlockedStatus instantiates a BlockManager and calls
BlockManager::getUserBlock. However, checking the block often depends
on knowing more about the state than the User should know. As a result,
the global user and request objects were passed into the block manager
on construction.

Whether the global request object should be passed into a service
constructor is still up for debate, so this moves the check for the
global state back to User::getBlockedStatus for now. (Note that it
reintroduces the problem of the User knowing more about state than it
should.)

This change also makes clearer the cases in which
BlockManager::getUserBlock is called from the User.

Different blocks may be sought, depending on the user and their
permissions. The user may be:
(1) The global user (and can be affected by IP blocks). The global
    request object is needed for checking the IP address, the XFF
    header and the cookies.
(2) The global user (and exempt from IP blocks). The global request
    object is needed for checking the cookies.
(3) Another user (not the global user). No request object is available
    or needed; just look for a block against the user account.

Cases #1 and #2 check whether the global user is blocked in practice;
the block may due to their user account being blocked or to an IP
address block or cookie block (or multiple of these). Case #3 simply
checks whether a user's account is blocked, and does not determine
whether the person using that account is affected in practice by any
IP address or cookie blocks.

Bug: T231919
Change-Id: I3f51fd3579514b83b567dfe20926df2f0930dc85
2019-09-11 08:23:54 +01:00