Skip to content

Commit

Permalink
[5.x] Create edit/{id} route for control panel access (#11092)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
aaronbushnell and jasonvarga authored Nov 7, 2024
1 parent 31fe562 commit c6727f3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
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();
}
}

0 comments on commit c6727f3

Please sign in to comment.