Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / structure / AutoLoaderTest.php
blob8674329af20d0f2cfcc25adef5618aab5c2a3373
1 <?php
3 class AutoLoaderTest extends MediaWikiTestCase {
4 protected function setUp() {
5 parent::setUp();
7 // Fancy dance to trigger a rebuild of AutoLoader::$autoloadLocalClassesLower
8 $this->mergeMwGlobalArrayValue( 'wgAutoloadLocalClasses', array(
9 'TestAutoloadedLocalClass' =>
10 __DIR__ . '/../data/autoloader/TestAutoloadedLocalClass.php',
11 'TestAutoloadedCamlClass' =>
12 __DIR__ . '/../data/autoloader/TestAutoloadedCamlClass.php',
13 'TestAutoloadedSerializedClass' =>
14 __DIR__ . '/../data/autoloader/TestAutoloadedSerializedClass.php',
15 ) );
16 AutoLoader::resetAutoloadLocalClassesLower();
18 $this->mergeMwGlobalArrayValue( 'wgAutoloadClasses', array(
19 'TestAutoloadedClass' => __DIR__ . '/../data/autoloader/TestAutoloadedClass.php',
20 ) );
23 /**
24 * Assert that there were no classes loaded that are not registered with the AutoLoader.
26 * For example foo.php having class Foo and class Bar but only registering Foo.
27 * This is important because we should not be relying on Foo being used before Bar.
29 public function testAutoLoadConfig() {
30 $results = self::checkAutoLoadConf();
32 $this->assertEquals(
33 $results['expected'],
34 $results['actual']
38 protected static function checkAutoLoadConf() {
39 global $wgAutoloadLocalClasses, $wgAutoloadClasses, $IP;
41 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
42 $expected = $wgAutoloadLocalClasses + $wgAutoloadClasses;
43 $actual = array();
45 $files = array_unique( $expected );
47 foreach ( $files as $class => $file ) {
48 // Only prefix $IP if it doesn't have it already.
49 // Generally local classes don't have it, and those from extensions and test suites do.
50 if ( substr( $file, 0, 1 ) != '/' && substr( $file, 1, 1 ) != ':' ) {
51 $filePath = "$IP/$file";
52 } else {
53 $filePath = $file;
56 if ( !file_exists( $filePath ) ) {
57 $actual[$class] = "[file '$filePath' does not exist]";
58 continue;
61 MediaWiki\suppressWarnings();
62 $contents = file_get_contents( $filePath );
63 MediaWiki\restoreWarnings();
65 if ( $contents === false ) {
66 $actual[$class] = "[couldn't read file '$filePath']";
67 continue;
70 // We could use token_get_all() here, but this is faster
71 $matches = array();
72 preg_match_all( '/
73 ^ [\t ]* (?:
74 (?:final\s+)? (?:abstract\s+)? (?:class|interface) \s+
75 (?P<class> [a-zA-Z0-9_]+)
77 class_alias \s* \( \s*
78 ([\'"]) (?P<original> [^\'"]+) \g{-2} \s* , \s*
79 ([\'"]) (?P<alias> [^\'"]+ ) \g{-2} \s*
80 \) \s* ;
82 /imx', $contents, $matches, PREG_SET_ORDER );
84 $namespaceMatch = array();
85 preg_match( '/
86 ^ [\t ]*
87 namespace \s+
88 ([a-zA-Z0-9_]+(\\\\[a-zA-Z0-9_]+)*)
89 \s* ;
90 /imx', $contents, $namespaceMatch );
91 $fileNamespace = $namespaceMatch ? $namespaceMatch[1] . '\\' : '';
93 $classesInFile = array();
94 $aliasesInFile = array();
96 foreach ( $matches as $match ) {
97 if ( !empty( $match['class'] ) ) {
98 $class = $fileNamespace . $match['class'];
99 $actual[$class] = $file;
100 $classesInFile[$class] = true;
101 } else {
102 $aliasesInFile[$match['alias']] = $match['original'];
106 // Only accept aliases for classes in the same file, because for correct
107 // behavior, all aliases for a class must be set up when the class is loaded
108 // (see <https://bugs.php.net/bug.php?id=61422>).
109 foreach ( $aliasesInFile as $alias => $class ) {
110 if ( isset( $classesInFile[$class] ) ) {
111 $actual[$alias] = $file;
112 } else {
113 $actual[$alias] = "[original class not in $file]";
118 return array(
119 'expected' => $expected,
120 'actual' => $actual,
124 function testCoreClass() {
125 $this->assertTrue( class_exists( 'TestAutoloadedLocalClass' ) );
128 function testExtensionClass() {
129 $this->assertTrue( class_exists( 'TestAutoloadedClass' ) );
132 function testWrongCaseClass() {
133 $this->setMwGlobals( 'wgAutoloadAttemptLowercase', true );
135 $this->assertTrue( class_exists( 'testautoLoadedcamlCLASS' ) );
138 function testWrongCaseSerializedClass() {
139 $this->setMwGlobals( 'wgAutoloadAttemptLowercase', true );
141 $dummyCereal = 'O:29:"testautoloadedserializedclass":0:{}';
142 $uncerealized = unserialize( $dummyCereal );
143 $this->assertFalse( $uncerealized instanceof __PHP_Incomplete_Class,
144 "unserialize() can load classes case-insensitively." );