summaryrefslogtreecommitdiff
blob: 5b1526c2e173b516f5d10def561fac4d28f432f9 (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
/**
 * Tests for TTMServer
 *
 * @file
 * @author Niklas Laxström
 * @copyright Copyright © 2012-2013, Niklas Laxström
 * @license GPL-2.0-or-later
 */

class TTMServerTest extends MediaWikiTestCase {
	protected $config;

	protected function setUp() {
		global $wgTranslateTranslationServices;
		$this->config = $wgTranslateTranslationServices;
		parent::setUp();

		$wgTranslateTranslationServices = [];
		$wgTranslateTranslationServices['localtm'] = [
			'url' => 'http://example.com/sandwiki/api.php',
			'displayname' => 'example.com',
			'cutoff' => 0.75,
			'type' => 'ttmserver',
		];

		$wgTranslateTranslationServices['apitm'] = [
			'url' => 'http://example.com/w/api.php',
			'displayname' => 'example.com',
			'cutoff' => 0.75,
			'timeout-sync' => 4,
			'timeout-async' => 4,
			'type' => 'ttmserver',
			'class' => 'RemoteTTMServer',
		];
	}

	protected function tearDown() {
		global $wgTranslateTranslationServices;
		$wgTranslateTranslationServices = $this->config;
		parent::tearDown();
	}

	public function testConstruct() {
		$server = TTMServer::primary();
		$this->assertEquals(
			'FakeTTMServer',
			get_class( $server ),
			'Fake server given when default server is disabled'
		);
		global $wgTranslateTranslationServices,
			$wgTranslateTranslationDefaultService;
		$wgTranslateTranslationServices[$wgTranslateTranslationDefaultService] = [
			'database' => false, // Passed to wfGetDB
			'cutoff' => 0.75,
			'type' => 'ttmserver',
			'public' => false,
		];
		$server = TTMServer::primary();
		$this->assertEquals(
			'DatabaseTTMServer',
			get_class( $server ),
			'Real server given when default server is enabled'
		);
		unset( $wgTranslateTranslationServices[$wgTranslateTranslationDefaultService] );
	}

	public function testFakeTTMServer() {
		$server = new FakeTTMServer();
		$this->assertEquals(
			[],
			$server->query( 'en', 'fi', 'daa' ),
			'FakeTTMServer returns no suggestions for all queries'
		);

		$title = new Title();
		$handle = new MessageHandle( $title );

		$this->assertNull(
			$server->update( $handle, 'text' ),
			'FakeTTMServer returns null on update'
		);
	}

	public function testMirrorsConfig() {
		global $wgTranslateTranslationServices;
		$wgTranslateTranslationServices['primary'] = [
			'class' => 'ElasticSearchTTMServer',
			'mirrors' => [ 'secondary' ]
		];
		$wgTranslateTranslationServices['secondary'] = [
			'class' => 'ElasticSearchTTMServer',
			'mirrors' => [ 'primary', 'unknown' ]
		];
		$primary = TTMServer::factory( $wgTranslateTranslationServices['primary'] );
		$this->assertEquals( [ 'secondary' ], $primary->getMirrors() );
		$secondary = TTMServer::factory( $wgTranslateTranslationServices['secondary'] );
		$this->setExpectedException( TTMServerException::class );
		$secondary->getMirrors();
	}
}