wiki.techinc.nl/tests/phpunit/includes/api/query/ApiQueryRevisionsTest.php
Máté Szabó f15e0a62f9 ApiQueryRevisionsBase: Fix 'rvdiffto' parameter handling on PHP 8.0
The legacy 'rvdiffto' API parameter allows consumers to supply a
revision ID or the special strings 'cur', 'prev', 'next' to
revision-related API modules. This will then be resolved to a revision
and the diff of returned revisions against this revision will be
attached to the response.

On PHP 8.0, this parameter is broken as 'cur', 'prev', 'next' no longer
get resolved to the appropriate revision ID but rather are treated as
invalid literal revision IDs. This is because given an associative array
`$params = [ 'diffto' => 'prev' ];`, the expression `$params['diffto']
!= 0` is false on PHP < 8.0 but true on PHP >= 8.0.[1]

Fix it by also checking whether the parameter value is numeric before
treating it as a revision ID, and add a test case for this logic. To
facilitate testing, convert the counter used by ApiQueryRevisionsBase to
count the number of uncached diffs served in the current response to an
instance variable; it is currently a static variable which persists
across test boundaries.

Change-Id: Ifbe14cd2880c98dddc5577d226bef5878bdd657f

---
[1] https://onlinephp.io/c/81e7b

Bug: T322335
Change-Id: Id5032932842c94f4d3ca3707fbc733f5b9704151
2022-11-03 15:49:04 +01:00

101 lines
3 KiB
PHP

<?php
use MediaWiki\Revision\SlotRecord;
/**
* @group API
* @group Database
* @group medium
* @covers ApiQueryRevisions
*/
class ApiQueryRevisionsTest extends ApiTestCase {
/**
* @group medium
*/
public function testContentComesWithContentModelAndFormat() {
$pageName = 'Help:' . __METHOD__;
$page = $this->getExistingTestPage( $pageName );
$user = $this->getTestUser()->getUser();
$page->newPageUpdater( $user )
->setContent( SlotRecord::MAIN, new WikitextContent( 'Some text' ) )
->saveRevision( CommentStoreComment::newUnsavedComment( 'inserting content' ) );
$apiResult = $this->doApiRequest( [
'action' => 'query',
'prop' => 'revisions',
'titles' => $pageName,
'rvprop' => 'content',
'rvslots' => 'main',
] );
$this->assertArrayHasKey( 'query', $apiResult[0] );
$this->assertArrayHasKey( 'pages', $apiResult[0]['query'] );
foreach ( $apiResult[0]['query']['pages'] as $page ) {
$this->assertArrayHasKey( 'revisions', $page );
foreach ( $page['revisions'] as $revision ) {
$this->assertArrayHasKey( 'slots', $revision );
$this->assertArrayHasKey( 'main', $revision['slots'] );
$this->assertArrayHasKey( 'contentformat', $revision['slots']['main'],
'contentformat should be included when asking content so client knows how to interpret it'
);
$this->assertArrayHasKey( 'contentmodel', $revision['slots']['main'],
'contentmodel should be included when asking content so client knows how to interpret it'
);
}
}
}
/**
* @group medium
*/
public function testResolvesPrevNextInDiffto() {
$pageName = 'Help:' . __METHOD__;
$page = $this->getExistingTestPage( $pageName );
$user = $this->getTestUser()->getUser();
$revRecord = $page->newPageUpdater( $user )
->setContent( SlotRecord::MAIN, new WikitextContent( 'Some text' ) )
->saveRevision( CommentStoreComment::newUnsavedComment( 'inserting more content' ) );
[ $rvDiffToPrev ] = $this->doApiRequest( [
'action' => 'query',
'prop' => 'revisions',
'titles' => $pageName,
'rvdiffto' => 'prev',
] );
$this->assertSame(
$revRecord->getId(),
$rvDiffToPrev['query']['pages'][$page->getId()]['revisions'][0]['revid']
);
$this->assertSame(
$revRecord->getId(),
$rvDiffToPrev['query']['pages'][$page->getId()]['revisions'][0]['diff']['to']
);
$this->assertSame(
$revRecord->getParentId(),
$rvDiffToPrev['query']['pages'][$page->getId()]['revisions'][0]['diff']['from']
);
[ $rvDiffToNext ] = $this->doApiRequest( [
'action' => 'query',
'prop' => 'revisions',
'titles' => $pageName,
'rvdiffto' => 'next',
'rvdir' => 'newer'
] );
$this->assertSame(
$revRecord->getParentId(),
$rvDiffToNext['query']['pages'][$page->getId()]['revisions'][0]['revid']
);
$this->assertSame(
$revRecord->getId(),
$rvDiffToNext['query']['pages'][$page->getId()]['revisions'][0]['diff']['to']
);
$this->assertSame(
$revRecord->getParentId(),
$rvDiffToNext['query']['pages'][$page->getId()]['revisions'][0]['diff']['from']
);
}
}