2019-07-09 16:10:31 +00:00
|
|
|
<?php
|
|
|
|
|
2022-05-03 18:11:54 +00:00
|
|
|
namespace MediaWiki\Minerva;
|
2019-07-09 16:10:31 +00:00
|
|
|
|
2021-02-27 14:36:08 +00:00
|
|
|
use ILanguageConverter;
|
2022-05-03 18:11:54 +00:00
|
|
|
use Language;
|
2021-02-27 14:36:08 +00:00
|
|
|
use MediaWiki\Languages\LanguageConverterFactory;
|
2023-08-19 04:23:00 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-05-03 18:11:54 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
|
|
|
use OutputPage;
|
2019-07-09 16:10:31 +00:00
|
|
|
use PHPUnit\Framework\MockObject\Invocation;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package Tests\MediaWiki\Minerva
|
|
|
|
* @group MinervaNeue
|
|
|
|
* @coversDefaultClass \MediaWiki\Minerva\LanguagesHelper
|
|
|
|
*/
|
2022-05-03 18:11:54 +00:00
|
|
|
class LanguagesHelperTest extends MediaWikiIntegrationTestCase {
|
2019-07-09 16:10:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build test Output object
|
|
|
|
* @param array $langLinks
|
2022-05-03 18:11:54 +00:00
|
|
|
* @return OutputPage
|
2019-07-09 16:10:31 +00:00
|
|
|
*/
|
|
|
|
private function getOutput( array $langLinks ) {
|
2022-05-03 18:11:54 +00:00
|
|
|
$out = $this->createMock( OutputPage::class );
|
2019-07-09 16:10:31 +00:00
|
|
|
$out->expects( $this->once() )
|
|
|
|
->method( 'getLanguageLinks' )
|
|
|
|
->willReturn( $langLinks );
|
|
|
|
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build test Title object
|
2021-01-15 20:57:04 +00:00
|
|
|
* @param bool $hasVariants
|
2019-07-09 16:10:31 +00:00
|
|
|
* @param Invocation|null $matcher
|
2022-05-03 18:11:54 +00:00
|
|
|
* @return Title
|
2019-07-09 16:10:31 +00:00
|
|
|
*/
|
|
|
|
private function getTitle( $hasVariants, Invocation $matcher = null ) {
|
2022-05-03 18:11:54 +00:00
|
|
|
$languageMock = $this->createMock( Language::class );
|
2021-02-27 14:36:08 +00:00
|
|
|
$langConv = $this->createMock( ILanguageConverter::class );
|
|
|
|
$langConv->expects( $matcher ?? $this->any() )->method( 'hasVariants' )->willReturn( $hasVariants );
|
|
|
|
$langConvFactory = $this->createMock( LanguageConverterFactory::class );
|
|
|
|
$langConvFactory->method( 'getLanguageConverter' )->with( $languageMock )->willReturn( $langConv );
|
|
|
|
$this->setService( 'LanguageConverterFactory', $langConvFactory );
|
2019-07-09 16:10:31 +00:00
|
|
|
|
2022-05-03 18:11:54 +00:00
|
|
|
$title = $this->createMock( Title::class );
|
2019-07-09 16:10:31 +00:00
|
|
|
$title->expects( $matcher ?? $this->any() )
|
|
|
|
->method( 'getPageLanguage' )
|
|
|
|
->willReturn( $languageMock );
|
|
|
|
|
|
|
|
return $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::__construct
|
|
|
|
* @covers ::doesTitleHasLanguagesOrVariants
|
|
|
|
*/
|
|
|
|
public function testReturnsWhenOutputPageHasLangLinks() {
|
|
|
|
$helper = new LanguagesHelper( $this->getOutput( [ 'pl:StronaTestowa', 'en:TestPage' ] ) );
|
|
|
|
|
|
|
|
$this->assertTrue( $helper->doesTitleHasLanguagesOrVariants( $this->getTitle( false ) ) );
|
|
|
|
$this->assertTrue( $helper->doesTitleHasLanguagesOrVariants( $this->getTitle( true ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::__construct
|
|
|
|
* @covers ::doesTitleHasLanguagesOrVariants
|
|
|
|
*/
|
|
|
|
public function testReturnsWhenOutputDoesNotHaveLangLinks() {
|
|
|
|
$helper = new LanguagesHelper( $this->getOutput( [] ) );
|
|
|
|
|
|
|
|
$this->assertFalse( $helper->doesTitleHasLanguagesOrVariants(
|
|
|
|
$this->getTitle( false ), $this->once() ) );
|
|
|
|
$this->assertTrue( $helper->doesTitleHasLanguagesOrVariants(
|
|
|
|
$this->getTitle( true ), $this->once() ) );
|
|
|
|
}
|
|
|
|
}
|