summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Mermaid/tests/phpunit/Integration/I18nJsonFileIntegrityTest.php
blob: c5a1c4ae47dc9c2adbd480d19d5ce2437a90fd57 (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
<?php

namespace Mermaid\Tests\Integration;

/**
 * @group mermaid
 * @group medium
 *
 * @license GNU GPL v2+
 * @since 1.0
 *
 * @author mwjames
 */
class I18nJsonFileIntegrityTest extends \PHPUnit_Framework_TestCase {

	/**
	 * @dataProvider i18nFileProvider
	 */
	public function testI18NJsonDecodeEncode( $file ) {

		$contents = file_get_contents( $file );

		$this->assertInternalType(
			'array',
			json_decode( $contents, true ),
			'Failed with ' . $this->getDescriptiveJsonError( json_last_error() ) . ' in ' . $file
		);
	}

	public function i18nFileProvider() {

		$provider = array();

		$files = $this->findFilesForExtension(
			$GLOBALS['wgMessagesDirs']['Mermaid'],
			'json'
		);

		foreach ( $files as $file ) {
			$provider[] = array( $file );
		}

		return $provider;
	}

	private function findFilesForExtension( $path, $extension ) {

		$files = array();

		$directoryIterator = new \RecursiveDirectoryIterator(
			str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $path )
		);

		foreach ( new \RecursiveIteratorIterator( $directoryIterator ) as $fileInfo ) {
			if ( strtolower( substr( $fileInfo->getFilename(), -( strlen( $extension ) + 1 ) ) ) === ( '.' . $extension ) ) {
				$files[$fileInfo->getFilename()] = $fileInfo->getPathname();
			}
		}

		return $files;
	}

	private function getDescriptiveJsonError( $errorCode ) {

		$errorMessages = array(
			JSON_ERROR_NONE => '',
			JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch, malformed JSON',
			JSON_ERROR_CTRL_CHAR => 'Unexpected control character found, possibly incorrectly encoded',
			JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
			JSON_ERROR_UTF8   => 'Malformed UTF-8 characters, possibly incorrectly encoded',
			JSON_ERROR_DEPTH  => 'The maximum stack depth has been exceeded'
		);

		return $errorMessages[$errorCode];
	}

}