Commit graph

122 commits

Author SHA1 Message Date
Reedy
a8b006426e Fix tests/ PSR12.Properties.ConstantVisibility.NotFound
Change-Id: I0beed1a35e046705fb84c9d1f63cf92afd009bb4
2020-05-16 04:30:21 +01:00
MusikAnimal
a1ddac1d54 Add methods to check if a page is temporarily watched
Add 'mw-watchlink-temp' class to watch link when temporarily watched.

Bug: T248495
Change-Id: I61b24fddf6aaf5233f426602bfa1cc4fafce942b
2020-05-13 19:11:52 +00:00
Nikki Nikkhoui
8d378e977c Update setTemporaryHook() to use scopedRegister()
The new HookContainer.php introduces a scopedRegister() method for
temporarily setting hooks. Let's use that in MediaWikiUnitTestCase
and MediaWikiIntegrationTestCase instead of directly accessing
global $wgHooks to do so.

Also introduces setTemporaryHook() and removeTemporaryHook()
methods in MWIntegrationTestCase for easily adding/removing of
temporary hooks.

Bug: T250300
Change-Id: I8cefd41b66f882c53646b76de76c51f0d8730f72
2020-05-11 14:12:00 +10:00
Petr Pchelko
c339be9ef0 UserTest::testGetNewMessageLinks pass artificial clock by reference
Change-Id: I872cb7c8e07df1aa065dceb8202d514d73c8b391
2020-05-08 07:34:45 -07:00
daniel
5c6df39131 UserTest: Ensure revisions have distinct timestamps
UserTest::testGetNewMessageLinks was sometimes failing under postgres,
when the two revisions got the same timestamp. The reason for this is
that User::getNewMessageLinks() is based on
TalkPageNotificationManager::getLatestSeenMessageTimestamp(). To
reliably test the behavior of User::getNewMessageLinks(), we need to
operate on revisions with distinct timestamps.

Change-Id: I701983c3b241a70167a6bf212c41a6571b9b0f62
2020-05-08 10:37:53 +02:00
Petr Pchelko
d6084c9dcd Fix Postgres user_newtalk table
In mysql the user_id and user_ip fields are not nullable,
but include a default 0 and '', however in Postgress the default
was not set, while user_id was set to not nullable. In the
code that uses this table, we set either user_id or user_ip
depending on whether the user is anon or registered. Thus both
fields should either be nullable, or contain a default.
Given that mysql has defaults set, I've opted for bringing
Postgress closer to what mysql does.

This was always broken and was discovered by adding new tests
for this functionality.

Bug: T239640
Change-Id: I75bf469a30225687a4b0cb550a4068cb07208c01
2020-05-04 11:17:03 -07:00
Petr Pchelko
788331c48a Introduce UserOptionsManager and DefaultOptionsManager
This converts user options management to a separate
service for use in DI context.

User options are accessed quite early on in installation
process and full-on options management depends on the
database. Prior we have protected from accessing the DB
by setting a hacky $wgUser with 0 id, and relying on the
implementation that it doesn't go into the database to
get the default user options. Now we can't really do that
since DBLoadBalancer is required to instantiate the options
manager. Instead, we redefine the options manager with
a DefaultOptionsManager, that only provides access to
default options and doesn't require DB access.

UserOptionsManager uses PreferencesFactory, however
injecting it will produce a cyclic dependency. The problem
is that we separate options to different kinds, which are
inferred from the PreferencesFactory declaration for those
options (e.g. if it's a radio button in the UI declaration,
the option is of multiselect kind). This is plain wrong,
the dependency should be wise versa. This will be addressed
separately, since it's requires larger refactoring. For now
the PreferencesFactory is obtained on demand. This will be
addressed in a followup.

Bug: T248527
Change-Id: I74917c5eaec184d188911a319895b941ed55ee87
2020-04-28 15:42:43 -07:00
Nikki Nikkhoui
7ac981a03e Set logger to null in UserTest.php
The code is appropriately logging when it encounters a database
'Duplicate entry' error. However, the CI interprets that as
an actual exception being thrown. Setting the logger to null
allows the CI to complete.

Bug: T248195
Change-Id: I6419fdb7a6ae54478da1236a3c14b491ade65022
2020-04-07 16:45:05 -07:00
DannyS712
23242d04d1 Add a new UserNameUtils service
This replaces User::isValidUserName, ::isUsableName, ::isCreatableName,
::getCanonicalName, and ::isIP.

Unlike User::isIP, UserNameUtils::isIP will //not// return true
for IPv6 ranges.

UserNameUtils::isIPRange, like User::isIPRange, accepts a name and
simply calls IPUtils::isValidRange.

User::isValidUserName, ::isUsableName, ::isCreatableName,
::getCanonical, ::isIP, and ::isValidRange are all soft deprecated

A follow up patch will add this to the release notes, to avoid merge
conflicts.

Bug: T245231
Bug: T239527
Change-Id: I46684bc492bb74b728ff102971f6cdd4d746a50a
2020-03-26 01:31:14 +00:00
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