Skip to content

Commit

Permalink
feat: Further add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
octfx committed Aug 21, 2023
1 parent 23dbe1a commit c9dbaa3
Show file tree
Hide file tree
Showing 14 changed files with 1,105 additions and 39 deletions.
20 changes: 8 additions & 12 deletions includes/EmbedVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ public static function parseEVTag( $input, array $args, Parser $parser, PPFrame
* @param array $arguments Method arguments
*/
public static function __callStatic( $name, $arguments ) {
if ( strpos( $name, 'parseTag' ) !== 0 ) {
return;
if ( !str_starts_with( $name, 'parseTag' ) ) {
return '';
}

[
Expand All @@ -172,9 +172,7 @@ public static function __callStatic( $name, $arguments ) {
* @return array
*/
public function output(): array {
[
'service' => $service,
] = $this->args;
$service = $this->args['service'] ?? null;

try {
$enabledServices = $this->config->get( 'EmbedVideoEnabledServices' ) ?? [];
Expand Down Expand Up @@ -233,16 +231,14 @@ private function parseArgs( array $args, bool $fromTag ): array {
];

if ( $fromTag === true ) {
$supportedArgs['service'] = $args['service'] ?? null;

return array_merge( $supportedArgs, $args );
}

$keys = array_keys( $supportedArgs );

if ( $fromTag ) {
$serviceName = $args['service'] ?? null;
} else {
$serviceName = array_shift( $args );
}
$serviceName = array_shift( $args );

$counter = 0;

Expand All @@ -254,7 +250,7 @@ private function parseArgs( array $args, bool $fromTag ): array {
$pair = [ $arg ];
// Only split arg if it is not an url and not urlArgs
// phpcs:ignore Generic.Files.LineLength.TooLong
if ( ( $keys[$counter] !== 'urlArgs' || strpos( $arg, 'urlArgs' ) !== false ) && preg_match( '/https?:/', $arg ) !== 1 ) {
if ( ( $keys[$counter] !== 'urlArgs' || str_contains( $arg, 'urlArgs' ) ) && preg_match( '/https?:/', $arg ) !== 1 ) {
$pair = explode( '=', $arg, 2 );
}
$pair = array_map( 'trim', $pair );
Expand All @@ -275,7 +271,7 @@ private function parseArgs( array $args, bool $fromTag ): array {
++$counter;
}

$supportedArgs['service'] = $serviceName;
$supportedArgs['service'] = $serviceName ?? false;

// An intentional weak check
if ( $supportedArgs['autoresize'] == true ) {
Expand Down
4 changes: 4 additions & 0 deletions includes/EmbedVideoHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ public function onArticlePurge( $wikiPage ): void {

$file = $this->repoGroup->findFile( $wikiPage->getTitle() );

if ( $file === false ) {
return;
}

// The last part is only ever a:0 or v:0
$audioKey = $this->cache->makeGlobalKey( 'EmbedVideo', 'ffprobe', $file->getSha1(), 'a:0' );
$videoKey = $this->cache->makeGlobalKey( 'EmbedVideo', 'ffprobe', $file->getSha1(), 'v:0' );
Expand Down
5 changes: 3 additions & 2 deletions includes/Media/FFProbe/FFProbe.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,12 @@ private function invokeFFProbe(): ?array {
return null;
}

if ( Shell::isDisabled() || $ffprobeLocation === false || !file_exists( $ffprobeLocation ) ) {
if ( Shell::isDisabled() || empty( $ffprobeLocation ) || !file_exists( $ffprobeLocation ) ) {
return null;
}

$command = Shell::command( $ffprobeLocation );
$command = MediaWikiServices::getInstance()->getShellCommandFactory()->create();
$command->params( $ffprobeLocation );

$command->unsafeParams( [
'-v quiet',
Expand Down
2 changes: 1 addition & 1 deletion includes/Media/TransformOutput/VideoTransformOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function toHtml( $options = [] ): string {
'muted' => isset( $this->parameters['muted'] ),
];

if ( $this->parameters['lazy'] === true && !isset( $this->parameters['gif'] ) ) {
if ( ( $this->parameters['lazy'] ?? false ) === true && !isset( $this->parameters['gif'] ) ) {
$attrs['preload'] = 'none';
}

Expand Down
80 changes: 79 additions & 1 deletion tests/phpunit/EmbedVideoHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,33 @@
namespace MediaWiki\Extension\EmbedVideo\Tests;

use Exception;
use HashBagOStuff;
use LocalFile;
use MediaWiki\Extension\EmbedVideo\EmbedService\EmbedServiceFactory;
use MediaWiki\Extension\EmbedVideo\EmbedVideoHooks;
use MediaWiki\Extension\EmbedVideo\Media\AudioHandler;
use MediaWiki\Title\Title;
use MediaWikiIntegrationTestCase;
use OutputPage;
use RepoGroup;
use RequestContext;
use WANObjectCache;

class EmbedVideoHooksTest extends \MediaWikiIntegrationTestCase {
class EmbedVideoHooksTest extends MediaWikiIntegrationTestCase {
/**
* @covers \MediaWiki\Extension\EmbedVideo\EmbedVideoHooks
* @return void
* @throws Exception
*/
public function testConstructor() {
$hooks = new EmbedVideoHooks(
$this->getServiceContainer()->getConfigFactory(),
$this->getServiceContainer()->getRepoGroup(),
$this->getServiceContainer()->getMainWANObjectCache()
);

$this->assertInstanceOf( EmbedVideoHooks::class, $hooks );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\EmbedVideoHooks::onBeforePageDisplay
Expand Down Expand Up @@ -157,4 +177,62 @@ public function testAddFunctionHooksPartial() {
}
}
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\EmbedVideoHooks::onArticlePurge
* @return void
* @throws Exception
*/
public function testOnArticlePurgeHooksNotAFile() {
$repo = $this->getMockBuilder( RepoGroup::class )->disableOriginalConstructor()->getMock();

$repo->expects( $this->never() )->method( 'findFile' );

$hooks = new EmbedVideoHooks(
$this->getServiceContainer()->getConfigFactory(),
$repo,
$this->getServiceContainer()->getMainWANObjectCache()
);

$title = Title::newFromText( 'OnArticlePurge' );
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );

$hooks->onArticlePurge( $page );
}

/**
* @covers \MediaWiki\Extension\EmbedVideo\EmbedVideoHooks::onArticlePurge
* @return void
* @throws Exception
*/
public function testOnArticlePurgeHooksFile() {
$file = $this->getMockBuilder( LocalFile::class )->disableOriginalConstructor()->getMock();
$file->expects( $this->exactly( 2 ) )
->method( 'getSha1' )
->willReturn( sha1( 'foo' ) );

$repo = $this->getMockBuilder( RepoGroup::class )->disableOriginalConstructor()->getMock();
$repo->expects( $this->once() )
->method( 'findFile' )
->willReturn( $file );

$cache = $this->getMockBuilder( WANObjectCache::class )
->setConstructorArgs( [
[ 'cache' => new HashBagOStuff() ],
] )->getMock();
$cache->expects( $this->exactly( 2 ) )
->method( 'makeGlobalKey' )
->willReturn( 'foo' );

$hooks = new EmbedVideoHooks(
$this->getServiceContainer()->getConfigFactory(),
$repo,
$cache
);

$title = Title::newFromText( 'OnArticlePurge.jpg', NS_FILE );
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );

$hooks->onArticlePurge( $page );
}
}
Loading

0 comments on commit c9dbaa3

Please sign in to comment.