Skip to content

Commit 5c77557

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

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-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: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
namespace Hyde\Framework\Testing\Unit\Support;
66

77
use Mockery;
8+
use Illuminate\View\Factory;
9+
use Hyde\Support\Facades\Render;
10+
use Hyde\Support\Models\RenderData;
811
use Hyde\Framework\Exceptions\FileNotFoundException;
912
use Hyde\Hyde;
1013
use Hyde\Support\Filesystem\MediaFile;
@@ -51,6 +54,7 @@ protected function setUp(): void
5154
// Set up default expectations for commonly called methods
5255
$this->mockFilesystem->shouldReceive('isFile')->andReturn(true)->byDefault();
5356
$this->mockFilesystem->shouldReceive('missing')->andReturn(false)->byDefault();
57+
$this->mockFilesystem->shouldReceive('exists')->andReturn(true)->byDefault();
5458
$this->mockFilesystem->shouldReceive('extension')->andReturn('txt')->byDefault();
5559
$this->mockFilesystem->shouldReceive('size')->andReturn(12)->byDefault();
5660
$this->mockFilesystem->shouldReceive('mimeType')->andReturn('text/plain')->byDefault();
@@ -60,6 +64,12 @@ protected function setUp(): void
6064
// Mock Hyde facade
6165
$hyde = Mockery::mock(Hyde::kernel())->makePartial();
6266
$hyde->shouldReceive('assets')->andReturn(collect(['app.css' => new MediaFile('_media/app.css')]))->byDefault();
67+
68+
// Mock render data
69+
\Illuminate\Support\Facades\View::swap(Mockery::mock(Factory::class)->makePartial());
70+
Render::swap(new RenderData());
71+
72+
self::mockConfig(['hyde.enable_cache_busting' => false]);
6373
}
6474

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

0 commit comments

Comments
 (0)