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

namespace MediaWiki\Extension\Translate\Synchronization;

use MediaWikiUnitTestCase;
use MessageUpdateJob;
use Title;

/** @covers \MediaWiki\Extension\Translate\Synchronization\MessageUpdateParameter */
class MessageUpdateParameterTest extends MediaWikiUnitTestCase {
	/** @dataProvider provideSerializable */
	public function testSerializable(
		string $title,
		string $content,
		bool $isRename,
		string $target = '',
		string $replacement = '',
		bool $isFuzzy = false,
		array $otherLangs = []
	) {
		$job = $this->getJobFromInput(
			$title, $content, $isRename, $target, $replacement, $isFuzzy, $otherLangs
		);

		$messageParam = MessageUpdateParameter::createFromJob( $job );

		$serializedMessageParam = unserialize( serialize( $messageParam ) );
		$this->assertEquals( $messageParam, $serializedMessageParam );
	}

	/** @dataProvider provideSerializable */
	public function testCreateFromJob(
		string $title,
		string $content,
		bool $isRename,
		string $target = '',
		string $replacement = '',
		bool $isFuzzy = false,
		array $otherLangs = []
	) {
		$job = $this->getJobFromInput(
			$title, $content, $isRename, $target, $replacement, $isFuzzy, $otherLangs
		);
		$messageParams = MessageUpdateParameter::createFromJob( $job );
		$this->assertEquals( $title, $messageParams->getPageName() );
		$this->assertEquals( $content, $messageParams->getContent() );
		$this->assertEquals( $isRename, $messageParams->isRename() );
		$this->assertEquals( $isFuzzy, $messageParams->isFuzzy() );

		if ( $isRename ) {
			$this->assertEquals( $target, $messageParams->getTargetValue() );
			$this->assertEquals( $replacement, $messageParams->getReplacementValue() );
			$this->assertEquals( $otherLangs, $messageParams->getOtherLangs() );
		} else {
			$this->assertNull( $messageParams->getOtherLangs() );
		}
	}

	private function getJobFromInput(
		string $title,
		string $content,
		bool $isRename,
		string $target,
		string $replacement,
		bool $isFuzzy,
		array $otherLangs
	): MessageUpdateJob {
		$title = Title::makeTitle( NS_MAIN, $title );
		if ( $isRename ) {
			$job = MessageUpdateJob::newRenameJob(
				$title, $target, $replacement, $isFuzzy, $content, $otherLangs
			);
		} else {
			$job = MessageUpdateJob::newJob( $title, $content, $isFuzzy );
		}

		return $job;
	}

	public function provideSerializable() {
		yield [
			'Normal_Job/en',
			'Hello World!',
			false,
		];

		yield [
			'Rename_Job/en',
			'Hello World - Rename!',
			true,
			'target',
			'replacement',
			true,
			[ 'hello' => 'world' ]
		];
	}

}