public function testGetTranslationPageText() { $title = Title::newFromText(__CLASS__); $page = TranslatablePage::newFromText($title, '<translate>Hello <tvar|abc>peter!</></translate>'); $prefix = $title->getPrefixedDBKey() . '/'; $parse = $page->getParse(); $collection = array(); $expected = 'Hello peter!'; $actual = $parse->getTranslationPageText($collection); $this->assertEquals($expected, $actual, 'Variable declarations are substituted when no translation'); foreach ($parse->sections as $section) { $key = $prefix . $section->id; $message = new FatMessage($key, $section->getText()); $message->setTranslation($section->getText()); $collection[$key] = $message; } $actual = $parse->getTranslationPageText($collection); $this->assertEquals($expected, $actual, 'Variable declarations are substituted in source language'); foreach ($parse->sections as $section) { $key = $prefix . $section->id; $message = new FatMessage($key, $section->getText()); $message->setTranslation($section->getTextForTrans()); $collection[$key] = $message; } $actual = $parse->getTranslationPageText($collection); $this->assertEquals($expected, $actual, 'Variable declarations are substituted in translation'); }
/** * @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 = $file; 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($source, file_get_contents("{$pattern}.ptsource")); } if (file_exists("{$pattern}.pttarget")) { $target = $parse->getTranslationPageText(MessageCollection::newEmpty('foo')); $this->assertEquals($target, file_get_contents("{$pattern}.pttarget")); } // Custom tests written in php if (file_exists("{$pattern}.pttest")) { require "{$pattern}.pttest"; } }
public function execute() { $dir = dirname( __FILE__ ); $testDirectory = "$dir/../tests/pagetranslation"; $testFiles = glob( "$testDirectory/*.ptfile" ); foreach ( $testFiles as $file ) { $filename = basename( $file ); list( $pagename, ) = explode( '.', $filename, 2 ); $title = Title::newFromText( $pagename ); $translatablePage = TranslatablePage::newFromText( $title, file_get_contents( $file ) ); $pattern = realpath( "$testDirectory" ) . "/$pagename"; $failureExpected = strpos( $pagename, 'Fail' ) === 0; try { $parse = $translatablePage->getParse(); if ( $failureExpected ) { $target = $parse->getTranslationPageText( MessageCollection::newEmpty( "foo" ) ); $this->output( "Testfile $filename should have failed... see $pattern.pttarget.fail\n" ); file_put_contents( "$pattern.pttarget.fail", $target ); } } catch ( TPException $e ) { if ( !$failureExpected ) { $this->output( "Testfile $filename failed to parse... see $pattern.ptfile.fail\n" ); file_put_contents( "$pattern.ptfile.fail", $e->getMessage() ); } continue; } if ( file_exists( "$pattern.ptsource" ) ) { $source = $parse->getSourcePageText(); if ( $source !== file_get_contents( "$pattern.ptsource" ) ) { $this->output( "Testfile $filename failed with source page output... writing $pattern.ptsource.fail\n" ); file_put_contents( "$pattern.ptsource.fail", $source ); } } if ( file_exists( "$pattern.pttarget" ) ) { $target = $parse->getTranslationPageText( MessageCollection::newEmpty( "foo" ) ); if ( $target !== file_get_contents( "$pattern.pttarget" ) ) { $this->output( "Testfile $filename failed with target page output... writing $pattern.pttarget.fail\n" ); file_put_contents( "$pattern.pttarget.fail", $target ); } } // Custom tests written in php if ( file_exists( "$pattern.pttest" ) ) { require( "$pattern.pttest" ); } } }
/** * When attempting to save, last resort. Edit page would only display * edit conflict if there wasn't tpSyntaxCheckForEditPage. * Hook: PageContentSave */ public static function tpSyntaxCheck($wikiPage, $user, $content, $summary, $minor, $_, $_, $flags, $status) { if ($content instanceof TextContent) { $text = $content->getNativeData(); } else { // Screw it, not interested return true; } // Quick escape on normal pages if (strpos($text, '<translate>') === false) { return true; } $page = TranslatablePage::newFromText($wikiPage->getTitle(), $text); try { $page->getParse(); } catch (TPException $e) { call_user_func_array(array($status, 'fatal'), $e->getMsg()); return false; } return true; }
/** * When attempting to save, last resort. Edit page would only display * edit conflict if there wasn't tpSyntaxCheckForEditPage * Hook: ArticleSave */ public static function tpSyntaxCheck($article, $user, $text, $summary, $minor, $_, $_, $flags, $status) { // Quick escape on normal pages if (strpos($text, '<translate>') === false) { return true; } $page = TranslatablePage::newFromText($article->getTitle(), $text); try { $page->getParse(); } catch (TPException $e) { call_user_func_array(array($status, 'fatal'), $e->getMsg()); return false; } return true; }