summaryrefslogtreecommitdiff
blob: 9990971564e57055874242b5df037ddd31300932 (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
<?php
declare( strict_types = 1 );

use MediaWiki\Extension\Translate\PageTranslation\ParserOutput;
use MediaWiki\Extension\Translate\PageTranslation\ParsingFailure;
use MediaWiki\Extension\Translate\PageTranslation\TestingParsingPlaceholderFactory;
use MediaWiki\Extension\Translate\PageTranslation\TranslatablePageParser;
use MediaWiki\Extension\Translate\PageTranslation\TranslationPage;

/**
 * Custom testing framework for page translation parser.
 * @ingroup PageTranslation
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 */
class PageTranslationParserTest extends MediaWikiIntegrationTestCase {
	public static function provideTestFiles() {
		$dir = __DIR__;
		$testFiles = glob( "$dir/pagetranslation/*.ptfile" );
		foreach ( $testFiles as $i => $file ) {
			$testFiles[$i] = [ basename( $file, '.ptfile' ), $file ];
		}

		return $testFiles;
	}

	/** @dataProvider provideTestFiles */
	public function testParsing( string $name, string $file ) {
		if ( $name === 'FailNotAtomic' ) {
			$this->markTestSkipped( 'Extended validation not yet implemented' );
		}

		if ( $name !== 'Whitespace' ) {
			$this->markTestSkipped( 'Extended validation not yet implemented' );
		}

		if ( strpos( $name, 'Fail' ) === 0 ) {
			$this->expectException( ParsingFailure::class );
		}

		$title = Title::newFromText( $name );
		$inputSourceText = file_get_contents( $file );
		$parser = new TranslatablePageParser( new TestingParsingPlaceholderFactory() );
		$parserOutput = $parser->parse( $inputSourceText );

		$pattern = dirname( $file ) . "/$name";

		if ( file_exists( "$pattern.ptsource" ) ) {
			$source = $parserOutput->sourcePageTextForSaving();
			$this->assertSame(
				file_get_contents( "$pattern.ptsource" ),
				$source,
				'Marked source text is as expected'
			);
		}

		if ( file_exists( "$pattern.pttarget" ) ) {
			$translationPage = $this->getTranslationPage( $title, $parserOutput );
			$target = $translationPage->generateSourceFromTranslations( [] );
			$this->assertEquals(
				file_get_contents( "$pattern.pttarget" ),
				$target,
				'Generated translation page text is as expected'
			);
		}

		// Custom tests written in php
		if ( file_exists( "$pattern.pttest" ) ) {
			require "$pattern.pttest";
		}
	}

	// This is copy of TranslatablePage::getTranslationPage, to mock WikiPageMessageGroup
	private function getTranslationPage(
		Title $title,
		ParserOutput $parserOutput
	): TranslationPage {
		$showOutdated = false;
		$wrapUntranslated = false;

		return new TranslationPage(
			$parserOutput,
			$this->createMock( WikiPageMessageGroup::class ),
			Language::factory( 'en' ),
			Language::factory( 'en' ),
			$showOutdated,
			$wrapUntranslated,
			$title->getPrefixedDBkey() . '/'
		);
	}
}