wiki.techinc.nl/tests/phpunit/includes/media/FormatMetadataTest.php

182 lines
4.5 KiB
PHP
Raw Normal View History

<?php
use Wikimedia\TestingAccessWrapper;
/**
* @group Media
*/
class FormatMetadataTest extends MediaWikiMediaTestCase {
protected function setUp() : void {
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
parent::setUp();
$this->checkPHPExtension( 'exif' );
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
$this->setMwGlobals( 'wgShowEXIF', true );
}
/**
* @covers File::formatMetadata
*/
public function testInvalidDate() {
$file = $this->dataFile( 'broken_exif_date.jpg', 'image/jpeg' );
// Throws an error if bug hit
$meta = $file->formatMetadata();
$this->assertNotEquals( false, $meta, 'Valid metadata extracted' );
// Find date exif entry
$this->assertArrayHasKey( 'visible', $meta );
$dateIndex = null;
foreach ( $meta['visible'] as $i => $data ) {
if ( $data['id'] == 'exif-datetimeoriginal' ) {
$dateIndex = $i;
}
}
$this->assertNotNull( $dateIndex, 'Date entry exists in metadata' );
$this->assertSame( '0000:01:00 00:02:27',
$meta['visible'][$dateIndex]['value'],
'File with invalid date metadata (T31471)' );
}
/**
* @param mixed $input
* @param mixed $output
* @dataProvider provideResolveMultivalueValue
* @covers FormatMetadata::resolveMultivalueValue
*/
public function testResolveMultivalueValue( $input, $output ) {
$formatMetadata = new FormatMetadata();
$class = new ReflectionClass( FormatMetadata::class );
$method = $class->getMethod( 'resolveMultivalueValue' );
$method->setAccessible( true );
$actualInput = $method->invoke( $formatMetadata, $input );
$this->assertEquals( $output, $actualInput );
}
public function provideResolveMultivalueValue() {
return [
'nonArray' => [
'foo',
'foo'
],
'multiValue' => [
[ 'first', 'second', 'third', '_type' => 'ol' ],
'first'
],
'noType' => [
[ 'first', 'second', 'third' ],
'first'
],
'typeFirst' => [
[ '_type' => 'ol', 'first', 'second', 'third' ],
'first'
],
'multilang' => [
[
'en' => 'first',
'de' => 'Erste',
'_type' => 'lang'
],
[
'en' => 'first',
'de' => 'Erste',
'_type' => 'lang'
],
],
'multilang-multivalue' => [
[
'en' => [ 'first', 'second' ],
'de' => [ 'Erste', 'Zweite' ],
'_type' => 'lang'
],
[
'en' => 'first',
'de' => 'Erste',
'_type' => 'lang'
],
],
];
}
/**
* @param mixed $input
* @param mixed $output
* @dataProvider provideGetFormattedData
* @covers FormatMetadata::getFormattedData
*/
public function testGetFormattedData( $input, $output ) {
$this->assertEquals( $output, FormatMetadata::getFormattedData( $input ) );
}
public function provideGetFormattedData() {
return [
[
[ 'Software' => 'Adobe Photoshop CS6 (Macintosh)' ],
[ 'Software' => 'Adobe Photoshop CS6 (Macintosh)' ],
],
[
[ 'Software' => [ 'FotoWare FotoStation' ] ],
[ 'Software' => 'FotoWare FotoStation' ],
],
[
[ 'Software' => [ [ 'Capture One PRO', '3.7.7' ] ] ],
[ 'Software' => 'Capture One PRO (Version 3.7.7)' ],
],
[
[ 'Software' => [ [ 'FotoWare ColorFactory', '' ] ] ],
[ 'Software' => 'FotoWare ColorFactory (Version )' ],
],
[
[ 'Software' => [ 'x-default' => 'paint.net 4.0.12', '_type' => 'lang' ] ],
[ 'Software' => '<ul class="metadata-langlist">' .
'<li class="mw-metadata-lang-default">' .
'<span class="mw-metadata-lang-value">paint.net 4.0.12</span>' .
"</li>\n" .
'</ul>'
],
],
[
// https://phabricator.wikimedia.org/T178130
// WebMHandler.php turns both 'muxingapp' & 'writingapp' to 'Software'
[ 'Software' => [ [ 'Lavf57.25.100' ], [ 'Lavf57.25.100' ] ] ],
[ 'Software' => "<ul><li>Lavf57.25.100</li>\n<li>Lavf57.25.100</li></ul>" ],
],
];
}
/**
* @covers FormatMetadata::getPriorityLanguages
* @dataProvider provideGetPriorityLanguagesData
* @param string $languageClass
* @param string[] $expected
*/
public function testGetPriorityLanguagesInternal_language_expect(
string $languageClass,
array $expected
): void {
$formatMetadata = TestingAccessWrapper::newFromObject( new FormatMetadata() );
$context = $formatMetadata->getContext();
$context->setLanguage( new $languageClass() );
$x = $formatMetadata->getPriorityLanguages();
$this->assertSame( $expected, $x );
}
public function provideGetPriorityLanguagesData() {
return [
'LanguageMl' => [
LanguageMl::class,
[ 'ml', 'en' ],
],
'LanguageEn' => [
LanguageEn::class,
[ 'en', 'en' ],
],
'LanguageQqx' => [
LanguageQqx::class,
[ 'qqx', 'en' ],
],
];
}
2011-07-20 15:35:48 +00:00
}