7 * @covers ConfiguredReadOnlyMode
9 class ReadOnlyModeTest
extends MediaWikiTestCase
{
10 public function provider() {
12 'None of anything' => [
13 'confMessage' => null,
14 'hasFileName' => false,
15 'fileContents' => false,
17 'expectedState' => false,
18 'expectedMessage' => false,
19 'expectedConfState' => false,
20 'expectedConfMessage' => false
23 'confMessage' => null,
24 'hasFileName' => true,
25 'fileContents' => false,
27 'expectedState' => false,
28 'expectedMessage' => false,
29 'expectedConfState' => false,
30 'expectedConfMessage' => false
33 'confMessage' => null,
34 'hasFileName' => true,
37 'expectedState' => false,
38 'expectedMessage' => false,
39 'expectedConfState' => false,
40 'expectedConfMessage' => false
42 'File has message' => [
43 'confMessage' => null,
44 'hasFileName' => true,
45 'fileContents' => 'Message',
47 'expectedState' => true,
48 'expectedMessage' => 'Message',
49 'expectedConfState' => true,
50 'expectedConfMessage' => 'Message',
52 'Conf has message' => [
53 'confMessage' => 'Message',
54 'hasFileName' => false,
55 'fileContents' => false,
57 'expectedState' => true,
58 'expectedMessage' => 'Message',
59 'expectedConfState' => true,
60 'expectedConfMessage' => 'Message'
62 "Conf=false means don't check the file" => [
63 'confMessage' => false,
64 'hasFileName' => true,
65 'fileContents' => 'Message',
67 'expectedState' => false,
68 'expectedMessage' => false,
69 'expectedConfState' => false,
70 'expectedConfMessage' => false,
73 'confMessage' => null,
74 'hasFileName' => false,
75 'fileContents' => false,
76 'lbMessage' => 'Message',
77 'expectedState' => true,
78 'expectedMessage' => 'Message',
79 'expectedConfState' => false,
80 'expectedConfMessage' => false
82 'All three have a message: conf wins' => [
83 'confMessage' => 'conf',
84 'hasFileName' => true,
85 'fileContents' => 'file',
87 'expectedState' => true,
88 'expectedMessage' => 'conf',
89 'expectedConfState' => true,
90 'expectedConfMessage' => 'conf'
94 foreach ( $rawTests as $desc => $test ) {
95 $cookedTests[$desc] = [ $test ];
100 private function createMode( $params, $makeLB ) {
101 $config = new HashConfig( [
102 'ReadOnly' => $params['confMessage'],
103 'ReadOnlyFile' => $this->createFile( $params ),
106 $rom = new ConfiguredReadOnlyMode( $config );
109 $lb = $this->createLB( $params );
110 $rom = new ReadOnlyMode( $rom, $lb );
116 private function createLB( $params ) {
117 $lb = $this->getMockBuilder( \Wikimedia\Rdbms\LoadBalancer
::class )
118 ->disableOriginalConstructor()
120 $lb->expects( $this->any() )->method( 'getReadOnlyReason' )
121 ->willReturn( $params['lbMessage'] );
125 private function createFile( $params ) {
126 if ( $params['hasFileName'] ) {
127 $fileName = $this->getNewTempFile();
129 if ( $params['fileContents'] === false ) {
132 file_put_contents( $fileName, $params['fileContents'] );
141 * @dataProvider provider
143 public function testWithLB( $params ) {
144 $rom = $this->createMode( $params, true );
145 $this->assertSame( $params['expectedMessage'], $rom->getReason() );
146 $this->assertSame( $params['expectedState'], $rom->isReadOnly() );
150 * @dataProvider provider
152 public function testWithoutLB( $params ) {
153 $cro = $this->createMode( $params, false );
154 $this->assertSame( $params['expectedConfMessage'], $cro->getReason() );
155 $this->assertSame( $params['expectedConfState'], $cro->isReadOnly() );
158 public function testSetReadOnlyReason() {
159 $rom = $this->createMode(
161 'confMessage' => 'conf',
162 'hasFileName' => false,
163 'fileContents' => false,
167 $rom->setReason( 'override' );
168 $this->assertSame( 'override', $rom->getReason() );
172 * @covers ReadOnlyMode::clearCache
173 * @covers ConfiguredReadOnlyMode::clearCache
175 public function testClearCache() {
176 $fileName = $this->getNewTempFile();
178 $config = new HashConfig( [
180 'ReadOnlyFile' => $fileName,
182 $cro = new ConfiguredReadOnlyMode( $config );
183 $lb = $this->createLB( [ 'lbMessage' => false ] );
184 $rom = new ReadOnlyMode( $cro, $lb );
186 $this->assertSame( false, $rom->getReason(), 'initial' );
188 file_put_contents( $fileName, 'file' );
189 $this->assertSame( false, $rom->getReason(), 'stale' );
192 $this->assertSame( 'file', $rom->getReason(), 'fresh' );