From 7c6c05a45926bd47eefc21ef75c9a3af83ae6cfc Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Wed, 7 Nov 2012 02:27:30 +0100 Subject: [PATCH] (bug 41833) Test: Add AutoLoaderTest. * Move scattered pieces from tests/* in main AutoLoader.php into tests/TestsAutoLoader.php. Verified with: 'ack -Q i --ignore-dir tests/phpunit/ ' on mediawiki/core that these classes are not used outside tests/phpunit/. * Moved entry for maintenance/backup.inc to the main AutoLoader. * Refactored assertion logic in maintenance/checkAutoLoader.php into a public static method used in it's execute method and in the (new) AutoLoaderTest suite. * The new test was immediately failing, added missing classes and removed old ones that don't exist. And CheckAutoLoader itself, so that it can actually be used in AutoLoaderTest.php * Per discussion on Gerrit, moved the logic into the unit test instead of refactoring the maintenance script, we no longer need the maintenance script. * Fixed the regex to also detect abstract, final and interface. The test was failing badly, claiming many classes did not exist. * Improved the logic to also catch entries in the AutoLoader configuration for inexisting classes in existing files. So far it only catched entries with wrong files and missing entries for classes in known files. An entry like "BlablaSomethingHere => includes/Action.php" did not emit any kind of warning. The refactored logic builds a reverse index and uses a simple assertEquals to find any inconsistencies (whatever the cause). Change-Id: I0a307f23175d52345180cdfc7c2d5e172536be1b --- includes/AutoLoader.php | 55 +++------------------ maintenance/checkAutoLoader.php | 63 ----------------------- tests/TestsAutoLoader.php | 85 +++++++++++++++++++++++--------- tests/phpunit/AutoLoaderTest.php | 51 +++++++++++++++++++ 4 files changed, 120 insertions(+), 134 deletions(-) delete mode 100644 maintenance/checkAutoLoader.php create mode 100644 tests/phpunit/AutoLoaderTest.php diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index dcaaf5a8d4..a49d901adc 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -152,6 +152,7 @@ $wgAutoloadLocalClasses = array( 'IndexPager' => 'includes/Pager.php', 'Interwiki' => 'includes/interwiki/Interwiki.php', 'IP' => 'includes/IP.php', + 'LCStore' => 'includes/LocalisationCache.php', 'LCStore_Accel' => 'includes/LocalisationCache.php', 'LCStore_CDB' => 'includes/LocalisationCache.php', 'LCStore_DB' => 'includes/LocalisationCache.php', @@ -204,6 +205,7 @@ $wgAutoloadLocalClasses = array( 'RdfMetaData' => 'includes/Metadata.php', 'ReadOnlyError' => 'includes/Exception.php', 'RecentChange' => 'includes/RecentChange.php', + 'RedirectSpecialArticle' => 'includes/SpecialPage.php', 'RedirectSpecialPage' => 'includes/SpecialPage.php', 'RegexlikeReplacer' => 'includes/StringUtils.php', 'ReplacementArray' => 'includes/StringUtils.php', @@ -577,7 +579,6 @@ $wgAutoloadLocalClasses = array( 'CopyFileOp' => 'includes/filebackend/FileOp.php', 'MoveFileOp' => 'includes/filebackend/FileOp.php', 'DeleteFileOp' => 'includes/filebackend/FileOp.php', - 'ConcatenateFileOp' => 'includes/filebackend/FileOp.php', 'CreateFileOp' => 'includes/filebackend/FileOp.php', 'NullFileOp' => 'includes/filebackend/FileOp.php', @@ -613,7 +614,6 @@ $wgAutoloadLocalClasses = array( 'Ibm_db2Updater' => 'includes/installer/Ibm_db2Updater.php', 'InstallDocFormatter' => 'includes/installer/InstallDocFormatter.php', 'Installer' => 'includes/installer/Installer.php', - 'LBFactory_InstallerFake' => 'includes/installer/Installer.php', 'LocalSettingsGenerator' => 'includes/installer/LocalSettingsGenerator.php', 'MysqlInstaller' => 'includes/installer/MysqlInstaller.php', 'MysqlUpdater' => 'includes/installer/MysqlUpdater.php', @@ -850,7 +850,6 @@ $wgAutoloadLocalClasses = array( 'RevDel_LogList' => 'includes/revisiondelete/RevisionDelete.php', 'RevDel_RevisionItem' => 'includes/revisiondelete/RevisionDelete.php', 'RevDel_RevisionList' => 'includes/revisiondelete/RevisionDelete.php', - 'RevisionDelete' => 'includes/revisiondelete/RevisionDelete.php', 'RevisionDeleter' => 'includes/revisiondelete/RevisionDeleter.php', 'RevisionDeleteUser' => 'includes/revisiondelete/RevisionDeleteUser.php', @@ -894,8 +893,6 @@ $wgAutoloadLocalClasses = array( 'BrokenRedirectsPage' => 'includes/specials/SpecialBrokenRedirects.php', 'CategoryPager' => 'includes/specials/SpecialCategories.php', 'ContribsPager' => 'includes/specials/SpecialContributions.php', - 'DBLockForm' => 'includes/specials/SpecialLockdb.php', - 'DBUnlockForm' => 'includes/specials/SpecialUnlockdb.php', 'DeadendPagesPage' => 'includes/specials/SpecialDeadendpages.php', 'DeletedContribsPager' => 'includes/specials/SpecialDeletedContributions.php', 'DeletedContributionsPage' => 'includes/specials/SpecialDeletedContributions.php', @@ -960,7 +957,6 @@ $wgAutoloadLocalClasses = array( 'SpecialLockdb' => 'includes/specials/SpecialLockdb.php', 'SpecialLog' => 'includes/specials/SpecialLog.php', 'SpecialMergeHistory' => 'includes/specials/SpecialMergeHistory.php', - 'SpecialMostlinkedtemplates' => 'includes/specials/SpecialMostlinkedtemplates.php', 'SpecialNewFiles' => 'includes/specials/SpecialNewimages.php', 'SpecialNewpages' => 'includes/specials/SpecialNewpages.php', 'SpecialPasswordReset' => 'includes/specials/SpecialPasswordReset.php', @@ -1020,7 +1016,6 @@ $wgAutoloadLocalClasses = array( 'UploadFromUrl' => 'includes/upload/UploadFromUrl.php', 'UploadStash' => 'includes/upload/UploadStash.php', 'UploadStashBadPathException' => 'includes/upload/UploadStash.php', - 'UploadStashBadVersionException' => 'includes/upload/UploadStash.php', 'UploadStashFile' => 'includes/upload/UploadStash.php', 'UploadStashFileException' => 'includes/upload/UploadStash.php', 'UploadStashFileNotFoundException' => 'includes/upload/UploadStash.php', @@ -1044,14 +1039,17 @@ $wgAutoloadLocalClasses = array( 'CLDRPluralRuleError' => 'languages/utils/CLDRPluralRuleEvaluator.php', # maintenance + 'BackupDumper' => 'maintenance/backup.inc', 'ConvertLinks' => 'maintenance/convertLinks.php', 'DeleteArchivedFilesImplementation' => 'maintenance/deleteArchivedFiles.inc', 'DeleteArchivedRevisionsImplementation' => 'maintenance/deleteArchivedRevisions.inc', 'DeleteDefaultMessages' => 'maintenance/deleteDefaultMessages.php', + 'DumpDBZip2Output' => 'maintenance/backup.inc', + 'ExportProgressFilter' => 'maintenance/backup.inc', 'FakeMaintenance' => 'maintenance/Maintenance.php', + 'FixExtLinksProtocolRelative' => 'maintenance/fixExtLinksProtocolRelative.php', 'LoggedUpdateMaintenance' => 'maintenance/Maintenance.php', 'Maintenance' => 'maintenance/Maintenance.php', - 'FixExtLinksProtocolRelative' => 'maintenance/fixExtLinksProtocolRelative.php', 'PopulateCategory' => 'maintenance/populateCategory.php', 'PopulateImageSha1' => 'maintenance/populateImageSha1.php', 'PopulateFilearchiveSha1' => 'maintenance/populateFilearchiveSha1.php', @@ -1084,47 +1082,6 @@ $wgAutoloadLocalClasses = array( 'InstallerOverrides' => 'mw-config/overrides.php', 'MyLocalSettingsGenerator' => 'mw-config/overrides.php', - # tests - 'DbTestPreviewer' => 'tests/testHelpers.inc', - 'DbTestRecorder' => 'tests/testHelpers.inc', - 'DelayedParserTest' => 'tests/testHelpers.inc', - 'TestFileIterator' => 'tests/testHelpers.inc', - 'TestRecorder' => 'tests/testHelpers.inc', - - # tests/phpunit - 'RevisionStorageTest' => 'tests/phpunit/includes/RevisionStorageTest.php', - 'WikiPageTest' => 'tests/phpunit/includes/WikiPageTest.php', - - # tests/phpunit/content - 'DummyContentHandlerForTesting' => 'tests/phpunit/includes/content/ContentHandlerTest.php', - 'DummyContentForTesting' => 'tests/phpunit/includes/content/ContentHandlerTest.php', - 'JavascriptContentTest' => 'tests/phpunit/includes/content/JavascriptContentTest.php', - 'TextContentTest' => 'tests/phpunit/includes/content/TextContentTest.php', - - # tests/phpunit/includes - 'GenericArrayObjectTest' => 'tests/phpunit/includes/libs/GenericArrayObjectTest.php', - - # tests/phpunit/includes/db - 'ORMRowTest' => 'tests/phpunit/includes/db/ORMRowTest.php', - - # tests/phpunit/includes/site - 'SiteObjectTest' => 'tests/phpunit/includes/site/SiteObjectTest.php', - 'TestSites' => 'tests/phpunit/includes/site/TestSites.php', - - # tests/parser - 'ParserTest' => 'tests/parser/parserTest.inc', - 'ParserTestParserHook' => 'tests/parser/parserTestsParserHook.php', - - # tests/selenium - 'Selenium' => 'tests/selenium/Selenium.php', - 'SeleniumLoader' => 'tests/selenium/SeleniumLoader.php', - 'SeleniumTestCase' => 'tests/selenium/SeleniumTestCase.php', - 'SeleniumTestConsoleLogger' => 'tests/selenium/SeleniumTestConsoleLogger.php', - 'SeleniumTestHTMLLogger' => 'tests/selenium/SeleniumTestHTMLLogger.php', - 'SeleniumTestListener' => 'tests/selenium/SeleniumTestListener.php', - 'SeleniumTestSuite' => 'tests/selenium/SeleniumTestSuite.php', - 'SeleniumConfig' => 'tests/selenium/SeleniumConfig.php', - # skins 'CologneBlueTemplate' => 'skins/CologneBlue.php', 'ModernTemplate' => 'skins/Modern.php', diff --git a/maintenance/checkAutoLoader.php b/maintenance/checkAutoLoader.php deleted file mode 100644 index 8d0e442bdf..0000000000 --- a/maintenance/checkAutoLoader.php +++ /dev/null @@ -1,63 +0,0 @@ -mDescription = "AutoLoader sanity checks"; - } - public function execute() { - global $wgAutoloadLocalClasses, $IP; - $files = array_unique( $wgAutoloadLocalClasses ); - - foreach ( $files as $file ) { - if ( function_exists( 'parsekit_compile_file' ) ) { - $parseInfo = parsekit_compile_file( "$IP/$file" ); - $classes = array_keys( $parseInfo['class_table'] ); - } else { - $contents = file_get_contents( "$IP/$file" ); - $m = array(); - preg_match_all( '/\n\s*class\s+([a-zA-Z0-9_]+)/', $contents, $m, PREG_PATTERN_ORDER ); - $classes = $m[1]; - } - foreach ( $classes as $class ) { - if ( !isset( $wgAutoloadLocalClasses[$class] ) ) { - // printf( "%-50s Unlisted, in %s\n", $class, $file ); - $this->output( "\t'$class' => '$file',\n" ); - } elseif ( $wgAutoloadLocalClasses[$class] !== $file ) { - $this->output( "$class: Wrong file: found in $file, listed in " . $wgAutoloadLocalClasses[$class] . "\n" ); - } - } - } - } -} - -$maintClass = "CheckAutoLoader"; -require_once( RUN_MAINTENANCE_IF_MAIN ); diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index 7a835d6c66..48a758e18e 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -22,26 +22,28 @@ */ global $wgAutoloadClasses; -$testFolder = __DIR__; +$testDir = __DIR__; $wgAutoloadClasses += array( - //PHPUnit - 'MediaWikiTestCase' => "$testFolder/phpunit/MediaWikiTestCase.php", - 'MediaWikiPHPUnitCommand' => "$testFolder/phpunit/MediaWikiPHPUnitCommand.php", - 'MediaWikiLangTestCase' => "$testFolder/phpunit/MediaWikiLangTestCase.php", - 'NewParserTest' => "$testFolder/phpunit/includes/parser/NewParserTest.php", + # tests + 'DbTestPreviewer' => "$testDir/testHelpers.inc", + 'DbTestRecorder' => "$testDir/testHelpers.inc", + 'DelayedParserTest' => "$testDir/testHelpers.inc", + 'TestFileIterator' => "$testDir/testHelpers.inc", + 'TestRecorder' => "$testDir/testHelpers.inc", - //includes - 'BlockTest' => "$testFolder/phpunit/includes/BlockTest.php", + # tests/phpunit + 'MediaWikiTestCase' => "$testDir/phpunit/MediaWikiTestCase.php", + 'MediaWikiPHPUnitCommand' => "$testDir/phpunit/MediaWikiPHPUnitCommand.php", + 'MediaWikiLangTestCase' => "$testDir/phpunit/MediaWikiLangTestCase.php", + 'MediaWikiProvide' => "$testDir/phpunit/includes/Providers.php", + 'TestUser' => "$testDir/phpunit/includes/TestUser.php", - //API - 'ApiFormatTestBase' => "$testFolder/phpunit/includes/api/format/ApiFormatTestBase.php", - 'ApiTestCase' => "$testFolder/phpunit/includes/api/ApiTestCase.php", - 'TestUser' => "$testFolder/phpunit/includes/TestUser.php", - 'MockApi' => "$testFolder/phpunit/includes/api/ApiTestCase.php", - 'RandomImageGenerator' => "$testFolder/phpunit/includes/api/RandomImageGenerator.php", - 'UserWrapper' => "$testFolder/phpunit/includes/api/ApiTestCase.php", + # tests/phpunit/includes + 'BlockTest' => "$testDir/phpunit/includes/BlockTest.php", + 'RevisionStorageTest' => "$testDir/phpunit/includes/RevisionStorageTest.php", + 'WikiPageTest' => "$testDir/phpunit/includes/WikiPageTest.php", //db 'ORMTableTest' => "$testFolder/phpunit/includes/db/ORMTableTest.php", @@ -49,14 +51,53 @@ $wgAutoloadClasses += array( //Selenium 'SeleniumTestConstants' => "$testFolder/selenium/SeleniumTestConstants.php", - //maintenance - 'DumpTestCase' => "$testFolder/phpunit/maintenance/DumpTestCase.php", - 'BackupDumper' => "$testFolder/../maintenance/backup.inc", + # tests/phpunit/includes/api + 'ApiFormatTestBase' => "$testDir/phpunit/includes/api/format/ApiFormatTestBase.php", + 'ApiTestCase' => "$testDir/phpunit/includes/api/ApiTestCase.php", + 'ApiTestContext' => "$testDir/phpunit/includes/api/ApiTestCase.php", + 'MockApi' => "$testDir/phpunit/includes/api/ApiTestCase.php", + 'RandomImageGenerator' => "$testDir/phpunit/includes/api/RandomImageGenerator.php", + 'UserWrapper' => "$testDir/phpunit/includes/api/ApiTestCase.php", - //language - 'LanguageClassesTestCase' => "$testFolder/phpunit/languages/LanguageClassesTestCase.php", + # tests/phpunit/includes/content + 'DummyContentHandlerForTesting' => "$testDir/phpunit/includes/content/ContentHandlerTest.php", + 'DummyContentForTesting' => "$testDir/phpunit/includes/content/ContentHandlerTest.php", + 'ContentHandlerTest' => "$testDir/phpunit/includes/content/ContentHandlerTest.php", + 'JavascriptContentTest' => "$testDir/phpunit/includes/content/JavascriptContentTest.php", + 'TextContentTest' => "$testDir/phpunit/includes/content/TextContentTest.php", - //Generic providers - 'MediaWikiProvide' => "$testFolder/phpunit/includes/Providers.php", + # tests/phpunit/includes/db + 'ORMRowTest' => "$testDir/phpunit/includes/db/ORMRowTest.php", + + # tests/phpunit/includes/parser + 'NewParserTest' => "$testDir/phpunit/includes/parser/NewParserTest.php", + + # tests/phpunit/includes/libs + 'GenericArrayObjectTest' => "$testDir/phpunit/includes/libs/GenericArrayObjectTest.php", + + # tests/phpunit/includes/site + 'SiteObjectTest' => "$testDir/phpunit/includes/site/SiteObjectTest.php", + 'TestSites' => "$testDir/phpunit/includes/site/TestSites.php", + + # tests/phpunit/languages + 'LanguageClassesTestCase' => "$testDir/phpunit/languages/LanguageClassesTestCase.php", + + # tests/phpunit/maintenance + 'DumpTestCase' => "$testDir/phpunit/maintenance/DumpTestCase.php", + + # tests/parser + 'ParserTest' => "$testDir/parser/parserTest.inc", + 'ParserTestParserHook' => "$testDir/parser/parserTestsParserHook.php", + + # tests/selenium + 'Selenium' => "$testDir/selenium/Selenium.php", + 'SeleniumLoader' => "$testDir/selenium/SeleniumLoader.php", + 'SeleniumTestCase' => "$testDir/selenium/SeleniumTestCase.php", + 'SeleniumTestConsoleLogger' => "$testDir/selenium/SeleniumTestConsoleLogger.php", + 'SeleniumTestConstants' => "$testDir/selenium/SeleniumTestConstants.php", + 'SeleniumTestHTMLLogger' => "$testDir/selenium/SeleniumTestHTMLLogger.php", + 'SeleniumTestListener' => "$testDir/selenium/SeleniumTestListener.php", + 'SeleniumTestSuite' => "$testDir/selenium/SeleniumTestSuite.php", + 'SeleniumConfig' => "$testDir/selenium/SeleniumConfig.php", ); diff --git a/tests/phpunit/AutoLoaderTest.php b/tests/phpunit/AutoLoaderTest.php new file mode 100644 index 0000000000..c8f38685a1 --- /dev/null +++ b/tests/phpunit/AutoLoaderTest.php @@ -0,0 +1,51 @@ +assertEquals( + $results['expected'], + $results['actual'] + ); + } + + protected static function checkAutoLoadConf() { + global $wgAutoloadLocalClasses, $wgAutoloadClasses, $IP; + static $supportsParsekit; + $supportsParsekit = function_exists( 'parsekit_compile_file' ); + + // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php + $expected = $wgAutoloadLocalClasses + $wgAutoloadClasses; + $actual = array(); + + $files = array_unique( $expected ); + + foreach ( $files as $file ) { + // Only prefix $IP if it doesn't have it already. + // Generally local classes don't have it, and those from extensions and test suites do. + if ( substr( $file, 0, 1 ) != '/' && substr( $file, 1, 1 ) != ':' ) { + $filePath = "$IP/$file"; + } else { + $filePath = $file; + } + if ( $supportsParsekit ) { + $parseInfo = parsekit_compile_file( "$filePath" ); + $classes = array_keys( $parseInfo['class_table'] ); + } else { + $contents = file_get_contents( "$filePath" ); + $m = array(); + preg_match_all( '/\n\s*(?:final)?\s*(?:abstract)?\s*(?:class|interface)\s+([a-zA-Z0-9_]+)/', $contents, $m, PREG_PATTERN_ORDER ); + $classes = $m[1]; + } + foreach ( $classes as $class ) { + $actual[$class] = $file; + } + } + + return array( + 'expected' => $expected, + 'actual' => $actual, + ); + } +} -- 2.20.1