Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / utils / FileContentsHasherTest.php
bloba03e1fc4160aad2abb45ef5da703af0007c4ccfb
1 <?php
3 /**
4 * @covers FileContentsHasherTest
5 */
6 class FileContentsHasherTest extends MediaWikiTestCase {
8 public function provideSingleFile() {
9 return array_map( function ( $file ) {
10 return array( $file, file_get_contents( $file ) );
11 }, glob( __DIR__ . '/../../data/filecontentshasher/*.*' ) );
14 public function provideMultipleFiles() {
15 return array(
16 array( $this->provideSingleFile() )
20 /**
21 * @covers FileContentsHasher::getFileContentHash
22 * @covers FileContentsHasher::getFileContentsHashInternal
23 * @dataProvider provideSingleFile
25 public function testSingleFileHash( $fileName, $contents ) {
26 foreach ( array( 'md4', 'md5' ) as $algo ) {
27 $expectedHash = hash( $algo, $contents );
28 $actualHash = FileContentsHasher::getFileContentsHash( $fileName, $algo );
29 $this->assertEquals( $expectedHash, $actualHash );
30 $actualHashRepeat = FileContentsHasher::getFileContentsHash( $fileName, $algo );
31 $this->assertEquals( $expectedHash, $actualHashRepeat );
35 /**
36 * @covers FileContentsHasher::getFileContentHash
37 * @covers FileContentsHasher::getFileContentsHashInternal
38 * @dataProvider provideMultipleFiles
40 public function testMultipleFileHash( $files ) {
41 $fileNames = array();
42 $hashes = array();
43 foreach ( $files as $fileInfo ) {
44 list( $fileName, $contents ) = $fileInfo;
45 $fileNames[] = $fileName;
46 $hashes[] = md5( $contents );
49 $expectedHash = md5( implode( '', $hashes ) );
50 $actualHash = FileContentsHasher::getFileContentsHash( $fileNames, 'md5' );
51 $this->assertEquals( $expectedHash, $actualHash );
52 $actualHashRepeat = FileContentsHasher::getFileContentsHash( $fileNames, 'md5' );
53 $this->assertEquals( $expectedHash, $actualHashRepeat );