Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Create edit/{id} route for control panel access #11092

Merged
merged 3 commits into from
Nov 7, 2024
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
3 changes: 3 additions & 0 deletions routes/cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Statamic\Http\Controllers\CP\Collections\CollectionBlueprintsController;
use Statamic\Http\Controllers\CP\Collections\CollectionsController;
use Statamic\Http\Controllers\CP\Collections\CollectionTreeController;
use Statamic\Http\Controllers\CP\Collections\EditRedirectController;
use Statamic\Http\Controllers\CP\Collections\EntriesController;
use Statamic\Http\Controllers\CP\Collections\EntryActionController;
use Statamic\Http\Controllers\CP\Collections\EntryPreviewController;
Expand Down Expand Up @@ -361,5 +362,7 @@

Route::view('/playground', 'statamic::playground')->name('playground');

Route::get('edit/{id}', EditRedirectController::class);

Route::get('{segments}', [CpController::class, 'pageNotFound'])->where('segments', '.*')->name('404');
});
20 changes: 20 additions & 0 deletions src/Http/Controllers/CP/Collections/EditRedirectController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Statamic\Http\Controllers\CP\Collections;

use Illuminate\Http\Request;
use Statamic\Exceptions\NotFoundHttpException;
use Statamic\Facades\Data;
use Statamic\Http\Controllers\CP\CpController;

class EditRedirectController extends CpController
{
public function __invoke(Request $request)
{
if ($data = Data::find($request->id)) {
return redirect($data->editUrl());
}

throw new NotFoundHttpException;
}
}
39 changes: 39 additions & 0 deletions tests/CP/EditRedirectControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Tests\CP;

use Mockery;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Data;
use Statamic\Facades\User;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;

class EditRedirectControllerTest extends TestCase
{
use PreventSavingStacheItemsToDisk;

#[Test]
public function it_redirects_to_edit_page()
{
$item = Mockery::mock();
$item->shouldReceive('editUrl')->once()->andReturn($targetUrl = '/somewhere');
Data::shouldReceive('find')->with('123')->once()->andReturn($item);

$this
->actingAs(tap(User::make()->makeSuper())->save())
->get('/cp/edit/123')
->assertRedirect($targetUrl);
}

#[Test]
public function it_404s_if_id_doesnt_exist()
{
Data::shouldReceive('find')->with('123')->once()->andReturnNull();

$this
->actingAs(tap(User::make()->makeSuper())->save())
->get('/cp/edit/123')
->assertNotFound();
}
}
Loading