From 1bb00307a8ecfa9ecfa222abf6932ed91271f9c8 Mon Sep 17 00:00:00 2001 From: Brian Wolff Date: Wed, 5 Jun 2013 13:14:27 -0300 Subject: [PATCH] Make generating Parser test class names more robust 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 --- .../includes/parser/MediaWikiParserTest.php | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/includes/parser/MediaWikiParserTest.php b/tests/phpunit/includes/parser/MediaWikiParserTest.php index a450972334..df891f5a4b 100644 --- a/tests/phpunit/includes/parser/MediaWikiParserTest.php +++ b/tests/phpunit/includes/parser/MediaWikiParserTest.php @@ -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 = <<