| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools\Tests; |
| 4 |
|
| 5 |
use MediaWiki\Cache\GenderCache; |
| 6 |
use MediaWiki\Context\IContextSource; |
| 7 |
use MediaWiki\Extension\DiscussionTools\BatchModifyElements; |
| 8 |
use MediaWiki\Json\FormatJson; |
| 9 |
use MediaWiki\MainConfigNames; |
| 10 |
use MediaWiki\Output\OutputPage; |
| 11 |
use MediaWiki\Parser\ParserOutput; |
| 12 |
use MediaWiki\Title\Title; |
| 13 |
use MediaWiki\User\User; |
| 14 |
use Skin; |
| 15 |
use Wikimedia\TestingAccessWrapper; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class CommentFormatterTest extends IntegrationTestCase { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function testIsLanguageRequiringReplyIcon( |
| 27 |
string $langCode, bool $expected, ?array $config = null |
| 28 |
): void { |
| 29 |
$lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( $langCode ); |
| 30 |
if ( $config ) { |
| 31 |
$this->overrideConfigValues( [ |
| 32 |
'DiscussionTools_visualenhancements_reply_icon_languages' => $config |
| 33 |
] ); |
| 34 |
} |
| 35 |
$actual = MockCommentFormatter::isLanguageRequiringReplyIcon( $lang ); |
| 36 |
static::assertEquals( $expected, $actual, $langCode ); |
| 37 |
} |
| 38 |
|
| 39 |
public static function provideIsLanguageRequiringReplyIcon(): array { |
| 40 |
return [ |
| 41 |
[ 'zh', true ], |
| 42 |
[ 'zh-hant', true ], |
| 43 |
[ 'ar', true ], |
| 44 |
[ 'arz', true ], |
| 45 |
[ 'arz', false, [ 'ar' => true, 'arz' => false ] ], |
| 46 |
[ 'en', false ], |
| 47 |
[ 'he', false ], |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function testAddDiscussionToolsInternal( |
| 55 |
string $name, string $titleText, string $dom, string $expected, string $config, string $data, |
| 56 |
bool $isMobile, bool $useButtons |
| 57 |
): void { |
| 58 |
if ( $isMobile ) { |
| 59 |
$this->markTestSkippedIfExtensionNotLoaded( 'MobileFrontend' ); |
| 60 |
} |
| 61 |
|
| 62 |
$this->setService( 'GenderCache', $this->createNoOpMock( GenderCache::class ) ); |
| 63 |
|
| 64 |
$dom = static::getHtml( $dom ); |
| 65 |
$expectedPath = $expected; |
| 66 |
$expected = static::getText( $expectedPath ); |
| 67 |
$config = static::getJson( $config ); |
| 68 |
$data = static::getJson( $data ); |
| 69 |
|
| 70 |
$this->overrideConfigValues( [ |
| 71 |
|
| 72 |
MainConfigNames::Server => 'https://example.org', |
| 73 |
MainConfigNames::CanonicalServer => 'https://example.org', |
| 74 |
MainConfigNames::ArticlePath => '/wiki/$1', |
| 75 |
MainConfigNames::ScriptPath => '/w', |
| 76 |
MainConfigNames::Script => '/w/index.php', |
| 77 |
] ); |
| 78 |
|
| 79 |
$title = Title::newFromText( $titleText ); |
| 80 |
$subscriptionStore = new MockSubscriptionStore(); |
| 81 |
$user = $this->createMock( User::class ); |
| 82 |
$qqxLang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'qqx' ); |
| 83 |
$skin = $this->createMock( Skin::class ); |
| 84 |
$skin->method( 'getSkinName' )->willReturn( 'minerva' ); |
| 85 |
$outputPage = $this->createMock( IContextSource::class ); |
| 86 |
$outputPage->method( 'getTitle' )->willReturn( $title ); |
| 87 |
$outputPage->method( 'getUser' )->willReturn( $user ); |
| 88 |
$outputPage->method( 'getLanguage' )->willReturn( $qqxLang ); |
| 89 |
$outputPage->method( 'getSkin' )->willReturn( $skin ); |
| 90 |
$outputPage->method( 'msg' )->willReturn( 'a label' ); |
| 91 |
|
| 92 |
MockCommentFormatter::$parser = $this->createParser( $config, $data ); |
| 93 |
$commentFormatter = TestingAccessWrapper::newFromClass( MockCommentFormatter::class ); |
| 94 |
|
| 95 |
$pout = new ParserOutput(); |
| 96 |
$preprocessed = $commentFormatter->addDiscussionToolsInternal( $dom, $pout, $title ); |
| 97 |
$preprocessed .= "\n<pre>\n" . |
| 98 |
"newestComment: " . FormatJson::encode( |
| 99 |
$pout->getExtensionData( 'DiscussionTools-newestComment' ), "\t", FormatJson::ALL_OK ) . "\n" . |
| 100 |
( $pout->getExtensionData( 'DiscussionTools-hasLedeContent' ) ? |
| 101 |
"hasLedeContent\n" : '' ) . |
| 102 |
( $pout->getExtensionData( 'DiscussionTools-hasCommentsInLedeContent' ) ? |
| 103 |
"hasCommentsInLedeContent\n" : '' ) . |
| 104 |
( $pout->getExtensionData( 'DiscussionTools-isEmptyTalkPage' ) ? |
| 105 |
"isEmptyTalkPage\n" : '' ) . |
| 106 |
FormatJson::encode( $pout->getJsConfigVars(), "\t", FormatJson::ALL_OK ) . |
| 107 |
"\n</pre>"; |
| 108 |
|
| 109 |
OutputPage::setupOOUI(); |
| 110 |
|
| 111 |
$batchModifyElements = new BatchModifyElements(); |
| 112 |
|
| 113 |
MockCommentFormatter::postprocessTimestampLinks( |
| 114 |
$preprocessed, $batchModifyElements, $outputPage |
| 115 |
); |
| 116 |
|
| 117 |
MockCommentFormatter::postprocessTopicSubscription( |
| 118 |
$preprocessed, $batchModifyElements, $outputPage, $subscriptionStore, $isMobile, $useButtons |
| 119 |
); |
| 120 |
|
| 121 |
MockCommentFormatter::postprocessVisualEnhancements( |
| 122 |
$preprocessed, $batchModifyElements, $outputPage, $isMobile |
| 123 |
); |
| 124 |
|
| 125 |
MockCommentFormatter::postprocessReplyTool( |
| 126 |
$preprocessed, $batchModifyElements, $outputPage, $isMobile, $useButtons |
| 127 |
); |
| 128 |
|
| 129 |
$actual = $batchModifyElements->apply( $preprocessed ); |
| 130 |
|
| 131 |
|
| 132 |
$actual = preg_replace( '/ id=[\'"]ooui-php-[0-9]+[\'"]/', '', $actual ); |
| 133 |
|
| 134 |
|
| 135 |
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) { |
| 136 |
static::overwriteTextFile( $expectedPath, $actual ); |
| 137 |
} |
| 138 |
|
| 139 |
static::assertEquals( $expected, $actual, $name ); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public static function provideAddDiscussionToolsInternal() { |
| 146 |
foreach ( static::getJson( '../cases/formatted.json' ) as $case ) { |
| 147 |
|
| 148 |
yield array_merge( $case, |
| 149 |
[ 'expected' => $case['expected']['desktop'], 'isMobile' => false, 'useButtons' => true ] ); |
| 150 |
yield array_merge( $case, |
| 151 |
[ 'expected' => $case['expected']['mobile'], 'isMobile' => true, 'useButtons' => true ] ); |
| 152 |
|
| 153 |
|
| 154 |
yield array_merge( $case, |
| 155 |
[ 'expected' => $case['expected']['legacy'], 'isMobile' => false, 'useButtons' => false ] ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
} |