diff --git a/src/Feature/Http/Controllers/Resource/Playground/CreateJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/CreateJsonTrait.php index 794c6aa..b883135 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/CreateJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/CreateJsonTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Playground\Test\Models\PlaygroundUser as User; /** @@ -11,6 +12,11 @@ */ trait CreateJsonTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ diff --git a/src/Feature/Http/Controllers/Resource/Playground/CreateTrait.php b/src/Feature/Http/Controllers/Resource/Playground/CreateTrait.php index 9c1221d..250e49d 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/CreateTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/CreateTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Playground\Test\Models\PlaygroundUser as User; /** @@ -11,6 +12,11 @@ */ trait CreateTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ diff --git a/src/Feature/Http/Controllers/Resource/Playground/DestroyJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/DestroyJsonTrait.php index 1a4a542..bec842f 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/DestroyJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/DestroyJsonTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Playground\Test\Models\PlaygroundUser as User; /** @@ -11,6 +12,11 @@ */ trait DestroyJsonTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ @@ -20,7 +26,7 @@ public function test_json_guest_cannot_destroy() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); @@ -52,7 +58,7 @@ public function test_json_destroy_as_admin_and_succeed() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -96,7 +102,7 @@ public function test_json_destroy_as_admin_and_succeed_with_force_delete() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -131,7 +137,7 @@ public function test_json_destroy_as_admin_and_succeed_with_no_content() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -171,7 +177,7 @@ public function test_json_destroy_as_admin_and_succeed_and_ignore_redirect() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -218,7 +224,7 @@ public function test_json_destroy_as_user_and_get_denied_and_no_force_delete_all { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->create(); diff --git a/src/Feature/Http/Controllers/Resource/Playground/DestroyTrait.php b/src/Feature/Http/Controllers/Resource/Playground/DestroyTrait.php index dc5df8f..49cfdae 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/DestroyTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/DestroyTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Playground\Test\Models\PlaygroundUser as User; /** @@ -11,6 +12,11 @@ */ trait DestroyTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ @@ -20,7 +26,7 @@ public function test_guest_cannot_destroy() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); @@ -52,7 +58,7 @@ public function test_destroy_as_admin_and_succeed() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -93,7 +99,7 @@ public function test_destroy_as_admin_and_succeed_with_force_delete() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -128,7 +134,7 @@ public function test_destroy_as_admin_and_succeed_with_redirect_to_index_with_tr { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -175,7 +181,7 @@ public function test_destroy_as_user_and_get_denied_and_no_force_delete_allowed( { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->create(); diff --git a/src/Feature/Http/Controllers/Resource/Playground/EditJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/EditJsonTrait.php index d8e1338..6970987 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/EditJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/EditJsonTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Playground\Test\Models\PlaygroundUser as User; /** @@ -11,6 +12,11 @@ */ trait EditJsonTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ @@ -25,7 +31,7 @@ public function test_json_guest_cannot_get_edit_info() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); @@ -44,7 +50,7 @@ public function test_json_admin_can_get_edit_info() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); @@ -73,7 +79,7 @@ public function test_json_edit_as_admin_view_rendered_by_user_with_invalid_param { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); diff --git a/src/Feature/Http/Controllers/Resource/Playground/EditTrait.php b/src/Feature/Http/Controllers/Resource/Playground/EditTrait.php index 7753645..88368d0 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/EditTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/EditTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Playground\Test\Models\PlaygroundUser as User; /** @@ -11,6 +12,11 @@ */ trait EditTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ @@ -20,7 +26,7 @@ public function test_guest_cannot_render_edit_view() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); @@ -40,7 +46,7 @@ public function test_admin_can_render_edit_view() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); @@ -64,7 +70,7 @@ public function test_admin_can_render_edit_view_with_return_url() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); @@ -96,7 +102,7 @@ public function test_edit_view_as_admin_with_invalid_parameter_and_fail_validati { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); diff --git a/src/Feature/Http/Controllers/Resource/Playground/IndexJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/IndexJsonTrait.php index d2c94ff..45003dc 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/IndexJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/IndexJsonTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Playground\Test\Models\PlaygroundUser as User; /** @@ -11,6 +12,11 @@ */ trait IndexJsonTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ @@ -25,13 +31,11 @@ public function test_json_guest_cannot_get_index() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); - $url = route($packageInfo['model_route'], [ - $packageInfo['model_slug'] => $model->id, - ]); + $url = route($packageInfo['model_route']); $response = $this->getJson($url); $response->assertStatus(403); @@ -41,15 +45,13 @@ public function test_json_admin_can_get_index() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); $user = User::factory()->admin()->create(); - $url = route($packageInfo['model_route'], [ - $packageInfo['model_slug'] => $model->id, - ]); + $url = route($packageInfo['model_route']); $response = $this->actingAs($user)->getJson($url); diff --git a/src/Feature/Http/Controllers/Resource/Playground/IndexTrait.php b/src/Feature/Http/Controllers/Resource/Playground/IndexTrait.php index daa7439..d5ed5a9 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/IndexTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/IndexTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Playground\Test\Models\PlaygroundUser as User; /** @@ -11,6 +12,11 @@ */ trait IndexTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ @@ -20,13 +26,11 @@ public function test_guest_cannot_render_index_view() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); - $url = route($packageInfo['model_route'], [ - $packageInfo['model_slug'] => $model->id, - ]); + $url = route($packageInfo['model_route']); $response = $this->get($url); @@ -37,15 +41,13 @@ public function test_admin_can_render_index_view() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); $user = User::factory()->admin()->create(); - $url = route($packageInfo['model_route'], [ - $packageInfo['model_slug'] => $model->id, - ]); + $url = route($packageInfo['model_route']); $response = $this->actingAs($user)->get($url); diff --git a/src/Feature/Http/Controllers/Resource/Playground/LockJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/LockJsonTrait.php index 69564cb..0608559 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/LockJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/LockJsonTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Playground\Test\Models\PlaygroundUser as User; /** @@ -11,6 +12,11 @@ */ trait LockJsonTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ @@ -25,7 +31,7 @@ public function test_json_guest_cannot_lock() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); @@ -57,7 +63,7 @@ public function test_json_lock_as_admin_and_succeed() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -93,7 +99,7 @@ public function test_json_lock_as_admin_and_succeed_with_no_redirect() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -142,7 +148,7 @@ public function test_json_lock_as_user_and_get_denied() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->create(); // $user = User::factory()->admin()->create(); diff --git a/src/Feature/Http/Controllers/Resource/Playground/LockTrait.php b/src/Feature/Http/Controllers/Resource/Playground/LockTrait.php index 4780f8d..d7976a5 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/LockTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/LockTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Playground\Test\Models\PlaygroundUser as User; /** @@ -11,6 +12,11 @@ */ trait LockTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ @@ -20,7 +26,7 @@ public function test_guest_cannot_lock() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); @@ -52,7 +58,7 @@ public function test_lock_as_admin_and_succeed() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -90,7 +96,7 @@ public function test_lock_as_admin_and_succeed_with_redirect_to_index_with_sorte { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -131,7 +137,7 @@ public function test_lock_as_user_and_get_denied() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->create(); diff --git a/src/Feature/Http/Controllers/Resource/Playground/RestoreJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RestoreJsonTrait.php index 2c2ed6d..db88d89 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/RestoreJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/RestoreJsonTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Carbon; use Playground\Test\Models\PlaygroundUser as User; @@ -12,6 +13,11 @@ */ trait RestoreJsonTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ @@ -21,7 +27,7 @@ public function test_json_guest_cannot_restore() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create([ 'deleted_at' => Carbon::now(), @@ -55,7 +61,7 @@ public function test_json_restore_as_admin_and_succeed() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -94,7 +100,7 @@ public function test_json_restore_as_admin_and_succeed_with_no_redirect() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -140,7 +146,7 @@ public function test_json_restore_as_user_and_get_denied() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->create(['role' => 'user']); diff --git a/src/Feature/Http/Controllers/Resource/Playground/RestoreRevisionJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RestoreRevisionJsonTrait.php new file mode 100644 index 0000000..708776d --- /dev/null +++ b/src/Feature/Http/Controllers/Resource/Playground/RestoreRevisionJsonTrait.php @@ -0,0 +1,204 @@ + + */ + abstract public function getGetFqdn(): string; + + /** + * @return class-string + */ + abstract public function getGetFqdnRevision(): string; + + /** + * @return array + */ + abstract public function getPackageInfo(): array; + + abstract public function getRevisionId(): string; + + abstract public function getRevisionRouteParameter(): string; + + /** + * @return array + */ + abstract public function getStructureData(): array; + + public function test_json_guest_cannot_restore_revision() + { + $packageInfo = $this->getPackageInfo(); + + $fqdn = $this->getGetFqdn(); + $fqdn_revision = $this->getGetFqdnRevision(); + + $model = $fqdn::factory()->create(); + + $revision = $fqdn_revision::factory()->create([ + $this->getRevisionId() => $model->id, + 'revision' => 10, + ]); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + + $url = route(sprintf( + '%1$s.revision.restore', + $packageInfo['model_route'] + ), [ + $this->getRevisionRouteParameter() => $revision->id, + ]); + + $response = $this->putJson($url); + + $response->assertStatus(403); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + } + + public function test_json_restore_revision_as_admin_and_succeed() + { + $user = User::factory()->admin()->create(); + + $packageInfo = $this->getPackageInfo(); + + $fqdn = $this->getGetFqdn(); + $fqdn_revision = $this->getGetFqdnRevision(); + + $model = $fqdn::factory()->create(); + + $revision = $fqdn_revision::factory()->create([ + $this->getRevisionId() => $model->id, + 'revision' => 10, + ]); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + + $url = route(sprintf( + '%1$s.revision.restore', + $packageInfo['model_route'] + ), [ + $this->getRevisionRouteParameter() => $revision->id, + ]); + + $response = $this->actingAs($user)->putJson($url); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 11, + ]); + + $response->assertStatus(200); + + $response->assertJsonStructure($this->getStructureData()); + } + + public function test_json_restore_revision_as_admin_and_succeed_without_redirect() + { + $packageInfo = $this->getPackageInfo(); + + $user = User::factory()->admin()->create(); + + $fqdn = $this->getGetFqdn(); + $fqdn_revision = $this->getGetFqdnRevision(); + + $model = $fqdn::factory()->create(); + + $revision = $fqdn_revision::factory()->create([ + $this->getRevisionId() => $model->id, + 'revision' => 10, + ]); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + + $_return_url = route($packageInfo['model_route'], [ + 'filter' => [ + 'trash' => 'only', + ], + ]); + + $url = route(sprintf( + '%1$s.revision.restore', + $packageInfo['model_route'] + ), [ + $this->getRevisionRouteParameter() => $revision->id, + '_return_url' => $_return_url, + ]); + + $response = $this->actingAs($user)->putJson($url); + + // $response->dd(); + // $response->dump(); + // $response->dumpHeaders(); + // $response->dumpSession(); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 11, + ]); + + $response->assertStatus(200); + + $response->assertJsonStructure($this->getStructureData()); + } + + public function test_json_restore_revision_as_user_and_get_denied() + { + $packageInfo = $this->getPackageInfo(); + + $user = User::factory()->create(['role' => 'user']); + + $fqdn = $this->getGetFqdn(); + $fqdn_revision = $this->getGetFqdnRevision(); + + $model = $fqdn::factory()->create(); + + $revision = $fqdn_revision::factory()->create([ + $this->getRevisionId() => $model->id, + 'revision' => 10, + ]); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + + $url = route(sprintf( + '%1$s.revision.restore', + $packageInfo['model_route'] + ), [ + $this->getRevisionRouteParameter() => $revision->id, + ]); + + $response = $this->actingAs($user)->putJson($url); + + $response->assertStatus(401); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + } +} diff --git a/src/Feature/Http/Controllers/Resource/Playground/RestoreRevisionTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RestoreRevisionTrait.php new file mode 100644 index 0000000..e0dc6ab --- /dev/null +++ b/src/Feature/Http/Controllers/Resource/Playground/RestoreRevisionTrait.php @@ -0,0 +1,197 @@ + + */ + abstract public function getGetFqdn(): string; + + /** + * @return class-string + */ + abstract public function getGetFqdnRevision(): string; + + /** + * @return array + */ + abstract public function getPackageInfo(): array; + + abstract public function getRevisionId(): string; + + abstract public function getRevisionRouteParameter(): string; + + public function test_guest_cannot_restore_revision() + { + $packageInfo = $this->getPackageInfo(); + + $fqdn = $this->getGetFqdn(); + $fqdn_revision = $this->getGetFqdnRevision(); + + $model = $fqdn::factory()->create(); + + $revision = $fqdn_revision::factory()->create([ + $this->getRevisionId() => $model->id, + 'revision' => 10, + ]); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + + $url = route(sprintf( + '%1$s.revision.restore', + $packageInfo['model_route'] + ), [ + $this->getRevisionRouteParameter() => $revision->id, + ]); + + $response = $this->put($url); + + $response->assertStatus(403); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + } + + public function test_restore_revision_as_admin_and_succeed() + { + $user = User::factory()->admin()->create(); + + $packageInfo = $this->getPackageInfo(); + + $fqdn = $this->getGetFqdn(); + $fqdn_revision = $this->getGetFqdnRevision(); + + $model = $fqdn::factory()->create(); + + $revision = $fqdn_revision::factory()->create([ + $this->getRevisionId() => $model->id, + 'revision' => 10, + ]); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + + $url = route(sprintf( + '%1$s.revision.restore', + $packageInfo['model_route'] + ), [ + $this->getRevisionRouteParameter() => $revision->id, + ]); + + $response = $this->actingAs($user)->put($url); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 11, + ]); + + $response->assertRedirect(route(sprintf('%1$s.show', $packageInfo['model_route']), [ + $packageInfo['model_slug'] => $model->id, + ])); + } + + public function test_restore_revision_as_admin_and_succeed_with_redirect_to_index_with_only_trash() + { + $packageInfo = $this->getPackageInfo(); + + $user = User::factory()->admin()->create(); + + $fqdn = $this->getGetFqdn(); + $fqdn_revision = $this->getGetFqdnRevision(); + + $model = $fqdn::factory()->create(); + + $revision = $fqdn_revision::factory()->create([ + $this->getRevisionId() => $model->id, + 'revision' => 10, + ]); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + + $_return_url = route($packageInfo['model_route'], [ + 'filter' => [ + 'trash' => 'only', + ], + ]); + + $url = route(sprintf( + '%1$s.revision.restore', + $packageInfo['model_route'] + ), [ + $this->getRevisionRouteParameter() => $revision->id, + '_return_url' => $_return_url, + ]); + + $response = $this->actingAs($user)->put($url); + + // $response->dd(); + // $response->dump(); + // $response->dumpHeaders(); + // $response->dumpSession(); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 11, + ]); + + $response->assertRedirect($_return_url); + } + + public function test_restore_revision_as_user_and_get_denied() + { + $packageInfo = $this->getPackageInfo(); + + $user = User::factory()->create(['role' => 'user']); + + $fqdn = $this->getGetFqdn(); + $fqdn_revision = $this->getGetFqdnRevision(); + + $model = $fqdn::factory()->create(); + + $revision = $fqdn_revision::factory()->create([ + $this->getRevisionId() => $model->id, + 'revision' => 10, + ]); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + + $url = route(sprintf( + '%1$s.revision.restore', + $packageInfo['model_route'] + ), [ + $this->getRevisionRouteParameter() => $revision->id, + ]); + + $response = $this->actingAs($user)->put($url); + + $response->assertStatus(401); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + } +} diff --git a/src/Feature/Http/Controllers/Resource/Playground/RestoreTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RestoreTrait.php index d972b27..e30f971 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/RestoreTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/RestoreTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Carbon; use Playground\Test\Models\PlaygroundUser as User; @@ -12,6 +13,11 @@ */ trait RestoreTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ @@ -21,7 +27,7 @@ public function test_guest_cannot_restore() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create([ 'deleted_at' => Carbon::now(), @@ -55,7 +61,7 @@ public function test_restore_as_admin_and_succeed() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -94,7 +100,7 @@ public function test_restore_as_admin_and_succeed_with_redirect_to_index_with_on { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -143,7 +149,7 @@ public function test_restore_as_user_and_get_denied() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->create(['role' => 'user']); diff --git a/src/Feature/Http/Controllers/Resource/Playground/RevisionJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RevisionJsonTrait.php new file mode 100644 index 0000000..b47f918 --- /dev/null +++ b/src/Feature/Http/Controllers/Resource/Playground/RevisionJsonTrait.php @@ -0,0 +1,98 @@ + + */ + abstract public function getGetFqdn(): string; + + /** + * @return class-string + */ + abstract public function getGetFqdnRevision(): string; + + /** + * @return array + */ + abstract public function getPackageInfo(): array; + + abstract public function getRevisionId(): string; + + abstract public function getRevisionRouteParameter(): string; + + public function test_json_guest_cannot_render_revision_view() + { + $packageInfo = $this->getPackageInfo(); + + $fqdn = $this->getGetFqdn(); + $fqdn_revision = $this->getGetFqdnRevision(); + + $model = $fqdn::factory()->create(); + + $revision = $fqdn_revision::factory()->create([ + $this->getRevisionId() => $model->id, + 'revision' => 10, + ]); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + + $url = route(sprintf( + '%1$s.revision', + $packageInfo['model_route'] + ), [ + $this->getRevisionRouteParameter() => $revision->id, + ]); + + $response = $this->getJson($url); + + $response->assertStatus(403); + } + + public function test_json_revision_view_rendered_by_admin() + { + $packageInfo = $this->getPackageInfo(); + + $fqdn = $this->getGetFqdn(); + $fqdn_revision = $this->getGetFqdnRevision(); + + $model = $fqdn::factory()->create(); + + $revision = $fqdn_revision::factory()->create([ + $this->getRevisionId() => $model->id, + 'revision' => 10, + ]); + + $user = User::factory()->admin()->create(); + + $index = route($packageInfo['model_route']); + + $url = route(sprintf( + '%1$s.revision', + $packageInfo['model_route'] + ), [ + $this->getRevisionRouteParameter() => $revision->id, + ]); + + $response = $this->actingAs($user)->getJson($url); + + $response->assertStatus(200); + + $response->assertJsonStructure($this->getStructureData()); + + $this->assertAuthenticated(); + } +} diff --git a/src/Feature/Http/Controllers/Resource/Playground/RevisionTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RevisionTrait.php new file mode 100644 index 0000000..4ef1177 --- /dev/null +++ b/src/Feature/Http/Controllers/Resource/Playground/RevisionTrait.php @@ -0,0 +1,96 @@ + + */ + abstract public function getGetFqdn(): string; + + /** + * @return class-string + */ + abstract public function getGetFqdnRevision(): string; + + /** + * @return array + */ + abstract public function getPackageInfo(): array; + + abstract public function getRevisionId(): string; + + abstract public function getRevisionRouteParameter(): string; + + public function test_guest_cannot_render_revision_view() + { + $packageInfo = $this->getPackageInfo(); + + $fqdn = $this->getGetFqdn(); + $fqdn_revision = $this->getGetFqdnRevision(); + + $model = $fqdn::factory()->create(); + + $revision = $fqdn_revision::factory()->create([ + $this->getRevisionId() => $model->id, + 'revision' => 10, + ]); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + + $url = route(sprintf( + '%1$s.revision', + $packageInfo['model_route'] + ), [ + $this->getRevisionRouteParameter() => $revision->id, + ]); + + $response = $this->get($url); + + $response->assertStatus(403); + } + + public function test_revision_view_rendered_by_admin() + { + $packageInfo = $this->getPackageInfo(); + + $fqdn = $this->getGetFqdn(); + $fqdn_revision = $this->getGetFqdnRevision(); + + $model = $fqdn::factory()->create(); + + $revision = $fqdn_revision::factory()->create([ + $this->getRevisionId() => $model->id, + 'revision' => 10, + ]); + + $user = User::factory()->admin()->create(); + + $index = route($packageInfo['model_route']); + + $url = route(sprintf( + '%1$s.revision', + $packageInfo['model_route'] + ), [ + $this->getRevisionRouteParameter() => $revision->id, + ]); + + $response = $this->actingAs($user)->get($url); + + $response->assertStatus(200); + + $this->assertAuthenticated(); + } +} diff --git a/src/Feature/Http/Controllers/Resource/Playground/RevisionsTrait.php b/src/Feature/Http/Controllers/Resource/Playground/RevisionsTrait.php new file mode 100644 index 0000000..26b08a8 --- /dev/null +++ b/src/Feature/Http/Controllers/Resource/Playground/RevisionsTrait.php @@ -0,0 +1,81 @@ + + */ + abstract public function getGetFqdnRevision(): string; + + /** + * @return array + */ + abstract public function getPackageInfo(): array; + + public function test_guest_cannot_render_revisions_view() + { + $packageInfo = $this->getPackageInfo(); + + $fqdn = $this->getGetFqdn(); + $fqdn_revision = $this->getGetFqdnRevision(); + + $model = $fqdn::factory()->create(); + + $revision = $fqdn_revision::factory()->create([ + $this->getRevisionId() => $model->id, + 'revision' => 10, + ]); + + $this->assertDatabaseHas($packageInfo['table'], [ + 'id' => $model->id, + 'revision' => 0, + ]); + + $url = route(sprintf( + '%1$s.revisions', + $packageInfo['model_route'] + ), $model); + + $response = $this->get($url); + + $response->assertStatus(403); + } + + public function test_admin_can_render_revisions_view() + { + $packageInfo = $this->getPackageInfo(); + + $fqdn = $this->getGetFqdn(); + $fqdn_revision = $this->getGetFqdnRevision(); + + $model = $fqdn::factory()->create(); + + $revision = $fqdn_revision::factory()->create([ + $this->getRevisionId() => $model->id, + 'revision' => 10, + ]); + + $user = User::factory()->admin()->create(); + + $url = route(sprintf( + '%1$s.revisions', + $packageInfo['model_route'] + ), $model); + + $response = $this->actingAs($user)->get($url); + + $response->assertStatus(200); + + $this->assertAuthenticated(); + } +} diff --git a/src/Feature/Http/Controllers/Resource/Playground/ShowJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/ShowJsonTrait.php index d65430a..07780cd 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/ShowJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/ShowJsonTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Playground\Test\Models\PlaygroundUser as User; /** @@ -11,6 +12,11 @@ */ trait ShowJsonTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ @@ -20,7 +26,7 @@ public function test_json_guest_cannot_see_info() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); @@ -39,7 +45,7 @@ public function test_json_show_info_for_admin() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); diff --git a/src/Feature/Http/Controllers/Resource/Playground/ShowTrait.php b/src/Feature/Http/Controllers/Resource/Playground/ShowTrait.php index 6d25ca8..3a02c78 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/ShowTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/ShowTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Playground\Test\Models\PlaygroundUser as User; /** @@ -11,6 +12,11 @@ */ trait ShowTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ @@ -20,7 +26,7 @@ public function test_guest_cannot_render_show_view() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); @@ -40,7 +46,7 @@ public function test_show_view_rendered_by_admin() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create(); diff --git a/src/Feature/Http/Controllers/Resource/Playground/UnlockJsonTrait.php b/src/Feature/Http/Controllers/Resource/Playground/UnlockJsonTrait.php index 3b124e6..7b02d10 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/UnlockJsonTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/UnlockJsonTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Playground\Test\Models\PlaygroundUser as User; /** @@ -11,6 +12,11 @@ */ trait UnlockJsonTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ @@ -25,7 +31,7 @@ public function test_json_guest_cannot_unlock() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create([ 'locked' => true, @@ -59,7 +65,7 @@ public function test_json_unlock_as_admin_and_succeed() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -98,7 +104,7 @@ public function test_json_unlock_as_admin_and_succeed_with_no_redirect() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -142,7 +148,7 @@ public function test_json_unlock_as_user_and_get_denied() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(['role' => 'user']); diff --git a/src/Feature/Http/Controllers/Resource/Playground/UnlockTrait.php b/src/Feature/Http/Controllers/Resource/Playground/UnlockTrait.php index 1b22c9b..23147dc 100644 --- a/src/Feature/Http/Controllers/Resource/Playground/UnlockTrait.php +++ b/src/Feature/Http/Controllers/Resource/Playground/UnlockTrait.php @@ -4,6 +4,7 @@ */ namespace Playground\Test\Feature\Http\Controllers\Resource\Playground; +use Illuminate\Database\Eloquent\Model; use Playground\Test\Models\PlaygroundUser as User; /** @@ -11,6 +12,11 @@ */ trait UnlockTrait { + /** + * @return class-string + */ + abstract public function getGetFqdn(): string; + /** * @return array */ @@ -25,7 +31,7 @@ public function test_guest_cannot_unlock() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $model = $fqdn::factory()->create([ 'locked' => true, @@ -59,7 +65,7 @@ public function test_unlock_as_admin_and_succeed() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -103,7 +109,7 @@ public function test_unlock_as_admin_and_succeed_with_redirect_to_index_with_sor { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(); @@ -145,7 +151,7 @@ public function test_unlock_as_user_and_get_denied() { $packageInfo = $this->getPackageInfo(); - $fqdn = $this->fqdn; + $fqdn = $this->getGetFqdn(); $user = User::factory()->admin()->create(['role' => 'user']); diff --git a/src/Feature/Models/ModelCase.php b/src/Feature/Models/ModelCase.php index ecec362..d5523ba 100644 --- a/src/Feature/Models/ModelCase.php +++ b/src/Feature/Models/ModelCase.php @@ -34,28 +34,28 @@ abstract class ModelCase extends OrchestraTestCase * @var array> Test belongsTo relationships. */ protected array $belongsTo = [ - // 'type' => ['use' => 'type', 'rule' => '_first', 'modelClass' => \Mods\Dam\Models\DamAssetType::class], + // 'tag' => ['use' => 'type', 'rule' => '_first', 'modelClass' => \App\Models\Tag::class], ]; /** * @var array> Test belongsToMany relationships. */ protected array $belongsToMany = [ - // 'tags' => ['use' => 'factory', 'rule' => '_first', 'modelClass' => \Mods\Dam\Models\DamAssetTag::class], + // 'tags' => ['use' => 'factory', 'rule' => '_first', 'modelClass' => \App\Models\Tag::class], ]; /** * @var array> Test hasMany relationships. */ protected array $hasMany = [ - // 'lines' => ['key' => 'cart_id', 'modelClass' => \App\Models\Cartline::class], + // 'tags' => ['key' => 'coupon_id', 'modelClass' => \App\Models\Tag::class], ]; /** * @var array> Test hasOne relationships. */ protected array $hasOne = [ - // 'coupon' => ['key' => 'coupon_id', 'rule' => 'create', 'modelClass' => \App\Coupon::class], + // 'tag' => ['key' => 'coupon_id', 'rule' => 'create', 'modelClass' => \App\Models\Tag::class], ]; /**