71ab14783a231e94e69f27a8ee07cc7b3725a7db
[lhc/web/wiklou.git] / tests / phpunit / includes / logging / LogFormatterTestCase.php
1 <?php
2
3 /**
4 * @since 1.26
5 */
6 abstract class LogFormatterTestCase extends MediaWikiLangTestCase {
7
8 public function doTestLogFormatter( $row, $extra, $userGroups = [] ) {
9 RequestContext::resetMain();
10 $row = $this->expandDatabaseRow( $row, $this->isLegacy( $extra ) );
11
12 $context = new RequestContext();
13 $context->setUser( $this->getTestUser( $userGroups )->getUser() );
14
15 $formatter = LogFormatter::newFromRow( $row );
16 $formatter->setContext( $context );
17
18 $this->assertEquals(
19 $extra['text'],
20 self::removeSomeHtml( $formatter->getActionText() ),
21 'Action text is equal to expected text'
22 );
23
24 $this->assertSame( // ensure types and array key order
25 $extra['api'],
26 self::removeApiMetaData( $formatter->formatParametersForApi() ),
27 'Api log params is equal to expected array'
28 );
29 }
30
31 protected function isLegacy( $extra ) {
32 return isset( $extra['legacy'] ) && $extra['legacy'];
33 }
34
35 protected function expandDatabaseRow( $data, $legacy ) {
36 return [
37 // no log_id because no insert in database
38 'log_type' => $data['type'],
39 'log_action' => $data['action'],
40 'log_timestamp' => $data['timestamp'] ?? wfTimestampNow(),
41 'log_user' => $data['user'] ?? 0,
42 'log_user_text' => $data['user_text'] ?? 'User',
43 'log_actor' => $data['actor'] ?? 0,
44 'log_namespace' => $data['namespace'] ?? NS_MAIN,
45 'log_title' => $data['title'] ?? 'Main_Page',
46 'log_page' => $data['page'] ?? 0,
47 'log_comment_text' => $data['comment'] ?? '',
48 'log_comment_data' => null,
49 'log_params' => $legacy
50 ? LogPage::makeParamBlob( $data['params'] )
51 : LogEntryBase::makeParamBlob( $data['params'] ),
52 'log_deleted' => $data['deleted'] ?? 0,
53 ];
54 }
55
56 private static function removeSomeHtml( $html ) {
57 $html = str_replace( '&quot;', '"', $html );
58 $html = preg_replace( '/\xE2\x80[\x8E\x8F]/', '', $html ); // Strip lrm/rlm
59 return trim( strip_tags( $html ) );
60 }
61
62 private static function removeApiMetaData( $val ) {
63 if ( is_array( $val ) ) {
64 unset( $val['_element'] );
65 unset( $val['_type'] );
66 foreach ( $val as $key => $value ) {
67 $val[$key] = self::removeApiMetaData( $value );
68 }
69 }
70 return $val;
71 }
72 }