summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Unit/Lang/JsonContentsFileReaderTest.php
blob: db42b246e39165224e1e4b619a3b3461b6da245d (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php

namespace SMW\Tests\Lang;

use SMW\Lang\JsonContentsFileReader;
use SMW\Tests\PHPUnitCompat;

/**
 * @covers \SMW\Lang\JsonContentsFileReader
 * @group semantic-mediawiki
 *
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class JsonContentsFileReaderTest extends \PHPUnit_Framework_TestCase {

	use PHPUnitCompat;

	public function testCanConstruct() {

		$this->assertInstanceOf(
			JsonContentsFileReader::class,
			new JsonContentsFileReader()
		);
	}

	/**
	 * @dataProvider languageCodeProvider
	 */
	public function testReadByLanguageCode( $languageCode ) {

		$instance = new JsonContentsFileReader();

		$this->assertInternalType(
			'array',
			$instance->readByLanguageCode( $languageCode )
		);
	}

	/**
	 * @dataProvider languageCodeProvider
	 */
	public function testReadByLanguageCodeWithCache( $languageCode ) {

		$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
			->disableOriginalConstructor()
			->getMockForAbstractClass();

		$cache->expects( $this->atLeastOnce() )
			->method( 'contains' )
			->will( $this->returnValue( true ) );

		$cache->expects( $this->atLeastOnce() )
			->method( 'fetch' )
			->will( $this->returnValue( [] ) );

		$instance = new JsonContentsFileReader( $cache );
		$instance->clear();

		$this->assertInternalType(
			'array',
			$instance->readByLanguageCode( $languageCode )
		);
	}

	public function testReadByLanguageCodeToUseInMemoryCache() {

		$instance = $this->getMockBuilder( JsonContentsFileReader::class )
			->setMethods( [ 'readJSONFile', 'getFileModificationTime' ] )
			->getMock();

		$instance->expects( $this->once() )
			->method( 'readJSONFile' )
			->will( $this->returnValue( [] ) );

		$instance->expects( $this->once() )
			->method( 'getFileModificationTime' )
			->will( $this->returnValue( 42 ) );

		$instance->readByLanguageCode( 'foo' );

		// InMemory use
		$instance->readByLanguageCode( 'foo' );
	}

	public function testReadByLanguageCodeIsForcedToRereadFromFile() {

		$instance = $this->getMockBuilder( JsonContentsFileReader::class )
			->setMethods( [ 'readJSONFile', 'getFileModificationTime' ] )
			->getMock();

		$instance->expects( $this->exactly( 2 ) )
			->method( 'readJSONFile' )
			->will( $this->returnValue( [] ) );

		$instance->expects( $this->exactly( 2 ) )
			->method( 'getFileModificationTime' )
			->will( $this->returnValue( 42 ) );

		$instance->readByLanguageCode( 'bar' );
		$instance->readByLanguageCode( 'bar', true );
	}

	public function testTryToReadInaccessibleFileByLanguageThrowsException() {

		$instance = new JsonContentsFileReader();

		$this->setExpectedException( 'RuntimeException' );
		$instance->readByLanguageCode( 'foo', true );
	}

	/**
	 * This method is just for convenience so that one can quickly add contents to files
	 * without requiring an extra class when extending the language content. Normally the
	 * test in active
	 *
	 * @dataProvider dataExtensionProvider
	 */
	public function WriteToFile( $topic, $extension ) {

		$instance = new JsonContentsFileReader();
		$list ='ar,arz,ca,de,es,fi,fr,he,hu,id,it,nb,nl,pl,pt,ru,sk,zh-cn,zh-tw';

		foreach ( explode( ',', $list ) as $lang ) {
			$contents = $instance->readByLanguageCode( $lang, true );

			if ( $contents === '' || !isset( $contents[$topic] ) ) {
				continue;
			}

			$contents[$topic] = $contents[$topic] + $extension;

			$instance->writeByLanguageCode( $lang, $contents );
		}
	}

	/**
	 * @dataProvider languageCodeProvider
	 */
	public function testgetFileModificationTime( $languageCode ) {

		$instance = new JsonContentsFileReader();

		$this->assertInternalType(
			'integer',
			$instance->getFileModificationTime( $languageCode )
		);
	}

	public function languageCodeProvider() {

		$provider[] = [
			'en'
		];

		return $provider;
	}

	public function dataExtensionProvider() {

		$provider[] = [
			'dataTypeLabels',
			[
				"_ref_rec" => "Reference"
			]
		];

		return $provider;
	}

}