wiki.techinc.nl/tests/phpunit/maintenance/CategoriesRdfTest.php
Timo Tijhof c02513c97e phpunit: Fix tests relying on implicit wgScript/wgArticlePath
A number of tests have hardcoded expections that pass only in WMF CI
where Quibble has LocalSettings.php with $wgScript and $wgArticlePath
set a certain way.

We could fix these by adding setMwGlobals() in their tests, as we
often do, but these are so often forgotten that I'd rather we just
add them to TestSetup.php so that it is simply impossible to write a
test that that passes locally for you (if you have the same config)
but not for someone else.

There is a larger project in there somewhere about expanding this
slowly such that we basically only pluck DB-settings and extension
enablement from LocalSettings and otherwise run the tests with the
default settings in PHPUnit. Pretty much by definition, any (other)
setting you have in LocalSettings is irrelevant because it either:
1. has no effect on the test (majority, harmless either way),
2. has a custom default via TestSetup.php (which has precedence over
   LocalSettings.php),
3. is relevant to the code being tested and the test case correctly
   calls setMwGlobals() to ensure a consistent value during test.
4. is relevant to the tested code but has no override, thus only
   passes if you happen to have the "right" value set for it
   (undesirable).

Case 4 is already categorically impossible for the most common config
settings that influence random code because we give them a value
in TestSetup.php. This patch expands that to include $wgScript
and $wgArticlePath. Perhaps in the future we can think about a way
to do this automatically by either re-applying MainConfigSchema
(sans db settings) or by only selectively applying LocalSettings.php
in the first place.

This patch follows-up I072ddf89562fe, which added a test case in
WikitextContentHandlerIntegrationTest.php that assumed "/index.php"
as the value of $wgScript. This passes in WMF CI since Quibble uses
that value, but the tests failed in most local development installs
since those tend to use "/w" instead.

Rather than one-off fixing that one test with overrideConfigValues(),
switch to a more general fixture, since the precise values don't
matter for this test.

Bug: T349087
Bug: T277470
Change-Id: If4304b7ca4a838bd892d4516a0b5c6dfbc30986e
2024-05-05 00:00:01 +00:00

104 lines
2.7 KiB
PHP

<?php
namespace MediaWiki\Tests\Maintenance;
use DumpCategoriesAsRdf;
use MediaWiki\MainConfigNames;
use MediaWikiLangTestCase;
use Wikimedia\Rdbms\IMaintainableDatabase;
/**
* @covers \MediaWiki\Category\CategoriesRdf
* @covers \DumpCategoriesAsRdf
*/
class CategoriesRdfTest extends MediaWikiLangTestCase {
public function getCategoryIterator() {
return [
// batch 1
[
(object)[
'page_title' => 'Category One',
'page_id' => 1,
'pp_propname' => null,
'cat_pages' => '20',
'cat_subcats' => '10',
'cat_files' => '3'
],
(object)[
'page_title' => '2 Category Two',
'page_id' => 2,
'pp_propname' => 'hiddencat',
'cat_pages' => 20,
'cat_subcats' => 0,
'cat_files' => 3
],
],
// batch 2
[
(object)[
'page_title' => 'Третья категория',
'page_id' => 3,
'pp_propname' => null,
'cat_pages' => '0',
'cat_subcats' => '0',
'cat_files' => '0'
],
]
];
}
public function getCategoryLinksIterator( $dbr, array $ids ) {
$res = [];
foreach ( $ids as $pageid ) {
$res[] = (object)[ 'cl_from' => $pageid, 'cl_to' => "Parent of $pageid" ];
}
return $res;
}
public function testCategoriesDump() {
$this->overrideConfigValues( [
MainConfigNames::Server => 'http://acme.test',
MainConfigNames::CanonicalServer => 'http://acme.test',
MainConfigNames::RightsUrl => 'https://creativecommons.org/licenses/by-sa/3.0/',
] );
$dumpScript =
$this->getMockBuilder( DumpCategoriesAsRdf::class )
->onlyMethods( [ 'getDB', 'getCategoryIterator', 'getCategoryLinksIterator' ] )
->getMock();
$dumpScript->method( 'getDB' )
->willReturn( $this->createNoOpMock( IMaintainableDatabase::class ) );
$dumpScript->expects( $this->once() )
->method( 'getCategoryIterator' )
->willReturn( $this->getCategoryIterator() );
$dumpScript->method( 'getCategoryLinksIterator' )
->willReturnCallback( [ $this, 'getCategoryLinksIterator' ] );
/** @var DumpCategoriesAsRdf $dumpScript */
$logFileName = $this->getNewTempFile();
$outFileName = $this->getNewTempFile();
$dumpScript->loadParamsAndArgs(
null,
[
'log' => $logFileName,
'output' => $outFileName,
'format' => 'nt',
]
);
$dumpScript->execute();
$actualOut = file_get_contents( $outFileName );
$actualOut = preg_replace(
'|<http://acme.test/wiki/Special:CategoryDump> <http://schema.org/dateModified> "[^"]+?"|',
'<http://acme.test/wiki/Special:CategoryDump> <http://schema.org/dateModified> "{DATE}"',
$actualOut
);
$outFile = __DIR__ . '/../data/categoriesrdf/categoriesRdf-out.nt';
$this->assertFileContains( $outFile, $actualOut );
}
}