wiki.techinc.nl/tests/phpunit/integration/includes/Rest/Handler/MediaFileHandlerTest.php

142 lines
3.9 KiB
PHP
Raw Normal View History

<?php
namespace MediaWiki\Tests\Rest\Handler;
use MediaWiki\Rest\Handler\MediaFileHandler;
use MediaWiki\Rest\LocalizedHttpException;
use MediaWiki\Rest\RequestData;
Introduce SearchResultThumbnailProvider & move hook + NS_FILE thumbs in What was previously a REST API-only feature (the thumbnails hook allowing for thumbnails for non-file pages via the PageImages extension) is now also being adopted in the main search page. That hook will now be called with NS_FILE result thumbnails pre-filled, which was not the case previously. PageImages essentially duplicated NS_FILE thumbnail logic that was already present in Special:Search, so that can (and will in a follow-up patch) then be removed there. Special:Search will then simply take whatever is produced from the provider (which will include both NS_FILE thumbs - which it handled already - as well as whatever else it receives from the hook), as will the REST API (which already received both) Since thumbnails can now come in for multiple namespaces & having some of those results with & others without a thumbnail can be quite jarring, it was decided that we'd display placeholder images (for certain namespaces). This is now controlled by $wgThumbnailNamespaces. I also split up a few things in FullSearchResultWidget:: generateFileHtml for more clarity. Meanwhile also updated mediawiki.special.search.styles.less to use variables for known colors. Also implemented a 'transform' (required for testing this change properly) and 'getDisplayWidthHeight' (it became needed after implementing transform) callback function for mock Files, and updated some existing tests in response to these changes. And some more Rest test files have been updated to allow passing around a HookContainer instead of only an array of hooks (from which a new HookContainer would then be created) to allow the same container to be used across all relevant objects, who may have it injected as dependency. Bug: T306883 Change-Id: I2a679b51758020d3e822da01a1bde1ae632b0b0a
2022-08-31 14:51:57 +00:00
use RequestContext;
use Title;
use Wikimedia\Message\MessageValue;
/**
* @covers \MediaWiki\Rest\Handler\MediaFileHandler
*
* @group Database
*/
class MediaFileHandlerTest extends \MediaWikiLangTestCase {
use MediaTestTrait;
public function addDBDataOnce() {
$this->editPage( 'File:' . __CLASS__ . '.jpg', 'Test image description' );
}
private function newHandler() {
return new MediaFileHandler(
$this->makeMockRepoGroup( [ __CLASS__ . '.jpg' ] ),
$this->getServiceContainer()->getPageStore()
);
}
private function assertFile( $expected, $actual ) {
foreach ( $expected as $key => $value ) {
$this->assertArrayHasKey( $key, $actual );
$this->assertSame( $value, $actual[$key], $key );
}
}
public function testExecute() {
// NOTE: "File:" namespace prefix is optional for title parameter.
$title = __CLASS__ . '.jpg';
$request = new RequestData( [ 'pathParams' => [ 'title' => $title ] ] );
Introduce SearchResultThumbnailProvider & move hook + NS_FILE thumbs in What was previously a REST API-only feature (the thumbnails hook allowing for thumbnails for non-file pages via the PageImages extension) is now also being adopted in the main search page. That hook will now be called with NS_FILE result thumbnails pre-filled, which was not the case previously. PageImages essentially duplicated NS_FILE thumbnail logic that was already present in Special:Search, so that can (and will in a follow-up patch) then be removed there. Special:Search will then simply take whatever is produced from the provider (which will include both NS_FILE thumbs - which it handled already - as well as whatever else it receives from the hook), as will the REST API (which already received both) Since thumbnails can now come in for multiple namespaces & having some of those results with & others without a thumbnail can be quite jarring, it was decided that we'd display placeholder images (for certain namespaces). This is now controlled by $wgThumbnailNamespaces. I also split up a few things in FullSearchResultWidget:: generateFileHtml for more clarity. Meanwhile also updated mediawiki.special.search.styles.less to use variables for known colors. Also implemented a 'transform' (required for testing this change properly) and 'getDisplayWidthHeight' (it became needed after implementing transform) callback function for mock Files, and updated some existing tests in response to these changes. And some more Rest test files have been updated to allow passing around a HookContainer instead of only an array of hooks (from which a new HookContainer would then be created) to allow the same container to be used across all relevant objects, who may have it injected as dependency. Bug: T306883 Change-Id: I2a679b51758020d3e822da01a1bde1ae632b0b0a
2022-08-31 14:51:57 +00:00
$user = RequestContext::getMain()->getUser();
$userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
$this->setMwGlobals( 'wgImageLimits', [
$userOptionsManager->getIntOption( $user, 'imagesize' ) => [ 100, 100 ],
$userOptionsManager->getIntOption( $user, 'thumbsize' ) => [ 20, 20 ],
] );
$handler = $this->newHandler();
$data = $this->executeHandlerAndGetBodyData( $handler, $request );
$this->assertFile(
[
'title' => $title,
// File repo mocks will end up calling File namespace ns6
'file_description_url' => 'https://example.com/wiki/ns6:' . $title,
'latest' => [
'timestamp' => '2020-01-02T03:04:05Z',
'user' => [
'id' => 7,
'name' => 'Alice',
],
],
'preferred' => [
'mediatype' => 'test',
'size' => null,
Introduce SearchResultThumbnailProvider & move hook + NS_FILE thumbs in What was previously a REST API-only feature (the thumbnails hook allowing for thumbnails for non-file pages via the PageImages extension) is now also being adopted in the main search page. That hook will now be called with NS_FILE result thumbnails pre-filled, which was not the case previously. PageImages essentially duplicated NS_FILE thumbnail logic that was already present in Special:Search, so that can (and will in a follow-up patch) then be removed there. Special:Search will then simply take whatever is produced from the provider (which will include both NS_FILE thumbs - which it handled already - as well as whatever else it receives from the hook), as will the REST API (which already received both) Since thumbnails can now come in for multiple namespaces & having some of those results with & others without a thumbnail can be quite jarring, it was decided that we'd display placeholder images (for certain namespaces). This is now controlled by $wgThumbnailNamespaces. I also split up a few things in FullSearchResultWidget:: generateFileHtml for more clarity. Meanwhile also updated mediawiki.special.search.styles.less to use variables for known colors. Also implemented a 'transform' (required for testing this change properly) and 'getDisplayWidthHeight' (it became needed after implementing transform) callback function for mock Files, and updated some existing tests in response to these changes. And some more Rest test files have been updated to allow passing around a HookContainer instead of only an array of hooks (from which a new HookContainer would then be created) to allow the same container to be used across all relevant objects, who may have it injected as dependency. Bug: T306883 Change-Id: I2a679b51758020d3e822da01a1bde1ae632b0b0a
2022-08-31 14:51:57 +00:00
'width' => 100,
'height' => 67,
'duration' => 678,
'url' => 'https://media.example.com/static/thumb/' . $title,
],
'original' => [
'mediatype' => 'test',
'size' => 12345,
'width' => 600,
'height' => 400,
'duration' => 678,
'url' => 'https://media.example.com/static/' . $title,
],
'thumbnail' => [
'mediatype' => 'test',
'size' => null,
Introduce SearchResultThumbnailProvider & move hook + NS_FILE thumbs in What was previously a REST API-only feature (the thumbnails hook allowing for thumbnails for non-file pages via the PageImages extension) is now also being adopted in the main search page. That hook will now be called with NS_FILE result thumbnails pre-filled, which was not the case previously. PageImages essentially duplicated NS_FILE thumbnail logic that was already present in Special:Search, so that can (and will in a follow-up patch) then be removed there. Special:Search will then simply take whatever is produced from the provider (which will include both NS_FILE thumbs - which it handled already - as well as whatever else it receives from the hook), as will the REST API (which already received both) Since thumbnails can now come in for multiple namespaces & having some of those results with & others without a thumbnail can be quite jarring, it was decided that we'd display placeholder images (for certain namespaces). This is now controlled by $wgThumbnailNamespaces. I also split up a few things in FullSearchResultWidget:: generateFileHtml for more clarity. Meanwhile also updated mediawiki.special.search.styles.less to use variables for known colors. Also implemented a 'transform' (required for testing this change properly) and 'getDisplayWidthHeight' (it became needed after implementing transform) callback function for mock Files, and updated some existing tests in response to these changes. And some more Rest test files have been updated to allow passing around a HookContainer instead of only an array of hooks (from which a new HookContainer would then be created) to allow the same container to be used across all relevant objects, who may have it injected as dependency. Bug: T306883 Change-Id: I2a679b51758020d3e822da01a1bde1ae632b0b0a
2022-08-31 14:51:57 +00:00
'width' => 20,
'height' => 13,
'duration' => 678,
'url' => 'https://media.example.com/static/thumb/' . $title,
],
],
$data
);
}
public function testCacheControl() {
$title = 'File:' . __CLASS__ . '.jpg';
$request = new RequestData( [ 'pathParams' => [ 'title' => $title ] ] );
$handler = $this->newHandler();
$response = $this->executeHandler( $handler, $request );
// Mock image timestamp from MediaTestTrait::makeMockFile
$this->assertSame(
'Thu, 02 Jan 2020 03:04:05 GMT',
$response->getHeaderLine( 'Last-Modified' )
);
// Mock image hash from MediaTestTrait::makeMockFile
$this->assertSame(
'"DEADBEEF"',
$response->getHeaderLine( 'ETag' )
);
}
public function testExecute_notFound() {
// NOTE: MediaTestTrait::makeMockRepoGroup() will treat files with "missing" in
// the name as non-existent.
$title = __CLASS__ . '_Missing.png';
$request = new RequestData( [ 'pathParams' => [ 'title' => $title ] ] );
$handler = $this->newHandler();
$this->expectExceptionObject(
new LocalizedHttpException( new MessageValue( 'rest-nonexistent-title' ), 404 )
);
$this->executeHandler( $handler, $request );
}
public function testExecute_wrongNamespace() {
$title = Title::newFromText( 'User:' . __CLASS__ . '.jpg' );
$this->editPage( $title->getPrefixedDBkey(), 'First' );
$request = new RequestData( [ 'pathParams' => [ 'title' => $title->getPrefixedDBkey() ] ] );
$handler = $this->newHandler();
$this->expectExceptionObject(
new LocalizedHttpException( new MessageValue( 'rest-cannot-load-file' ), 404 )
);
$this->executeHandler( $handler, $request );
}
}