diff --git a/BUILDING.md b/BUILDING.md index d1b17cf5..f506c258 100644 --- a/BUILDING.md +++ b/BUILDING.md @@ -1,6 +1,6 @@ # Building the development environment -cd into the project directory and run the following command: `sudo sh bin/startdocker.sh` +cd into the project directory and run the following command: `sh bin/startdocker.sh` This should start building the images and start the containers. After that you need to go into the controlpanel_php container and run some commands: @@ -24,9 +24,7 @@ Now you're ready to run the following commands which switches to the testing con After that you can switch back to your dev environment again. Clear the config from cache so changes will be instantly available. ```shell -php artisan migrate --env=testing -php artisan db:seed --env=testing -php artisan config:cache --env=dev +php artisan migrate:fresh --seed --env=testing php artisan config:clear ``` diff --git a/app/Http/Controllers/Admin/UsefulLinkController.php b/app/Http/Controllers/Admin/UsefulLinkController.php index 5c062fd4..f2e94694 100644 --- a/app/Http/Controllers/Admin/UsefulLinkController.php +++ b/app/Http/Controllers/Admin/UsefulLinkController.php @@ -41,10 +41,11 @@ class UsefulLinkController extends Controller */ public function store(Request $request) { + $request->validate([ 'icon' => 'required|string', 'title' => 'required|string|max:60', - 'link' => 'required|string|max:191', + 'link' => 'required|url|string|max:191', 'description' => 'required|string|max:2000', ]); @@ -88,7 +89,7 @@ class UsefulLinkController extends Controller $request->validate([ 'icon' => 'required|string', 'title' => 'required|string|max:60', - 'link' => 'required|string|max:191', + 'link' => 'required|url|string|max:191', 'description' => 'required|string|max:2000', ]); diff --git a/database/factories/UsefulLinkFactory.php b/database/factories/UsefulLinkFactory.php new file mode 100644 index 00000000..1bfde967 --- /dev/null +++ b/database/factories/UsefulLinkFactory.php @@ -0,0 +1,31 @@ + 'fas fa-user', + 'title' => $this->faker->text(30), + 'link' => $this->faker->url, + 'description' => $this->faker->text, + ]; + } +} diff --git a/tests/Feature/TestUsefulLinksController.php b/tests/Feature/TestUsefulLinksController.php new file mode 100644 index 00000000..93be5c3f --- /dev/null +++ b/tests/Feature/TestUsefulLinksController.php @@ -0,0 +1,204 @@ +create([ + 'id' => 1 + ]); + + $response = $this->actingAs(User::factory()->create([ + 'role' => 'admin', + 'pterodactyl_id' => '1' + ]))->{$method}($route); + + $response->assertStatus($expectedStatus); + } + + /** + * @dataProvider usefulLinkDataProvider + * @param array $dataSet + * @param int $expectedCount + * @param bool $assertValidationErrors + */ + function test_creating_useful_link(array $dataSet, int $expectedCount, bool $assertValidationErrors) + { + $response = $this->actingAs($this->getTestUser())->post(route('admin.usefullinks.store'), $dataSet); + + if ($assertValidationErrors) $response->assertSessionHasErrors(); + else $response->assertSessionHasNoErrors(); + + $response->assertRedirect(); + $this->assertDatabaseCount('useful_links', $expectedCount); + } + + /** + * @dataProvider usefulLinkDataProvider + * @param array $dataSet + * @param int $expectedCount + * @param bool $assertValidationErrors + */ + function test_updating_useful_link(array $dataSet, int $expectedCount, bool $assertValidationErrors) + { + $link = UsefulLink::factory()->create([ + 'id' => 1 + ]); + + $response = $this->actingAs($this->getTestUser())->patch(route('admin.usefullinks.update', $link->id), $dataSet); + + if ($assertValidationErrors) $response->assertSessionHasErrors(); + else $response->assertSessionHasNoErrors(); + + $response->assertRedirect(); + $this->assertDatabaseCount('useful_links', 1); + } + + /** + * + */ + function test_deleting_useful_link() + { + $link = UsefulLink::factory()->create([ + 'id' => 1 + ]); + + $response = $this->actingAs($this->getTestUser())->delete(route('admin.usefullinks.update', $link->id)); + + $response->assertRedirect(); + $this->assertDatabaseCount('useful_links', 0); + } + + /** + * @return User + */ + private function getTestUser(): User + { + return User::factory()->create([ + 'role' => 'admin', + 'pterodactyl_id' => '1' + ]); + } + + /** + * @return array + */ + function usefulLinkDataProvider(): array + { + return [ + 'Valid dataset 1' => [ + 'dataSet' => [ + "icon" => "fas fa-user", + "title" => "Bitsec.Dev Dashboard", + "link" => "https://manage.bitsec.dev.com", + "description" => Str::random(1500), + ], + 'expectedCount' => 1, + 'assertValidationErrors' => false + ], + 'Valid dataset 2' => [ + 'dataSet' => [ + "icon" => "fas fa-user", + "title" => Str::random(30), + "link" => "https://somerandomsite.com", + "description" => Str::random(1500), + ], + 'expectedCount' => 1, + 'assertValidationErrors' => false + ], + 'Invalid dataset (invalid link)' => [ + 'dataSet' => [ + "icon" => "fas fa-user", + "title" => "Some Random Title", + "link" => "1221", + "description" => "

Some Random HTML

", + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (no title)' => [ + 'dataSet' => [ + "icon" => "fas fa-user", + "title" => "", + "link" => "https://somerandomsite.com", + "description" => "

Some Random HTML

", + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (to long title)' => [ + 'dataSet' => [ + "icon" => "fas fa-user", + "title" => Str::random(200), + "link" => "https://valid.com", + "description" => "

Some Random HTML

", + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (to long description)' => [ + 'dataSet' => [ + "icon" => "fas fa-user", + "title" => "Some Random Valid Title", + "link" => "https://valid.com", + "description" => Str::random(2100), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + 'Invalid dataset (no icon)' => [ + 'dataSet' => [ + "title" => "Some Random Valid Title", + "link" => "https://valid.com", + "description" => Str::random(200), + ], + 'expectedCount' => 0, + 'assertValidationErrors' => true + ], + ]; + } + + /** + * @return array[] + */ + public function accessibleRoutesDataProvider(): array + { + return [ + 'index page' => [ + 'method' => 'get', + 'route' => '/admin/usefullinks', + 'expectedStatus' => 200 + ], + 'Create page' => [ + 'method' => 'get', + 'route' => '/admin/usefullinks/create', + 'expectedStatus' => 200 + ], + 'Edit page' => [ + 'method' => 'get', + 'route' => '/admin/usefullinks/1/edit', + 'expectedStatus' => 200 + ], + ]; + } +}