Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EncodingServiceTest.php « Service « unit « tests - github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8458aaae9124d73a56d7fa6c236d069c825d0c4a (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
<?php

namespace OCA\Text\Service;

use Test\TestCase;

class LabelServiceTest extends TestCase {
	private $encodingService;

	protected function setUp(): void {
		parent::setUp();
		$this->encodingService = new EncodingService();
	}

	/**
	 * Attempt to decode the file using the default decoding order.
	 * For files with encodings not included in the COMMON_ENCODINGS array encoding to UTF-8 will fail.
	 * @dataProvider dataFileEncodings
	 */
	public function testDefault(string $file, string $encoding) {
		$utf8_string = $this->encodingService->encodeToUtf8(file_get_contents($file));

		// If encoding is not part of the default encodings we can expect it to fail
		// It might still succeed because encoding detection is not precise.
		if (!$utf8_string && !in_array($encoding, EncodingService::COMMON_ENCODINGS, true)) {
			return;
		}

		$this->assertNotNull($utf8_string);
		$this->assertNotFalse(mb_detect_encoding($utf8_string, 'UTF-8', true));
	}

	/**
	 * Includes the encoding of the file in the detection order config value.
	 * This means that all files should be successfully encoded to UTF-8.
	 * @dataProvider dataFileEncodings
	 */
	public function testCustomOrder(string $file, string $encoding) {
		$original_order = mb_detect_order();
		$this->assertNotFalse(mb_detect_order($encoding));

		$utf8_string = $this->encodingService->encodeToUtf8(file_get_contents($file));
		$this->assertNotNull($utf8_string);
		$this->assertNotFalse(mb_detect_encoding($utf8_string, 'UTF-8', true));

		mb_detect_order($original_order);
	}

	public function dataFileEncodings(): array {
		return [
			['./tests/data/iso-8859.txt', 'ISO-8859-1'],
			['./tests/data/big5.txt', 'BIG-5'],
			['./tests/data/gbk.txt', 'GBK']
		];
	}
}