|
| 1 | +<?php |
| 2 | + |
| 3 | +declare( strict_types = 1 ); |
| 4 | + |
| 5 | +namespace Maps\Tests\System; |
| 6 | + |
| 7 | +use Maps\Tests\Util\PageCreator; |
| 8 | +use Maps\Tests\Util\TestFactory; |
| 9 | +use MediaWiki\Parser\ParserOptions; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +/** |
| 13 | + * Needs the database: the queries need a page in the Semantic MediaWiki store. They do not ask for its coordinates, so |
| 14 | + * they find the page but no locations other than those they give. |
| 15 | + * |
| 16 | + * @covers \Maps\Map\SemanticFormat\MapPrinter |
| 17 | + * @covers \Maps\Presentation\ElementJsonSerializer |
| 18 | + */ |
| 19 | +class MapQueryEscapingTest extends TestCase { |
| 20 | + |
| 21 | + private const QUERY = '[[Coordinates::+]]'; |
| 22 | + private const SCRIPT = '<script>alert(1)</script>'; |
| 23 | + private const ESCAPED_SCRIPT = '<script>alert(1)</script>'; |
| 24 | + |
| 25 | + public static function setUpBeforeClass(): void { |
| 26 | + parent::setUpBeforeClass(); |
| 27 | + |
| 28 | + if ( !defined( 'SMW_VERSION' ) ) { |
| 29 | + self::markTestSkipped( 'SMW is not available' ); |
| 30 | + } |
| 31 | + |
| 32 | + PageCreator::instance()->createPage( |
| 33 | + 'Property:Coordinates', |
| 34 | + '[[Has type::Geographic coordinate|geographic coordinate]]' |
| 35 | + ); |
| 36 | + |
| 37 | + PageCreator::instance()->createPage( |
| 38 | + 'MapQueryEscapingTest location', |
| 39 | + '[[Coordinates::52° 31\' 0", 13° 24\' 0"]]' |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + public function testPageShowsScriptInDefaultAsText(): void { |
| 44 | + $this->assertStringContainsString( self::ESCAPED_SCRIPT, $this->renderPage( 'format=leaflet|default=' . self::SCRIPT ) ); |
| 45 | + } |
| 46 | + |
| 47 | + private function renderPage( string $askParameters ): string { |
| 48 | + return TestFactory::newInstance()->renderPage( |
| 49 | + '{{#ask:' . self::QUERY . '|' . $askParameters . '}}', |
| 50 | + ParserOptions::newFromAnon() |
| 51 | + )->getContentHolderText(); |
| 52 | + } |
| 53 | + |
| 54 | + public function testSpecialAskShowsScriptInDefaultAsText(): void { |
| 55 | + $this->assertStringContainsString( |
| 56 | + self::ESCAPED_SCRIPT, |
| 57 | + $this->renderOnSpecialAsk( [ 'format=leaflet', 'default=' . self::SCRIPT ], SMW_OUTPUT_HTML ) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param string[] $askParameters |
| 63 | + */ |
| 64 | + private function renderOnSpecialAsk( array $askParameters, int $outputMode ): string { |
| 65 | + return TestFactory::newInstance()->renderOnSpecialAsk( [ self::QUERY, ...$askParameters ], $outputMode ); |
| 66 | + } |
| 67 | + |
| 68 | + public function testPageRendersWikitextInDefault(): void { |
| 69 | + $this->assertStringContainsString( '<b>No locations</b>', $this->renderPage( "format=leaflet|default='''No locations'''" ) ); |
| 70 | + } |
| 71 | + |
| 72 | + public function testSpecialAskShowsWikitextInDefaultAsText(): void { |
| 73 | + $this->assertStringContainsString( |
| 74 | + '[[No locations]]', |
| 75 | + $this->renderOnSpecialAsk( [ 'format=leaflet', 'default=[[No locations]]' ], SMW_OUTPUT_HTML ) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Special:Ask serves the raw output of a query as HTML. |
| 81 | + */ |
| 82 | + public function testRawResultShowsScriptInDefaultAsText(): void { |
| 83 | + $this->assertStringContainsString( |
| 84 | + self::ESCAPED_SCRIPT, |
| 85 | + $this->renderOnSpecialAsk( [ 'format=leaflet', 'default=' . self::SCRIPT ], SMW_OUTPUT_RAW ) |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + public function testSpecialAskShowsScriptInInlineLabelAsText(): void { |
| 90 | + $html = $this->renderOnSpecialAsk( [ 'format=googlemaps3', 'locations=1,1~Title~Text~~~' . self::SCRIPT ], SMW_OUTPUT_HTML ); |
| 91 | + |
| 92 | + $this->assertStringContainsString( self::ESCAPED_SCRIPT, $this->mapDataOf( $html )['locations'][0]['inlineLabel'] ); |
| 93 | + } |
| 94 | + |
| 95 | + private function mapDataOf( string $html ): array { |
| 96 | + $this->assertSame( 1, preg_match( '/data-mw-maps-mapdata="([^"]*)"/', $html, $matches ), 'The output holds no map' ); |
| 97 | + |
| 98 | + return json_decode( htmlspecialchars_decode( $matches[1] ), true ); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @dataProvider introAndOutroProvider |
| 103 | + */ |
| 104 | + public function testPageShowsScriptInIntroOrOutroAsText( string $parameter ): void { |
| 105 | + $html = $this->renderPage( 'format=leaflet|locations=1,1|' . $parameter . '=' . self::SCRIPT ); |
| 106 | + |
| 107 | + $this->assertStringContainsString( self::ESCAPED_SCRIPT, $html ); |
| 108 | + $this->assertStringNotContainsString( self::SCRIPT, $html ); |
| 109 | + } |
| 110 | + |
| 111 | + public static function introAndOutroProvider(): iterable { |
| 112 | + yield 'intro' => [ 'intro' ]; |
| 113 | + yield 'outro' => [ 'outro' ]; |
| 114 | + } |
| 115 | + |
| 116 | + public function testPageRendersWikitextInIntro(): void { |
| 117 | + $this->assertStringContainsString( |
| 118 | + '<b>Some locations</b>', |
| 119 | + $this->renderPage( "format=leaflet|locations=1,1|intro='''Some locations'''" ) |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + public function testPageWrapsTheMapInIntroAndOutro(): void { |
| 124 | + $this->assertMatchesRegularExpression( |
| 125 | + '/<div class="map-box">\s*<div id="map_leaflet_/', |
| 126 | + $this->renderPage( 'format=leaflet|locations=1,1|intro=<div class="map-box">|outro=</div>' ) |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments