Merge "watchlist: fix nonsensical timestamp/boolean comparisons in EnhancedRecentChanges"
[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 * @covers MediaWikiTestCase::setMwGlobals
71 * @covers MediaWikiTestCase::tearDown
72 */
73 public function testSetNonExistentGlobalsAreUnsetOnTearDown() {
74 $globalKey = 'abcdefg1234567';
75 $this->setMwGlobals( $globalKey, true );
76 $this->assertTrue(
77 $GLOBALS[$globalKey],
78 'Global failed to correctly set'
79 );
80
81 $this->tearDown();
82
83 $this->assertFalse(
84 isset( $GLOBALS[$globalKey] ),
85 'Global failed to be correctly unset'
86 );
87 }
88
89 public function testOverrideMwServices() {
90 $initialServices = MediaWikiServices::getInstance();
91
92 $this->overrideMwServices();
93 $this->assertNotSame( $initialServices, MediaWikiServices::getInstance() );
94 }
95
96 public function testSetService() {
97 $initialServices = MediaWikiServices::getInstance();
98 $initialService = $initialServices->getDBLoadBalancer();
99 $mockService = $this->getMockBuilder( LoadBalancer::class )
100 ->disableOriginalConstructor()->getMock();
101
102 $this->setService( 'DBLoadBalancer', $mockService );
103 $this->assertNotSame(
104 $initialService,
105 MediaWikiServices::getInstance()->getDBLoadBalancer()
106 );
107 $this->assertSame( $mockService, MediaWikiServices::getInstance()->getDBLoadBalancer() );
108 }
109
110 /**
111 * @covers MediaWikiTestCase::setLogger
112 * @covers MediaWikiTestCase::restoreLoggers
113 */
114 public function testLoggersAreRestoredOnTearDown_replacingExistingLogger() {
115 $logger1 = LoggerFactory::getInstance( 'foo' );
116 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
117 $logger2 = LoggerFactory::getInstance( 'foo' );
118 $this->tearDown();
119 $logger3 = LoggerFactory::getInstance( 'foo' );
120
121 $this->assertSame( $logger1, $logger3 );
122 $this->assertNotSame( $logger1, $logger2 );
123 }
124
125 /**
126 * @covers MediaWikiTestCase::setLogger
127 * @covers MediaWikiTestCase::restoreLoggers
128 */
129 public function testLoggersAreRestoredOnTearDown_replacingNonExistingLogger() {
130 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
131 $logger1 = LoggerFactory::getInstance( 'foo' );
132 $this->tearDown();
133 $logger2 = LoggerFactory::getInstance( 'foo' );
134
135 $this->assertNotSame( $logger1, $logger2 );
136 $this->assertInstanceOf( \Psr\Log\LoggerInterface::class, $logger2 );
137 }
138
139 /**
140 * @covers MediaWikiTestCase::setLogger
141 * @covers MediaWikiTestCase::restoreLoggers
142 */
143 public function testLoggersAreRestoredOnTearDown_replacingSameLoggerTwice() {
144 $logger1 = LoggerFactory::getInstance( 'baz' );
145 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
146 $this->setLogger( 'foo', $this->createMock( LoggerInterface::class ) );
147 $this->tearDown();
148 $logger2 = LoggerFactory::getInstance( 'baz' );
149
150 $this->assertSame( $logger1, $logger2 );
151 }
152
153 /**
154 * @covers MediaWikiTestCase::setupDatabaseWithTestPrefix
155 * @covers MediaWikiTestCase::copyTestData
156 */
157 public function testCopyTestData() {
158 $this->markTestSkippedIfDbType( 'sqlite' );
159
160 $this->tablesUsed[] = 'objectcache';
161 $this->db->insert(
162 'objectcache',
163 [ 'keyname' => __METHOD__, 'value' => 'TEST', 'exptime' => $this->db->timestamp( 11 ) ],
164 __METHOD__
165 );
166
167 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
168 $lb = $lbFactory->newMainLB();
169 $db = $lb->getConnection( DB_REPLICA, DBO_TRX );
170
171 // sanity
172 $this->assertNotSame( $this->db, $db );
173
174 // Make sure the DB connection has the fake table clones and the fake table prefix
175 MediaWikiTestCase::setupDatabaseWithTestPrefix( $db, $this->dbPrefix(), false );
176
177 $this->assertSame( $this->db->tablePrefix(), $db->tablePrefix(), 'tablePrefix' );
178
179 // Make sure the DB connection has all the test data
180 $this->copyTestData( $this->db, $db );
181
182 $value = $db->selectField( 'objectcache', 'value', [ 'keyname' => __METHOD__ ], __METHOD__ );
183 $this->assertSame( 'TEST', $value, 'Copied Data' );
184 }
185
186 }