Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F5984961
CentralAuthDatabaseManagerTest.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
4 KB
Referenced Files
None
Subscribers
None
CentralAuthDatabaseManagerTest.php
View Options
<?php
use
MediaWiki\Config\ServiceOptions
;
use
MediaWiki\Extension\CentralAuth\CentralAuthDatabaseManager
;
use
MediaWiki\Extension\CentralAuth\CentralAuthReadOnlyError
;
use
Wikimedia\Rdbms\IDatabase
;
use
Wikimedia\Rdbms\IReadableDatabase
;
use
Wikimedia\Rdbms\LBFactory
;
use
Wikimedia\Rdbms\ReadOnlyMode
;
/**
* @covers \MediaWiki\Extension\CentralAuth\CentralAuthDatabaseManager
* @todo Convert to a pure unit test. Currently testGetCentralDBPrimaryReadOnly() needs global
* state, as it creates the CentralAuthReadOnlyError, which as an ErrorPageError creates the
* translations for it using global state (wfMessage()). This is tracked as T281935.
*/
class
CentralAuthDatabaseManagerTest
extends
MediaWikiIntegrationTestCase
{
public
function
testGetReadOnlyReasonReadOnlyMode
()
{
$roReason
=
'Database switchover script broke and left everything read only'
;
$roMode
=
$this
->
createMock
(
ReadOnlyMode
::
class
);
$roMode
->
method
(
'isReadOnly'
)->
willReturn
(
true
);
$roMode
->
method
(
'getReason'
)->
willReturn
(
$roReason
);
// Override global mode for ReadOnlyError::__construct()
$this
->
overrideMwServices
(
null
,
[
'ReadOnlyMode'
=>
static
function
()
use
(
$roMode
)
{
return
$roMode
;
}
]
);
$manager
=
new
CentralAuthDatabaseManager
(
new
ServiceOptions
(
CentralAuthDatabaseManager
::
CONSTRUCTOR_OPTIONS
,
[
'CentralAuthReadOnly'
=>
false
,
]
),
$this
->
createMock
(
LBFactory
::
class
),
$roMode
);
$this
->
expectException
(
ReadOnlyError
::
class
);
$this
->
expectExceptionMessageMatches
(
'/'
.
preg_quote
(
$roReason
,
'/'
)
.
'/'
);
$manager
->
assertNotReadOnly
();
}
public
function
testGetReadOnlyReasonDatabase
()
{
$roReason
=
'replace this with the real reason before the maintenance window'
;
$lbFactory
=
$this
->
createMock
(
LBFactory
::
class
);
$roMode
=
$this
->
createMock
(
ReadOnlyMode
::
class
);
$roMode
->
method
(
'isReadOnly'
)->
willReturn
(
false
);
$roMode
->
method
(
'getReason'
)->
willReturn
(
$roReason
);
$manager
=
new
CentralAuthDatabaseManager
(
new
ServiceOptions
(
CentralAuthDatabaseManager
::
CONSTRUCTOR_OPTIONS
,
[
'CentralAuthReadOnly'
=>
false
,
]
),
$lbFactory
,
$roMode
);
$this
->
expectException
(
CentralAuthReadOnlyError
::
class
);
$this
->
expectExceptionMessageMatches
(
'/'
.
preg_quote
(
$roReason
,
'/'
)
.
'/'
);
$manager
->
assertNotReadOnly
();
}
public
function
testGetReadOnlyReasonWriteable
()
{
$lbFactory
=
$this
->
createMock
(
LBFactory
::
class
);
$roMode
=
$this
->
createMock
(
ReadOnlyMode
::
class
);
$roMode
->
method
(
'isReadOnly'
)->
willReturn
(
false
);
$roMode
->
method
(
'getReason'
)->
willReturn
(
false
);
$manager
=
new
CentralAuthDatabaseManager
(
new
ServiceOptions
(
CentralAuthDatabaseManager
::
CONSTRUCTOR_OPTIONS
,
[
'CentralAuthReadOnly'
=>
false
,
]
),
$lbFactory
,
$roMode
);
$this
->
assertFalse
(
$manager
->
isReadOnly
()
);
$manager
->
assertNotReadOnly
();
}
public
function
testGetCentralDBPrimaryReadOnly
()
{
$database
=
$this
->
createMock
(
IDatabase
::
class
);
$lbFactory
=
$this
->
createNoOpMock
(
LBFactory
::
class
);
$lbFactory
->
method
(
'getPrimaryDatabase'
)->
with
(
'virtual-centralauth'
)->
willReturn
(
$database
);
$roMode
=
$this
->
createMock
(
ReadOnlyMode
::
class
);
$roMode
->
method
(
'isReadOnly'
)->
willReturn
(
false
);
$manager
=
new
CentralAuthDatabaseManager
(
new
ServiceOptions
(
CentralAuthDatabaseManager
::
CONSTRUCTOR_OPTIONS
,
[
'CentralAuthReadOnly'
=>
true
,
]
),
$lbFactory
,
$roMode
);
$this
->
expectException
(
CentralAuthReadOnlyError
::
class
);
$manager
->
getCentralPrimaryDB
();
}
public
function
testGetCentralDBReplicaReadOnly
()
{
$database
=
$this
->
createMock
(
IReadableDatabase
::
class
);
$lbFactory
=
$this
->
createMock
(
LBFactory
::
class
);
$lbFactory
->
method
(
'getReplicaDatabase'
)->
with
(
'virtual-centralauth'
)->
willReturn
(
$database
);
$roMode
=
$this
->
createMock
(
ReadOnlyMode
::
class
);
$roMode
->
method
(
'isReadOnly'
)->
willReturn
(
false
);
$manager
=
new
CentralAuthDatabaseManager
(
new
ServiceOptions
(
CentralAuthDatabaseManager
::
CONSTRUCTOR_OPTIONS
,
[
'CentralAuthReadOnly'
=>
true
,
]
),
$lbFactory
,
$roMode
);
$this
->
assertEquals
(
$database
,
$manager
->
getCentralReplicaDB
()
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Oct 3, 23:36 (2 w, 2 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
53/0a/4b773791d1971ccc83a05944c467
Default Alt Text
CentralAuthDatabaseManagerTest.php (4 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment