| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Unit\CommentFormatter; |
| 4 |
|
| 5 |
use MediaWiki\CommentFormatter\CommentBatch; |
| 6 |
use MediaWiki\CommentFormatter\CommentFormatter; |
| 7 |
use MediaWiki\CommentFormatter\CommentItem; |
| 8 |
use MediaWiki\Title\TitleValue; |
| 9 |
use MediaWikiUnitTestCase; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class CommentBatchTest extends MediaWikiUnitTestCase { |
| 19 |
|
| 20 |
private $calls; |
| 21 |
|
| 22 |
private function getFormatter() { |
| 23 |
return new class( $this->calls ) extends CommentFormatter { |
| 24 |
private $calls; |
| 25 |
|
| 26 |
public function __construct( &$calls ) { |
| 27 |
$this->calls =& $calls; |
| 28 |
} |
| 29 |
|
| 30 |
public function formatItemsInternal( |
| 31 |
$items, $selfLinkTarget = null, $samePage = null, $wikiId = null, |
| 32 |
$enableSectionLinks = null, $useBlock = null, $useParentheses = null |
| 33 |
) { |
| 34 |
$paramDump = CommentFormatterTestUtils::dumpArray( [ |
| 35 |
'selfLinkTarget' => $selfLinkTarget, |
| 36 |
'samePage' => $samePage, |
| 37 |
'wikiId' => $wikiId, |
| 38 |
'enableSectionLinks' => $enableSectionLinks, |
| 39 |
'useBlock' => $useBlock, |
| 40 |
'useParentheses' => $useParentheses, |
| 41 |
] ); |
| 42 |
if ( $paramDump !== '' ) { |
| 43 |
$lines = [ $paramDump ]; |
| 44 |
} |
| 45 |
|
| 46 |
foreach ( $items as $i => $item ) { |
| 47 |
$lines[] = "$i. " . CommentFormatterTestUtils::dumpArray( [ |
| 48 |
'comment' => $item->comment, |
| 49 |
'selfLinkTarget' => $item->selfLinkTarget, |
| 50 |
'samePage' => $item->samePage, |
| 51 |
'wikiId' => $item->wikiId, |
| 52 |
] ); |
| 53 |
} |
| 54 |
$this->calls[] = $lines; |
| 55 |
return []; |
| 56 |
} |
| 57 |
}; |
| 58 |
} |
| 59 |
|
| 60 |
private function newBatch() { |
| 61 |
return new CommentBatch( $this->getFormatter() ); |
| 62 |
} |
| 63 |
|
| 64 |
public function testComments() { |
| 65 |
$this->newBatch() |
| 66 |
->comments( [ new CommentItem( 'c' ) ] ) |
| 67 |
->execute(); |
| 68 |
$this->assertSame( |
| 69 |
[ |
| 70 |
[ '0. comment=c' ], |
| 71 |
], |
| 72 |
$this->calls |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
public function testStrings() { |
| 77 |
$this->newBatch() |
| 78 |
->strings( [ |
| 79 |
1 => 'one', |
| 80 |
3 => 'three', |
| 81 |
5 => 'five', |
| 82 |
] ) |
| 83 |
->execute(); |
| 84 |
$this->assertSame( |
| 85 |
[ |
| 86 |
[ |
| 87 |
'1. comment=one', |
| 88 |
'3. comment=three', |
| 89 |
'5. comment=five' |
| 90 |
], |
| 91 |
], |
| 92 |
$this->calls |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
public static function provideUseBlock() { |
| 97 |
return [ |
| 98 |
[ |
| 99 |
null, |
| 100 |
[ '0. comment=c' ] |
| 101 |
], |
| 102 |
[ |
| 103 |
false, |
| 104 |
[ '!useBlock', '0. comment=c' ] |
| 105 |
], |
| 106 |
[ |
| 107 |
true, |
| 108 |
[ 'useBlock', '0. comment=c' ] |
| 109 |
], |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
public function testUseBlock( $useBlock, $expected ) { |
| 115 |
$batch = $this->newBatch() |
| 116 |
->strings( [ 'c' ] ); |
| 117 |
if ( $useBlock !== null ) { |
| 118 |
$batch->useBlock( $useBlock ); |
| 119 |
} |
| 120 |
$batch->execute(); |
| 121 |
$this->assertSame( [ $expected ], $this->calls ); |
| 122 |
} |
| 123 |
|
| 124 |
public static function provideUseParentheses() { |
| 125 |
return [ |
| 126 |
[ |
| 127 |
null, |
| 128 |
[ '0. comment=c' ] |
| 129 |
], |
| 130 |
[ |
| 131 |
false, |
| 132 |
[ '!useParentheses', '0. comment=c' ] |
| 133 |
], |
| 134 |
[ |
| 135 |
true, |
| 136 |
[ 'useParentheses', '0. comment=c' ] |
| 137 |
], |
| 138 |
]; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
public function testUseParentheses( $useParentheses, $expected ) { |
| 143 |
$batch = $this->newBatch() |
| 144 |
->strings( [ 'c' ] ); |
| 145 |
if ( $useParentheses !== null ) { |
| 146 |
$batch->useParentheses( $useParentheses ); |
| 147 |
} |
| 148 |
$batch->execute(); |
| 149 |
$this->assertSame( [ $expected ], $this->calls ); |
| 150 |
} |
| 151 |
|
| 152 |
public static function provideSelfLinkTarget() { |
| 153 |
return [ |
| 154 |
[ |
| 155 |
null, |
| 156 |
[ '0. comment=c' ] |
| 157 |
], |
| 158 |
[ |
| 159 |
[ 0, 'Page' ], |
| 160 |
[ 'selfLinkTarget=0:Page', '0. comment=c' ] |
| 161 |
], |
| 162 |
]; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
public function testSelfLinkTarget( $selfLinkTarget, $expected ) { |
| 167 |
$batch = $this->newBatch() |
| 168 |
->strings( [ 'c' ] ); |
| 169 |
if ( $selfLinkTarget !== null ) { |
| 170 |
$batch->selfLinkTarget( new TitleValue( $selfLinkTarget[0], $selfLinkTarget[1] ) ); |
| 171 |
} |
| 172 |
$batch->execute(); |
| 173 |
$this->assertSame( [ $expected ], $this->calls ); |
| 174 |
} |
| 175 |
|
| 176 |
public static function provideEnableSectionLinks() { |
| 177 |
return [ |
| 178 |
[ |
| 179 |
null, |
| 180 |
[ '0. comment=c' ] |
| 181 |
], |
| 182 |
[ |
| 183 |
false, |
| 184 |
[ '!enableSectionLinks', '0. comment=c' ] |
| 185 |
], |
| 186 |
[ |
| 187 |
true, |
| 188 |
[ 'enableSectionLinks', '0. comment=c' ] |
| 189 |
], |
| 190 |
]; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
public function testEnableSectionLinks( $enableSectionLinks, $expected ) { |
| 195 |
$batch = $this->newBatch() |
| 196 |
->strings( [ 'c' ] ); |
| 197 |
if ( $enableSectionLinks !== null ) { |
| 198 |
$batch->enableSectionLinks( $enableSectionLinks ); |
| 199 |
} |
| 200 |
$batch->execute(); |
| 201 |
$this->assertSame( [ $expected ], $this->calls ); |
| 202 |
} |
| 203 |
|
| 204 |
public static function provideDisableSectionLinks() { |
| 205 |
return [ |
| 206 |
[ |
| 207 |
null, |
| 208 |
[ '0. comment=c' ] |
| 209 |
], |
| 210 |
[ |
| 211 |
true, |
| 212 |
[ '!enableSectionLinks', '0. comment=c' ] |
| 213 |
], |
| 214 |
]; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
public function testDisableSectionLinks( $disableSectionLinks, $expected ) { |
| 219 |
$batch = $this->newBatch() |
| 220 |
->strings( [ 'c' ] ); |
| 221 |
if ( $disableSectionLinks !== null ) { |
| 222 |
$batch->disableSectionLinks(); |
| 223 |
} |
| 224 |
$batch->execute(); |
| 225 |
$this->assertSame( [ $expected ], $this->calls ); |
| 226 |
} |
| 227 |
|
| 228 |
public static function provideSamePage() { |
| 229 |
return [ |
| 230 |
[ |
| 231 |
null, |
| 232 |
[ '0. comment=c' ] |
| 233 |
], |
| 234 |
[ |
| 235 |
false, |
| 236 |
[ '!samePage', '0. comment=c' ] |
| 237 |
], |
| 238 |
[ |
| 239 |
true, |
| 240 |
[ 'samePage', '0. comment=c' ] |
| 241 |
], |
| 242 |
]; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
public function testSamePage( $samePage, $expected ) { |
| 247 |
$batch = $this->newBatch() |
| 248 |
->strings( [ 'c' ] ); |
| 249 |
if ( $samePage !== null ) { |
| 250 |
$batch->samePage( $samePage ); |
| 251 |
} |
| 252 |
$batch->execute(); |
| 253 |
$this->assertSame( [ $expected ], $this->calls ); |
| 254 |
} |
| 255 |
|
| 256 |
public static function provideWikiId() { |
| 257 |
return [ |
| 258 |
[ |
| 259 |
null, |
| 260 |
[ '0. comment=c' ] |
| 261 |
], |
| 262 |
[ |
| 263 |
'enwiki', |
| 264 |
[ 'wikiId=enwiki', '0. comment=c' ] |
| 265 |
], |
| 266 |
]; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
public function testWikiId( $wikiId, $expected ) { |
| 271 |
$batch = $this->newBatch() |
| 272 |
->strings( [ 'c' ] ); |
| 273 |
if ( $wikiId !== null ) { |
| 274 |
$batch->wikiId( $wikiId ); |
| 275 |
} |
| 276 |
$batch->execute(); |
| 277 |
$this->assertSame( [ $expected ], $this->calls ); |
| 278 |
} |
| 279 |
|
| 280 |
public function testItemSelfLinkTarget() { |
| 281 |
$item = ( new CommentItem( 'c' ) ) |
| 282 |
->selfLinkTarget( new TitleValue( 0, 'Page' ) ); |
| 283 |
$this->newBatch() |
| 284 |
->comments( [ $item ] ) |
| 285 |
->execute(); |
| 286 |
$this->assertSame( |
| 287 |
[ |
| 288 |
[ '0. comment=c, selfLinkTarget=0:Page' ], |
| 289 |
], |
| 290 |
$this->calls |
| 291 |
); |
| 292 |
} |
| 293 |
|
| 294 |
public function testItemSamePage() { |
| 295 |
$item = ( new CommentItem( 'c' ) ) |
| 296 |
->samePage(); |
| 297 |
$this->newBatch() |
| 298 |
->comments( [ $item ] ) |
| 299 |
->execute(); |
| 300 |
$this->assertSame( |
| 301 |
[ |
| 302 |
[ '0. comment=c, samePage' ], |
| 303 |
], |
| 304 |
$this->calls |
| 305 |
); |
| 306 |
} |
| 307 |
|
| 308 |
public function testItemWikiId() { |
| 309 |
$item = ( new CommentItem( 'c' ) ) |
| 310 |
->wikiId( 'enwiki' ); |
| 311 |
$this->newBatch() |
| 312 |
->comments( [ $item ] ) |
| 313 |
->execute(); |
| 314 |
$this->assertSame( |
| 315 |
[ |
| 316 |
[ '0. comment=c, wikiId=enwiki' ], |
| 317 |
], |
| 318 |
$this->calls |
| 319 |
); |
| 320 |
} |
| 321 |
|
| 322 |
} |