Commit graph

113 commits

Author SHA1 Message Date
Thiemo Kreuz
6b2c9deef5 Replace all new stdClass() with identical (object)[]
This should be the exact same. Its more a style change than anything.
So why do it then?
* I believe this is much less confusing than code mentioning a weird
"standard class". Barely anybody knows what this is, and what the
difference between "object" and "stdClass" is.
* The code is shorter.
* It's even faster. In my micro benchmark it's twice as fast.

Change-Id: I7ee0e8ae6d9264a89b6cd1dd861f0466ae620ccc
2020-03-04 21:18:30 +00:00
Daimona Eaytoy
183edf76fd tests: Replace a very long string with str_repeat
Strings like this can make it problematic to scroll the page.

Change-Id: Ic2a5905fc64ea2d3bc1cb76746170953a671d107
2020-02-27 09:41:25 +00:00
Thiemo Kreuz
05fd10ffa9 user: Use more native PHPUnit assertions in UserTest
* Make use of assertGreaterThan() where possible.
* Use the more trivial assertSame() for arrays with only 0 or 1
  elements.
* Flip a few assertions where the expected and actual value have been
  flipped.
* Remove some unused function arguments.

Change-Id: I1d3b5dd55736931fa484065a4aa8d7e336980682
2020-02-20 08:24:00 +01:00
jenkins-bot
bd9bef8add Merge "Use existing $this->user property in UserTest where possible" 2020-02-20 01:35:12 +00:00
Thiemo Kreuz
59f9effce1 Use existing $this->user property in UserTest where possible
Change-Id: Id9fd3dfe4a5c37ec8641f53bce466f4592d47b47
2020-02-17 09:39:01 +01:00
DannyS712
3b0f44970b Add more tests for the user class
Added tests for:
* ::setEmailWithConfirmation
* ::newFromSession, ::getRequest
* ::validateCache, ::getTouched
* ::logout
* ::newFromRow, ::loadFromRow

Fix coverage doc for ::getOptions
Cover missing edge case for ::addGroup

Bug: T233385
Change-Id: I7d6a9718a272be5bd3ef2d135b43f1ae3ed768fa
2020-02-15 02:33:39 +00:00
DannyS712
989c4f7723 Add more tests for the User class
Methods newly tested:
* ::isAllowedAny, ::isAllowedAll, ::isAllowed, ::isNewbie
* ::pingLimiter (basic testing)

Testing expanded for:
* ::checkPasswordValidity
* ::testRealName

Bug: T233385
Change-Id: I2848ecc401b55e4202a853f270f084a093ed46e4
2020-02-11 01:57:04 +00:00
DannyS712
fc9671bdf5 Add more tests for the User class
Newly tested:
* ::getDefaultOption
* ::isCreatableName, ::isUsableName
* ::addToDatabase
* ::changeableGroups
* ::isBlockedFromUpload

Testing expanded for (mostly just specific edge cases):
* ::getIntOption, ::getId, ::getDefaultOptions
* ::addGroup, ::removeGroup
* ::newFromConfirmationCode
* ::idFromName
* ::findUsersByGroup

Bug: T233385
Change-Id: I12bdcffcc6af96da4425e0064f8d395153d05953
2020-02-04 21:14:26 +00:00
DannyS712
0e301253f1 Add more tests for the user class (factory and preferences mostly)
Add tests for
* ::newFromId, ::newFromConfirmationCode
* ::getStubThreshold, ::requiresHTTPS
* ::setItemLoaded, ::isItemLoaded
* ::getGroupMemberships

Expand tests for
* ::getCanonicalName

Bug: T233385
Change-Id: I2c47590e1cef35387aa376303ebfa09e54c0cc34
2020-02-02 07:55:06 +00:00
DannyS712
262501f064 Add more tests for the user class (email and username related mostly)
Add tests for
* ::setId
* ::getName, ::setName
* ::getEmail, ::setEmail, ::invalidateEmail
* ::whoIs

Expand tests for
* ::newFromName

Add coverage tag for ::getDBTouched

Bug: T233385
Change-Id: I565cbc0660d7a9fb16e02595598003228f6fb238
2020-01-27 01:40:57 +00:00
sbassett
b86c6751d0 Password policy to check if a password is a substring of a username
Similar to checkPasswordCannotMatchUsername, MediaWiki should ensure
that users are unable to accidentally create or edit their username
to include their password as a substring of said username.

Bug: T241845
Change-Id: I437e91a5b83a792c5ad31e291915491bbb382dfa
2020-01-13 23:09:13 +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
James D. Forrester
5e9fca47b9 Coding style: Auto-fix MediaWiki.Usage.PHPUnit*
Change-Id: I86fc55a4fc8ceafe368692173211bbcd6d8581d7
2020-01-10 10:17:12 +00:00
DannyS712
1fcb8a1396 Expand options-related tests for User class
Bug: T233385
Change-Id: I8ef7331eba11a9d275699da6a972fe58b40983f6
2020-01-02 17:05:07 +00:00
jenkins-bot
c14a3ca034 Merge "Expand watchlist-related tests for User class" 2019-12-19 09:32:56 +00: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
DannyS712
6582ba0cf0 Expand watchlist-related tests for User class
Bug: T233385
Change-Id: I763c3ee7d21e686f98070211f04019040058ed38
2019-12-14 00:40:16 +00:00
DannyS712
47a2da4b5c Split out separate User::isSystemUser method.
Drawing from comments associated with `User::newSystemUser` for
checking if a user is a system user, a new method, `User:isSystemUser`
is split out to avoid duplication elsewhere.

Bug: T237356
Change-Id: I73f25a10df2c28a69f612eb1db3e91b7125383d9
2019-11-25 16:48:38 +00:00
DannyS712
a429a75b94 Expand testing for User class
Methods newly tested:
* ::getTalkPage, ::getUserPage, ::getTitleKey
* ::getBlockId, ::isAllowedToCreateAccount, ::isBlockedFromEmailuser
* ::getGroups, ::isBot
* ::newFromName, ::getName
* ::setEditCountInternal
* ::getRealName, ::setRealName
* ::useRCPatrol, ::useNPPatrol, ::useFilePatrol

Testing expanded for:
* User::incEditCount
* User::getId

Bug: T233385
Change-Id: I836851b586196f5f12f994b7446d60dba640fd2a
2019-11-25 13:57:26 +00:00
Vedmaka
6f6829fb4f
Adds missing tests for User class related to group management, covers:
- getAutomaticGroups
- getEffectiveGroups
- getGroups
- getFormerGroups
- addGroup
- removeGroup

Bug: T234921
Change-Id: I15c021534471ddad0b4d0a13cae66f3b89616711
2019-11-24 01:11:37 +03: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
Cormac Parle
c4eae0dad4 Search: Provide new preference to control redirects on search matches
To avoid preference bloat, this preference is hidden unless the new
sysadmin config $wgSearchMatchRedirectPreference is set.

Bug: T235263
Change-Id: Ic16f53a4e6ddb6da071d63cd5da28d937d4692c8
2019-11-06 15:37:56 +00:00
Brad Jorsch
685b505628 User: Allow newSystemUser() to create over anonymous actors
Various maintenance scripts assume reserved usernames like
"MediaWiki default" exist, but since they're reserved
User::isUsableName() returns false and therefore the actor migration
created them as anonymous actors. Which would then prevent those
maintenance scripts from using User::newSystemUser() to ensure they
actually exist.

This adjusts User::newSystemUser() to be able to create users for
those anonymous actors.

This also adjusts uses of "MediaWiki default" in core to create it as a
system user.

Bug: T236444
Change-Id: I59a646df36ff9343cc43c05aa20b2b69b2ee124a
2019-11-01 10:36:59 -04: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
cf8516d427 UserTest: Don't assign mBlock manually, it's done by BlockManager
Before this commit, several tests in UserTest set User::mBlock, then
call BlockManager::trackBlockWithCookie, which indirectly sets
User::mBlock. These lines are safe to remove, ahead of deprecating
public access to the property (see T229035).

Change-Id: I36a833ee0904ed1573ac414c8524b60210d97feb
2019-10-15 14:50:20 +00:00
Thiemo Kreuz
5b7dd14725 tests: Remove problematic (bool) casts from UserTest
The return value should never be anything but an actual boolean value.

Change-Id: Icaef4f7c41146c9cf401788e59af4e984c3fde49
2019-10-05 08:44:32 +00: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
Thiemo Kreuz
32a429e8c4 tests: Prefer assertSame() when comparing the integer 0
assertSame() is guaranteed to not do any type conversion. This can be
critical when acciden tially comparing, for example, 0 to 0.0.

Change-Id: Iffcc9bda69573623ba14af655dcd697d0fcce525
2019-09-19 15:35:23 +00:00
jenkins-bot
f9242d74f8 Merge "Pass the user and request into BlockManager::getUserBlock" 2019-09-11 18:58:32 +00: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
Brad Jorsch
c29909e59f Mostly drop old pre-actor user schemas
This removes most of the pre-actor user and user_text columns, and the
$wgActorTableSchemaMigrationStage setting that used to determine
whether the columns were used.

rev_user and rev_user_text remain in the code, as on Wikimedia wikis the
revision table is too large to alter at this time. A future change will
combine that with the removal of rev_comment, rev_content_model, and
rev_content_format (and the addition of rev_comment_id and rev_actor).

ActorMigration's constructor continues to take a $stage parameter, and
continues to have the logic for handling it, for the benefit of
extensions that might need their own migration process. Code using
ActorMigration for accessing the core fields should be updated to use
the new actor fields directly. That will be done for in a followup.

Bug: T188327
Change-Id: Id35544b879af1cd708f3efd303fce8d9a1b9eb02
2019-09-09 11:38:36 -04:00
Aryeh Gregor
7fb4a95563 Remove unneeded overrideMwServices/resetServices
Change-Id: If6cbdec05b8f310ef3a0b4649aaa16d9fb80a047
2019-08-29 14:26:18 +03:00
Petr Pchelko
e3ac564e2d PermissionManager should not cache anonymous rights under ID 0
Bug: T228253
Change-Id: I8a54830842f220ff1ac4402a3380c2229a99b619
2019-08-28 09:47:31 -07:00
Petr Pchelko
5bebae7f96 Remove usages of deprecated User::getRights.
Bug: T220191
Change-Id: Ia7472cf61765fe5fee9ae72cfa9b7060565dbe87
2019-08-20 19:43:54 -07:00
Thalia
f7cddcf7c1 Remove deprecated handling of array keys for $wgProxyList
Change-Id: Ic9cc2a5585180ab57fd361342cbac8210b094a5c
2019-07-24 21:45:45 +01:00
Derick Alangi
ff320d9e49 objectcache: Hard deprecate ObjectCache::getMainWANInstance()
This method was soft-deprecated in 1.28 and this patch cleans up
usage (via object cache) in core and uses services. So, this is now
fit for hard deprecation.

Usage
=====

https://codesearch.wmflabs.org/search/?q=getMainWANInstance&i=nope&files=&repos=

Change-Id: I081a979ed6ed7acf37168a6d81ca4048aae5dd9c
2019-07-09 09:29:57 +00:00
Vedmaka
dd6b94024c Re-apply: Factors out permissions check from User into PermissionManager service
Was reverted by I549810a4cd2e424cc4a438887d2f24614a24cc00 due to
T224607.

Original change by  Vedmaka Wakalaka was
Ia0d840b772ea5f20c9594ce151cc57adc270e48b.

Original commit message:

The following methods should are factored out of the User class into PermissionManager,
leaving only deprecated stubs:

- User::isAllowed -> PermissionManager::userHasRight
- User::getRights -> PermissionManager::getUserPermissions
- User::groupHasPermission -> PermissionManager::groupHasPermission
- User::getGroupPermissions -> PermissionManager::getGroupPermissions
 -User::getGroupsWithPermission -> PermissionManager::getGroupsWithPermission
- User::groupHasPermission -> PermissionManager::groupHasPermission
- User::isEveryoneAllowed -> PermissionManager::isEveryoneAllowed
- User::getAllRights -> PermissionManager::getAllPermissions

Depends-On: I7909e9bd6bbfbd708c0a00b861a9b22a38c6665d

Bug: T218558
Bug: T223294
Change-Id: I8899240378f636ea70f447616710516c0a3c5c31
2019-06-28 13:19:38 -07:00
Thalia
1eaf65d0a5 Add CompositeBlock class for enforcing multiple blocks
Create a CompositeBlock class which extends AbstractBlock and
adds the property $originalBlocks. This is for situations where
more than one block applies to a user/IP, and avoids the need
to choose just one of these blocks to enforce.

Behaviour of the resulting block is determined by combining the
strictest parameters of the original blocks.

Also add DatabaseBlock::newListFromTarget, which is similar to
DatabaseBlock::newFromTarget, but returns all relevant blocks,
rather than choosing the most specific one.

For tracking a CompositeBlock with a cookie, examine the
original blocks and only track the first trackable block that
is found.

Bug: T206163
Change-Id: I088401105ac8ceb2c6117c6d2fcdb277c754d882
2019-06-12 19:16:52 +01:00
Thalia
c5991f614f Move cookie-blocking methods to BlockManager
Move the cookie blocking logic into one place. Specifically, move
these methods to the BlockManager:
* User::trackBlockWithCookie
* DatabaseBlock::setCookie
* DatabaseBlock::clearCookie
* DatabaseBlock::getCookieValue
* DatabaseBlock::getIdFromCookieValue
* AbstractBlock::shouldTrackWithCookie

After this, BlockManager::trackBlockWithCookie should be called to
track a block, and BlockManager::clearBlockCookie should be called
to unset the cookie. The other methods in the above list are
helper methods that are made private or marked internal.

Also update places in core that call User::trackBlockWithCookie to
BlockManager::trackBlockWithCookie

Bug: T225141
Change-Id: I818962c6932c01c841a549a101637e00a7593e48
2019-06-11 15:08:21 +01:00
Kosta Harlan
7f90d1e3a3 Revert "Factors out permissions check from User into PermissionManager service"
This reverts commit 7faa7a7420.

Reason for revert: T224607

Change-Id: I549810a4cd2e424cc4a438887d2f24614a24cc00
2019-05-30 13:51:37 +00:00
Vedmaka
7faa7a7420 Factors out permissions check from User into PermissionManager service
The following methods should are factored out of the User class into PermissionManager, leaving only deprecated stubs:

- User::isAllowed -> PermissionManager::userHasRight
- User::getRights -> PermissionManager::getUserPermissions
- User::groupHasPermission -> PermissionManager::groupHasPermission
- User::getGroupPermissions -> PermissionManager::getGroupPermissions
 -User::getGroupsWithPermission -> PermissionManager::getGroupsWithPermission
- User::groupHasPermission -> PermissionManager::groupHasPermission
- User::isEveryoneAllowed -> PermissionManager::isEveryoneAllowed
- User::getAllRights -> PermissionManager::getAllPermissions

Depends-On: I258f02e286b6ba0387e1bff540a744fafb03dc55
Depends-On: Ie4cedf457eaaa93ec3055c37539322855e02ce26
Depends-On: Id274f240d687efa61cb9f7a15033ae2a7a532083

Bug: T218558
Bug: T223294
Change-Id: Ia0d840b772ea5f20c9594ce151cc57adc270e48b
2019-05-29 17:41:07 +02:00
Thalia
e65a5b5882 Rename Block to MediaWiki\Block\DatabaseBlock
Keep Block as a deprecated class alias for DatabaseBlock.
Update calls to the Block constructor and Block static
methods from external classes.

Also update documentation in several places that refer to
blocks as Blocks.

Bug: T222737
Change-Id: I6d96b63ca0a84bee19486471e0a16a53a79d768a
2019-05-28 12:20:48 +01:00
Derick Alangi
fffadf725e user: Remove deprecated and unused method getPasswordValidity()
This method was deprecated in 1.33 and no longer used. See usage;

Usage
=====

https://codesearch.wmflabs.org/search/?q=%5CbgetPasswordValidity%5Cb&i=nope&files=&repos=

Bug: T220656
Change-Id: I28829f33d40b5568bedb9678fc43beb146b72e56
2019-05-10 00:13:20 +01:00
Thalia
824655f3b7 Separate Block into AbstractBlock, Block and SystemBlock
This commit splits the existing Block class into AbstractBlock, Block
and SystemBlock.

Before this patch, the Block class represents several types of
blocks, which can be separated into blocks stored in the database,
and temporary blocks created by the system. These are now
represented by Block and SystemBlock, which inherit from
AbstractBlock.

This lays the foundations for:
* enforcing block parameters from multiple blocks that apply to a
user/IP address
* improvements to the Block API, including the addition of services

Breaking changes: functions expecting a Block object should still
expect a Block object if it came from the database, but other
functions may now need to expect an AbstractBlock or SystemBlock
object. (Note that an alternative naming scheme, in which the
abstract class is called Block and the subclasses are DatabaseBlock
and SystemBlock, avoids this breakage. However, it introduces more
breakages to calls to static Block methods and new Block
instantiations.)

Changes to tests: system blocks don't set the $blockCreateAccount or
$mExipry block properties, so remove/change any tests that assume
they do.

Bug: T222737
Change-Id: I83bceb5e5049e254c90ace060f8f8fad44696c67
2019-05-07 17:36:31 -05:00
jenkins-bot
9d8759327a Merge "Let User::idFromName always return int or null" 2019-05-07 11:58:31 +00:00
jenkins-bot
8d9d8c8bb3 Merge "Force user id and actor id to 0 when loading from remote wikis" 2019-05-06 15:19:38 +00:00
Bill Pirkle
d4b72572a6 Force user id and actor id to 0 when loading from remote wikis
Stop-gap solution for the problem described in T222212.
Force the User ID and Actor ID to zero for users loaded
from the database of another wiki, to prevent subtle data
corruption and confusing failure modes.

Bug: T222381
Change-Id: Ic585f972d61da136744d080df13d8eb1ecd04cf5
2019-05-06 09:08:51 -05:00
Aryeh Gregor
f7201e3b9b Convert WatchedItem and friends to UserIdentity
I wasn't able to port some places that rely on isAllowed, getOption, or
related methods.

This adds isRegistered() to UserIdentity, which works like
User::isLoggedIn() but with a better name.

I also cleaned up User mocks in WatchedItemQueryServiceUnitTest in the
course of debugging test failures when switching them to
UserIdentityValue instead of mock Users where possible. They now specify
explicitly which methods are allowed to be called on their User objects,
which I believe is good practice for mocks (and unfortunately PHPUnit
makes it awkward).

Bug: T207972
Depends-On: I883d506197a011fe4c102b72df4d9deb58ab5ca2
Change-Id: Iadbf7bc31a496899dbef44e49065ff89f37aea89
2019-05-05 14:35:52 +03:00
jenkins-bot
12b7e6f38a Merge "Introduce a BlockManager service" 2019-05-01 18:26:59 +00:00
Brad Jorsch
d16d8bdf07 Default $wgActorTableSchemaMigrationStage to SCHEMA_COMPAT_NEW
Probably good to start testing this in CI sometime soon.

This also updates a bunch of tests that were forcing an older stage to
force SCHEMA_COMPAT_NEW instead, or to test both ways (until a future
patch removes the _OLD version).

Bug: T188327
Change-Id: Icb9b55cb9d754f2d30d6883005658b9670834756
2019-05-01 00:48:54 +00:00