summaryrefslogtreecommitdiff
blob: d6877db9669e7341d209359e096cec78b86496d0 (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
<?php

/** @group Database */
class TranslatablePageMessageGroupStoreTest extends MediaWikiIntegrationTestCase {
	use TranslatablePageTestTrait;

	/** @var TranslatablePageMessageGroupStore */
	protected $mgTranslateStore;

	protected function setUp(): void {
		parent::setUp();

		$this->setMwGlobals( [
			'wgEnablePageTranslation' => true
		] );

		$this->mgTranslateStore = new TranslatablePageMessageGroupStore(
			TranslateUtils::getSafeReadDB(),
			new MessageGroupWANCache(
				new WANObjectCache( [ 'cache' => wfGetCache( 'hash' ) ] )
			)
		);
	}

	public function testRecache() {
		$prevGroupCount = count( $this->mgTranslateStore->getGroups() );

		$this->createMarkedTranslatablePage( 'Upyog', 'Upyog', $this->getTestSysop()->getUser() );

		$countBeforeRecache = count( $this->mgTranslateStore->getGroups() );
		$this->assertEquals( $prevGroupCount, $countBeforeRecache,
			'new groups do not appear unless recache is called' );

		$this->mgTranslateStore->recache();

		$updatedCount = count( $this->mgTranslateStore->getGroups() );
		$this->assertEquals( ( $prevGroupCount + 1 ), $updatedCount,
			'new groups appear after recache is called' );
	}

	public function testGlobalFlag() {
		$this->createMarkedTranslatablePage( 'Upyon - 22', 'Upyog', $this->getTestSysop()->getUser() );
		$this->mgTranslateStore->recache();
		$prevCount = count( $this->mgTranslateStore->getGroups() );
		$this->assertGreaterThanOrEqual( 1, $prevCount, 'there is atleast 1 ' .
			'translatable page returned' );

		$this->setMwGlobals( [
			'wgEnablePageTranslation' => false
		] );

		$this->mgTranslateStore->recache();
		$this->assertCount( 0, $this->mgTranslateStore->getGroups(), 'no translatable pages returned' );
	}

	public function testCacheCalls() {
		$dummy = new DependencyWrapper( [], [] );

		/** @var MessageGroupWANCache $mockMgWANCache */
		$mockMgWANCache = $this->getMockBuilder( MessageGroupWANCache::class )
			->disableOriginalConstructor()
			->getMock();

		$translateStore = new TranslatablePageMessageGroupStore(
			TranslateUtils::getSafeReadDB(),
			$mockMgWANCache
		);

		$mockMgWANCache->expects( $this->once() )
			->method( 'getValue' )
			->with( 'recache' )
			->willReturn( $dummy );

		// should trigger a get call on cache
		$translateStore->recache();

		// should return the cached groups from process cache
		$this->assertEquals( [], $translateStore->getGroups() );

		$mockMgWANCache->expects( $this->once() )
			->method( 'delete' );

		// should trigger the delete method on cache
		$translateStore->clearCache();
	}
}