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