summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/debug/MWDebugTest.php
blob: 7280a97bd75e6e9fdeabcff3d0f08ef4ad605b9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

class MWDebugTest extends MediaWikiTestCase {

	protected function setUp() {
		parent::setUp();
		// Make sure MWDebug class is enabled
		static $MWDebugEnabled = false;
		if ( !$MWDebugEnabled ) {
			MWDebug::init();
			$MWDebugEnabled = true;
		}
		/** Clear log before each test */
		MWDebug::clearLog();
		MediaWiki\suppressWarnings();
	}

	protected function tearDown() {
		MediaWiki\restoreWarnings();
		parent::tearDown();
	}

	/**
	 * @covers MWDebug::log
	 */
	public function testAddLog() {
		MWDebug::log( 'logging a string' );
		$this->assertEquals(
			array( array(
				'msg' => 'logging a string',
				'type' => 'log',
				'caller' => __METHOD__,
			) ),
			MWDebug::getLog()
		);
	}

	/**
	 * @covers MWDebug::warning
	 */
	public function testAddWarning() {
		MWDebug::warning( 'Warning message' );
		$this->assertEquals(
			array( array(
				'msg' => 'Warning message',
				'type' => 'warn',
				'caller' => 'MWDebugTest::testAddWarning',
			) ),
			MWDebug::getLog()
		);
	}

	/**
	 * @covers MWDebug::deprecated
	 */
	public function testAvoidDuplicateDeprecations() {
		MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
		MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );

		// assertCount() not available on WMF integration server
		$this->assertEquals( 1,
			count( MWDebug::getLog() ),
			"Only one deprecated warning per function should be kept"
		);
	}

	/**
	 * @covers MWDebug::deprecated
	 */
	public function testAvoidNonConsecutivesDuplicateDeprecations() {
		MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );
		MWDebug::warning( 'some warning' );
		MWDebug::log( 'we could have logged something too' );
		// Another deprecation
		MWDebug::deprecated( 'wfOldFunction', '1.0', 'component' );

		// assertCount() not available on WMF integration server
		$this->assertEquals( 3,
			count( MWDebug::getLog() ),
			"Only one deprecated warning per function should be kept"
		);
	}

	/**
	 * @covers MWDebug::appendDebugInfoToApiResult
	 */
	public function testAppendDebugInfoToApiResultXmlFormat() {
		$request = $this->newApiRequest(
			array( 'action' => 'help', 'format' => 'xml' ),
			'/api.php?action=help&format=xml'
		);

		$context = new RequestContext();
		$context->setRequest( $request );

		$apiMain = new ApiMain( $context );

		$result = new ApiResult( $apiMain );

		MWDebug::appendDebugInfoToApiResult( $context, $result );

		$this->assertInstanceOf( 'ApiResult', $result );
		$data = $result->getResultData();

		$expectedKeys = array( 'mwVersion', 'phpEngine', 'phpVersion', 'gitRevision', 'gitBranch',
			'gitViewUrl', 'time', 'log', 'debugLog', 'queries', 'request', 'memory',
			'memoryPeak', 'includes', '_element' );

		foreach ( $expectedKeys as $expectedKey ) {
			$this->assertArrayHasKey( $expectedKey, $data['debuginfo'], "debuginfo has $expectedKey" );
		}

		$xml = ApiFormatXml::recXmlPrint( 'help', $data );

		// exception not thrown
		$this->assertInternalType( 'string', $xml );
	}

	/**
	 * @param string[] $params
	 * @param string $requestUrl
	 *
	 * @return FauxRequest
	 */
	private function newApiRequest( array $params, $requestUrl ) {
		$request = $this->getMockBuilder( 'FauxRequest' )
			->setMethods( array( 'getRequestURL' ) )
			->setConstructorArgs( array(
				$params
			) )
			->getMock();

		$request->expects( $this->any() )
			->method( 'getRequestURL' )
			->will( $this->returnValue( $requestUrl ) );

		return $request;
	}

}