2015-10-07 15:18:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PageImages\Tests;
|
|
|
|
|
2017-01-31 12:59:16 +00:00
|
|
|
use IContextSource;
|
2021-10-11 22:57:52 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2017-01-31 12:59:16 +00:00
|
|
|
use OutputPage;
|
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
|
|
|
use Title;
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
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() {
|
|
|
|
$pageImages = new PageImages();
|
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();
|
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();
|
|
|
|
( new PageImages() )->onBeforePageDisplay( $outputPage, $skinTemplate );
|
|
|
|
}
|
|
|
|
|
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();
|
2022-06-15 16:08:43 +00:00
|
|
|
( new PageImages() )->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' )
|
|
|
|
->with( $this->equalTo( 'og:image' ), $this->equalTo( $expected ) );
|
|
|
|
|
|
|
|
$skinTemplate = new SkinTemplate();
|
2022-06-15 16:08:43 +00:00
|
|
|
( new PageImages() )->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() );
|
2019-10-29 11:22:52 +00:00
|
|
|
$fauxRequest = new \FauxRequest();
|
2021-06-18 03:16:36 +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
|
|
|
}
|