Skip to content

Commit

Permalink
Rework the admin configuration to be a more general draft paths option
Browse files Browse the repository at this point in the history
  • Loading branch information
Kurt Friars committed Mar 8, 2024
1 parent e003a7d commit d750c11
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ When the value configured in `previewKey` is present in the GET query, AND the u
 
### Admin Panel
### Draft Paths
It is assumed that your Admin panel should always allow draft content to be visible. If you are using the package's middleware, you can specify the route prefix of your admin panel by setting the `admin.path` key in the configuration file.
You can specify [request path patterns](https://laravel.com/api/10.x/Illuminate/Http/Request.html#method_is) for endpoints you wish to force draft content to be enabled on. This is especially important for your Admin Panel and CMS routes, as you will want to be working with unpublished content during the editing/creation process.
When this key is set all routes that start with the specified prefix will always have draft content enabled.
By default, this package defaults to the Plank's standard for using Laravel Nova with `admin/*`, `nova-api/*` and `nova-vendor/*` defined.
 
Expand Down
6 changes: 4 additions & 2 deletions config/publisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
'rewrite' => true,
'previewKey' => 'preview',
],
'admin' => [
'path' => 'admin',
'draft_paths' => [
'admin*',
'nova-api*',
'nova-vendor*',
],
];
6 changes: 4 additions & 2 deletions src/Services/PublisherService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public function shouldEnableDraftContent(Request $request): bool
return false;
}

if (($path = config()->get('publisher.admin.path')) && str($request->path())->startsWith($path)) {
return true;
if ($patterns = config()->get('publisher.draft_paths')) {
if ($request->is($patterns)) {
return true;
}
}

return (bool) $request->query(config('publisher.urls.previewKey'));
Expand Down
10 changes: 10 additions & 0 deletions tests/Feature/DraftPathsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use function Pest\Laravel\get;

it('correctly forces draft content on the configured draft_paths', function () {
get('pages/1')->assertSee('Draft Content Restricted');
get('admin')->assertSee('Draft Content Visible');
get('admin/dashboard')->assertSee('Draft Content Visible');
get('admin/resources/pages/1/details')->assertSee('Draft Content Visible');
});
8 changes: 5 additions & 3 deletions tests/Feature/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use Plank\Publisher\Tests\Helpers\Models\Post;
use Plank\Publisher\Tests\Helpers\Models\User;

use function Pest\Laravel\get;

beforeEach(function () {
$draft = Post::create([
'author_id' => User::first()->id,
Expand All @@ -19,7 +21,7 @@
});

it('retrieves model attributes when not using the previewKey via middleware', function () {
$response = $this->get('/posts/1');
$response = get('/posts/1');
$response->assertStatus(200);

$post = $response->original;
Expand All @@ -28,7 +30,7 @@
});

it('retrieves draft attributes when using the previewKey via middleware', function () {
$response = $this->get('/posts/1?preview=true');
$response = get('/posts/1?preview=true');
$response->assertStatus(200);

$post = $response->original;
Expand All @@ -41,7 +43,7 @@
return false;
});

$response = $this->get('/posts/1?preview=true');
$response = get('/posts/1?preview=true');
$response->assertStatus(200);

$post = $response->original;
Expand Down
10 changes: 10 additions & 0 deletions tests/Helpers/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@
namespace Tests\Helpers\Controllers;

use Illuminate\Routing\Controller;
use Plank\Publisher\Facades\Publisher;
use Plank\Publisher\Tests\Helpers\Models\Post;

class PostController extends Controller
{
public function index()
{
if (Publisher::draftContentAllowed()) {
return 'Draft Content Index';
}

return 'Published Content Index';
}

public function show($id)
{
return Post::findOrFail($id);
Expand Down
18 changes: 18 additions & 0 deletions tests/Helpers/Controllers/TestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Tests\Helpers\Controllers;

use Illuminate\Routing\Controller;
use Plank\Publisher\Facades\Publisher;

class TestController extends Controller
{
public function test()
{
if (Publisher::draftContentAllowed()) {
return response('Draft Content Visible');
}

return response('Draft Content Restricted');
}
}
21 changes: 21 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Orchestra\Testbench\TestCase as Orchestra;
use Plank\Publisher\PublisherServiceProvider;
use Plank\Publisher\Tests\Helpers\Models\User;
use Tests\Helpers\Controllers\TestController;
use Tests\Helpers\Controllers\PostController;

class TestCase extends Orchestra
Expand Down Expand Up @@ -44,5 +45,25 @@ public function getEnvironmentSetUp($app)
->middleware('publisher')
->get('posts/{id}', [PostController::class, 'show'])
->name('posts.show');

$app['router']
->middleware('publisher')
->get('pages/{id}', [TestController::class, 'test'])
->name('pages.show');

$app['router']
->middleware('publisher')
->get('admin', [TestController::class, 'test'])
->name('admin');

$app['router']
->middleware('publisher')
->get('admin/dashboard', [TestController::class, 'test'])
->name('admin.dashboard');

$app['router']
->middleware('publisher')
->get('admin/resources/{resource}/{resourceId}/details', [TestController::class, 'test'])
->name('admin.show');
}
}

0 comments on commit d750c11

Please sign in to comment.