aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/collation/RemoteIcuCollationTest.php
blob: 7f4d86eaf7b3aa281611a8f764e02322c093590b (plain) (blame)
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
<?php

use Wikimedia\TestingAccessWrapper;

/**
 * @covers \RemoteIcuCollation
 */
class RemoteIcuCollationTest extends MediaWikiLangTestCase {
	public static function provideEncode() {
		return [
			[
				[],
				''
			],
			[
				[ 'foo' ],
				'00000003foo'
			],
			[
				[ 'foo', 'a somewhat longer string' ],
				'00000003foo00000018a somewhat longer string'
			],
		];
	}

	/** @dataProvider provideEncode */
	public function testEncode( $input, $expected ) {
		$coll = TestingAccessWrapper::newFromClass( RemoteIcuCollation::class );
		$this->assertSame( $expected, $coll->encode( $input ) );
	}

	public static function provideEncodeDecode() {
		return [
			[ [ "\000" ] ],
			[ [ "a\000b" ] ],
			[ [ str_repeat( "\001", 100 ) ] ],
			[ [ 'foo' ] ],
			[ [ 'foo', 'bar' ] ],
			[ [ 'foo', 'bar', str_repeat( 'x', 1000 ) ] ]
		];
	}

	/** @dataProvider provideEncodeDecode */
	public function testEncodeDecode( $input ) {
		$coll = TestingAccessWrapper::newFromClass( RemoteIcuCollation::class );
		$this->assertSame( $input, $coll->decode( $coll->encode( $input ) ) );
	}

	public static function provideGetSortKeys() {
		$cases = [
			[],
			[ '' ],
			[ 'test1' => 'bar', 'test2' => 'foo' ],
			[
				'bar',
				'foo'
			],
			[
				'first',
				'Second'
			],
			[
				'',
				'second'
			],
			[
				'Berić',
				'Berisha',
			],
			[
				'2',
				'10',
			]
		];
		foreach ( $cases as $case ) {
			yield [ $case ];
		}
	}

	/** @dataProvider provideGetSortKeys */
	public function testGetSortKeys( $inputs ) {
		$coll = new RemoteIcuCollation(
			$this->getServiceContainer()->getShellboxClientFactory(),
			'uca-default-u-kn'
		);
		$sortKeys = $coll->getSortKeys( $inputs );
		$prevKey = null;
		if ( count( $inputs ) ) {
			foreach ( $inputs as $i => $input ) {
				$key = $sortKeys[$i];
				$this->assertIsString( $key );
				if ( $prevKey ) {
					$this->assertLessThan( 0, strcmp( $prevKey, $key ) );
				}
				$prevKey = $key;
			}
		} else {
			$this->assertSame( [], $sortKeys );
		}
	}

	/** @dataProvider provideGetSortKeys */
	public function testGetSortKey( $inputs ) {
		if ( !count( $inputs ) ) {
			// Not risky, it's just handy to reuse the provider
			$this->assertTrue( true );
		}
		$coll = new RemoteIcuCollation(
			$this->getServiceContainer()->getShellboxClientFactory(),
			'uca-default-u-kn'
		);
		$prevKey = null;
		foreach ( $inputs as $input ) {
			$key = $coll->getSortKey( $input );
			$this->assertIsString( $key );
			if ( $prevKey ) {
				$this->assertLessThan( 0, strcmp( $prevKey, $key ) );
			}
			$prevKey = $key;
		}
	}
}