Resolves Mod Detail Page Version Issue

The latest version on the mod detail page was being selected by created date instead of highest version number. This has been resolved. Also adds a test for this issue.
This commit is contained in:
Refringe 2024-08-09 12:35:46 -04:00
parent 713ea7e076
commit 593b44150c
Signed by: Refringe
SSH Key Fingerprint: SHA256:t865XsQpfTeqPRBMN2G6+N8wlDjkgUCZF3WGW6O9N/k
5 changed files with 54 additions and 7 deletions

View File

@ -45,7 +45,7 @@ class ModController extends Controller
$this->authorize('view', $mod);
$latestVersion = $mod->versions->sortByDesc('created_at')->first();
$latestVersion = $mod->versions->sortByDesc('version')->first();
return view('mod.show', compact(['mod', 'latestVersion']));
}

View File

@ -80,10 +80,7 @@ class ModResource extends JsonResource
->values()
),
'links' => [
'self' => route('mod.show', [
'mod' => $this->id,
'slug' => $this->slug,
]),
'self' => $this->detailUrl(),
],
];
}

View File

@ -154,6 +154,14 @@ class Mod extends Model
return $filters->apply($builder);
}
/**
* Build the URL to the mod's detail page.
*/
public function detailUrl(): string
{
return route('mod.show', [$this->id, $this->slug]);
}
/**
* The attributes that should be cast to native types.
*/

View File

@ -114,7 +114,7 @@
{{ __('Dependencies:') }}
@foreach ($version->dependencies as $dependency)
@if ($dependency->resolvedVersion?->mod)
<a href="{{ route('mod.show', [$dependency->resolvedVersion->mod->id, $dependency->resolvedVersion->mod->slug]) }}">
<a href="{{ $dependency->resolvedVersion->mod->detailUrl() }}">
{{ $dependency->resolvedVersion->mod->name }}&nbsp;({{ $dependency->resolvedVersion->version }})
</a>@if (!$loop->last), @endif
@endif
@ -184,7 +184,7 @@
<h3>{{ __('Latest Version Dependencies') }}</h3>
<p class="truncate">
@foreach ($latestVersion->dependencies as $dependency)
<a href="{{ route('mod.show', [$dependency->resolvedVersion->mod->id, $dependency->resolvedVersion->mod->slug]) }}">
<a href="{{ $dependency->resolvedVersion->mod->detailUrl() }}">
{{ $dependency->resolvedVersion->mod->name }}&nbsp;({{ $dependency->resolvedVersion->version }})
</a><br />
@endforeach

42
tests/Feature/ModTest.php Normal file
View File

@ -0,0 +1,42 @@
<?php
use App\Models\Mod;
use App\Models\ModVersion;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('shows the latest version on the mod detail page', function () {
// Create a mod instance
$mod = Mod::factory()->create();
// Create 5 mod versions with specified versions
$versions = [
'1.0.0',
'1.1.0',
'1.2.0',
'2.0.0',
'2.1.0',
];
// get the highest version in the array
$latestVersion = max($versions);
foreach ($versions as $version) {
ModVersion::factory()->create([
'mod_id' => $mod->id,
'version' => $version,
]);
}
// Make a request to the mod's detail URL
$response = $this->get($mod->detailUrl());
$this->assertEquals('2.1.0', $latestVersion);
// Assert the latest version is next to the mod's name
$response->assertSeeInOrder(explode(' ', "$mod->name $latestVersion"));
// Assert the latest version is in the latest download button
$response->assertSeeText(__('Download Latest Version')." ($latestVersion)");
});