Skip to content

Commit

Permalink
#14 - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
KarolZygadlo committed Sep 28, 2023
1 parent d2a294b commit 0053bf6
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 16 deletions.
9 changes: 9 additions & 0 deletions app/Models/Faq.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Stevebauman\Purify\Facades\Purify;

/**
* @property string $id
Expand All @@ -22,4 +24,11 @@ class Faq extends Model
use HasUlids;

protected $guarded = [];

protected function answer(): Attribute
{
return Attribute::make(
set: fn(?string $value): string => Purify::clean($value),
);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"inertiajs/inertia-laravel": "^0.6.9",
"laravel/framework": "^10.13.0",
"laravel/sanctum": "^3.2.5",
"laravel/tinker": "^2.8.1"
"laravel/tinker": "^2.8.1",
"stevebauman/purify": "^6.0"
},
"require-dev": {
"blumilksoftware/codestyle": "^2.3.0",
Expand Down
131 changes: 129 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions database/factories/FaqFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\Faq;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<Faq>
*/
class FaqFactory extends Factory
{
public function definition(): array
{
return [
"question" => fake()->sentence(),
"answer" => fake()->realText(),
];
}

}
2 changes: 1 addition & 1 deletion resources/js/Pages/Dashboard/FAQ/Create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const form = useForm({
})
function createFAQ() {
form.post('/dashboard/semesters')
form.post('/dashboard/faqs')
}
</script>

Expand Down
2 changes: 1 addition & 1 deletion resources/js/Pages/Dashboard/FAQ/Edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const form = useForm({
})
function createFAQ() {
form.patch('/dashboard/semesters/${props.faq.id}')
form.patch('/dashboard/faqs/${props.faq.id}')
}
</script>

Expand Down
7 changes: 2 additions & 5 deletions resources/js/Pages/Dashboard/FAQ/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ const faqToDeleteId = ref(0)
ID
</TableHeader>
<TableHeader class="w-1/5">
Nazwa
</TableHeader>
<TableHeader class="w-1/5">
Status
Pytanie
</TableHeader>
<TableHeader />
</template>
Expand All @@ -67,7 +64,7 @@ const faqToDeleteId = ref(0)
{{ faq.question }}
</TableCell>
<TableCell class="flex justify-end gap-2">
<Button :href="`/dashboard/semesters/${faq.id}/edit`">
<Button :href="`/dashboard/faqs/${faq.id}/edit`">
<Cog6ToothIcon class="w-5" />
</Button>
<Button class="text-red-600" @click="[showModal = true, faqToDeleteId = faq.id]">
Expand Down
12 changes: 6 additions & 6 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
Route::post("/semesters/{semester}/activate", "toggleActive")->name("semesters.toggle.active");
});
Route::controller(FaqController::class)->group(function (): void {
Route::get("/faqs", "index")->name("faq.index");
Route::get("/faqs/create", "create")->name("faq.create");
Route::post("/faqs", "store")->name("faq.store");
Route::get("/faqs/{faq}/edit", "edit")->name("faq.edit");
Route::patch("/faqs/{faq}", "update")->name("faq.update");
Route::delete("/faqs/{faq}", "destroy")->name("faq.destroy");
Route::get("/faqs", "index")->name("faqs.index");
Route::get("/faqs/create", "create")->name("faqs.create");
Route::post("/faqs", "store")->name("faqs.store");
Route::get("/faqs/{faq}/edit", "edit")->name("faqs.edit");
Route::patch("/faqs/{faq}", "update")->name("faqs.update");
Route::delete("/faqs/{faq}", "destroy")->name("faqs.destroy");
});
});
75 changes: 75 additions & 0 deletions tests/Feature/FaqTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Tests\Feature;

use App\Models\Faq;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Tests\TestCase;

class FaqTest extends TestCase
{
use RefreshDatabase;

protected function setUp(): void
{
parent::setUp();

$this->user = User::factory()->create();
$this->actingAs($this->user);
}

public function testFaqCanBeCreated(): void
{
$this->assertDatabaseCount("faqs", 0);

$this->post("/dashboard/faqs", [
"question" => "Example question ?",
"answer" => "Content of the wiki",
])->assertSessionHasNoErrors();

$this->assertDatabaseCount("faqs", 1);
}

public function testFaqCanBeUpdated(): void
{
$faq = Faq::factory()->create();

$this->assertDatabaseMissing("faqs", [
"question" => "Example question ?",
"answer" => "Content of the wiki",
]);

$this->patch("/dashboard/faqs/{$faq->id}", [
"question" => "Example question ?",
"answer" => "Content of the wiki",
])->assertSessionHasNoErrors();

$this->assertDatabaseHas("faqs", [
"question" => "Example question ?",
"answer" => "Content of the wiki",
]);
}

public function testSemesterCannotBeCreatedWithInvalidData(): void
{
$this->post("/dashboard/faqs", [
"question" => Str::random(256),
])->assertSessionHasErrors([
"question",
]);

$this->assertDatabaseCount("faqs", 0);
}

public function testFaqCanBeDeleted(): void
{
$faq = Faq::factory()->create();
$this->assertDatabaseCount("faqs", 1);

$this->delete("/dashboard/faqs/$faq->id");
$this->assertDatabaseCount("faqs", 0);

}
}

0 comments on commit 0053bf6

Please sign in to comment.