MediaWiki REL1_32
ReadOnlyModeTest.php
Go to the documentation of this file.
1<?php
2
10 public function provider() {
11 $rawTests = [
12 'None of anything' => [
13 'confMessage' => null,
14 'hasFileName' => false,
15 'fileContents' => false,
16 'lbMessage' => false,
17 'expectedState' => false,
18 'expectedMessage' => false,
19 'expectedConfState' => false,
20 'expectedConfMessage' => false
21 ],
22 'File missing' => [
23 'confMessage' => null,
24 'hasFileName' => true,
25 'fileContents' => false,
26 'lbMessage' => false,
27 'expectedState' => false,
28 'expectedMessage' => false,
29 'expectedConfState' => false,
30 'expectedConfMessage' => false
31 ],
32 'File empty' => [
33 'confMessage' => null,
34 'hasFileName' => true,
35 'fileContents' => '',
36 'lbMessage' => false,
37 'expectedState' => false,
38 'expectedMessage' => false,
39 'expectedConfState' => false,
40 'expectedConfMessage' => false
41 ],
42 'File has message' => [
43 'confMessage' => null,
44 'hasFileName' => true,
45 'fileContents' => 'Message',
46 'lbMessage' => false,
47 'expectedState' => true,
48 'expectedMessage' => 'Message',
49 'expectedConfState' => true,
50 'expectedConfMessage' => 'Message',
51 ],
52 'Conf has message' => [
53 'confMessage' => 'Message',
54 'hasFileName' => false,
55 'fileContents' => false,
56 'lbMessage' => false,
57 'expectedState' => true,
58 'expectedMessage' => 'Message',
59 'expectedConfState' => true,
60 'expectedConfMessage' => 'Message'
61 ],
62 "Conf=false means don't check the file" => [
63 'confMessage' => false,
64 'hasFileName' => true,
65 'fileContents' => 'Message',
66 'lbMessage' => false,
67 'expectedState' => false,
68 'expectedMessage' => false,
69 'expectedConfState' => false,
70 'expectedConfMessage' => false,
71 ],
72 'LB has message' => [
73 'confMessage' => null,
74 'hasFileName' => false,
75 'fileContents' => false,
76 'lbMessage' => 'Message',
77 'expectedState' => true,
78 'expectedMessage' => 'Message',
79 'expectedConfState' => false,
80 'expectedConfMessage' => false
81 ],
82 'All three have a message: conf wins' => [
83 'confMessage' => 'conf',
84 'hasFileName' => true,
85 'fileContents' => 'file',
86 'lbMessage' => 'lb',
87 'expectedState' => true,
88 'expectedMessage' => 'conf',
89 'expectedConfState' => true,
90 'expectedConfMessage' => 'conf'
91 ]
92 ];
93 $cookedTests = [];
94 foreach ( $rawTests as $desc => $test ) {
95 $cookedTests[$desc] = [ $test ];
96 }
97 return $cookedTests;
98 }
99
100 private function createMode( $params, $makeLB ) {
101 $config = new HashConfig( [
102 'ReadOnly' => $params['confMessage'],
103 'ReadOnlyFile' => $this->createFile( $params ),
104 ] );
105
106 $rom = new ConfiguredReadOnlyMode( $config );
107
108 if ( $makeLB ) {
109 $lb = $this->createLB( $params );
110 $rom = new ReadOnlyMode( $rom, $lb );
111 }
112
113 return $rom;
114 }
115
116 private function createLB( $params ) {
117 $lb = $this->getMockBuilder( \Wikimedia\Rdbms\LoadBalancer::class )
118 ->disableOriginalConstructor()
119 ->getMock();
120 $lb->expects( $this->any() )->method( 'getReadOnlyReason' )
121 ->willReturn( $params['lbMessage'] );
122 return $lb;
123 }
124
125 private function createFile( $params ) {
126 if ( $params['hasFileName'] ) {
127 $fileName = $this->getNewTempFile();
128
129 if ( $params['fileContents'] === false ) {
130 unlink( $fileName );
131 } else {
132 file_put_contents( $fileName, $params['fileContents'] );
133 }
134 } else {
135 $fileName = null;
136 }
137 return $fileName;
138 }
139
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() );
147 }
148
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() );
156 }
157
158 public function testSetReadOnlyReason() {
159 $rom = $this->createMode(
160 [
161 'confMessage' => 'conf',
162 'hasFileName' => false,
163 'fileContents' => false,
164 'lbMessage' => 'lb'
165 ],
166 true );
167 $rom->setReason( 'override' );
168 $this->assertSame( 'override', $rom->getReason() );
169 }
170
175 public function testClearCache() {
176 $fileName = $this->getNewTempFile();
177 unlink( $fileName );
178 $config = new HashConfig( [
179 'ReadOnly' => null,
180 'ReadOnlyFile' => $fileName,
181 ] );
182 $cro = new ConfiguredReadOnlyMode( $config );
183 $lb = $this->createLB( [ 'lbMessage' => false ] );
184 $rom = new ReadOnlyMode( $cro, $lb );
185
186 $this->assertSame( false, $rom->getReason(), 'initial' );
187
188 file_put_contents( $fileName, 'file' );
189 $this->assertSame( false, $rom->getReason(), 'stale' );
190
191 $rom->clearCache();
192 $this->assertSame( 'file', $rom->getReason(), 'fresh' );
193 }
194}
A read-only mode service which does not depend on LoadBalancer.
A Config instance which stores all settings as a member variable.
getNewTempFile()
Obtains a new temporary file name.
testWithLB( $params)
provider
createMode( $params, $makeLB)
testWithoutLB( $params)
provider
testClearCache()
ReadOnlyMode::clearCache ConfiguredReadOnlyMode::clearCache.
A service class for fetching the wiki's current read-only mode.
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
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
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:37
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
$params