Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/Http/Controllers/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Relaticle\Ink\Http\Controllers;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
Expand Down Expand Up @@ -39,14 +40,12 @@ public function index(Request $request): View
->withQueryString();

seo()->for(BlogListingSeo::forIndex(
page: (int) $request->query('page', 1),
page: $posts->currentPage(),
searchQuery: $request->query('q'),
locale: $locale,
));

return view($this->viewFor('index'), [
'posts' => $posts,
]);
return $this->archive('index', $posts);
}

public function show(string $slug): View
Expand Down Expand Up @@ -84,14 +83,11 @@ public function category(string $slug): View

seo()->for(BlogListingSeo::forCategory(
category: $category,
page: (int) request()->query('page', 1),
page: $posts->currentPage(),
locale: $locale,
));

return view($this->viewFor('category'), [
'category' => $category,
'posts' => $posts,
]);
return $this->archive('category', $posts, ['category' => $category]);
}

public function preview(Post $post): View
Expand Down Expand Up @@ -127,14 +123,11 @@ public function tag(string $slug): View

seo()->for(BlogListingSeo::forTag(
tag: $tag,
page: (int) request()->query('page', 1),
page: $posts->currentPage(),
locale: $locale,
));

return view($this->viewFor('tag'), [
'tag' => $tag,
'posts' => $posts,
]);
return $this->archive('tag', $posts, ['tag' => $tag]);
}

public function feed(): Response
Expand All @@ -154,6 +147,17 @@ public function feed(): Response
->header('Content-Type', 'application/rss+xml; charset=UTF-8');
}

private function archive(string $key, LengthAwarePaginator $posts, array $data = []): View
{
$view = view($this->viewFor($key), ['posts' => $posts, ...$data]);

if ($posts->currentPage() > $posts->lastPage()) {
abort(response($view, Response::HTTP_NOT_FOUND));
}

return $view;
}

private function viewFor(string $key): string
{
$view = config("ink.views.{$key}");
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/BlogPaginationViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Illuminate\Support\Facades\Route;
use Relaticle\Ink\InkServiceProvider;
use Relaticle\Ink\Models\Category;
use Relaticle\Ink\Models\Post;
use Relaticle\Ink\Models\Tag;

beforeEach(function () {
config()->set('ink.features.public_routes', true);
Expand Down Expand Up @@ -34,3 +36,23 @@
$response->assertOk();
$response->assertSee('aria-current="page"', escape: false);
});

it('serves the archive view with a 404 beyond the filtered last page', function () {
config()->set('ink.features.tags', true);
$category = Category::factory()->create();
$tag = Tag::factory()->create();
$post = Post::factory()->published()->create(['category_id' => $category->id]);
$post->tags()->attach($tag);
Post::factory(2)->published()->create();

$this->get('/blog?page=2')->assertOk();
$this->get('/blog?page=3')->assertNotFound()->assertViewIs('ink::pages.index');
$this->get("/blog/category/{$category->slug}?page=2")->assertNotFound()->assertViewIs('ink::pages.category');
$this->get("/blog/tag/{$tag->slug}?page=2")->assertNotFound()->assertViewIs('ink::pages.tag');
});

it('serves empty first pages and rejects later pages of an empty search', function () {
$this->get('/blog')->assertOk();
$this->get('/blog?q=missing')->assertOk();
$this->get('/blog?q=missing&page=2')->assertNotFound()->assertViewIs('ink::pages.index');
});