Debian Codegraph @

tests/phpunit/includes/media/FormatMetadataTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MainConfigNames;
4 use Wikimedia\TestingAccessWrapper;
5
6 /**
7 * @group Media
8 * @requires extension exif
9 */
10 class FormatMetadataTest extends MediaWikiMediaTestCase {
11
12 protected function setUp(): void {
13 parent::setUp();
14
15 $this->overrideConfigValues( [
16 MainConfigNames::LanguageCode => 'en',
17 MainConfigNames::ShowEXIF => true,
18 ] );
19 }
20
21 /**
22 * @covers \File::formatMetadata
23 */
24 public function testInvalidDate() {
25 $file = $this->dataFile( 'broken_exif_date.jpg', 'image/jpeg' );
26
27 // Throws an error if bug hit
28 $meta = $file->formatMetadata();
29 $this->assertIsArray( $meta, 'Valid metadata extracted' );
30
31 // Find date exif entry
32 $this->assertArrayHasKey( 'visible', $meta );
33 $dateIndex = null;
34 foreach ( $meta['visible'] as $i => $data ) {
35 if ( $data['id'] == 'exif-datetimeoriginal' ) {
36 $dateIndex = $i;
37 }
38 }
39 $this->assertNotNull( $dateIndex, 'Date entry exists in metadata' );
40 $this->assertSame( '0000:01:00 00:02:27',
41 $meta['visible'][$dateIndex]['value'],
42 'File with invalid date metadata (T31471)' );
43 }
44
45 /**
46 * @dataProvider provideResolveMultivalueValue
47 * @covers \FormatMetadata::resolveMultivalueValue
48 */
49 public function testResolveMultivalueValue( $input, $output ) {
50 /** @var FormatMetadata $formatMetadata */
51 $formatMetadata = TestingAccessWrapper::newFromObject( new FormatMetadata() );
52 $actualInput = $formatMetadata->resolveMultivalueValue( $input );
53 $this->assertEquals( $output, $actualInput );
54 }
55
56 public static function provideResolveMultivalueValue() {
57 return [
58 'nonArray' => [
59 'foo',
60 'foo'
61 ],
62 'multiValue' => [
63 [ 'first', 'second', 'third', '_type' => 'ol' ],
64 'first'
65 ],
66 'noType' => [
67 [ 'first', 'second', 'third' ],
68 'first'
69 ],
70 'typeFirst' => [
71 [ '_type' => 'ol', 'first', 'second', 'third' ],
72 'first'
73 ],
74 'multilang' => [
75 [
76 'en' => 'first',
77 'de' => 'Erste',
78 '_type' => 'lang'
79 ],
80 [
81 'en' => 'first',
82 'de' => 'Erste',
83 '_type' => 'lang'
84 ],
85 ],
86 'multilang-multivalue' => [
87 [
88 'en' => [ 'first', 'second' ],
89 'de' => [ 'Erste', 'Zweite' ],
90 '_type' => 'lang'
91 ],
92 [
93 'en' => 'first',
94 'de' => 'Erste',
95 '_type' => 'lang'
96 ],
97 ],
98 ];
99 }
100
101 /**
102 * @dataProvider provideGetFormattedData
103 * @covers \FormatMetadata::getFormattedData
104 */
105 public function testGetFormattedData( $input, $output ) {
106 $this->assertEquals( $output, FormatMetadata::getFormattedData( $input ) );
107 }
108
109 public static function provideGetFormattedData() {
110 return [
111 [
112 [ 'Software' => 'Adobe Photoshop CS6 (Macintosh)' ],
113 [ 'Software' => 'Adobe Photoshop CS6 (Macintosh)' ],
114 ],
115 [
116 [ 'Software' => [ 'FotoWare FotoStation' ] ],
117 [ 'Software' => 'FotoWare FotoStation' ],
118 ],
119 [
120 [ 'Software' => [ [ 'Capture One PRO', '3.7.7' ] ] ],
121 [ 'Software' => 'Capture One PRO (Version 3.7.7)' ],
122 ],
123 [
124 [ 'Software' => [ [ 'FotoWare ColorFactory', '' ] ] ],
125 [ 'Software' => 'FotoWare ColorFactory (Version )' ],
126 ],
127 [
128 [ 'Software' => [ 'x-default' => 'paint.net 4.0.12', '_type' => 'lang' ] ],
129 [ 'Software' => '<ul class="metadata-langlist">' .
130 '<li class="mw-metadata-lang-default">' .
131 '<span class="mw-metadata-lang-value">paint.net 4.0.12</span>' .
132 "</li>\n" .
133 '</ul>'
134 ],
135 ],
136 [
137 // https://phabricator.wikimedia.org/T178130
138 // WebMHandler.php turns both 'muxingapp' & 'writingapp' to 'Software'
139 [ 'Software' => [ [ 'Lavf57.25.100' ], [ 'Lavf57.25.100' ] ] ],
140 [ 'Software' => "<ul><li>Lavf57.25.100</li>\n<li>Lavf57.25.100</li></ul>" ],
141 ],
142 ];
143 }
144
145 /**
146 * @covers \FormatMetadata::getPriorityLanguages
147 * @dataProvider provideGetPriorityLanguagesData
148 * @param string $language
149 * @param string[] $expected
150 */
151 public function testGetPriorityLanguagesInternal_language_expect(
152 string $language,
153 array $expected
154 ): void {
155 $formatMetadata = TestingAccessWrapper::newFromObject( new FormatMetadata() );
156 $context = $formatMetadata->getContext();
157 $context->setLanguage( $this->getServiceContainer()->getLanguageFactory()->getLanguage( $language ) );
158
159 $x = $formatMetadata->getPriorityLanguages();
160 $this->assertSame( $expected, $x );
161 }
162
163 public static function provideGetPriorityLanguagesData() {
164 return [
165 'LanguageMl' => [
166 'ml',
167 [ 'ml', 'en' ],
168 ],
169 'LanguageEn' => [
170 'en',
171 [ 'en', 'en' ],
172 ],
173 'LanguageQqx' => [
174 'qqx',
175 [ 'qqx', 'en' ],
176 ],
177 ];
178 }
179 }