Skip to content

Commit

Permalink
(tests) Verify that image thumbnails are removed from HTML snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardspec committed Dec 30, 2024
1 parent ba2ba25 commit a648959
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion tests/phpunit/EventCalendarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* @file
*/

use MediaWiki\MediaWikiServices;

/**
* Integration test of <eventcalendar> tag.
*
Expand Down Expand Up @@ -599,7 +601,6 @@ public function dataProvider() {
]
];

// TODO: test removal of thumbnails.
yield 'calendar with snippets (symbols=50)' => [
[
'January 1: New Year' =>
Expand Down Expand Up @@ -771,6 +772,40 @@ public function testSnippetForCompressedRevision() {
$this->assertSame( $expectedSnippet, $actualData[0]['title'] );
}

/**
* Verify that unwanted parts of HTML (such as images) are removed from the snippet.
*/
public function testSnippetSanitizer() {
$filename = 'Testimage.png';
$pageText = "Expected snippet [[File:$filename]]";

$isMW39 = version_compare( MW_VERSION, '1.40.0', '<' );
$expectedSnippet = '<p>Expected snippet ' .
( $isMW39 ? '' : '<span class="mw-default-size" typeof="mw:File">' ) .
"<a href=\"/wiki/File:$filename\" class=\"" .
( $isMW39 ? 'image' : 'mw-file-description' ) .
'"></a>' . ( $isMW39 ? '' : '</span>' ) . '</p>';

// Upload a test file, so that [[File:]] syntax would create an actual thumbnail, not a redlink.
$user = $this->getTestUser()->getUser();
$stash = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()->getUploadStash( $user );
$key = $stash->stashFile( __DIR__ . '/../resources/image100x100.png' )->getFileKey();

$upload = new UploadFromStash( $user );
$upload->initialize( $key, $filename );

$status = $upload->performUpload( '', false, false, $user );
$this->assertTrue( $status->isOK(), 'Failed to upload test file: ' . $status->getMessage()->plain() );

$this->insertPage( 'January 1: New Year', $pageText );

// Render the calendar.
$wikitext = "titleRegex = ^([A-Za-z]+_[0-9][0-9]?).*\ndateFormat = F_j\nsymbols = 500";
$actualData = $this->parseCalendarEvents( $wikitext );

$this->assertSame( $expectedSnippet, $actualData[0]['title'] );
}

/**
* Verify that parameters like height=300 and aspectratio=1.5 are provided to JavaScript library.
* @dataProvider dataProviderOptionalAttributes
Expand Down
Binary file added tests/resources/image100x100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a648959

Please sign in to comment.