Commit graph

30 commits

Author SHA1 Message Date
Matěj Suchánek
676fcf4379 Replace substr with cleaner string methods
Use str_starts_with, str_ends_with or string offset where appropriate.

This fixes a bug in MimeAnalyzer where the "UTF-16LE" header could not
be identified because of wrong constant. This is the exact type of bug
that the new functions can avoid.

Change-Id: I9f30881e7e895f011db29cf5dcbe43bc4f341062
2023-05-20 15:40:21 +02:00
Máté Szabó
c04cb6f607 Include core PSR-4 classes in the generated classmap
It appears that autoloading classes via MediaWiki's PSR-4 autoloader has
a not insignificant performance penalty, especially when hundreds of
PSR-4 classes like HookRunner's hook interfaces are autoloaded. Using a
classmap autoloader, like we already do for PSR-4 classes from Composer
dependencies, is a potential way to reduce the performance impact
here.[1]

For core classes, this can be done by simply not excluding PSR-4 classes
in AutoloadGenerator, causing it to include appropriate mappings in the
generated autoload.php classmap. I had to exclude one class_alias()
declared in Result.php from the classmap with a NO_AUTOLOAD stanza,
because including it broke AutoLoaderStructureTest's assertion that all
aliases should be defined in the same file as the aliased class.
Assuming this is still an issue, this would already have been a problem
because the test was previously skipping every PSR-4 class. Excluding
this file via NO_AUTOLOAD just restores that status quo.
----
[1] https://phabricator.wikimedia.org/T274041#8358399

Bug: T274041
Change-Id: I0aa62c944d874bf7a9f3a240e72e58fe6a887b28
2022-11-08 12:13:32 +01:00
Daimona Eaytoy
1c60cafab4 Add @coversNothing to all structure tests
With PHPUnit 9, tests without @covers tags are considered risky and emit
warnings. Not having @covers is bad practice anyway, so use
@coversNothing instead to make the intention clear, and re-enable the
phpcs rule.

Also rewrite an assertion in ResourcesTest that was bothering me.

Bug: T243600
Change-Id: I6dd683f93b6b2faed5f107be2ca7860602277fbc
2022-10-07 03:21:54 +02:00
jenkins-bot
783a763bd2 Merge "AutoLoader: provide methods for registering classes" 2022-05-10 06:05:52 +00:00
daniel
37af531721 AutoLoader: provide methods for registering classes
This introduces static methods for registering classes and namespaces,
so we don't have to mess with global variables and static members
fields.

Bug: T240535
Change-Id: I1abb27bd70898d04c9253e0b7467447fa96d15a4
2022-05-10 13:55:53 +10:00
Aaron Schulz
774abe3ba6 Split out slow ScopeStructureTest for easier paratest parallelization
Change-Id: I677f27847e635003c932102b3876fcf81d270c88
2022-05-10 11:18:24 +10:00
Timo Tijhof
128debb64b tests: Change use of AtEase to at operator
Follows-up I361fde0de7f4406bce6ed075ed397effa5be3359.

Per T253461, not mass-changing source code, but the use of the native
error silencing operator (@) is especially useful in tests because:

1. It requires any/all statements to be explicitly marked. The
   suppressWarnings/restoreWarnings sections encourage developers to
   be "lazy" and thus encapsulate more than needed if there are multiple
   ones near each other, which would ignore potentially important
   warnings in a test case, which is generally exactly the time when
   it is really useful to get warnings etc.

2. It avoids leaking state, for example in LBFactoryTest the
   assertFalse call would throw a PHPUnit assertion error (not meant
   to be caught by the local catch), and thus won't reach
   AtEase::restoreWarnings. This then causes later code to end up
   in a mismatching state and creates a confusing error_reporting
   state.

See .phpcs.xml, where the at operator is allowed for all test code.

Change-Id: I68d1725d685e0a7586468bc9de6dc29ceea31b8a
2022-02-24 21:29:51 +00:00
Reedy
12aae45101 tests: Replace some usages of Wikimedia\(suppress|restore)Warnings()
Change-Id: I361fde0de7f4406bce6ed075ed397effa5be3359
2022-02-24 12:55:25 +00:00
Kosta Harlan
0c2cc804e1 phpunit: Use is_file/is_dir instead of file_exists
Yes, it's a micro-optimization. See https://bugs.php.net/bug.php?id=78285
and https://thephp.cc/articles/caching-makes-everything-faster-right
for more info.

Change-Id: Ib8e8e9794e15066476f35cdb1236df8b983274d6
2022-01-03 21:47:56 +01:00
Tim Starling
20d06b34bb Safer autoloading with respect to file-scope code
Many files were in the autoloader despite having potentially harmful
file-scope code.

* Exclude all CommandLineInc maintenance scripts from the autoloader.
* Introduce  "NO_AUTOLOAD" tag which excludes the file containing it
  from the autoloader. Use it on CommandLineInc.php and a few
  suspicious-looking files without classes in case they are refactored
  to add classes in the future.
* Add a test which parses all non-PSR4 class files and confirms that
  they do not contain dangerous file-scope code. It's slow (15s) but
  its results were enlightening.
* Several maintenance scripts define constants in the file scope,
  intending to modify the behaviour of MediaWiki. Either move the
  define() to a later setup function, or protect with NO_AUTOLOAD.
* Use require_once consistently with Maintenance.php and
  doMaintenance.php, per the original convention which is supposed to
  allow one maintenance script to use the class of another maintenance
  script. Using require breaks autoloading of these maintenance class
  files.
* When Maintenance.php is included, check if MediaWiki has already
  started, and if so, return early. Revert the fix for T250003 which
  is incompatible with this safety measure. Hopefully it was superseded
  by splitting out the class file.
* In runScript.php add a redundant PHP_SAPI check since it does some
  things in file-scope code before any other check will be run.
* Change the if(false) class_alias(...) to something more hackish and
  more compatible with the new test.
* Some site-related scripts found Maintenance.php in a non-standard way.
  Use the standard way.
* fileOpPerfTest.php called error_reporting(). Probably debugging code
  left in; removed.
* Moved mediawiki.compress.7z registration from the class file to the
  caller.

Change-Id: I1b1be90343a5ab678df6f1b1bdd03319dcf6537f
2021-01-11 11:59:36 +11:00
Umherirrender
339f86629f AutoLoaderStructureTest: Re-write slashes so test passes on Windows
A difference between UNIX and Windows is the dir separator,
which makes this test failing, because the file name does not match to
filter out the psr4 loaded classes to just keep the alias, which must be
part of the autoloader class list.

1) AutoLoaderStructureTest::testAutoLoadConfig
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
     'ParserTestFileSuite' => '[...]...te.php'
     'ParserTestTopLevelSuite' => '[...]...te.php'
     'SuiteEventsTrait' => '[...]...it.php'
+    'MediaWiki\Block\DatabaseBlock' => '[...]...ck.php'
+    'MediaWiki\SpecialPage\SpecialPageFactory' => '[...]...ry.php'
+    'MediaWiki\Revision\IncompleteRevisionException' =>
'[...]...on.php'
+    'MediaWiki\Revision\MutableRevisionRecord' => '[...]...rd.php'
+    'MediaWiki\Revision\MutableRevisionSlots' => '[...]...ts.php'
+    'MediaWiki\Revision\RevisionAccessException' => '[...]...on.php'
+    'MediaWiki\Revision\RevisionArchiveRecord' => '[...]...rd.php'
+    'MediaWiki\Revision\RevisionFactory' => '[...]...ry.php'
+    'MediaWiki\Revision\RevisionLookup' => '[...]...up.php'
+    'MediaWiki\Revision\RevisionRecord' => '[...]...rd.php'
+    'MediaWiki\Revision\RevisionSlots' => '[...]...ts.php'
+    'MediaWiki\Revision\RevisionStore' => '[...]...re.php'
+    'MediaWiki\Revision\RevisionStoreRecord' => '[...]...rd.php'
+    'MediaWiki\Revision\SlotRecord' => '[...]...rd.php'
+    'MediaWiki\Revision\SuppressedDataException' => '[...]...on.php'
 )

Change-Id: I1485cc7309c20d131f398473367d281a3ce78a25
2020-12-13 17:16:53 +00:00
Kunal Mehta
61c751cf89 Add structure test to verify all PSR-4 directories exist
We use AutoLoader::$psr4Namespaces to include extension entries too.

All PSR-4 directories need to exist, otherwise there's no point in trying
to autoload from them.

I verified that this test would have caught T259448 and prevented it from
happening.

Change-Id: If2df3b79a926847e443f887218ab09274ace1a44
2020-08-06 12:07:46 -07:00
addshore
959bc315f2 MediaWikiTestCase to MediaWikiIntegrationTestCase
The name change happened some time ago, and I think its
about time to start using the name name!
(Done with a find and replace)

My personal motivation for doing this is that I have started
trying out vscode as an IDE for mediawiki development, and
right now it doesn't appear to handle php aliases very well
or at all.

Change-Id: I412235d91ae26e4c1c6a62e0dbb7e7cf3c5ed4a6
2020-06-30 17:02:22 +01:00
Tim Starling
f81462048a Add PSR-4 mappings for existing namespaces and the top level
This reduces the size of the class map ($wgAutoloadLocalClasses),
and allows new classes and namespaces to be added without modifying
the class or namespace map as long as they conform to PSR-4.

Adding a PSR-4 mapping for the top-level MediaWiki namespace means that
conforming subnamespaces do not need to be listed.

I did not add some odd or broken cases, since I figure it's better to
fix them by moving the files, which can be done in a separate commit.

I removed testPSR4Completeness, since PSR-4 completeness is no longer
required, that's the point.

Bug: T166010
Change-Id: Ie5e50ecb519b99a1197688c046c7be245ce6da1b
2020-03-10 21:08:30 +11:00
Thiemo Kreuz
e1dd371e11 Make use of PHPUnit's assertCount feature where possible
… and avoid assertEmpty() on arrays, in favor of a much more strict
assertSame( [] ).

Change-Id: I20266b0b1fc38a3a87666ba1b0793cb2b37d94a9
2020-03-02 15:58:41 +00:00
Tim Starling
44d51cb04e Fix the namespace of SpecialPageFactory
Follows-up d4045035b0.

This class was added to the MediaWiki\Special namespace, contrary to the
plan in T166010 which reserves that namespace for core special pages.
Instead, use MediaWiki\SpecialPage, following the directory in which it
is located.

Also, fix two bugs which prevented the introduction of a namespaced
class alias.

Bug: T166010
Change-Id: I6e31340aaae32a89beb7e45b79d76a7fea9808d2
2020-02-21 13:46:19 +11:00
Max Semenik
4a17fd1202 Upgrade PHPUnit to version 7
Going directly to 8 is too much pain.

Depends-On: Iafcea151389663d79e70ad6ab3741ce8b2e25cac
Bug: T192167
Change-Id: I77ea560f4a5344bd1c356ecc3e7857968ad829b8
2019-11-20 21:02:33 -08:00
Timo Tijhof
8035a00e8c AutoLoader: Skip tokenizing of irrelevant lines in ClassCollector
This makes AutoLoaderStructureTest in PHPUnit and the
generateLocalAutoload.php maintenance script much faster.

On my machine, it made it 35X faster (or time spent reduced by 97%).

Bug: T225730
Change-Id: Ife959bd17ce9c2ae952dfbd158ddb3d8475e8cb2
2019-06-19 15:09:49 +00:00
Legoktm
4e35134f7a Revert "Separate MediaWiki unit and integration tests"
This reverts commit 0a2b996278.

Reason for revert: Broke postgres tests.

Change-Id: I27d8e0c807ad5f0748b9611a4f3df84cc213fbe1
2019-06-13 23:00:08 +00:00
Máté Szabó
0a2b996278 Separate MediaWiki unit and integration tests
This changeset implements T89432 and related tickets and is based on exploration
done at the Prague Hackathon. The goal is to identify tests in MediaWiki core
that can be run without having to install & configure MediaWiki and its dependencies,
and provide a way to execute these tests via the standard phpunit entry point,
allowing for faster development and integration with existing tooling like IDEs.

The initial set of tests that met these criteria were identified using the work Amir did in
I88822667693d9e00ac3d4639c87bc24e5083e5e8. These tests were then moved into a new subdirectory
under phpunit/ and organized into a separate test suite. The environment for this suite
is set up via a PHPUnit bootstrap file without a custom entry point.

You can execute these tests by running:
$ vendor/bin/phpunit -d memory_limit=512M -c tests/phpunit/unit-tests.xml

Bug: T89432
Bug: T87781
Bug: T84948
Change-Id: Iad01033a0548afd4d2a6f2c1ef6fcc9debf72c0d
2019-06-13 22:56:31 +02:00
Gergő Tisza
45d4e8d13a Exempt structure tests from @covers checks
@covers does not make any sense for structure tests, which either
do not cover any PHP lines (they test things like configuration or
messages), or cover lines which cannot be determined at the time
of writing the tests (e.g. they cover all classes implementing a
certain interface). Requiring @coversNothing to be manually added
for all of them is a waste of developer time.

tests/phpunit/suite.xml has forceCoversAnnotation=true so removing
the annotations will not change test coverage, these files will
still be skipped.

Change-Id: I27cb58e92341b9b1a76f109701f5bc843adbaa9b
2019-06-11 21:40:14 +00:00
Lucas Werkmeister
2744dbd9b7 Normalize dir path in AutoLoaderStructureTest
The file paths we inspect are always normalized (File_Iterator_Facade
applies realpath()), but the directory we compare against may not be,
depending on how wfLoadExtension() was called. Normalize the directory
before we remove the directory prefix from each file, so that we don’t
end up stripping away the wrong parts of the path.

Change-Id: Ib272fb892c18d989f8d439ed50c6a5a8fd542cc9
2019-06-07 12:53:31 +02:00
Reedy
c13fee87d4 Collapse some nested if statements
Change-Id: I9a97325d738d09370d29d35d5254bc0dadc57ff4
2019-04-04 19:02:22 +00:00
Alangi Derick
8221a7a4f2 Remove array_unique() on expected classes in checkAutoLoadConf()
Sometimes classes can be loaded via autoload (PSR-4) and class_aliasing
and due to this calling array_unique() on expected classes will remove
one of these classes due to them sharing the same file (and array_unique()
works on array values).

Also allow for ::class suffixed classes to be namespaced (ie look for \
in the regexes)

Cleanup some of the regexes, remove redunant code (don't need multi cased
letters when we have /i), simplify them

Bug: T206728
Change-Id: I235274d579b1bfd12a448448ddf020546c9aa89b
2018-11-30 00:22:06 +00:00
Brad Jorsch
ca3789a271 AutoloadGenerator: Filter PSR4-compliant classes instead of ignoring directories
Per discussion in T166010, we're going to handle class aliases (e.g. for
BC) by including the class_alias() call in the same file as the target
class. When the target class is a PSR4-compliant class, we still need to
pick up that alias for inclusion in autoload.php.

Thus, instead of excluding whole directories, we need to process the
files and filter out only those found classes that are PSR4 compliant.

Bug: T204983
Change-Id: I1c516998df368531c90ea54acc5be8be96e1db6c
2018-09-20 15:15:27 -04:00
Hashar
afb2269cb9 Revert "Use pathinfo() in AutoLoaderStructureTest::testPSR4Completeness"
This reverts commit 634c2ec2af.

Reason for revert: that strips the PSR4 directories from the class.

Example:
$dir BlueSpiceFoundation/src/"
$file BlueSpiceFoundation/src/ConfigDefinition/IntSetting.php

$abbrFileName: IntSetting
$expectedClassName: BlueSpice\IntSetting

$abbrFileName should be relative to $dir and not just the filename.

Bug: T198077
Change-Id: Ie934e309fee0392439b4e26d86249f0650e5ea67
2018-06-25 12:38:07 +00:00
Antoine Musso
634c2ec2af Use pathinfo() in AutoLoaderStructureTest::testPSR4Completeness
When setting AutoloadNamespaces to './' in extension.json, the test
AutoLoaderStructureTest::testPSR4Completeness would fail. The directory
path is not made canonical while the file is, which causes the substr()
call being used to strip too many characters. For example:

  $dir : /mediawiki/extensions/Wikidata.org/./
  $file: /mediawiki/extensions/Wikidata.org/Hooks.php

$abbrFileName = substr( substr( $file, strlen( $dir ) ), 0, -4 );
>>> oks

Use pathinfo() to parse the filename. Yields 'Hooks' as expected.

Bug: T198077
Change-Id: Ia8a11d87788b32ddb426a16a61b410b05ff5f15e
2018-06-25 11:47:52 +00:00
Kunal Mehta
b5e6da43e4 Don't autoload classes in AutoloadStructureTest
Autoloading classes is a 100% accurate way to ensure the autoloader
worked, but there are cases where if optional dependencies aren't
installed, then autoloading the class will fail. We can re-implement the
logic behind the PSR-4 autoloader, and ensure that classes will be
autoloadable by turning the filename into a class name, and making sure
that class name is the one we found in the file.

Bug: T195823
Change-Id: I5df378180e567c257386482383ef73812592f989
2018-05-29 11:09:57 -07:00
Kunal Mehta
e7ae019fc0 AutoLoaderStructureTest: Allow PSR-4 directories to have files with 0 classes
Files like ServiceWiring.php can be safely located in a PSR-4 autoloaded
directory, because they have no classes.

Change-Id: I359b305df9071d6bc5afe4b5f29e762041f4aaef
2018-05-26 16:51:56 -07:00
Kunal Mehta
e298f548f6 Split AutoloaderTest into a structure and class test
AutoloaderTest covers the AutoLoader class, and AutoLoaderStructureTest
covers the structure part of the test.

Change-Id: Ic4e7bfd670e1c3413631bda31260cc1cc825b1a2
2018-05-26 16:16:02 -07:00
Renamed from tests/phpunit/structure/AutoLoaderTest.php (Browse further)