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

namespace SMW\Tests\System;

use SMW\Tests\Utils\UtilityFactory;

/**
 * @group semantic-mediawiki
 * @group system-test
 *
 * @license GNU GPL v2+
 * @since 2.1
 *
 * @author mwjames
 */
class I18nJsonFileIntegrityTest extends \PHPUnit_Framework_TestCase {

	/**
	 * @dataProvider mediawikiI18nFileProvider
	 */
	public function testMediaWikiI18NJsonDecodeEncode( $file ) {

		$jsonFileReader = UtilityFactory::getInstance()->newJsonFileReader( $file );

		$this->assertInternalType(
			'integer',
			$jsonFileReader->getModificationTime()
		);

		$this->assertInternalType(
			'array',
			$jsonFileReader->read()
		);
	}

	/**
	 * @dataProvider semanticMediaWikiI18nFileProvider
	 */
	public function testSemanticMediaWikiI18NJsonDecodeEncode( $file ) {

		$jsonFileReader = UtilityFactory::getInstance()->newJsonFileReader( $file );

		$this->assertInternalType(
			'integer',
			$jsonFileReader->getModificationTime()
		);

		$contents = $jsonFileReader->read();

		$this->assertInternalType(
			'array',
			$contents
		);

		$expectedKeys = [
			'fallback_language',
			'datatype'  => [ 'labels', 'aliases' ],
			'property'  => [ 'labels', 'aliases' ],
			'namespace' => [ 'labels', 'aliases' ],
			'date'      => [ 'precision', 'format', 'months', 'days' ]
		];

		// If the file is marked with isLanguageRedirect then only check for the fallbackLanguage
		if ( isset( $contents['isLanguageRedirect'] ) && $contents['isLanguageRedirect'] ) {
			return $this->assertArrayHasKey( 'fallback_language', $contents, $file );
		}

		foreach ( $expectedKeys as $key => $val ) {
			if ( !is_array( $val ) ) {
				$this->assertArrayHasKey( $val, $contents, "Failed on $file with key: $val" );
			} else {
				foreach ( $val as $k => $v ) {
					$this->assertArrayHasKey( $v, $contents[$key], "Failed on $file with key: $key/$v" );
				}
			}
		}
	}

	public function mediawikiI18nFileProvider() {
		return $this->findFilesIn( $GLOBALS['wgMessagesDirs']['SemanticMediaWiki'] );
	}

	public function semanticMediaWikiI18nFileProvider() {
		return $this->findFilesIn( $GLOBALS['smwgExtraneousLanguageFileDir'] );
	}

	private function findFilesIn( $location ) {

		$provider = [];

		$bulkFileProvider = UtilityFactory::getInstance()->newBulkFileProvider( $location );
		$bulkFileProvider->searchByFileExtension( 'json' );

		foreach ( $bulkFileProvider->getFiles() as $file ) {
			$provider[] = [ $file ];
		}

		return $provider;
	}

}