Merge "Increase BotPasswordSessionProvider's default priority"
[lhc/web/wiklou.git] / tests / phpunit / includes / debug / MWDebugTest.php
1 <?php
2
3 class MWDebugTest extends MediaWikiTestCase {
4
5 protected function setUp() {
6 parent::setUp();
7 /** Clear log before each test */
8 MWDebug::clearLog();
9 }
10
11 public static function setUpBeforeClass() {
12 MWDebug::init();
13 MediaWiki\suppressWarnings();
14 }
15
16 public static function tearDownAfterClass() {
17 MWDebug::deinit();
18 MediaWiki\restoreWarnings();
19 }
20
21 /**
22 * @covers MWDebug::log
23 */
24 public function testAddLog() {
25 MWDebug::log( 'logging a string' );
26 $this->assertEquals(
27 [ [
28 'msg' => 'logging a string',
29 'type' => 'log',
30 'caller' => 'MWDebugTest->testAddLog',
31 ] ],
32 MWDebug::getLog()
33 );
34 }
35
36 /**
37 * @covers MWDebug::warning
38 */
39 public function testAddWarning() {
40 MWDebug::warning( 'Warning message' );
41 $this->assertEquals(
42 [ [
43 'msg' => 'Warning message',
44 'type' => 'warn',
45 'caller' => 'MWDebugTest::testAddWarning',
46 ] ],
47 MWDebug::getLog()
48 );
49 }
50
51 /**
52 * @covers MWDebug::deprecated
53 */
54 public function testAvoidDuplicateDeprecations() {
55 MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
56 MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
57
58 // assertCount() not available on WMF integration server
59 $this->assertEquals( 1,
60 count( MWDebug::getLog() ),
61 "Only one deprecated warning per function should be kept"
62 );
63 }
64
65 /**
66 * @covers MWDebug::deprecated
67 */
68 public function testAvoidNonConsecutivesDuplicateDeprecations() {
69 MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
70 MWDebug::warning( 'some warning' );
71 MWDebug::log( 'we could have logged something too' );
72 // Another deprecation
73 MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
74
75 // assertCount() not available on WMF integration server
76 $this->assertEquals( 3,
77 count( MWDebug::getLog() ),
78 "Only one deprecated warning per function should be kept"
79 );
80 }
81
82 /**
83 * @covers MWDebug::appendDebugInfoToApiResult
84 */
85 public function testAppendDebugInfoToApiResultXmlFormat() {
86 $request = $this->newApiRequest(
87 [ 'action' => 'help', 'format' => 'xml' ],
88 '/api.php?action=help&format=xml'
89 );
90
91 $context = new RequestContext();
92 $context->setRequest( $request );
93
94 $apiMain = new ApiMain( $context );
95
96 $result = new ApiResult( $apiMain );
97
98 MWDebug::appendDebugInfoToApiResult( $context, $result );
99
100 $this->assertInstanceOf( 'ApiResult', $result );
101 $data = $result->getResultData();
102
103 $expectedKeys = [ 'mwVersion', 'phpEngine', 'phpVersion', 'gitRevision', 'gitBranch',
104 'gitViewUrl', 'time', 'log', 'debugLog', 'queries', 'request', 'memory',
105 'memoryPeak', 'includes', '_element' ];
106
107 foreach ( $expectedKeys as $expectedKey ) {
108 $this->assertArrayHasKey( $expectedKey, $data['debuginfo'], "debuginfo has $expectedKey" );
109 }
110
111 $xml = ApiFormatXml::recXmlPrint( 'help', $data );
112
113 // exception not thrown
114 $this->assertInternalType( 'string', $xml );
115 }
116
117 /**
118 * @param string[] $params
119 * @param string $requestUrl
120 *
121 * @return FauxRequest
122 */
123 private function newApiRequest( array $params, $requestUrl ) {
124 $request = $this->getMockBuilder( 'FauxRequest' )
125 ->setMethods( [ 'getRequestURL' ] )
126 ->setConstructorArgs( [
127 $params
128 ] )
129 ->getMock();
130
131 $request->expects( $this->any() )
132 ->method( 'getRequestURL' )
133 ->will( $this->returnValue( $requestUrl ) );
134
135 return $request;
136 }
137
138 }