aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/api/query/ApiQueryRevisionsTest.php
diff options
context:
space:
mode:
authorMáté Szabó <mszabo@fandom.com>2022-11-18 15:19:54 +0100
committerMáté Szabó <mszabo@fandom.com>2022-11-18 15:23:46 +0100
commitb523ab0081d200d847248f93514b6aa661c3a6b7 (patch)
tree71b8f3c4da1984b308cc9cb76b32b8f6f2c3a06e /tests/phpunit/includes/api/query/ApiQueryRevisionsTest.php
parent4a4efed2b5d093ada9df0190032b35f80cbfc4be (diff)
downloadmediawikicore-b523ab0081d200d847248f93514b6aa661c3a6b7.tar.gz
mediawikicore-b523ab0081d200d847248f93514b6aa661c3a6b7.zip
Parser: Fix extractSections() behavior for PHP >= 8.0
PHP 8.0 changed the behavior of numeric comparisons such that non-numeric strings no longer weakly equal 0.[1] This breaks the logic in Parser::extractSections(), which was relying on the old comparison behavior for section indexes and in turn causes the revisions API to return a bogus 'nosuchsection' for error when called with rvsection=new. Fix the logic by explicitly casting the section index to a number, which will yield the appropriate numeric section index for a numbered section index and 0 for a non-numeric section index (like 'new'). Also add test cases for the relevant API module. -- [1] https://wiki.php.net/rfc/string_to_number_comparison Change-Id: If32aa4d575cff66bd4eee56f9e3b0b0d9ba04fde Bug: T323373
Diffstat (limited to 'tests/phpunit/includes/api/query/ApiQueryRevisionsTest.php')
-rw-r--r--tests/phpunit/includes/api/query/ApiQueryRevisionsTest.php43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/phpunit/includes/api/query/ApiQueryRevisionsTest.php b/tests/phpunit/includes/api/query/ApiQueryRevisionsTest.php
index 722be487fc66..5914d6a00c46 100644
--- a/tests/phpunit/includes/api/query/ApiQueryRevisionsTest.php
+++ b/tests/phpunit/includes/api/query/ApiQueryRevisionsTest.php
@@ -98,4 +98,47 @@ class ApiQueryRevisionsTest extends ApiTestCase {
$rvDiffToNext['query']['pages'][$page->getId()]['revisions'][0]['diff']['from']
);
}
+
+ /**
+ * @dataProvider provideSectionNewTestCases
+ * @param string $pageContent
+ * @param string $expectedSectionContent
+ * @group medium
+ */
+ public function testSectionNewReturnsEmptyContentForPageWithSection(
+ $pageContent,
+ $expectedSectionContent
+ ) {
+ $pageName = 'Help:' . __METHOD__;
+ $page = $this->getExistingTestPage( $pageName );
+ $user = $this->getTestUser()->getUser();
+ $revRecord = $page->newPageUpdater( $user )
+ ->setContent( SlotRecord::MAIN, new WikitextContent( $pageContent ) )
+ ->saveRevision( CommentStoreComment::newUnsavedComment( 'inserting content' ) );
+
+ [ $response ] = $this->doApiRequest( [
+ 'action' => 'query',
+ 'prop' => 'revisions',
+ 'revids' => $revRecord->getId(),
+ 'rvprop' => 'content|ids',
+ 'rvslots' => 'main',
+ 'rvsection' => 'new'
+ ] );
+
+ $this->assertSame(
+ $expectedSectionContent,
+ $response['query']['pages'][$page->getId()]['revisions'][0]['slots']['main']['content']
+ );
+ }
+
+ public function provideSectionNewTestCases() {
+ yield 'page with existing section' => [
+ "==A section==\ntext",
+ ''
+ ];
+ yield 'page with no sections' => [
+ 'This page has no sections',
+ 'This page has no sections'
+ ];
+ }
}