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
|
<?php
/**
* @file
* @author Niklas Laxström
* @license GPL-2.0-or-later
*/
namespace MediaWiki\Extensions\Translate\Statistics;
use EmptyBagOStuff;
use HashBagOStuff;
use InvalidArgumentException;
use JobQueueGroup;
use MediaWikiUnitTestCase;
use Wikimedia\Timestamp\ConvertibleTimestamp;
/**
* @covers \MediaWiki\Extensions\Translate\Statistics\TranslatorActivity
*/
class TranslatorActivityTest extends MediaWikiUnitTestCase {
public function testInvalidLanguage() {
$cache = $this->createMock( EmptyBagOStuff::class );
$query = $this->createMock( TranslatorActivityQuery::class );
$jobQueue = $this->createMock( JobQueueGroup::class );
$languageValidator = function ( string $language ): bool {
return false;
};
$service = new TranslatorActivity( $cache, $query, $jobQueue, $languageValidator );
$this->expectException( InvalidArgumentException::class );
$service->inLanguage( 'not-a-valid-language-code' );
}
public function testInLanguage() {
$language = 'en';
$translators = $this->getExampleData();
$cacheKey = 'cache:en';
$fakeTime1 = 1;
$expected = [
'users' => $translators,
'asOfTime' => $fakeTime1,
];
$cache = $this->createMock( HashBagOStuff::class );
$cache->method( 'makeKey' )->willReturn( $cacheKey );
$cache->expects( $this->once() )->method( 'set' )->with( $cacheKey, $expected );
$query = $this->createMock( TranslatorActivityQuery::class );
$query
->method( 'inLanguage' )
->willReturn( $translators )
->with( $this->equalTo( $language ) );
$jobQueue = $this->createMock( JobQueueGroup::class );
$jobQueue->expects( $this->never() )->method( 'push' );
$languageValidator = function ( string $language ): bool {
return true;
};
$service = new TranslatorActivity( $cache, $query, $jobQueue, $languageValidator );
ConvertibleTimestamp::setFakeTime( $fakeTime1 );
$actual = $service->inLanguage( $language );
$this->assertEquals( $expected, $actual, 'Correct value is returned' );
}
public function testInLanguageStale() {
$language = 'en';
$translators = $this->getExampleData();
$fakeTime1 = 1;
$fakeTime2 = $fakeTime1 + TranslatorActivity::CACHE_STALE;
$expected = [
'users' => $translators,
'asOfTime' => $fakeTime1,
];
$cache = new HashBagOStuff();
$query = $this->createMock( TranslatorActivityQuery::class );
$query
->method( 'inLanguage' )
->willReturn( $translators )
->with( $this->equalTo( $language ) );
$jobQueue = $this->createMock( JobQueueGroup::class );
$jobQueue->expects( $this->once() )->method( 'push' );
$languageValidator = function ( string $language ): bool {
return true;
};
$service = new TranslatorActivity( $cache, $query, $jobQueue, $languageValidator );
ConvertibleTimestamp::setFakeTime( $fakeTime1 );
$cache->setMockTime( $fakeTime1 );
$service->inLanguage( $language );
ConvertibleTimestamp::setFakeTime( $fakeTime2 );
$cache->setMockTime( $fakeTime2 );
$actual = $service->inLanguage( $language );
$this->assertEquals( $expected, $actual, 'Correct value is returned' );
}
public function testInLanguageExpired() {
$language = 'en';
$translators = $this->getExampleData();
$fakeTime1 = 1;
$fakeTime2 = $fakeTime1 + TranslatorActivity::CACHE_TIME;
$expected = [
'users' => $translators,
'asOfTime' => $fakeTime2,
];
$cache = new HashBagOStuff();
$query = $this->createMock( TranslatorActivityQuery::class );
$query
->method( 'inLanguage' )
->willReturn( $translators )
->with( $this->equalTo( $language ) );
$jobQueue = $this->createMock( JobQueueGroup::class );
$jobQueue->expects( $this->never() )->method( 'push' );
$languageValidator = function ( string $language ): bool {
return true;
};
$service = new TranslatorActivity( $cache, $query, $jobQueue, $languageValidator );
ConvertibleTimestamp::setFakeTime( $fakeTime1 );
$cache->setMockTime( $fakeTime1 );
$service->inLanguage( $language );
ConvertibleTimestamp::setFakeTime( $fakeTime2 );
$cache->setMockTime( $fakeTime2 );
$actual = $service->inLanguage( $language );
$this->assertEquals( $expected, $actual, 'Correct value is returned' );
}
private function getExampleData(): array {
$translators = [
'Hunter' => [
TranslatorActivityQuery::USER_TRANSLATIONS => 1234,
TranslatorActivityQuery::USER_LAST_ACTIVITY => 10,
],
'Farmer' => [
TranslatorActivityQuery::USER_TRANSLATIONS => 2,
TranslatorActivityQuery::USER_LAST_ACTIVITY => 20,
],
];
return $translators;
}
}
|