2015-10-07 15:18:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PageImages\Tests;
|
|
|
|
|
2017-01-31 12:59:16 +00:00
|
|
|
use IContextSource;
|
2024-01-05 21:37:40 +00:00
|
|
|
use MediaWiki\Config\HashConfig;
|
|
|
|
use MediaWiki\Output\OutputPage;
|
2023-08-19 23:54:45 +00:00
|
|
|
use MediaWiki\Request\FauxRequest;
|
2023-08-19 18:18:00 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2021-10-11 22:57:52 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2020-04-19 22:41:19 +00:00
|
|
|
use PageImages\PageImages;
|
2017-01-31 12:59:16 +00:00
|
|
|
use SkinTemplate;
|
2015-10-07 15:18:54 +00:00
|
|
|
|
|
|
|
/**
|
2020-04-19 22:41:19 +00:00
|
|
|
* @covers \PageImages\PageImages
|
2015-10-07 15:18:54 +00:00
|
|
|
*
|
|
|
|
* @group PageImages
|
|
|
|
* @group Database
|
|
|
|
*
|
2018-05-25 04:42:29 +00:00
|
|
|
* @license WTFPL
|
2017-11-24 07:33:49 +00:00
|
|
|
* @author Thiemo Kreuz
|
2015-10-07 15:18:54 +00:00
|
|
|
*/
|
2021-10-11 22:57:52 +00:00
|
|
|
class PageImagesTest extends MediaWikiIntegrationTestCase {
|
2015-10-07 15:18:54 +00:00
|
|
|
|
2023-10-15 13:29:43 +00:00
|
|
|
private function newPageImages() {
|
2023-10-15 13:39:15 +00:00
|
|
|
$services = $this->getServiceContainer();
|
2023-10-15 13:29:43 +00:00
|
|
|
return new PageImages(
|
2023-10-15 18:48:50 +00:00
|
|
|
$services->getDBLoadBalancerFactory(),
|
2023-10-15 13:39:15 +00:00
|
|
|
$services->getRepoGroup(),
|
|
|
|
$services->getUserOptionsLookup()
|
2023-10-15 13:29:43 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-10 00:02:00 +00:00
|
|
|
public function testPagePropertyNames() {
|
2015-10-07 15:18:54 +00:00
|
|
|
$this->assertSame( 'page_image', PageImages::PROP_NAME );
|
2016-11-10 00:02:00 +00:00
|
|
|
$this->assertSame( 'page_image_free', PageImages::PROP_NAME_FREE );
|
2015-10-07 15:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructor() {
|
2023-10-15 13:29:43 +00:00
|
|
|
$pageImages = $this->newPageImages();
|
2020-05-01 01:33:19 +00:00
|
|
|
$this->assertInstanceOf( PageImages::class, $pageImages );
|
2015-10-07 15:18:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 22:11:25 +00:00
|
|
|
public function testGivenNonExistingPageGetPageImageReturnsFalse() {
|
2017-01-31 12:59:16 +00:00
|
|
|
$title = $this->newTitle();
|
2023-10-16 06:00:36 +00:00
|
|
|
$this->assertNull( $this->newPageImages()->getPageImageInternal( $title ) );
|
2015-10-07 15:18:54 +00:00
|
|
|
$this->assertFalse( PageImages::getPageImage( $title ) );
|
|
|
|
}
|
|
|
|
|
2016-11-10 00:02:00 +00:00
|
|
|
public function testGetPropName() {
|
|
|
|
$this->assertSame( 'page_image', PageImages::getPropName( false ) );
|
|
|
|
$this->assertSame( 'page_image_free', PageImages::getPropName( true ) );
|
|
|
|
}
|
2017-01-31 12:59:16 +00:00
|
|
|
|
2020-04-20 13:34:12 +00:00
|
|
|
public function testGetPropNames() {
|
|
|
|
$this->assertSame(
|
|
|
|
[ PageImages::PROP_NAME_FREE, PageImages::PROP_NAME ],
|
|
|
|
PageImages::getPropNames( PageImages::LICENSE_ANY )
|
|
|
|
);
|
|
|
|
$this->assertSame(
|
|
|
|
PageImages::PROP_NAME_FREE,
|
|
|
|
PageImages::getPropNames( PageImages::LICENSE_FREE )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:27:15 +00:00
|
|
|
public function testGivenDeactivedOpenGraphOnBeforePageDisplayDoesNotAddMeta() {
|
|
|
|
$outputPage = $this->mockOutputPage( [
|
|
|
|
'PageImagesOpenGraph' => false,
|
|
|
|
] );
|
|
|
|
$outputPage->expects( $this->never() )
|
|
|
|
->method( 'addMeta' );
|
|
|
|
|
|
|
|
$skinTemplate = new SkinTemplate();
|
2023-10-15 13:29:43 +00:00
|
|
|
$this->newPageImages()->onBeforePageDisplay( $outputPage, $skinTemplate );
|
2022-07-28 15:27:15 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 22:11:25 +00:00
|
|
|
public function testGivenNonExistingPageOnBeforePageDisplayDoesNotAddMeta() {
|
2021-06-18 03:16:36 +00:00
|
|
|
$outputPage = $this->mockOutputPage( [
|
2022-07-28 15:27:15 +00:00
|
|
|
'PageImagesOpenGraph' => true,
|
2021-06-18 03:16:36 +00:00
|
|
|
'PageImagesOpenGraphFallbackImage' => false
|
|
|
|
] );
|
|
|
|
$outputPage->expects( $this->never() )
|
|
|
|
->method( 'addMeta' );
|
|
|
|
|
|
|
|
$skinTemplate = new SkinTemplate();
|
2023-10-15 13:29:43 +00:00
|
|
|
$this->newPageImages()->onBeforePageDisplay( $outputPage, $skinTemplate );
|
2021-06-18 03:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function provideFallbacks() {
|
|
|
|
return [
|
|
|
|
[ 'http://wiki.test/example.png', '/example.png' ],
|
|
|
|
[ 'http://wiki.test/img/default.png', '/img/default.png' ],
|
|
|
|
[ 'https://example.org/example.png', 'https://example.org/example.png' ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideFallbacks
|
|
|
|
*/
|
|
|
|
public function testGivenFallbackImageOnBeforePageDisplayAddMeta( $expected, $fallback ) {
|
|
|
|
$this->setMwGlobals( [ 'wgCanonicalServer' => 'http://wiki.test' ] );
|
|
|
|
$outputPage = $this->mockOutputPage( [
|
2022-07-28 15:27:15 +00:00
|
|
|
'PageImagesOpenGraph' => true,
|
2021-06-18 03:16:36 +00:00
|
|
|
'PageImagesOpenGraphFallbackImage' => $fallback
|
|
|
|
] );
|
|
|
|
$outputPage->expects( $this->once() )
|
|
|
|
->method( 'addMeta' )
|
2024-03-11 19:13:21 +00:00
|
|
|
->with( 'og:image', $expected );
|
2021-06-18 03:16:36 +00:00
|
|
|
|
|
|
|
$skinTemplate = new SkinTemplate();
|
2023-10-15 13:29:43 +00:00
|
|
|
$this->newPageImages()->onBeforePageDisplay( $outputPage, $skinTemplate );
|
2021-06-18 03:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $config
|
|
|
|
* @return OutputPage
|
|
|
|
*/
|
|
|
|
private function mockOutputPage( $config ) {
|
2019-10-05 01:41:15 +00:00
|
|
|
$context = $this->createMock( IContextSource::class );
|
2017-01-31 12:59:16 +00:00
|
|
|
$context->method( 'getTitle' )
|
2021-03-20 05:22:04 +00:00
|
|
|
->willReturn( $this->newTitle() );
|
2023-08-19 23:54:45 +00:00
|
|
|
$fauxRequest = new FauxRequest();
|
2024-01-05 21:37:40 +00:00
|
|
|
$config = new HashConfig( $config );
|
2019-10-29 11:22:52 +00:00
|
|
|
$context->method( 'getRequest' )
|
|
|
|
->willReturn( $fauxRequest );
|
|
|
|
$context->method( 'getConfig' )
|
|
|
|
->willReturn( $config );
|
2017-01-31 12:59:16 +00:00
|
|
|
|
2019-10-05 01:41:15 +00:00
|
|
|
$outputPage = $this->getMockBuilder( OutputPage::class )
|
2021-05-12 09:23:38 +00:00
|
|
|
->onlyMethods( [ 'addMeta' ] )
|
2019-10-05 01:41:15 +00:00
|
|
|
->setConstructorArgs( [ $context ] )
|
|
|
|
->getMock();
|
2021-06-18 03:16:36 +00:00
|
|
|
return $outputPage;
|
2017-01-31 12:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Title
|
|
|
|
*/
|
|
|
|
private function newTitle() {
|
|
|
|
$title = Title::newFromText( 'New' );
|
|
|
|
$title->resetArticleID( 0 );
|
|
|
|
return $title;
|
|
|
|
}
|
|
|
|
|
2015-10-07 15:18:54 +00:00
|
|
|
}
|