Make generating Parser test class names more robust
authorBrian Wolff <bawolff+wn@gmail.com>
Wed, 5 Jun 2013 16:14:27 +0000 (13:14 -0300)
committerReedy <reedy@wikimedia.org>
Tue, 16 Sep 2014 20:38:04 +0000 (20:38 +0000)
This includes the extension name, and it also does much
more stringent validation. In the (now rather unlikely)
event of a duplicate name, it will append a number.

This is important, as it is very confusing when this bug strikes.
There exists extensions like CharRangeSpan which will trigger this bug.

Bug: 42174
Change-Id: Idf14b4cbdb8ec103340d48855e0361acf707b101

tests/phpunit/includes/parser/MediaWikiParserTest.php

index a450972..df891f5 100644 (file)
@@ -83,14 +83,28 @@ class MediaWikiParserTest {
                        . implode( ' ', $filesToTest ) );
 
                $suite = new PHPUnit_Framework_TestSuite;
+               $testList = array();
+               $counter = 0;
                foreach ( $filesToTest as $fileName ) {
-                       $testsName = basename( $fileName, '.txt' );
+                       // Call the highest level directory the extension name.
+                       // It may or may not actually be, but it should be close
+                       // enough to cause there to be separate names for different
+                       // things, which is good enough for our purposes.
+                       $extensionName = basename( dirname( $fileName ) );
+                       $testsName = $extensionName . '⁄' . basename( $fileName, '.txt' );
                        $escapedFileName = strtr( $fileName, array( "'" => "\\'", '\\' => '\\\\' ) );
-                       /* This used to be ucfirst( basename( dirname( $filename ) ) )
-                        * and then was ucfirst( basename( $filename, '.txt' )
-                        * but that didn't work with names like foo.tests.txt
-                        */
-                       $parserTestClassName = str_replace( '.', '_', ucfirst( $testsName ) );
+                       $parserTestClassName = ucfirst( $testsName );
+                       // Official spec for class names: http://php.net/manual/en/language.oop5.basic.php
+                       // Prepend 'ParserTest_' to be paranoid about it not starting with a number
+                       $parserTestClassName = 'ParserTest_' . preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '_', $parserTestClassName );
+                       if ( isset( $testList[$parserTestClassName] ) ) {
+                               // If a conflict happens, gives a very unclear fatal.
+                               // So as a last ditch effort to prevent that eventuality, if there
+                               // is a conflict, append a number.
+                               $counter++;
+                               $parserTestClassName .= $counter;
+                       }
+                       $testList[$parserTestClassName] = true;
                        $parserTestClassDefinition = <<<EOT
 /**
  * @group Database