1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
<?php
namespace MediaWiki\Tests\Rest\Handler;
use LocalFile;
use LocalRepo;
use MediaWiki\Page\PageReference;
use MediaWiki\User\UserIdentityValue;
use MockTitleTrait;
use PHPUnit\Framework\MockObject\MockObject;
use RepoGroup;
use ThumbnailImage;
/**
* A trait providing utility functions for mocking media-related objects.
*
*/
trait MediaTestTrait {
use HandlerTestTrait;
use MockTitleTrait;
/**
* @param PageReference|string $title
*
* @return LocalFile|MockObject
*/
private function makeMissingMockFile( $title ) {
$title = $title instanceof PageReference
? $this->makeMockTitle( $title->getDBkey(), [ 'namespace' => $title->getNamespace() ] )
: $this->makeMockTitle( 'File:' . $title, [ 'namespace' => NS_FILE ] );
/** @var MockObject|LocalFile $file */
$file = $this->createNoOpMock(
LocalFile::class,
[ 'getTitle', 'exists', 'getDescriptionUrl', 'load' ]
);
$file->method( 'getTitle' )->willReturn( $title );
$file->method( 'exists' )->willReturn( false );
$file->method( 'getDescriptionUrl' )->willReturn(
'https://example.com/wiki/' . $title->getPrefixedDBkey()
);
return $file;
}
/**
* @param PageReference|string $title
*
* @return LocalFile|MockObject
*/
private function makeMockFile( $title ) {
$title = $title instanceof PageReference
? $this->makeMockTitle( $title->getDBkey(), [ 'namespace' => $title->getNamespace() ] )
: $this->makeMockTitle( 'File:' . $title, [ 'namespace' => NS_FILE ] );
/** @var MockObject|LocalFile $file */
$file = $this->createNoOpMock(
LocalFile::class,
[ 'getName', 'getTitle', 'getDescriptionUrl', 'exists', 'userCan', 'getUploader', 'getTimestamp',
'getMediaType', 'getSize', 'getHeight', 'getWidth', 'getDisplayWidthHeight',
'getLength', 'getUrl', 'allowInlineDisplay', 'transform', 'getSha1', 'load', 'getMimeType' ]
);
$file->method( 'getName' )->willReturn( ucfirst( $title->getDBkey() ) );
$file->method( 'getTitle' )->willReturn( $title );
$file->method( 'exists' )->willReturn( true );
$file->method( 'userCan' )->willReturn( true );
$file->method( 'getUploader' )
->willReturn( UserIdentityValue::newRegistered( 7, 'Alice' ) );
$file->method( 'getTimestamp' )->willReturn( '20200102030405' );
$file->method( 'getMediaType' )->willReturn( 'test' );
$file->method( 'getSize' )->willReturn( 12345 );
$file->method( 'getHeight' )->willReturn( 400 );
$file->method( 'getWidth' )->willReturn( 600 );
$file->method( 'getLength' )->willReturn( 678 );
$file->method( 'getSha1' )->willReturn( 'DEADBEEF' );
$file->method( 'allowInlineDisplay' )->willReturn( true );
$file->method( 'getUrl' )->willReturn(
'https://media.example.com/static/' . $title->getDBkey()
);
$file->method( 'getDescriptionUrl' )->willReturn(
'https://example.com/wiki/' . $title->getPrefixedDBkey()
);
$file->method( 'getMimeType' )->willReturn( 'image/jpeg' );
$getDisplayWidthHeight = static function ( $maxWidth, $maxHeight, $page = 1 ) use ( $file ) {
$width = $file->getWidth( $page );
$height = $file->getHeight( $page );
$ratio = $width / $height;
if ( $width / $maxWidth > $height / $maxHeight ) {
return [ $maxWidth, round( $maxWidth / $ratio ) ];
}
return [ round( $maxHeight * $ratio ), $maxHeight ];
};
$file->method( 'getDisplayWidthHeight' )->willReturnCallback( $getDisplayWidthHeight );
$transform = static function ( $params ) use ( $file, $title ) {
return new ThumbnailImage(
$file,
'https://media.example.com/static/thumb/' . $title->getDBkey(),
false,
[
'width' => $params['width'] ?? 64,
'height' => $params['height'] ?? 64,
'page' => $params['page'] ?? false
]
);
};
$file->method( 'transform' )->willReturnCallback( $transform );
return $file;
}
/**
* @param array $existingFileDBKeys
* @return MockObject|RepoGroup
*/
private function makeMockRepoGroup( array $existingFileDBKeys ) {
$findFile = function ( $title ) use ( $existingFileDBKeys ) {
$title = $title instanceof PageReference
? $this->makeMockTitle( $title->getDBkey(), [ 'namespace' => $title->getNamespace() ] )
: $this->makeMockTitle( 'File:' . $title, [ 'namespace' => NS_FILE ] );
if ( !in_array( $title->getDBkey(), $existingFileDBKeys ) || !$title->inNamespace( NS_FILE ) ) {
return $this->makeMissingMockFile( $title );
} else {
return $this->makeMockFile( $title );
}
};
$findFiles = static function ( array $inputItems ) use ( $findFile ) {
$files = [];
foreach ( $inputItems as $item ) {
$files[] = ( $findFile )( $item['title'] );
}
return $files;
};
$mockLocalRepo = $this->createMock( LocalRepo::class );
$mockLocalRepo->method( 'newFile' )->willReturnCallback( $findFile );
/** @var RepoGroup|MockObject $repoGroup */
$repoGroup = $this->createNoOpMock( RepoGroup::class, [ 'findFiles', 'findFile', 'getLocalRepo' ] );
$repoGroup->method( 'findFile' )->willReturnCallback( $findFile );
$repoGroup->method( 'findFiles' )->willReturnCallback( $findFiles );
$repoGroup->method( 'getLocalRepo' )->willReturn( $mockLocalRepo );
return $repoGroup;
}
}
|