StringMatcherTest.php

Go to the documentation of this file.
00001 <?php
00009 class StringMatcherTest extends MediaWikiTestCase {
00013     public function testKeyPrefixing( $key, $expected, $prefix, $rules, $comment ) {
00014         $matcher = new StringMatcher( $prefix, $rules );
00015         $mangled = $matcher->mangle( $key );
00016         $title = Title::makeTitleSafe( NS_MEDIAWIKI, $mangled );
00017         $this->assertInstanceOf( 'Title', $title, "Key '$mangled' did not produce valid title" );
00018         $unmangled = $matcher->unMangle( $mangled );
00019         $this->assertEquals( $key, $unmangled, 'Mangling is reversable' );
00020         $this->assertEquals( $expected, $mangled, 'Message is prefixed correctly' );
00021     }
00022 
00023     public function messageKeyProvider() {
00024         // The fourth parameter causes the key to be prefixed or unprefixed
00025         $keys = array(
00026             array( 'key', 'p-key', 'p-', array( 'key' ), 'Exact match' ),
00027             array( 'key', 'key', 'p-', array( 'bar' ), 'Exact not match' ),
00028             array( 'key', 'p-key', 'p-', array( 'k*' ), 'Prefix match' ),
00029             array( 'key', 'key', 'p-', array( 'b*' ), 'Prefix not match' ),
00030             array( 'key', 'p-key', 'p-', array( '*y' ), 'Suffix match' ),
00031             array( 'key', 'key', 'p-', array( '*r' ), 'Suffix not match' ),
00032             array( 'key', 'p-key', 'p-', array( 'k*y' ), 'Wildcard match' ),
00033             array( 'key', 'key', 'p-', array( '*a*' ), 'Wildcard not match' ),
00034             array( 'key', 'p-key', 'p-', array( 'key', '*ey', 'ke*' ), 'Multiple rules match' ),
00035             array( 'key', 'key', 'p-', array( '*a*', '*ar', 'ba*' ), 'Multiple rules not match' ),
00036             array( 'key', 'p-key', 'p-', array( '*' ), 'All match' ),
00037             array(
00038                 '[k.ssa]', 'p-=5Bk.ssa=5D', 'p-', array( '[k.s*' ),
00039                 'Message key with special chars'
00040             ),
00041             array(
00042                 '[kissa]', '=5Bkissa=5D', 'p-', array( '[k.s*' ),
00043                 'Message key with special chars'
00044             ),
00045         );
00046 
00047         return $keys;
00048     }
00049 
00053     public function testKeyMangling( $key, $comment ) {
00054         $matcher = StringMatcher::emptyMatcher();
00055         $mangled = $matcher->mangle( $key );
00056 
00057         $title = Title::makeTitleSafe( NS_MEDIAWIKI, $mangled );
00058         $this->assertInstanceOf( 'Title', $title, "Key '$mangled' did not produce a valid title" );
00059         $unmangled = $matcher->unMangle( $mangled );
00060         $this->assertEquals( $key, $unmangled, 'Mangling is reversible' );
00061     }
00062 
00066     public function testKeyManglingWithPrefixing( $key, $comment ) {
00067         $matcher = new StringMatcher( 'prefix', array( '*' ) );
00068         $mangled = $matcher->mangle( $key );
00069         $title = Title::makeTitleSafe( NS_MEDIAWIKI, $mangled );
00070         $this->assertInstanceOf( 'Title', $title, "Key '$mangled' did not produce a valid title" );
00071 
00072         $unmangled = $matcher->unMangle( $mangled );
00073         $this->assertEquals( $key, $unmangled, 'Mangling is reversible' );
00074     }
00075 
00076     public function problematicMessageKeyProvider() {
00077         $keys = array(
00078             array( 'key', 'simple string' ),
00079             array( 'key[]', 'string with brackets' ),
00080             array( 'key%AB', 'string with invalid url encoding' ),
00081             array( 'key&amp;', 'string with html entity' ),
00082             array( 'key=2A', 'string with fake escaping' ),
00083             array( 'abcdefgh', 'string with fake escaping' ),
00084             array( 'общегосударственные', 'Unicode string' ),
00085         );
00086 
00087         // Add tests for ranges of exotic ASCII characters
00088         foreach ( range( 0, 7 ) as $k ) {
00089             $key = '';
00090             foreach ( range( 0, 15 ) as $c ) {
00091                 $key .= chr( $c + 16 * $k );
00092             }
00093 
00094             $start = $k * 16;
00095             $end = $start + 16;
00096             $keys[] = array( $key, "ASCII range $start..$end" );
00097         }
00098 
00099         return $keys;
00100     }
00101 }
Generated on Tue Oct 29 00:00:24 2013 for MediaWiki Translate Extension by  doxygen 1.6.3