138ca8b27SAndreas Gohr<?php 238ca8b27SAndreas Gohr 338ca8b27SAndreas Gohrnamespace dokuwiki\plugin\dw2pdf\test; 438ca8b27SAndreas Gohr 538ca8b27SAndreas Gohruse dokuwiki\plugin\dw2pdf\src\MediaLinkResolver; 638ca8b27SAndreas Gohruse DokuWikiTest; 738ca8b27SAndreas Gohr 838ca8b27SAndreas Gohr/** 938ca8b27SAndreas Gohr * Tests for translating Dokuwiki media references into local cached files. 1038ca8b27SAndreas Gohr * 1138ca8b27SAndreas Gohr * @group plugin_dw2pdf 1238ca8b27SAndreas Gohr * @group plugins 1338ca8b27SAndreas Gohr */ 1438ca8b27SAndreas Gohrclass MediaLinkResolverTest extends DokuWikiTest 1538ca8b27SAndreas Gohr{ 1638ca8b27SAndreas Gohr private $resolver; 1738ca8b27SAndreas Gohr 1838ca8b27SAndreas Gohr public function setUp(): void 1938ca8b27SAndreas Gohr { 2038ca8b27SAndreas Gohr parent::setUp(); 2138ca8b27SAndreas Gohr $this->resolver = new MediaLinkResolver(); 2238ca8b27SAndreas Gohr } 2338ca8b27SAndreas Gohr 2438ca8b27SAndreas Gohr /** 2538ca8b27SAndreas Gohr * @return array<string, array{0:string,1:string,2:string}> 2638ca8b27SAndreas Gohr */ 2738ca8b27SAndreas Gohr public static function resolveProvider(): array 2838ca8b27SAndreas Gohr { 2938ca8b27SAndreas Gohr global $conf; 3038ca8b27SAndreas Gohr 3138ca8b27SAndreas Gohr return [ 3238ca8b27SAndreas Gohr 'internal fetch url' => [ 3338ca8b27SAndreas Gohr DOKU_URL . 'lib/exe/fetch.php?media=wiki:dokuwiki-128.png', 3438ca8b27SAndreas Gohr $conf['mediadir'] . '/wiki/dokuwiki-128.png', 3538ca8b27SAndreas Gohr 'image/png', 3638ca8b27SAndreas Gohr ], 3738ca8b27SAndreas Gohr 'static local file' => [ 3838ca8b27SAndreas Gohr DOKU_URL . 'lib/images/throbber.gif', 3938ca8b27SAndreas Gohr DOKU_INC . 'lib/images/throbber.gif', 4038ca8b27SAndreas Gohr 'image/gif', 4138ca8b27SAndreas Gohr ], 4238ca8b27SAndreas Gohr ]; 4338ca8b27SAndreas Gohr } 4438ca8b27SAndreas Gohr 4538ca8b27SAndreas Gohr /** 4638ca8b27SAndreas Gohr * @dataProvider resolveProvider 4738ca8b27SAndreas Gohr */ 4838ca8b27SAndreas Gohr public function testResolveReturnsLocalPathAndMime(string $input, string $expectedPath, string $expectedMime): void 4938ca8b27SAndreas Gohr { 5038ca8b27SAndreas Gohr $resolved = $this->resolver->resolve($input); 5138ca8b27SAndreas Gohr 5238ca8b27SAndreas Gohr $this->assertNotNull($resolved); 5338ca8b27SAndreas Gohr $this->assertSame($expectedPath, $resolved['path']); 5438ca8b27SAndreas Gohr $this->assertSame($expectedMime, $resolved['mime']); 5538ca8b27SAndreas Gohr $this->assertFileExists($resolved['path']); 5638ca8b27SAndreas Gohr } 5738ca8b27SAndreas Gohr 58*92200750SAndreas Gohr /** 59*92200750SAndreas Gohr * The resolver must support our dw2pdf:// pseudo scheme for temporary files. 60*92200750SAndreas Gohr */ 6138ca8b27SAndreas Gohr public function testResolveDw2pdfScheme(): void 6238ca8b27SAndreas Gohr { 6338ca8b27SAndreas Gohr $temp = tempnam(sys_get_temp_dir(), 'dw2pdf'); 6438ca8b27SAndreas Gohr if ($temp === false) { 6538ca8b27SAndreas Gohr $this->fail('Unable to create temp file for dw2pdf:// test'); 6638ca8b27SAndreas Gohr } 6738ca8b27SAndreas Gohr 6838ca8b27SAndreas Gohr $image = $temp . '.png'; 6938ca8b27SAndreas Gohr if (!rename($temp, $image)) { 7038ca8b27SAndreas Gohr $this->fail('Unable to rename temp file for dw2pdf:// test'); 7138ca8b27SAndreas Gohr } 7238ca8b27SAndreas Gohr 7338ca8b27SAndreas Gohr $resolved = $this->resolver->resolve('dw2pdf://' . $image); 7438ca8b27SAndreas Gohr 7538ca8b27SAndreas Gohr $this->assertNotNull($resolved); 7638ca8b27SAndreas Gohr $this->assertSame($image, $resolved['path']); 7738ca8b27SAndreas Gohr $this->assertSame('image/png', $resolved['mime']); 7838ca8b27SAndreas Gohr 7938ca8b27SAndreas Gohr @unlink($image); 8038ca8b27SAndreas Gohr } 8138ca8b27SAndreas Gohr 8238ca8b27SAndreas Gohr /** 8338ca8b27SAndreas Gohr * @group internet 8438ca8b27SAndreas Gohr */ 8538ca8b27SAndreas Gohr public function testResolveFetchesExternalMedia(): void 8638ca8b27SAndreas Gohr { 8738ca8b27SAndreas Gohr global $conf; 8838ca8b27SAndreas Gohr $conf['fetchsize'] = 512 * 1024; // 512 KB 8938ca8b27SAndreas Gohr 9038ca8b27SAndreas Gohr $external = 'https://php.net/images/php.gif'; 9138ca8b27SAndreas Gohr $input = DOKU_URL . 'lib/exe/fetch.php?media=' . rawurlencode($external); 9238ca8b27SAndreas Gohr $resolved = $this->resolver->resolve($input); 9338ca8b27SAndreas Gohr 94*92200750SAndreas Gohr if ($resolved === null) { 95*92200750SAndreas Gohr $this->markTestSkipped('External media fetching is not available in this environment.'); 96*92200750SAndreas Gohr } 97*92200750SAndreas Gohr 9838ca8b27SAndreas Gohr $this->assertNotNull($resolved); 9938ca8b27SAndreas Gohr $this->assertFileExists($resolved['path']); 10038ca8b27SAndreas Gohr $this->assertSame('image/gif', $resolved['mime']); 10138ca8b27SAndreas Gohr $this->assertSame(2523, filesize($resolved['path'])); 10238ca8b27SAndreas Gohr } 10338ca8b27SAndreas Gohr 104*92200750SAndreas Gohr /** 105*92200750SAndreas Gohr * Non-image payloads should never be returned to the PDF generator. 106*92200750SAndreas Gohr */ 10738ca8b27SAndreas Gohr public function testResolveRejectsNonImages(): void 10838ca8b27SAndreas Gohr { 10938ca8b27SAndreas Gohr $resolved = $this->resolver->resolve(DOKU_URL . 'README'); 11038ca8b27SAndreas Gohr 11138ca8b27SAndreas Gohr $this->assertNull($resolved); 11238ca8b27SAndreas Gohr } 11338ca8b27SAndreas Gohr 114*92200750SAndreas Gohr /** 115*92200750SAndreas Gohr * Resizing parameters from fetch.php should trigger scaled copies in cache. 116*92200750SAndreas Gohr */ 11738ca8b27SAndreas Gohr public function testResolveAppliesResizeParameter(): void 11838ca8b27SAndreas Gohr { 11938ca8b27SAndreas Gohr global $conf; 12038ca8b27SAndreas Gohr 12138ca8b27SAndreas Gohr $input = DOKU_URL . 'lib/exe/fetch.php?w=32&media=wiki:dokuwiki-128.png'; 12238ca8b27SAndreas Gohr $original = $conf['mediadir'] . '/wiki/dokuwiki-128.png'; 12338ca8b27SAndreas Gohr $resolved = $this->resolver->resolve($input); 12438ca8b27SAndreas Gohr 12538ca8b27SAndreas Gohr $this->assertNotNull($resolved); 12638ca8b27SAndreas Gohr $this->assertNotSame($original, $resolved['path']); 12738ca8b27SAndreas Gohr $this->assertSame('image/png', $resolved['mime']); 12838ca8b27SAndreas Gohr $this->assertFileExists($resolved['path']); 12938ca8b27SAndreas Gohr } 13038ca8b27SAndreas Gohr} 131