-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7.x] Fix error when running resource actions (#638)
Co-authored-by: duncanmcclean <duncanmcclean@users.noreply.github.com>
- Loading branch information
1 parent
faece2f
commit e3e2ddd
Showing
16 changed files
with
147 additions
and
80 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
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
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
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,47 @@ | ||
<?php | ||
|
||
namespace StatamicRadPack\Runway\Http\Controllers\CP; | ||
|
||
use Illuminate\Http\Request; | ||
use Statamic\Facades\Action; | ||
use Statamic\Http\Controllers\CP\ActionController; | ||
use StatamicRadPack\Runway\Resource; | ||
|
||
class ModelActionController extends ActionController | ||
{ | ||
use Traits\ExtractsFromModelFields; | ||
|
||
protected $resource; | ||
|
||
public function runAction(Request $request, Resource $resource) | ||
{ | ||
$this->resource = $resource; | ||
|
||
return parent::run($request); | ||
} | ||
|
||
public function bulkActionsList(Request $request, Resource $resource) | ||
{ | ||
$this->resource = $resource; | ||
|
||
return parent::bulkActions($request); | ||
} | ||
|
||
protected function getSelectedItems($items, $context) | ||
{ | ||
return $this->resource->findMany($items); | ||
} | ||
|
||
protected function getItemData($model, $context): array | ||
{ | ||
$blueprint = $this->resource->blueprint(); | ||
|
||
[$values] = $this->extractFromFields($model, $this->resource, $blueprint); | ||
|
||
return [ | ||
'title' => $model->getAttribute($this->resource->titleField()), | ||
'values' => array_merge($values, ['id' => $model->getKey()]), | ||
'itemActions' => Action::for($model, $context), | ||
]; | ||
} | ||
} |
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
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
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
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
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
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
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,67 @@ | ||
<?php | ||
|
||
namespace StatamicRadPack\Runway\Tests\Http\Controllers\CP; | ||
|
||
use PHPUnit\Framework\Attributes\Test; | ||
use Statamic\Actions\Action; | ||
use Statamic\Facades\User; | ||
use StatamicRadPack\Runway\Tests\Fixtures\Models\Post; | ||
use StatamicRadPack\Runway\Tests\TestCase; | ||
|
||
class ModelActionControllerTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
FooAction::register(); | ||
} | ||
|
||
#[Test] | ||
public function can_run_action() | ||
{ | ||
$post = Post::factory()->create(); | ||
|
||
$this->assertFalse(FooAction::$hasRun); | ||
|
||
$this | ||
->actingAs(User::make()->makeSuper()->save()) | ||
->post('/cp/runway/post/models/actions', [ | ||
'action' => 'foo', | ||
'selections' => [$post->id], | ||
'values' => [], | ||
]) | ||
->assertOk() | ||
->assertJson(['message' => 'Foo action run!']); | ||
|
||
$this->assertTrue(FooAction::$hasRun); | ||
} | ||
|
||
#[Test] | ||
public function can_get_bulk_actions_list() | ||
{ | ||
$post = Post::factory()->create(); | ||
|
||
$this | ||
->actingAs(User::make()->makeSuper()->save()) | ||
->post('/cp/runway/post/models/actions/list', [ | ||
'selections' => [$post->id], | ||
]) | ||
->assertOk() | ||
->assertJsonPath('0.handle', 'unpublish'); | ||
} | ||
} | ||
|
||
class FooAction extends Action | ||
{ | ||
protected static $handle = 'foo'; | ||
|
||
public static bool $hasRun = false; | ||
|
||
public function run($items, $values) | ||
{ | ||
static::$hasRun = true; | ||
|
||
return 'Foo action run!'; | ||
} | ||
} |
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
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
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
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
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