-
-
Notifications
You must be signed in to change notification settings - Fork 554
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[5.x] Create edit/{id} route for control panel access (#11092)
Co-authored-by: Jason Varga <[email protected]>
- Loading branch information
1 parent
31fe562
commit c6727f3
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Http/Controllers/CP/Collections/EditRedirectController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |