Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F7485909
ContextAttributesFactoryTest.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
8 KB
Referenced Files
None
Subscribers
None
ContextAttributesFactoryTest.php
View Options
<?php
use
MediaWiki\Context\RequestContext
;
use
MediaWiki\Extension\EventLogging\Libs\UserBucketProvider\UserBucketService
;
use
MediaWiki\Extension\EventLogging\MetricsPlatform\ContextAttributesFactory
;
use
MediaWiki\MainConfigNames
;
use
MediaWiki\Registration\ExtensionRegistry
;
use
MediaWiki\Title\Title
;
use
MediaWiki\User\User
;
/**
* @covers \MediaWiki\Extension\EventLogging\MetricsPlatform\ContextAttributesFactory
* @group Database
*/
class
ContextAttributesFactoryTest
extends
MediaWikiLangTestCase
{
/**
* @var array
*/
private
$services
;
/**
* @var ContextAttributesFactory
*/
private
$contextAttributesFactory
;
public
function
setUp
():
void
{
parent
::
setUp
();
$services
=
$this
->
getServiceContainer
();
$this
->
services
=
[
'mainConfig'
=>
$services
->
getMainConfig
(),
'extensionRegistry'
=>
$this
->
createMock
(
ExtensionRegistry
::
class
),
'namespaceInfo'
=>
$services
->
getNamespaceInfo
(),
'restrictionStore'
=>
$services
->
getRestrictionStore
(),
'userOptionsLookup'
=>
$services
->
getUserOptionsLookup
(),
'contentLanguage'
=>
$services
->
getContentLanguage
(),
'userGroupManager'
=>
$services
->
getUserGroupManager
(),
'languageConverterFactory'
=>
$services
->
getLanguageConverterFactory
(),
'userBucketService'
=>
new
UserBucketService
(
$services
->
getUserEditTracker
()
),
];
// Unit Under Test
$this
->
contextAttributesFactory
=
$this
->
getMockBuilder
(
ContextAttributesFactory
::
class
)
->
setConstructorArgs
(
$this
->
services
)
->
onlyMethods
(
[
'shouldDisplayMobileView'
]
)
->
getMock
();
}
public
static
function
provideAgentContextAttributes
():
Generator
{
yield
[
'shouldDisplayMobileView'
=>
false
,
'expectedClientPlatformFamily'
=>
'desktop_browser'
,
];
yield
[
'shouldDisplayMobileView'
=>
true
,
'expectedClientPlatformFamily'
=>
'mobile_browser'
,
];
}
/**
* @dataProvider provideAgentContextAttributes
*/
public
function
testAgentContextAttributes
(
$shouldDisplayMobileView
,
$expectedClientPlatformFamily
):
void
{
$user
=
$this
->
createMock
(
User
::
class
);
$user
->
method
(
'getName'
)->
willReturn
(
'TestUser'
);
$user
->
method
(
'getEditCount'
)->
willReturn
(
42
);
$title
=
Title
::
makeTitle
(
NS_SPECIAL
,
'Blankpage'
);
$contextSource
=
RequestContext
::
newExtraneousContext
(
$title
);
$contextSource
->
setUser
(
$user
);
$this
->
contextAttributesFactory
->
method
(
'shouldDisplayMobileView'
)
->
willReturn
(
$shouldDisplayMobileView
);
$contextAttributes
=
$this
->
contextAttributesFactory
->
newContextAttributes
(
$contextSource
);
$this
->
assertNull
(
$contextAttributes
[
'agent_app_install_id'
]
);
$this
->
assertSame
(
'mediawiki_php'
,
$contextAttributes
[
'agent_client_platform'
]
);
$this
->
assertSame
(
$expectedClientPlatformFamily
,
$contextAttributes
[
'agent_client_platform_family'
]
);
}
public
function
testPageContextAttributes
():
void
{
$title
=
Title
::
makeTitle
(
NS_SPECIAL
,
'Blankpage'
);
$contextSource
=
RequestContext
::
newExtraneousContext
(
$title
);
$expectedWikidataItemId
=
'QFooBarBaz'
;
$output
=
$contextSource
->
getOutput
();
$output
->
setProperty
(
'wikibase_item'
,
$expectedWikidataItemId
);
$contextAttributes
=
$this
->
contextAttributesFactory
->
newContextAttributes
(
$contextSource
);
$this
->
assertSame
(
0
,
$contextAttributes
[
'page_id'
]
);
$this
->
assertSame
(
'Blankpage'
,
$contextAttributes
[
'page_title'
]
);
$this
->
assertSame
(
NS_SPECIAL
,
$contextAttributes
[
'page_namespace_id'
]
);
$this
->
assertSame
(
'Special'
,
$contextAttributes
[
'page_namespace_name'
]
);
$this
->
assertSame
(
0
,
$contextAttributes
[
'page_revision_id'
]
);
$this
->
assertSame
(
$expectedWikidataItemId
,
$contextAttributes
[
'page_wikidata_qid'
]
);
$this
->
assertSame
(
'en'
,
$contextAttributes
[
'page_content_language'
]
);
$this
->
assertSame
(
false
,
$contextAttributes
[
'page_is_redirect'
]
);
$this
->
assertSame
(
[],
$contextAttributes
[
'page_groups_allowed_to_move'
]
);
$this
->
assertSame
(
[],
$contextAttributes
[
'page_groups_allowed_to_edit'
]
);
}
public
function
testPageWikidataIdHandlesNull
():
void
{
$title
=
Title
::
makeTitle
(
NS_SPECIAL
,
'Blankpage'
);
$contextSource
=
RequestContext
::
newExtraneousContext
(
$title
);
$contextAttributes
=
$this
->
contextAttributesFactory
->
newContextAttributes
(
$contextSource
);
$this
->
assertNull
(
$contextAttributes
[
'page_wikidata_qid'
]
);
}
public
function
testMediaWikiContextAttributes
():
void
{
$title
=
Title
::
makeTitle
(
NS_SPECIAL
,
'Blankpage'
);
$contextSource
=
RequestContext
::
newExtraneousContext
(
$title
);
$contextAttributes
=
$this
->
contextAttributesFactory
->
newContextAttributes
(
$contextSource
);
$mainConfig
=
$this
->
services
[
'mainConfig'
];
$expectedMediaWikiIsProduction
=
strpos
(
MW_VERSION
,
'wmf'
)
!==
false
;
$expectedMediaWikiDBName
=
$mainConfig
->
get
(
MainConfigNames
::
DBname
);
$expectedMediaWikiSiteContentLanguage
=
$this
->
services
[
'contentLanguage'
]->
getCode
();
$this
->
assertSame
(
MW_VERSION
,
$contextAttributes
[
'mediawiki_version'
]
);
$this
->
assertSame
(
$expectedMediaWikiIsProduction
,
$contextAttributes
[
'mediawiki_is_production'
]
);
$this
->
assertSame
(
$expectedMediaWikiDBName
,
$contextAttributes
[
'mediawiki_db_name'
]
);
$this
->
assertSame
(
$expectedMediaWikiSiteContentLanguage
,
$contextAttributes
[
'mediawiki_site_content_language'
]
);
}
public
static
function
provideMediaWikiIsDebugMode
():
Generator
{
yield
[
'eventLoggingDisplayConsole'
=>
0
,
'expectedMediaWikiIsDebugMode'
=>
false
,
];
yield
[
'eventLoggingDisplayConsole'
=>
1
,
'expectedMediaWikiIsDebugMode'
=>
true
,
];
}
/**
* @dataProvider provideMediaWikiIsDebugMode
*/
public
function
testBuildIsDebugMode
(
$eventLoggingDisplayConsole
,
$expectedMediaWikiIsDebugMode
)
{
$user
=
$this
->
getMutableTestUser
()->
getUser
();
$title
=
Title
::
makeTitle
(
NS_SPECIAL
,
'Blankpage'
);
$contextSource
=
RequestContext
::
newExtraneousContext
(
$title
);
$contextSource
->
setUser
(
$user
);
$services
=
$this
->
getServiceContainer
();
$userOptionsManager
=
$services
->
getUserOptionsManager
();
$userOptionsManager
->
setOption
(
$user
,
'eventlogging-display-console'
,
$eventLoggingDisplayConsole
);
$userOptionsManager
->
saveOptions
(
$user
);
$contextAttributes
=
$this
->
contextAttributesFactory
->
newContextAttributes
(
$contextSource
);
$this
->
assertSame
(
$expectedMediaWikiIsDebugMode
,
$contextAttributes
[
'mediawiki_is_debug_mode'
]
);
}
public
function
testPerformerContextAttributes
():
void
{
$groups
=
[
'foo'
,
'bar'
];
$user
=
$this
->
getTestUser
(
$groups
)->
getUser
();
$expectedGroups
=
$this
->
services
[
'userGroupManager'
]->
getUserEffectiveGroups
(
$user
);
$title
=
Title
::
makeTitle
(
NS_SPECIAL
,
'Blankpage'
);
$contextSource
=
RequestContext
::
newExtraneousContext
(
$title
);
$contextSource
->
setUser
(
$user
);
$expectedUserLanguage
=
$contextSource
->
getLanguage
();
$languageConverter
=
$this
->
services
[
'languageConverterFactory'
]->
getLanguageConverter
(
$expectedUserLanguage
);
$expectedUserLanguageVariant
=
$languageConverter
->
hasVariants
()
?
$languageConverter
->
getPreferredVariant
()
:
null
;
$expectedUserCanProbablyEditPage
=
$user
->
probablyCan
(
'edit'
,
$title
);
$expectedUserEditCountBucket
=
$this
->
services
[
'userBucketService'
]->
getUserEditCountBucket
(
$user
);
$expectedUserRegistrationTimestamp
=
$user
->
getRegistration
();
if
(
$expectedUserRegistrationTimestamp
)
{
$expectedUserRegistrationTimestamp
=
wfTimestamp
(
TS_ISO_8601
,
$expectedUserRegistrationTimestamp
);
}
$contextAttributes
=
$this
->
contextAttributesFactory
->
newContextAttributes
(
$contextSource
);
$this
->
assertSame
(
!
$user
->
isAnon
(),
$contextAttributes
[
'performer_is_logged_in'
]
);
$this
->
assertSame
(
$user
->
getId
(),
$contextAttributes
[
'performer_id'
]
);
$this
->
assertSame
(
$user
->
getName
(),
$contextAttributes
[
'performer_name'
]
);
$this
->
assertSame
(
$expectedGroups
,
$contextAttributes
[
'performer_groups'
]
);
$this
->
assertSame
(
$user
->
isBot
(),
$contextAttributes
[
'performer_is_bot'
]
);
$this
->
assertSame
(
$expectedUserLanguage
->
getCode
(),
$contextAttributes
[
'performer_language'
]
);
$this
->
assertSame
(
$expectedUserLanguageVariant
,
$contextAttributes
[
'performer_language_variant'
]
);
$this
->
assertSame
(
$expectedUserCanProbablyEditPage
,
$contextAttributes
[
'performer_can_probably_edit_page'
]
);
$this
->
assertSame
(
$user
->
getEditCount
(),
$contextAttributes
[
'performer_edit_count'
]
);
$this
->
assertSame
(
$expectedUserEditCountBucket
,
$contextAttributes
[
'performer_edit_count_bucket'
]
);
$this
->
assertSame
(
$expectedUserRegistrationTimestamp
,
$contextAttributes
[
'performer_registration_dt'
]
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Mon, Nov 23, 15:02 (5 d, 21 h ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
66/1e/8bf7c60d77e3918d14d32f4e3ca9
Default Alt Text
ContextAttributesFactoryTest.php (8 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment