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

[10.x] Fixes singleton and api singletons creatable|destryoable|only|except combinations #47098

Merged
merged 5 commits into from
May 18, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 7 additions & 20 deletions src/Illuminate/Routing/ResourceRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Illuminate\Routing;

use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class ResourceRegistrar
Expand Down Expand Up @@ -149,6 +148,12 @@ public function singleton($name, $controller, array $options = [])

$defaults = $this->singletonResourceDefaults;

if (isset($options['creatable'])) {
$defaults = array_merge($defaults, ['create', 'store', 'destroy']);
} elseif (isset($options['destroyable'])) {
$defaults = array_merge($defaults, ['destroy']);
}

$collection = new RouteCollection;

$resourceMethods = $this->getResourceMethods($defaults, $options);
Expand Down Expand Up @@ -249,25 +254,7 @@ protected function getResourceMethods($defaults, $options)
$methods = array_diff($methods, (array) $options['except']);
}

if (isset($options['creatable'])) {
$methods = isset($options['apiSingleton'])
? array_merge(['store', 'destroy'], $methods)
: array_merge(['create', 'store', 'destroy'], $methods);

return $this->getResourceMethods(
$methods, array_values(Arr::except($options, ['creatable']))
);
}

if (isset($options['destroyable'])) {
$methods = array_merge(['destroy'], $methods);

return $this->getResourceMethods(
$methods, array_values(Arr::except($options, ['destroyable']))
);
}

return $methods;
return array_values($methods);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,14 @@ public function apiSingletons(array $singletons, array $options = [])
*/
public function apiSingleton($name, $controller, array $options = [])
{
$only = ['show', 'update', 'destroy'];
$only = ['store', 'show', 'update', 'destroy'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jessarcher why was store added here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We previously had some singleton-specific overrides in getResourceMethods that were applied after only, leading to unexpected behavior.

Now, the creatable and destroyable methods just expand the defaults that are passed to getResourceMethods, which can then be reduced by only.

We need to specify store here so that it will be included when using creatable. It's safe to include even when creatable is not called because only can only reduce the defaults, but not expand them.


if (isset($options['except'])) {
$only = array_diff($only, (array) $options['except']);
}

return $this->singleton($name, $controller, array_merge([
'only' => $only,
'apiSingleton' => true,
], $options));
}

Expand Down
264 changes: 262 additions & 2 deletions tests/Integration/Routing/RouteSingletonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,58 @@ public function testCreatableSingleton()
$this->assertSame('singleton destroy', $response->getContent());
}

public function testCreatableSingletonOnly()
{
Route::singleton('avatar', CreatableSingletonTestController::class)->creatable()->only('show');

$response = $this->get('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->get('/avatar/create');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->post('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->get('/avatar/edit');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->put('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->patch('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->delete('/avatar');
$this->assertEquals(405, $response->getStatusCode());
}

public function testCreatableSingletonExcept()
{
Route::singleton('avatar', CreatableSingletonTestController::class)->creatable()->except('show');

$response = $this->get('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->get('/avatar/create');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->post('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->get('/avatar/edit');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->put('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->patch('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->delete('/avatar');
$this->assertEquals(200, $response->getStatusCode());
}

public function testDestroyableSingleton()
{
Route::singleton('avatar', CreatableSingletonTestController::class)->destroyable();
Expand All @@ -87,15 +139,93 @@ public function testDestroyableSingleton()
$this->assertSame('singleton destroy', $response->getContent());
}

public function testDestroyableSingletonOnly()
{
Route::singleton('avatar', SingletonTestController::class)->destroyable()->only('destroy');

$response = $this->get('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->get('/avatar/create');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->post('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->get('/avatar/edit');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->put('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->patch('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->delete('/avatar');
$this->assertEquals(200, $response->getStatusCode());
}

public function testDestroyableSingletonExcept()
{
Route::singleton('avatar', SingletonTestController::class)->destroyable()->except('destroy');

$response = $this->get('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->get('/avatar/create');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->post('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->get('/avatar/edit');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->put('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->patch('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->delete('/avatar');
$this->assertEquals(405, $response->getStatusCode());
}

public function testCreatableDestroyableSingletonOnlyExceptTest()
{
Route::singleton('avatar', SingletonTestController::class)->creatable()->destroyable()->only(['show'])->except(['destroy']);

$response = $this->get('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->get('/avatar/create');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->post('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->get('/avatar/edit');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->put('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->patch('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->delete('/avatar');
$this->assertEquals(405, $response->getStatusCode());
}

public function testApiSingleton()
{
Route::apiSingleton('avatar', SingletonTestController::class);

$response = $this->get('/avatar/create');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->post('/avatar/store');
$this->assertEquals(404, $response->getStatusCode());
$response = $this->post('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$this->assertSame('http://localhost/avatar', route('avatar.update'));
$response = $this->put('/avatar');
Expand All @@ -121,6 +251,58 @@ public function testCreatableApiSingleton()
$this->assertSame('singleton update', $response->getContent());
}

public function testCreatableApiSingletonOnly()
{
Route::apiSingleton('avatar', CreatableSingletonTestController::class)->creatable()->only(['create', 'store']);

$response = $this->get('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->get('/avatar/create');
$this->assertEquals(200, $response->getStatusCode());
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved

$response = $this->post('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->get('/avatar/edit');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->put('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->patch('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->delete('/avatar');
$this->assertEquals(405, $response->getStatusCode());
}

public function testCreatableApiSingletonExcept()
{
Route::apiSingleton('avatar', CreatableSingletonTestController::class)->creatable()->except(['create', 'store']);

$response = $this->get('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->get('/avatar/create');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->post('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->get('/avatar/edit');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->put('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->patch('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->delete('/avatar');
$this->assertEquals(200, $response->getStatusCode());
}

public function testDestroyableApiSingleton()
{
Route::apiSingleton('avatar', CreatableSingletonTestController::class)->destroyable();
Expand All @@ -141,6 +323,84 @@ public function testDestroyableApiSingleton()
$this->assertSame('singleton destroy', $response->getContent());
}

public function testDestroyableApiSingletonOnly()
{
Route::apiSingleton('avatar', CreatableSingletonTestController::class)->destroyable()->only(['destroy']);

$response = $this->get('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->get('/avatar/create');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->post('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->get('/avatar/edit');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->put('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->patch('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->delete('/avatar');
$this->assertEquals(200, $response->getStatusCode());
}

public function testDestroyableApiSingletonExcept()
{
Route::apiSingleton('avatar', CreatableSingletonTestController::class)->destroyable()->except(['destroy', 'show']);

$response = $this->get('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->get('/avatar/create');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->post('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->get('/avatar/edit');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->put('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->patch('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->delete('/avatar');
$this->assertEquals(405, $response->getStatusCode());
}

public function testCreatableDestroyableApiSingletonOnlyExceptTest()
{
Route::apiSingleton('avatar', CreatableSingletonTestController::class)->creatable()->destroyable()->only(['show'])->except(['destroy']);

$response = $this->get('/avatar');
$this->assertEquals(200, $response->getStatusCode());

$response = $this->get('/avatar/create');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->post('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->get('/avatar/edit');
$this->assertEquals(404, $response->getStatusCode());

$response = $this->put('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->patch('/avatar');
$this->assertEquals(405, $response->getStatusCode());

$response = $this->delete('/avatar');
$this->assertEquals(405, $response->getStatusCode());
}

public function testSingletonOnly()
{
Route::singleton('avatar', SingletonTestController::class)->only('show');
Expand Down