MediaWiki  1.29.2
ReadOnlyModeTest.php
Go to the documentation of this file.
1 <?php
2 
4 
12  public function provider() {
13  $rawTests = [
14  'None of anything' => [
15  'confMessage' => null,
16  'hasFileName' => false,
17  'fileContents' => false,
18  'lbMessage' => false,
19  'expectedState' => false,
20  'expectedMessage' => false,
21  'expectedConfState' => false,
22  'expectedConfMessage' => false
23  ],
24  'File missing' => [
25  'confMessage' => null,
26  'hasFileName' => true,
27  'fileContents' => false,
28  'lbMessage' => false,
29  'expectedState' => false,
30  'expectedMessage' => false,
31  'expectedConfState' => false,
32  'expectedConfMessage' => false
33  ],
34  'File empty' => [
35  'confMessage' => null,
36  'hasFileName' => true,
37  'fileContents' => '',
38  'lbMessage' => false,
39  'expectedState' => false,
40  'expectedMessage' => false,
41  'expectedConfState' => false,
42  'expectedConfMessage' => false
43  ],
44  'File has message' => [
45  'confMessage' => null,
46  'hasFileName' => true,
47  'fileContents' => 'Message',
48  'lbMessage' => false,
49  'expectedState' => true,
50  'expectedMessage' => 'Message',
51  'expectedConfState' => true,
52  'expectedConfMessage' => 'Message',
53  ],
54  'Conf has message' => [
55  'confMessage' => 'Message',
56  'hasFileName' => false,
57  'fileContents' => false,
58  'lbMessage' => false,
59  'expectedState' => true,
60  'expectedMessage' => 'Message',
61  'expectedConfState' => true,
62  'expectedConfMessage' => 'Message'
63  ],
64  "Conf=false means don't check the file" => [
65  'confMessage' => false,
66  'hasFileName' => true,
67  'fileContents' => 'Message',
68  'lbMessage' => false,
69  'expectedState' => false,
70  'expectedMessage' => false,
71  'expectedConfState' => false,
72  'expectedConfMessage' => false,
73  ],
74  'LB has message' => [
75  'confMessage' => null,
76  'hasFileName' => false,
77  'fileContents' => false,
78  'lbMessage' => 'Message',
79  'expectedState' => true,
80  'expectedMessage' => 'Message',
81  'expectedConfState' => false,
82  'expectedConfMessage' => false
83  ],
84  'All three have a message: conf wins' => [
85  'confMessage' => 'conf',
86  'hasFileName' => true,
87  'fileContents' => 'file',
88  'lbMessage' => 'lb',
89  'expectedState' => true,
90  'expectedMessage' => 'conf',
91  'expectedConfState' => true,
92  'expectedConfMessage' => 'conf'
93  ]
94  ];
95  $cookedTests = [];
96  foreach ( $rawTests as $desc => $test ) {
97  $cookedTests[$desc] = [ $test ];
98  }
99  return $cookedTests;
100  }
101 
102  private function createMode( $params, $makeLB ) {
103  $config = new HashConfig( [
104  'ReadOnly' => $params['confMessage'],
105  'ReadOnlyFile' => $this->createFile( $params ),
106  ] );
107 
108  $rom = new ConfiguredReadOnlyMode( $config );
109 
110  if ( $makeLB ) {
111  $lb = $this->createLB( $params );
112  $rom = new ReadOnlyMode( $rom, $lb );
113  }
114 
115  return $rom;
116  }
117 
118  private function createLB( $params ) {
119  $lb = $this->getMockBuilder( \Wikimedia\Rdbms\LoadBalancer::class )
120  ->disableOriginalConstructor()
121  ->getMock();
122  $lb->expects( $this->any() )->method( 'getReadOnlyReason' )
123  ->willReturn( $params['lbMessage'] );
124  return $lb;
125  }
126 
127  private function createFile( $params ) {
128  if ( $params['hasFileName'] ) {
129  $fileName = $this->getNewTempFile();
130 
131  if ( $params['fileContents'] === false ) {
132  unlink( $fileName );
133  } else {
134  file_put_contents( $fileName, $params['fileContents'] );
135  }
136  } else {
137  $fileName = null;
138  }
139  return $fileName;
140  }
141 
145  public function testWithLB( $params ) {
146  $rom = $this->createMode( $params, true );
147  $this->assertSame( $params['expectedMessage'], $rom->getReason() );
148  $this->assertSame( $params['expectedState'], $rom->isReadOnly() );
149  }
150 
154  public function testWithoutLB( $params ) {
155  $cro = $this->createMode( $params, false );
156  $this->assertSame( $params['expectedConfMessage'], $cro->getReason() );
157  $this->assertSame( $params['expectedConfState'], $cro->isReadOnly() );
158  }
159 
160  public function testSetReadOnlyReason() {
161  $rom = $this->createMode(
162  [
163  'confMessage' => 'conf',
164  'hasFileName' => false,
165  'fileContents' => false,
166  'lbMessage' => 'lb'
167  ],
168  true );
169  $rom->setReason( 'override' );
170  $this->assertSame( 'override', $rom->getReason() );
171  }
172 
177  public function testClearCache() {
178  $fileName = $this->getNewTempFile();
179  unlink( $fileName );
180  $config = new HashConfig( [
181  'ReadOnly' => null,
182  'ReadOnlyFile' => $fileName,
183  ] );
184  $cro = new ConfiguredReadOnlyMode( $config );
185  $lb = $this->createLB( [ 'lbMessage' => false ] );
186  $rom = new ReadOnlyMode( $cro, $lb );
187 
188  $this->assertSame( false, $rom->getReason(), 'initial' );
189 
190  file_put_contents( $fileName, 'file' );
191  $this->assertSame( false, $rom->getReason(), 'stale' );
192 
193  $rom->clearCache();
194  $this->assertSame( 'file', $rom->getReason(), 'fresh' );
195  }
196 }
ReadOnlyModeTest\createFile
createFile( $params)
Definition: ReadOnlyModeTest.php:127
ReadOnlyModeTest\createMode
createMode( $params, $makeLB)
Definition: ReadOnlyModeTest.php:102
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
ReadOnlyModeTest\testClearCache
testClearCache()
ReadOnlyMode::clearCache ConfiguredReadOnlyMode::clearCache.
Definition: ReadOnlyModeTest.php:177
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
ReadOnlyModeTest\provider
provider()
Definition: ReadOnlyModeTest.php:12
ReadOnlyMode
A service class for fetching the wiki's current read-only mode.
Definition: ReadOnlyMode.php:11
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ReadOnlyModeTest
Database.
Definition: ReadOnlyModeTest.php:11
ConfiguredReadOnlyMode
A read-only mode service which does not depend on LoadBalancer.
Definition: ReadOnlyMode.php:76
$params
$params
Definition: styleTest.css.php:40
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ReadOnlyModeTest\testSetReadOnlyReason
testSetReadOnlyReason()
Definition: ReadOnlyModeTest.php:160
MediaWikiTestCase\getNewTempFile
getNewTempFile()
Obtains a new temporary file name.
Definition: MediaWikiTestCase.php:443
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
ReadOnlyModeTest\createLB
createLB( $params)
Definition: ReadOnlyModeTest.php:118
ReadOnlyModeTest\testWithoutLB
testWithoutLB( $params)
provider
Definition: ReadOnlyModeTest.php:154
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Wikimedia
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
ReadOnlyModeTest\testWithLB
testWithLB( $params)
provider
Definition: ReadOnlyModeTest.php:145