Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / tests / MediaWikiTestCaseTest.php
1 <?php
2
3 use MediaWiki\Logger\LoggerFactory;
4 use MediaWiki\MediaWikiServices;
5 use Psr\Log\LoggerInterface;
6 use Wikimedia\Rdbms\LoadBalancer;
7
8 /**
9 * @covers MediaWikiTestCase
10 * @group MediaWikiTestCaseTest
11 * @group Database
12 *
13 * @author Addshore
14 */
15 class MediaWikiTestCaseTest extends MediaWikiTestCase {
16
17 private static $startGlobals = [
18 'MediaWikiTestCaseTestGLOBAL-ExistingString' => 'foo',
19 'MediaWikiTestCaseTestGLOBAL-ExistingStringEmpty' => '',
20 'MediaWikiTestCaseTestGLOBAL-ExistingArray' => [ 1, 'foo' => 'bar' ],
21 'MediaWikiTestCaseTestGLOBAL-ExistingArrayEmpty' => [],
22 ];
23
24 public static function setUpBeforeClass() {
25 parent::setUpBeforeClass();
26 foreach ( self::$startGlobals as $key => $value ) {
27 $GLOBALS[$key] = $value;
28 }
29 }
30
31 public static function tearDownAfterClass() {
32 parent::tearDownAfterClass();
33 foreach ( self::$startGlobals as $key => $value ) {
34 unset( $GLOBALS[$key] );
35 }
36 }
37
38 public function provideExistingKeysAndNewValues() {
39 $providedArray = [];
40 foreach ( array_keys( self::$startGlobals ) as $key ) {
41 $providedArray[] = [ $key, 'newValue' ];
42 $providedArray[] = [ $key, [ 'newValue' ] ];
43 }
44 return $providedArray;
45 }
46
47 /**
48 * @dataProvider provideExistingKeysAndNewValues
49 *
50 * @covers MediaWikiTestCase::setMwGlobals
51 * @covers MediaWikiTestCase::tearDown
52 */
53 public function testSetGlobalsAreRestoredOnTearDown( $globalKey, $newValue ) {
54 $this->setMwGlobals( $globalKey, $newValue );
55 $this->assertEquals(
56 $newValue,
57 $GLOBALS[$globalKey],
58 'Global failed to correctly set'
59 );
60
61 $this->tearDown();
62
63 $this->assertEquals(
64 self::$startGlobals[$globalKey],
65 $GLOBALS[$globalKey],
66 'Global failed to be restored on tearDown'
67 );
68 }
69
70 /**
71 * @covers MediaWikiTestCase::setMwGlobals
72 * @covers MediaWikiTestCase::tearDown
73 */
74 public function testSetNonExistentGlobalsAreUnsetOnTearDown() {
75 $globalKey = 'abcdefg1234567';
76 $this->setMwGlobals( $globalKey, true );
77 $this->assertTrue(
78 $GLOBALS[$globalKey],
79 'Global failed to correctly set'
80 );
81
82 $this->tearDown();
83
84 $this->assertFalse(
85 isset( $GLOBALS[$globalKey] ),
86 'Global failed to be correctly unset'
87 );
88 }
89
90 public function testOverrideMwServices() {
91 $initialServices = MediaWikiServices::getInstance();
92
93 $this->overrideMwServices();
94 $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
95 }
96
97 public function testSetService() {
98 $initialServices = MediaWikiServices::getInstance();
99 $initialService = $initialServices->getDBLoadBalancer();
100 $mockService = $this->getMockBuilder( LoadBalancer::class )
101 ->disableOriginalConstructor()->getMock();
102
103 $this->setService( 'DBLoadBalancer', $mockService );
104 $this->assertNotSame(
105 $initialService,
106 MediaWikiServices::getInstance()->getDBLoadBalancer()
107 );
108 $this->assertSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
109 }
110
111 /**
112 * @covers MediaWikiTestCase::setLogger
113 * @covers MediaWikiTestCase::restoreLoggers
114 */
115 public function testLoggersAreRestoredOnTearDown_replacingExistingLogger() {
116 $logger1 = LoggerFactory::getInstance( 'foo' );
117 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
118 $logger2 = LoggerFactory::getInstance( 'foo' );
119 $this->tearDown();
120 $logger3 = LoggerFactory::getInstance( 'foo' );
121
122 $this->assertSame( $logger1, $logger3 );
123 $this->assertNotSame( $logger1, $logger2 );
124 }
125
126 /**
127 * @covers MediaWikiTestCase::setLogger
128 * @covers MediaWikiTestCase::restoreLoggers
129 */
130 public function testLoggersAreRestoredOnTearDown_replacingNonExistingLogger() {
131 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
132 $logger1 = LoggerFactory::getInstance( 'foo' );
133 $this->tearDown();
134 $logger2 = LoggerFactory::getInstance( 'foo' );
135
136 $this->assertNotSame( $logger1, $logger2 );
137 $this->assertInstanceOf( \Psr\Log\LoggerInterface::class, $logger2 );
138 }
139
140 /**
141 * @covers MediaWikiTestCase::setLogger
142 * @covers MediaWikiTestCase::restoreLoggers
143 */
144 public function testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice() {
145 $logger1 = LoggerFactory::getInstance( 'baz' );
146 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
147 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
148 $this->tearDown();
149 $logger2 = LoggerFactory::getInstance( 'baz' );
150
151 $this->assertSame( $logger1, $logger2 );
152 }
153
154 /**
155 * @covers MediaWikiTestCase::setupDatabaseWithTestPrefix
156 * @covers MediaWikiTestCase::copyTestData
157 */
158 public function testCopyTestData() {
159 $this->markTestSkippedIfDbType( 'sqlite' );
160
161 $this->tablesUsed[] = 'objectcache';
162 $this->db->insert(
163 'objectcache',
164 [ 'keyname' => __METHOD__, 'value' => 'TEST', 'exptime' => $this->db->timestamp( 11 ) ],
165 __METHOD__
166 );
167
168 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
169 $lb = $lbFactory->newMainLB();
170 $db = $lb->getConnection( DB_REPLICA );
171
172 // sanity
173 $this->assertNotSame( $this->db, $db );
174
175 // Make sure the DB connection has the fake table clones and the fake table prefix
176 MediaWikiTestCase::setupDatabaseWithTestPrefix( $db, $this->dbPrefix(), false );
177
178 $this->assertSame( $this->db->tablePrefix(), $db->tablePrefix(), 'tablePrefix' );
179
180 // Make sure the DB connection has all the test data
181 $this->copyTestData( $this->db, $db );
182
183 $value = $db->selectField( 'objectcache', 'value', [ 'keyname' => __METHOD__ ], __METHOD__ );
184 $this->assertSame( 'TEST', $value, 'Copied Data' );
185 }
186
187 public function testResetServices() {
188 $services = MediaWikiServices::getInstance();
189
190 // override a service instance
191 $myReadOnlyMode = $this->getMockBuilder( ReadOnlyMode::class )
192 ->disableOriginalConstructor()
193 ->getMock();
194 $this->setService( 'ReadOnlyMode', $myReadOnlyMode );
195
196 // sanity check
197 $this->assertSame( $myReadOnlyMode, $services->getService( 'ReadOnlyMode' ) );
198
199 // define a custom service
200 $services->defineService(
201 '_TEST_ResetService_Dummy',
202 function ( MediaWikiServices $services ) {
203 $conf = $services->getMainConfig();
204 return (object)[ 'lang' => $conf->get( 'LanguageCode' ) ];
205 }
206 );
207
208 // sanity check
209 $lang = $services->getMainConfig()->get( 'LanguageCode' );
210 $dummy = $services->getService( '_TEST_ResetService_Dummy' );
211 $this->assertSame( $lang, $dummy->lang );
212
213 // the actual test: change config, reset services.
214 $this->setMwGlobals( 'wgLanguageCode', 'qqx' );
215 $this->resetServices();
216
217 // the overridden service instance should still be there
218 $this->assertSame( $myReadOnlyMode, $services->getService( 'ReadOnlyMode' ) );
219
220 // our custom service should have been re-created with the new language code
221 $dummy2 = $services->getService( '_TEST_ResetService_Dummy' );
222 $this->assertNotSame( $dummy2, $dummy );
223 $this->assertSame( 'qqx', $dummy2->lang );
224 }
225
226 }