summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/tests/phpunit/PageTranslationParserTest.php
blob: 5e82176e6d0a4c8fb15e4a883e9986fb9e9bf284 (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
<?php
/**
 * @author Niklas Laxström
 * @copyright Copyright © 2010-2013, Niklas Laxström
 * @license GPL-2.0-or-later
 * @file
 */

/**
 * Custom testing framework for page translation parser.
 * @ingroup PageTranslation
 * @group Database
 */
class PageTranslationParserTest extends MediaWikiTestCase {
	public static function provideTestFiles() {
		$dir = __DIR__;
		$testFiles = glob( "$dir/pagetranslation/*.ptfile" );
		foreach ( $testFiles as $i => $file ) {
			$testFiles[$i] = [ $file ];
		}

		return $testFiles;
	}

	/**
	 * @dataProvider provideTestFiles
	 */
	public function testParsing( $file ) {
		$filename = basename( $file );
		list( $pagename, ) = explode( '.', $filename, 2 );
		$title = Title::newFromText( $pagename );
		$translatablePage = TranslatablePage::newFromText( $title, file_get_contents( $file ) );

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

		if ( $filename === 'FailNotAtomic.ptfile' ) {
			$this->markTestSkipped( 'Extended validation not yet implemented' );
		}

		$failureExpected = strpos( $pagename, 'Fail' ) === 0;

		if ( $failureExpected ) {
			$this->setExpectedException( 'TPException' );
		}

		$parse = $translatablePage->getParse();
		$this->assertInstanceOf( 'TPParse', $parse );

		if ( file_exists( "$pattern.ptsource" ) ) {
			$source = $parse->getSourcePageText();
			$this->assertEquals( file_get_contents( "$pattern.ptsource" ), $source );
		}

		if ( file_exists( "$pattern.pttarget" ) ) {
			$target = $parse->getTranslationPageText( [] );
			$this->assertEquals( file_get_contents( "$pattern.pttarget" ), $target );
		}

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