summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/media/FormatMetadataTest.php
blob: 54758f9486261312da4b449fdf126485d8ffcec5 (plain)
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
<?php

/**
 * @group Media
 */
class FormatMetadataTest extends MediaWikiMediaTestCase {

	protected function setUp() {
		parent::setUp();

		$this->checkPHPExtension( 'exif' );
		$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->assertEquals( '0000:01:00 00:02:27',
			$meta['visible'][$dateIndex]['value'],
			'File with invalid date metadata (bug 29471)' );
	}

	/**
	 * @param string $filename
	 * @param int $expected Total image area
	 * @dataProvider provideFlattenArray
	 * @covers FormatMetadata::flattenArray
	 */
	public function testFlattenArray( $vals, $type, $noHtml, $ctx, $expected ) {
		$actual = FormatMetadata::flattenArray( $vals, $type, $noHtml, $ctx );
		$this->assertEquals( $expected, $actual );
	}

	public static function provideFlattenArray() {
		return array(
			array(
				array( 1, 2, 3 ), 'ul', false, false,
				"<ul><li>1</li>\n<li>2</li>\n<li>3</li></ul>",
			),
			array(
				array( 1, 2, 3 ), 'ol', false, false,
				"<ol><li>1</li>\n<li>2</li>\n<li>3</li></ol>",
			),
			array(
				array( 1, 2, 3 ), 'ul', true, false,
				"\n*1\n*2\n*3",
			),
			array(
				array( 1, 2, 3 ), 'ol', true, false,
				"\n#1\n#2\n#3",
			),
			// TODO: more test cases
		);
	}

	/**
	 * @param mixed $input
	 * @param mixed $output
	 * @dataProvider provideResolveMultivalueValue
	 * @covers FormatMetadata::resolveMultivalueValue
	 */
	public function testResolveMultivalueValue( $input, $output ) {
		$formatMetadata = new FormatMetadata();
		$class = new ReflectionClass( 'FormatMetadata' );
		$method = $class->getMethod( 'resolveMultivalueValue' );
		$method->setAccessible( true );
		$actualInput = $method->invoke( $formatMetadata, $input );
		$this->assertEquals( $output, $actualInput );
	}

	public function provideResolveMultivalueValue() {
		return array(
			'nonArray' => array( 'foo', 'foo' ),
			'multiValue' => array( array( 'first', 'second', 'third', '_type' => 'ol' ), 'first' ),
			'noType' => array( array( 'first', 'second', 'third' ), 'first' ),
			'typeFirst' => array( array( '_type' => 'ol', 'first', 'second', 'third' ), 'first' ),
			'multilang' => array(
				array( 'en' => 'first', 'de' => 'Erste', '_type' => 'lang' ),
				array( 'en' => 'first', 'de' => 'Erste', '_type' => 'lang' ),
			),
			'multilang-multivalue' => array(
				array( 'en' => array( 'first', 'second' ), 'de' => array( 'Erste', 'Zweite' ), '_type' => 'lang' ),
				array( 'en' => 'first', 'de' => 'Erste', '_type' => 'lang' ),
			),
		);
	}
}