Skip to content

Commit 8841068

Browse files
committed
Add new helper to get a relative web link to the media file
1 parent 687b119 commit 8841068

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

packages/framework/src/Support/Filesystem/MediaFile.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ public function getHash(): string
135135
return $this->hash;
136136
}
137137

138+
/**
139+
* Get a relative web link to the media file.
140+
*/
141+
public function getLink(): string
142+
{
143+
return Hyde::asset($this->getIdentifier());
144+
}
145+
138146
/** @internal */
139147
public static function getCacheBustKey(string $file): string
140148
{

packages/framework/tests/Unit/Support/MediaFileUnitTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
namespace Hyde\Framework\Testing\Unit\Support;
66

77
use Mockery;
8+
use Illuminate\View\View;
9+
use Illuminate\View\Factory;
10+
use Hyde\Support\Facades\Render;
11+
use Hyde\Support\Models\RenderData;
812
use Hyde\Framework\Exceptions\FileNotFoundException;
913
use Hyde\Hyde;
1014
use Hyde\Support\Filesystem\MediaFile;
@@ -51,6 +55,7 @@ protected function setUp(): void
5155
// Set up default expectations for commonly called methods
5256
$this->mockFilesystem->shouldReceive('isFile')->andReturn(true)->byDefault();
5357
$this->mockFilesystem->shouldReceive('missing')->andReturn(false)->byDefault();
58+
$this->mockFilesystem->shouldReceive('exists')->andReturn(true)->byDefault();
5459
$this->mockFilesystem->shouldReceive('extension')->andReturn('txt')->byDefault();
5560
$this->mockFilesystem->shouldReceive('size')->andReturn(12)->byDefault();
5661
$this->mockFilesystem->shouldReceive('mimeType')->andReturn('text/plain')->byDefault();
@@ -60,6 +65,12 @@ protected function setUp(): void
6065
// Mock Hyde facade
6166
$hyde = Mockery::mock(Hyde::kernel())->makePartial();
6267
$hyde->shouldReceive('assets')->andReturn(collect(['app.css' => new MediaFile('_media/app.css')]))->byDefault();
68+
69+
// Mock render data
70+
\Illuminate\Support\Facades\View::swap(Mockery::mock(Factory::class)->makePartial());
71+
Render::swap(new RenderData());
72+
73+
self::mockConfig(['hyde.enable_cache_busting' => false]);
6374
}
6475

6576
protected function tearDown(): void
@@ -389,4 +400,87 @@ public function testOutputPathWithLeadingSlash()
389400
{
390401
$this->assertSame(Hyde::sitePath('media/foo'), MediaFile::outputPath('/foo'));
391402
}
403+
404+
public function testGetLink()
405+
{
406+
$file = MediaFile::make('foo.txt');
407+
$this->assertSame('media/foo.txt', $file->getLink());
408+
}
409+
410+
public function testGetLinkWithCustomMediaDirectory()
411+
{
412+
Hyde::setMediaDirectory('custom_media');
413+
$file = MediaFile::make('foo.txt');
414+
$this->assertSame('custom_media/foo.txt', $file->getLink());
415+
Hyde::setMediaDirectory('_media'); // Reset to default
416+
}
417+
418+
public function testGetLinkWithPrettyUrls()
419+
{
420+
self::mockConfig(['hyde.enable_cache_busting' => false, 'hyde.pretty_urls' => true]);
421+
$file = MediaFile::make('foo.txt');
422+
$this->assertSame('media/foo.txt', $file->getLink());
423+
}
424+
425+
public function testGetLinkWithNestedFile()
426+
{
427+
$file = MediaFile::make('subdirectory/foo.txt');
428+
$this->assertSame('media/subdirectory/foo.txt', $file->getLink());
429+
}
430+
431+
public function testGetLinkWithBaseUrl()
432+
{
433+
self::mockConfig(['hyde.enable_cache_busting' => false, 'hyde.url' => 'https://example.com']);
434+
$file = MediaFile::make('foo.txt');
435+
$this->assertSame('https://example.com/media/foo.txt', $file->getLink());
436+
}
437+
438+
public function testGetLinkWithBaseUrlAndPrettyUrls()
439+
{
440+
self::mockConfig([
441+
'hyde.enable_cache_busting' => false,
442+
'hyde.url' => 'https://example.com',
443+
'hyde.pretty_urls' => true
444+
]);
445+
$file = MediaFile::make('foo.txt');
446+
$this->assertSame('https://example.com/media/foo.txt', $file->getLink());
447+
}
448+
449+
public function testGetLinkWithCacheBusting()
450+
{
451+
self::mockConfig(['hyde.enable_cache_busting' => true]);
452+
$this->mockFilesystem->shouldReceive('hash')
453+
->andReturn('abc123');
454+
$file = MediaFile::make('foo.txt');
455+
$this->assertSame('media/foo.txt?v=abc123', $file->getLink());
456+
}
457+
458+
public function testGetLinkWithCacheBustingDisabled()
459+
{
460+
self::mockConfig(['hyde.enable_cache_busting' => false]);
461+
$file = MediaFile::make('foo.txt');
462+
$this->assertSame('media/foo.txt', $file->getLink());
463+
}
464+
465+
public function testGetLinkWithCurrentPageContext()
466+
{
467+
$this->mockCurrentPage('foo/bar');
468+
$file = MediaFile::make('baz.txt');
469+
$this->assertSame('../media/baz.txt', $file->getLink());
470+
}
471+
472+
public function testGetLinkWithCurrentPageContextAndCustomMediaDirectory()
473+
{
474+
Hyde::setMediaDirectory('custom_media');
475+
$this->mockCurrentPage('foo/bar');
476+
$file = MediaFile::make('baz.txt');
477+
$this->assertSame('../custom_media/baz.txt', $file->getLink());
478+
Hyde::setMediaDirectory('_media'); // Reset to default
479+
}
480+
481+
// Helper method to mock the current page
482+
protected function mockCurrentPage(string $page): void
483+
{
484+
Render::shouldReceive('getRouteKey')->andReturn($page);
485+
}
392486
}

0 commit comments

Comments
 (0)