summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/logging/LogFormatterTestCase.php
blob: e88452b758b8acc2b2eb8e72d062eeb561bcfdd0 (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
<?php

/**
 * @since 1.26
 */
abstract class LogFormatterTestCase extends MediaWikiLangTestCase {

	public function doTestLogFormatter( $row, $extra ) {
		RequestContext::resetMain();
		$row = $this->expandDatabaseRow( $row, $this->isLegacy( $extra ) );

		$formatter = LogFormatter::newFromRow( $row );

		$this->assertEquals(
			$extra['text'],
			self::removeSomeHtml( $formatter->getActionText() ),
			'Action text is equal to expected text'
		);

		$this->assertSame( // ensure types and array key order
			$extra['api'],
			self::removeApiMetaData( $formatter->formatParametersForApi() ),
			'Api log params is equal to expected array'
		);
	}

	protected function isLegacy( $extra ) {
		return isset( $extra['legacy'] ) && $extra['legacy'];
	}

	protected function expandDatabaseRow( $data, $legacy ) {
		return array(
			// no log_id because no insert in database
			'log_type' => $data['type'],
			'log_action' => $data['action'],
			'log_timestamp' => isset( $data['timestamp'] ) ? $data['timestamp'] : wfTimestampNow(),
			'log_user' => isset( $data['user'] ) ? $data['user'] : 0,
			'log_user_text' => isset( $data['user_text'] ) ? $data['user_text'] : 'User',
			'log_namespace' => isset( $data['namespace'] ) ? $data['namespace'] : NS_MAIN,
			'log_title' => isset( $data['title'] ) ? $data['title'] : 'Main_Page',
			'log_page' => isset( $data['page'] ) ? $data['page'] : 0,
			'log_comment' => isset( $data['comment'] ) ? $data['comment'] : '',
			'log_params' => $legacy
				? LogPage::makeParamBlob( $data['params'] )
				: LogEntryBase::makeParamBlob( $data['params'] ),
			'log_deleted' => isset( $data['deleted'] ) ? $data['deleted'] : 0,
		);
	}

	private static function removeSomeHtml( $html ) {
		$html = str_replace( '&quot;', '"', $html );
		return trim( preg_replace( '/<(a|span)[^>]*>([^<]*)<\/\1>/', '$2', $html ) );
	}

	private static function removeApiMetaData( $val ) {
		if ( is_array( $val ) ) {
			unset( $val['_element'] );
			unset( $val['_type'] );
			foreach ( $val as $key => $value ) {
				$val[$key] = self::removeApiMetaData( $value );
			}
		}
		return $val;
	}
}