| 1 |
<?php |
| 2 |
|
| 3 |
namespace PageImages\Tests; |
| 4 |
|
| 5 |
use MediaWiki\Config\HashConfig; |
| 6 |
use MediaWiki\Context\IContextSource; |
| 7 |
use MediaWiki\Output\OutputPage; |
| 8 |
use MediaWiki\Request\FauxRequest; |
| 9 |
use MediaWiki\Title\Title; |
| 10 |
use MediaWikiIntegrationTestCase; |
| 11 |
use PageImages\PageImages; |
| 12 |
use SkinTemplate; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class PageImagesTest extends MediaWikiIntegrationTestCase { |
| 24 |
|
| 25 |
private function newPageImages() { |
| 26 |
$services = $this->getServiceContainer(); |
| 27 |
return new PageImages( |
| 28 |
$services->getMainConfig(), |
| 29 |
$services->getDBLoadBalancerFactory(), |
| 30 |
$services->getRepoGroup(), |
| 31 |
$services->getUserOptionsLookup() |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
public function testPagePropertyNames() { |
| 36 |
$this->assertSame( 'page_image', PageImages::PROP_NAME ); |
| 37 |
$this->assertSame( 'page_image_free', PageImages::PROP_NAME_FREE ); |
| 38 |
} |
| 39 |
|
| 40 |
public function testConstructor() { |
| 41 |
$pageImages = $this->newPageImages(); |
| 42 |
$this->assertInstanceOf( PageImages::class, $pageImages ); |
| 43 |
} |
| 44 |
|
| 45 |
public function testGivenNonExistingPageGetPageImageReturnsFalse() { |
| 46 |
$title = $this->newTitle(); |
| 47 |
$this->assertNull( $this->newPageImages()->getPageImageInternal( $title ) ); |
| 48 |
$this->assertFalse( PageImages::getPageImage( $title ) ); |
| 49 |
} |
| 50 |
|
| 51 |
public function testGetPropName() { |
| 52 |
$this->assertSame( 'page_image', PageImages::getPropName( false ) ); |
| 53 |
$this->assertSame( 'page_image_free', PageImages::getPropName( true ) ); |
| 54 |
} |
| 55 |
|
| 56 |
public function testGetPropNames() { |
| 57 |
$this->assertSame( |
| 58 |
[ PageImages::PROP_NAME_FREE, PageImages::PROP_NAME ], |
| 59 |
PageImages::getPropNames( PageImages::LICENSE_ANY ) |
| 60 |
); |
| 61 |
$this->assertSame( |
| 62 |
PageImages::PROP_NAME_FREE, |
| 63 |
PageImages::getPropNames( PageImages::LICENSE_FREE ) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
public function testGivenDeactivedOpenGraphOnBeforePageDisplayDoesNotAddMeta() { |
| 68 |
$outputPage = $this->mockOutputPage( [ |
| 69 |
'PageImagesOpenGraph' => false, |
| 70 |
] ); |
| 71 |
$outputPage->expects( $this->never() ) |
| 72 |
->method( 'addMeta' ); |
| 73 |
|
| 74 |
$skinTemplate = new SkinTemplate(); |
| 75 |
$this->newPageImages()->onBeforePageDisplay( $outputPage, $skinTemplate ); |
| 76 |
} |
| 77 |
|
| 78 |
public function testGivenNonExistingPageOnBeforePageDisplayDoesNotAddMeta() { |
| 79 |
$outputPage = $this->mockOutputPage( [ |
| 80 |
'PageImagesOpenGraph' => true, |
| 81 |
'PageImagesOpenGraphFallbackImage' => false |
| 82 |
] ); |
| 83 |
$outputPage->expects( $this->never() ) |
| 84 |
->method( 'addMeta' ); |
| 85 |
|
| 86 |
$skinTemplate = new SkinTemplate(); |
| 87 |
$this->newPageImages()->onBeforePageDisplay( $outputPage, $skinTemplate ); |
| 88 |
} |
| 89 |
|
| 90 |
public static function provideFallbacks() { |
| 91 |
return [ |
| 92 |
[ 'http://wiki.test/example.png', '/example.png' ], |
| 93 |
[ 'http://wiki.test/img/default.png', '/img/default.png' ], |
| 94 |
[ 'https://example.org/example.png', 'https://example.org/example.png' ], |
| 95 |
]; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function testGivenFallbackImageOnBeforePageDisplayAddMeta( $expected, $fallback ) { |
| 102 |
$this->overrideConfigValue( 'CanonicalServer', 'http://wiki.test' ); |
| 103 |
$outputPage = $this->mockOutputPage( [ |
| 104 |
'PageImagesOpenGraph' => true, |
| 105 |
'PageImagesOpenGraphFallbackImage' => $fallback |
| 106 |
] ); |
| 107 |
$outputPage->expects( $this->once() ) |
| 108 |
->method( 'addMeta' ) |
| 109 |
->with( 'og:image', $expected ); |
| 110 |
|
| 111 |
$skinTemplate = new SkinTemplate(); |
| 112 |
$this->newPageImages()->onBeforePageDisplay( $outputPage, $skinTemplate ); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
private function mockOutputPage( $config ) { |
| 120 |
$context = $this->createMock( IContextSource::class ); |
| 121 |
$context->method( 'getTitle' ) |
| 122 |
->willReturn( $this->newTitle() ); |
| 123 |
$fauxRequest = new FauxRequest(); |
| 124 |
$config = new HashConfig( $config ); |
| 125 |
$context->method( 'getRequest' ) |
| 126 |
->willReturn( $fauxRequest ); |
| 127 |
$context->method( 'getConfig' ) |
| 128 |
->willReturn( $config ); |
| 129 |
|
| 130 |
$outputPage = $this->getMockBuilder( OutputPage::class ) |
| 131 |
->onlyMethods( [ 'addMeta' ] ) |
| 132 |
->setConstructorArgs( [ $context ] ) |
| 133 |
->getMock(); |
| 134 |
return $outputPage; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
private function newTitle() { |
| 141 |
$title = Title::newFromText( 'New' ); |
| 142 |
$title->resetArticleID( 0 ); |
| 143 |
return $title; |
| 144 |
} |
| 145 |
|
| 146 |
} |