GettextFFSTest.php

Go to the documentation of this file.
00001 <?php
00014 class GettextFFSTest extends MediaWikiTestCase {
00015     protected $groupConfiguration;
00016 
00017     public function setUp() {
00018         parent::setUp();
00019         $this->groupConfiguration = array(
00020             'BASIC' => array(
00021                 'class' => 'FileBasedMessageGroup',
00022                 'id' => 'test-id',
00023                 'label' => 'Test Label',
00024                 'namespace' => 'NS_MEDIAWIKI',
00025                 'description' => 'Test description',
00026             ),
00027             'FILES' => array(
00028                 'class' => 'GettextFFS',
00029                 'sourcePattern' => __DIR__ . '/../data/gettext.po',
00030             ),
00031         );
00032     }
00033 
00037     public function testMangling( $expected, $item, $algo ) {
00038         $this->assertEquals( $expected, GettextFFS::generateKeyFromItem( $item, $algo ) );
00039     }
00040 
00041     public static function provideMangling() {
00042         return array(
00043             array(
00044                 '3f9999051ce0bc6e98f43224fe6ee1c220e34e49-Hello!_world_loooooooooooooooo',
00045                 array( 'id' => 'Hello! world loooooooooooooooooooooooooooooooooooooooooong', 'ctxt' => 'baa' ),
00046                 'legacy'
00047             ),
00048             array(
00049                 '3f9999-Hello!_world_loooooooooooooooo',
00050                 array( 'id' => 'Hello! world loooooooooooooooooooooooooooooooooooooooooong', 'ctxt' => 'baa' ),
00051                 'simple'
00052             ),
00053 
00054             array(
00055                 '1437e478b59e220640bf530f7e3bac93950eb8ae-"¤_=FJQ"_¤r_£_ab',
00056                 array( 'id' => '"¤#=FJQ"<>¤r £}[]}%ab', 'ctxt' => false ),
00057                 'legacy'
00058             ),
00059             array(
00060                 '1437e4-"¤#=FJQ"<>¤r_£}[]}%ab',
00061                 array( 'id' => '"¤#=FJQ"<>¤r £}[]}%ab', 'ctxt' => false ),
00062                 'simple'
00063             ),
00064 
00065         );
00066     }
00067 
00068     public function testHashing() {
00069         $item1 = array(
00070             'id' => 'a',
00071             'str' => 'b',
00072             'ctxt' => false,
00073         );
00074 
00075         $item2 = array(
00076             'id' => 'a',
00077             'str' => 'b',
00078             'ctxt' => '',
00079         );
00080 
00081         $this->assertNotEquals(
00082             GettextFFS::generateKeyFromItem( $item1, 'legacy' ),
00083             GettextFFS::generateKeyFromItem( $item2, 'legacy' ),
00084             'Empty msgctxt is different from no msgctxt'
00085         );
00086 
00087         $this->assertNotEquals(
00088             GettextFFS::generateKeyFromItem( $item1, 'simple' ),
00089             GettextFFS::generateKeyFromItem( $item2, 'simple' ),
00090             'Empty msgctxt is different from no msgctxt'
00091         );
00092 
00093         $this->assertEquals(
00094             sha1( $item1['id'] ) . '-' . $item1['id'],
00095             GettextFFS::generateKeyFromItem( $item1, 'legacy' )
00096         );
00097 
00098         $this->assertEquals(
00099             substr( sha1( $item1['id'] ), 0, 6 ) . '-' . $item1['id'],
00100             GettextFFS::generateKeyFromItem( $item1, 'simple' )
00101         );
00102     }
00103 
00104     public function testMsgctxtExport() {
00106         $group = MessageGroupBase::factory( $this->groupConfiguration );
00107         $ffs = new GettextFFS( $group );
00108 
00109         $object = new ReflectionObject( $ffs );
00110         $method = $object->getMethod( 'formatMessageBlock' );
00111         $method->setAccessible( true );
00112 
00113         $key = 'key';
00114         $m = new FatMessage( 'key', 'definition' );
00115         $m->setTranslation( 'translation' );
00116         $trans = array();
00117         $pot = array();
00118         $pluralCount = 0;
00119 
00120         $results = <<<GETTEXT
00121 #
00122 msgid "definition"
00123 msgstr "translation"
00124 
00125 #
00126 msgctxt ""
00127 msgid "definition"
00128 msgstr "translation"
00129 
00130 #
00131 msgctxt "context"
00132 msgid "definition"
00133 msgstr "translation"
00134 GETTEXT;
00135 
00136         $results = preg_split( '/\n\n/', $results );
00137 
00138         // Case 1: no context
00139         $this->assertEquals(
00140             $results[0],
00141             trim( $method->invoke( $ffs, $key, $m, $trans, $pot, $pluralCount ) )
00142         );
00143 
00144         // Case 2: empty context
00145         $pot['ctxt'] = '';
00146         $this->assertEquals(
00147             $results[1],
00148             trim( $method->invoke( $ffs, $key, $m, $trans, $pot, $pluralCount ) )
00149         );
00150 
00151         // Case 3: context
00152         $pot['ctxt'] = 'context';
00153         $this->assertEquals(
00154             $results[2],
00155             trim( $method->invoke( $ffs, $key, $m, $trans, $pot, $pluralCount ) )
00156         );
00157     }
00158 }
Generated on Tue Oct 29 00:00:24 2013 for MediaWiki Translate Extension by  doxygen 1.6.3