(bug 24620) Add tests for LogFormatter
[lhc/web/wiklou.git] / tests / phpunit / includes / logging / LogFormatterTest.php
1 <?php
2 /**
3 * @group Database
4 */
5 class LogFormatterTest extends MediaWikiLangTestCase {
6
7 /**
8 * @var User
9 */
10 protected $user;
11
12 /**
13 * @var Title
14 */
15 protected $title;
16
17 /**
18 * @var RequestContext
19 */
20 protected $context;
21
22 protected function setUp() {
23 parent::setUp();
24
25 global $wgLang;
26
27 $this->setMwGlobals( array(
28 'wgLogTypes' => array( 'phpunit' ),
29 'wgLogActionsHandlers' => array( 'phpunit/test' => 'LogFormatter',
30 'phpunit/param' => 'LogFormatter' ),
31 'wgUser' => User::newFromName( 'Testuser' ),
32 'wgExtensionMessagesFiles' => array( 'LogTests' => __DIR__.'/LogTests.i18n.php' ),
33 ) );
34
35 $wgLang->getLocalisationCache()->recache( $wgLang->getCode() );
36
37 $this->user = User::newFromName( 'Testuser' );
38 $this->title = Title::newMainPage();
39
40 $this->context = new RequestContext();
41 $this->context->setUser( $this->user );
42 $this->context->setTitle( $this->title );
43 $this->context->setLanguage( $wgLang );
44 }
45
46 public function newLogEntry( $action, $params ) {
47 $logEntry = new ManualLogEntry( 'phpunit', $action );
48 $logEntry->setPerformer( $this->user );
49 $logEntry->setTarget( $this->title );
50 $logEntry->setComment( 'A very good reason' );
51
52 $logEntry->setParameters( $params );
53
54 return $logEntry;
55 }
56
57 public function testNormalLogParams() {
58 $entry = $this->newLogEntry( 'test', array() );
59 $formatter = LogFormatter::newFromEntry( $entry );
60 $formatter->setContext( $this->context );
61
62 $formatter->setShowUserToolLinks( false );
63 $paramsWithoutTools = $formatter->getMessageParameters();
64 unset( $formatter->parsedParameters );
65
66 $formatter->setShowUserToolLinks( true );
67 $paramsWithTools = $formatter->getMessageParameters();
68
69 $userLink = Linker::userLink(
70 $this->user->getId(),
71 $this->user->getName()
72 );
73
74 $userTools = Linker::userToolLinksRedContribs(
75 $this->user->getId(),
76 $this->user->getName(),
77 $this->user->getEditCount()
78 );
79
80 $titleLink = Linker::link( $this->title, null, array(), array() );
81
82 // $paramsWithoutTools and $paramsWithTools should be only different
83 // in index 0
84 $this->assertEquals( $paramsWithoutTools[1], $paramsWithTools[1] );
85 $this->assertEquals( $paramsWithoutTools[2], $paramsWithTools[2] );
86
87 $this->assertEquals( $userLink, $paramsWithoutTools[0]['raw'] );
88 $this->assertEquals( $userLink . $userTools, $paramsWithTools[0]['raw'] );
89
90 $this->assertEquals( $this->user->getName(), $paramsWithoutTools[1] );
91
92 $this->assertEquals( $titleLink, $paramsWithoutTools[2]['raw'] );
93 }
94
95 public function testLogParamsTypeRaw() {
96 $params = array( '4:raw:raw' => Linker::link( $this->title, null, array(), array() ) );
97 $expected = Linker::link( $this->title, null, array(), array() );
98
99 $entry = $this->newLogEntry( 'param', $params );
100 $formatter = LogFormatter::newFromEntry( $entry );
101 $formatter->setContext( $this->context );
102
103 $logParam = $formatter->getActionText();
104
105 $this->assertEquals( $expected, $logParam );
106 }
107
108 public function testLogParamsTypeMsg() {
109 $params = array( '4:msg:msg' => 'log-description-phpunit' );
110 $expected = wfMessage( 'log-description-phpunit' )->text();
111
112 $entry = $this->newLogEntry( 'param', $params );
113 $formatter = LogFormatter::newFromEntry( $entry );
114 $formatter->setContext( $this->context );
115
116 $logParam = $formatter->getActionText();
117
118 $this->assertEquals( $expected, $logParam );
119 }
120
121 public function testLogParamsTypeMsgContent() {
122 $params = array( '4:msg-content:msgContent' => 'log-description-phpunit' );
123 $expected = wfMessage( 'log-description-phpunit' )->inContentLanguage()->text();
124
125 $entry = $this->newLogEntry( 'param', $params );
126 $formatter = LogFormatter::newFromEntry( $entry );
127 $formatter->setContext( $this->context );
128
129 $logParam = $formatter->getActionText();
130
131 $this->assertEquals( $expected, $logParam );
132 }
133
134 public function testLogParamsTypeNumber() {
135 global $wgLang;
136
137 $params = array( '4:number:number' => 123456789 );
138 $expected = $wgLang->formatNum( 123456789 );
139
140 $entry = $this->newLogEntry( 'param', $params );
141 $formatter = LogFormatter::newFromEntry( $entry );
142 $formatter->setContext( $this->context );
143
144 $logParam = $formatter->getActionText();
145
146 $this->assertEquals( $expected, $logParam );
147 }
148
149 public function testLogParamsTypeUserLink() {
150 $params = array( '4:user-link:userLink' => $this->user->getName() );
151 $expected = Linker::userLink(
152 $this->user->getId(),
153 $this->user->getName()
154 );
155
156 $entry = $this->newLogEntry( 'param', $params );
157 $formatter = LogFormatter::newFromEntry( $entry );
158 $formatter->setContext( $this->context );
159
160 $logParam = $formatter->getActionText();
161
162 $this->assertEquals( $expected, $logParam );
163 }
164
165 public function testLogParamsTypeTitleLink() {
166 $params = array( '4:title-link:titleLink' => $this->title->getText() );
167 $expected = Linker::link( $this->title, null, array(), array() );
168
169 $entry = $this->newLogEntry( 'param', $params );
170 $formatter = LogFormatter::newFromEntry( $entry );
171 $formatter->setContext( $this->context );
172
173 $logParam = $formatter->getActionText();
174
175 $this->assertEquals( $expected, $logParam );
176 }
177
178 public function testLogParamsTypePlain() {
179 $params = array( '4:plain:plain' => 'Some plain text' );
180 $expected = 'Some plain text';
181
182 $entry = $this->newLogEntry( 'param', $params );
183 $formatter = LogFormatter::newFromEntry( $entry );
184 $formatter->setContext( $this->context );
185
186 $logParam = $formatter->getActionText();
187
188 $this->assertEquals( $expected, $logParam );
189 }
190
191 public function testLogComment() {
192 $entry = $this->newLogEntry( 'test', array() );
193 $formatter = LogFormatter::newFromEntry( $entry );
194 $formatter->setContext( $this->context );
195
196 $comment = ltrim( Linker::commentBlock( $entry->getComment() ) );
197
198 $this->assertEquals( $comment, $formatter->getComment() );
199 }
200 }