From 23dbe1add5fcfdf2b44d59e09c1992d24ac63dd1 Mon Sep 17 00:00:00 2001 From: "H. C. Kruse" Date: Sat, 19 Aug 2023 15:04:46 +0200 Subject: [PATCH] feat: Add more tests --- includes/Media/AudioHandler.php | 14 +- tests/phpunit/Media/AudioHandlerTest.php | 280 +++++++++++++++++++++++ tests/phpunit/Media/VideoHandlerTest.php | 60 +++++ 3 files changed, 350 insertions(+), 4 deletions(-) create mode 100644 tests/phpunit/Media/AudioHandlerTest.php create mode 100644 tests/phpunit/Media/VideoHandlerTest.php diff --git a/includes/Media/AudioHandler.php b/includes/Media/AudioHandler.php index cc16d92..0ea7134 100644 --- a/includes/Media/AudioHandler.php +++ b/includes/Media/AudioHandler.php @@ -79,17 +79,20 @@ public function validateParam( $name, $value ): bool { * @return false|float|int Integer seconds or false for a bad format. */ public function parseTimeString( $time ) { - $parts = explode( ":", $time ); - if ( $parts === false ) { + $parts = explode( ':', $time ); + + if ( $parts === false || empty( $parts[0] ?? '' ) || !is_numeric( $parts[0] ?? null ) ) { return false; } $parts = array_reverse( $parts ); $magnitude = [ 1, 60, 3600, 86400 ]; $seconds = 0; + foreach ( $parts as $index => $part ) { - $seconds += $part * $magnitude[$index]; + $seconds += (int)$part * $magnitude[$index]; } + return $seconds; } @@ -293,7 +296,10 @@ protected function getFFProbeResult( $file, string $select = 'v:0' ): array { } if ( $file === false ) { - return []; + return [ + 'stream' => false, + 'format' => false, + ]; } $probe = new FFProbe( $path, $file ); diff --git a/tests/phpunit/Media/AudioHandlerTest.php b/tests/phpunit/Media/AudioHandlerTest.php new file mode 100644 index 0000000..6d5adc1 --- /dev/null +++ b/tests/phpunit/Media/AudioHandlerTest.php @@ -0,0 +1,280 @@ +assertIsArray( $handler->getParamMap() ); + $this->assertNotEmpty( $handler->getParamMap() ); + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::validateParam + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::parseTimeString + * @return void + */ + public function testValidateParam(): void { + $handler = new AudioHandler(); + + $test = [ + [ 'width', 0, false ], + [ 'width', 100, true ], + [ 'width', -100, false ], + [ 'width', '-100', false ], + + [ 'start', '1:30', true ], + [ 'start', '', false ], + + [ 'end', '1:30', true ], + [ 'end', '', false ], + + [ 'autoplay', null, true ], + [ 'loop', null, true ], + [ 'nocontrols', null, true ], + + [ 'gif', null, false ], + ]; + + foreach ( $test as $test ) { + $this->assertEquals( $test[2], $handler->validateParam( $test[0], $test[1] ) ); + } + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::makeParamString + * @return void + */ + public function testMakeParamString(): void { + $handler = new AudioHandler(); + + $this->assertEmpty( $handler->makeParamString( [] ) ); + $this->assertEmpty( $handler->makeParamString( [ 'foo' ] ) ); + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::parseParamString + * @return void + */ + public function testParseParamString(): void { + $handler = new AudioHandler(); + + $this->assertEmpty( $handler->parseParamString( '' ) ); + $this->assertEmpty( $handler->parseParamString( 'foo' ) ); + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::normaliseParams + * @return void + */ + public function testNormaliseParamsWidth(): void { + $handler = new AudioHandler(); + + $params = [ + 'width' => 500, + ]; + + $handler->normaliseParams( null, $params ); + + $this->assertEquals( 500, $params['width'] ); + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::normaliseParams + * @return void + */ + public function testNormaliseParamsDefaultWidth(): void { + $this->overrideConfigValues( [ + 'EmbedVideoDefaultWidth' => 123, + ] ); + + $handler = new AudioHandler(); + + $params = []; + + $handler->normaliseParams( null, $params ); + + $this->assertEquals( 123, $params['width'] ); + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::normaliseParams + * @return void + */ + public function testNormaliseParamsStart(): void { + $handler = new AudioHandler(); + + $params = [ + 'start' => '1:30', + ]; + + $handler->normaliseParams( null, $params ); + + $this->assertArrayHasKey( 'start', $params ); + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::normaliseParams + * @return void + */ + public function testNormaliseParamsInvalidStart(): void { + $handler = new AudioHandler(); + + $params = [ + 'start' => 'foo', + ]; + + $handler->normaliseParams( null, $params ); + + $this->assertArrayNotHasKey( 'start', $params ); + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::normaliseParams + * @return void + */ + public function testNormaliseParamsEnd(): void { + $handler = new AudioHandler(); + + $params = [ + 'end' => '1:30', + ]; + + $handler->normaliseParams( null, $params ); + + $this->assertArrayHasKey( 'end', $params ); + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::normaliseParams + * @return void + */ + public function testNormaliseParamsInvalidEnd(): void { + $handler = new AudioHandler(); + + $params = [ + 'end' => 'fobar', + ]; + + $handler->normaliseParams( null, $params ); + + $this->assertArrayNotHasKey( 'end', $params ); + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::doTransform + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::normaliseParams + * @return void + */ + public function testNormaliseParamsDoTransform(): void { + $handler = new AudioHandler(); + + $file = UnregisteredLocalFile::newFromPath( '/tmp', 'video/mp4' ); + + $transform = $handler->doTransform( $file, '', '', [] ); + + $this->assertInstanceOf( AudioTransformOutput::class, $transform ); + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getDimensionsString + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getFFProbeResult + * @return void + */ + public function testGetDimensionString(): void { + $handler = $this->getMockBuilder( AudioHandler::class ) + ->onlyMethods( [ 'getFFProbeResult' ] ) + ->getMock(); + + $handler->expects( $this->once() ) + ->method( 'getFFProbeResult' ) + ->willReturn( [ + 'stream' => new StreamInfo( [] ), + 'format' => new FormatInfo( [ + 'duration' => 10, + ] ), + ] ); + + $file = UnregisteredLocalFile::newFromPath( '/tmp', 'video/mp4' ); + + $this->assertIsString( $handler->getDimensionsString( $file ) ); + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getDimensionsString + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getFFProbeResult + * @return void + */ + public function testGetDimensionStringEmpty(): void { + $handler = $this->getMockBuilder( AudioHandler::class ) + ->onlyMethods( [ 'getFFProbeResult' ] ) + ->getMock(); + + $handler->expects( $this->once() ) + ->method( 'getFFProbeResult' ) + ->willReturn( [ + 'stream' => false, + 'format' => false, + ] ); + + $file = UnregisteredLocalFile::newFromPath( '/tmp', 'video/mp4' ); + + $this->assertEmpty( $handler->getDimensionsString( $file ) ); + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getDimensionsString + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getFFProbeResult + * @return void + */ + public function testGetSizeAndMetadataEmpty(): void { + $handler = $this->getMockBuilder( AudioHandler::class ) + ->onlyMethods( [ 'getFFProbeResult' ] ) + ->getMock(); + + $handler->expects( $this->once() ) + ->method( 'getFFProbeResult' ) + ->willReturn( [ + 'stream' => false, + 'format' => false, + ] ); + + $this->assertEquals( [ 'metadata' => [] ], $handler->getSizeAndMetadata( null, null ) ); + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getDimensionsString + * @covers \MediaWiki\Extension\EmbedVideo\Media\AudioHandler::getFFProbeResult + * @return void + */ + public function testGetSizeAndMetadata(): void { + $handler = $this->getMockBuilder( AudioHandler::class ) + ->onlyMethods( [ 'getFFProbeResult' ] ) + ->getMock(); + + $handler->expects( $this->once() ) + ->method( 'getFFProbeResult' ) + ->willReturn( [ + 'stream' => false, + 'format' => new FormatInfo( [ + 'bit_rate' => 100, + ] ), + ] ); + + $this->assertEquals( [ 'metadata' => [], 'bits' => 100 ], $handler->getSizeAndMetadata( null, null ) ); + } +} diff --git a/tests/phpunit/Media/VideoHandlerTest.php b/tests/phpunit/Media/VideoHandlerTest.php new file mode 100644 index 0000000..8c1adfd --- /dev/null +++ b/tests/phpunit/Media/VideoHandlerTest.php @@ -0,0 +1,60 @@ +assertIsArray( $handler->getParamMap() ); + $this->assertNotEmpty( $handler->getParamMap() ); + } + + /** + * @covers \MediaWiki\Extension\EmbedVideo\Media\VideoHandler::validateParam + * @covers \MediaWiki\Extension\EmbedVideo\Media\VideoHandler::parseTimeString + * @return void + */ + public function testValidateParam(): void { + $handler = new VideoHandler(); + + $test = [ + [ 'width', 0, false ], + [ 'width', 100, true ], + [ 'width', -100, false ], + [ 'width', '-100', false ], + + [ 'start', '1:30', true ], + [ 'start', '', false ], + + [ 'end', '1:30', true ], + [ 'end', '', false ], + + [ 'autoplay', null, true ], + [ 'loop', null, true ], + [ 'nocontrols', null, true ], + [ 'poster', null, true ], + [ 'gif', null, true ], + [ 'muted', null, true ], + [ 'title', null, true ], + [ 'description', null, true ], + [ 'lazy', null, true ], + [ 'autoresize', null, true ], + + [ 'explode', null, false ], + ]; + + foreach ( $test as $test ) { + $this->assertEquals( $test[2], $handler->validateParam( $test[0], $test[1] ) ); + } + } +}