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