Made test coverage for PageBuilder controllers and models
This commit is contained in:
parent
dd5585c3d1
commit
4ae0edad49
@ -33,7 +33,7 @@ class AuthenticatedSessionController extends Controller
|
||||
|
||||
$request->session()->regenerate();
|
||||
|
||||
return redirect()->intended(route('dashboard', absolute: false));
|
||||
return redirect()->intended(route('page-builder.index', absolute: false));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -36,6 +36,6 @@ class ConfirmablePasswordController extends Controller
|
||||
|
||||
$request->session()->put('auth.password_confirmed_at', time());
|
||||
|
||||
return redirect()->intended(route('dashboard', absolute: false));
|
||||
return redirect()->intended(route('page-builder.index', absolute: false));
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ class EmailVerificationNotificationController extends Controller
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
if ($request->user()->hasVerifiedEmail()) {
|
||||
return redirect()->intended(route('dashboard', absolute: false));
|
||||
return redirect()->intended(route('page-builder.index', absolute: false));
|
||||
}
|
||||
|
||||
$request->user()->sendEmailVerificationNotification();
|
||||
|
@ -16,7 +16,7 @@ class EmailVerificationPromptController extends Controller
|
||||
public function __invoke(Request $request): RedirectResponse|Response
|
||||
{
|
||||
return $request->user()->hasVerifiedEmail()
|
||||
? redirect()->intended(route('dashboard', absolute: false))
|
||||
? redirect()->intended(route('page-builder.index', absolute: false))
|
||||
: Inertia::render('auth/VerifyEmail', ['status' => $request->session()->get('status')]);
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ class VerifyEmailController extends Controller
|
||||
public function __invoke(EmailVerificationRequest $request): RedirectResponse
|
||||
{
|
||||
if ($request->user()->hasVerifiedEmail()) {
|
||||
return redirect()->intended(route('dashboard', absolute: false).'?verified=1');
|
||||
return redirect()->intended(route('page-builder.index', absolute: false).'?verified=1');
|
||||
}
|
||||
|
||||
if ($request->user()->markEmailAsVerified()) {
|
||||
@ -24,6 +24,6 @@ class VerifyEmailController extends Controller
|
||||
event(new Verified($user));
|
||||
}
|
||||
|
||||
return redirect()->intended(route('dashboard', absolute: false).'?verified=1');
|
||||
return redirect()->intended(route('page-builder.index', absolute: false).'?verified=1');
|
||||
}
|
||||
}
|
||||
|
30
database/factories/PageFactory.php
Normal file
30
database/factories/PageFactory.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
use App\Models\Page;
|
||||
use App\Models\User;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Page>
|
||||
*/
|
||||
class PageFactory extends Factory
|
||||
{
|
||||
protected $model = Page::class;
|
||||
|
||||
public function definition(): array {
|
||||
return [
|
||||
'user_id' => User::factory(),
|
||||
'title' => $this->faker->sentence,
|
||||
'slug' => $this->faker->slug,
|
||||
'content' => ['blocks' => []],
|
||||
'is_published' => $this->faker->boolean,
|
||||
'main' => false,
|
||||
'linked' => false,
|
||||
'linkorder' => $this->faker->numberBetween(1, 10),
|
||||
'visibility' => 'public',
|
||||
];
|
||||
}
|
||||
}
|
33
database/factories/PageRevisionFactory.php
Normal file
33
database/factories/PageRevisionFactory.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
use App\Models\PageRevision;
|
||||
use App\Models\Page;
|
||||
use App\Models\User;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\PageRevision>
|
||||
*/
|
||||
class PageRevisionFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array {
|
||||
return [
|
||||
'page_id' => Page::factory(),
|
||||
'user_id' => User::factory(),
|
||||
'version' => 1,
|
||||
'content' => ['blocks' => []],
|
||||
'title' => $this->faker->sentence,
|
||||
'slug' => $this->faker->slug,
|
||||
'label' => $this->faker->randomElement(['auto-save', 'manual-save', 'published', 'restored']),
|
||||
'active' => true,
|
||||
];
|
||||
}
|
||||
}
|
@ -27,7 +27,7 @@ class AuthenticationTest extends TestCase
|
||||
]);
|
||||
|
||||
$this->assertAuthenticated();
|
||||
$response->assertRedirect(route('dashboard', absolute: false));
|
||||
$response->assertRedirect(route('page-builder.index', absolute: false));
|
||||
}
|
||||
|
||||
public function test_users_can_not_authenticate_with_invalid_password()
|
||||
|
@ -38,7 +38,7 @@ class EmailVerificationTest extends TestCase
|
||||
|
||||
Event::assertDispatched(Verified::class);
|
||||
$this->assertTrue($user->fresh()->hasVerifiedEmail());
|
||||
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
|
||||
$response->assertRedirect(route('page-builder.index', absolute: false).'?verified=1');
|
||||
}
|
||||
|
||||
public function test_email_is_not_verified_with_invalid_hash()
|
||||
|
@ -26,6 +26,6 @@ class RegistrationTest extends TestCase
|
||||
]);
|
||||
|
||||
$this->assertAuthenticated();
|
||||
$response->assertRedirect(route('dashboard', absolute: false));
|
||||
$response->assertRedirect(route('page-builder.index', absolute: false));
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ class DashboardTest extends TestCase
|
||||
|
||||
public function test_guests_are_redirected_to_the_login_page()
|
||||
{
|
||||
$response = $this->get('/dashboard');
|
||||
$response = $this->get('/dashboard/page-admin');
|
||||
$response->assertRedirect('/login');
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ class DashboardTest extends TestCase
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user);
|
||||
|
||||
$response = $this->get('/dashboard');
|
||||
$response = $this->get('/dashboard/page-admin');
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
}
|
||||
|
179
tests/Feature/PageBuilderTest.php
Normal file
179
tests/Feature/PageBuilderTest.php
Normal file
@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Page;
|
||||
use App\Models\PageRevision;
|
||||
|
||||
class PageBuilderTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_authenticated_user_can_create_page() {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->post('/dashboard/page-admin/landing-pages/create', [
|
||||
'title' => 'Test Page',
|
||||
'content' => json_encode(['blocks' => []]),
|
||||
'publish' => true,
|
||||
'mainpage' => false,
|
||||
'linked' => false,
|
||||
'linkorder' => 1,
|
||||
'visibility' => 'public',
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
$this->assertDatabaseHas('pages', ['title' => 'Test Page']);
|
||||
$this->assertDatabaseHas('page_revisions', ['title' => 'Test Page', 'label' => 'published']);
|
||||
}
|
||||
|
||||
public function test_authenticated_user_can_update_page() {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$page = Page::factory()->create(['user_id' => $user->id]);
|
||||
$revision = PageRevision::factory()->create([
|
||||
'page_id' => $page->id,
|
||||
'user_id' => $user->id,
|
||||
'active' => true,
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->patch('/dashboard/page-admin/landing-pages/patch', [
|
||||
'revision_uuid' => $revision->uuid,
|
||||
'title' => 'Updated Title',
|
||||
'content' => json_encode(['blocks' => [['type' => 'text', 'data' => 'updated']]]),
|
||||
'publish' => true,
|
||||
'mainpage' => false,
|
||||
'linked' => false,
|
||||
'linkorder' => 2,
|
||||
'visibility' => 'public',
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
|
||||
$this->assertDatabaseHas('pages', [
|
||||
'id' => $page->id,
|
||||
'title' => 'Updated Title',
|
||||
'is_published' => true,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('page_revisions', [
|
||||
'id' => $revision->id,
|
||||
'title' => 'Updated Title',
|
||||
'label' => 'published',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_authenticated_user_can_delete_page() {
|
||||
$user = User::factory()->create();
|
||||
$page = Page::factory()->create(['user_id' => $user->id]);
|
||||
|
||||
$response = $this->actingAs($user)->delete('/dashboard/page-admin/landing-pages/delete', [
|
||||
'id' => $page->id,
|
||||
]);
|
||||
|
||||
$response->assertRedirect(route('page-builder.index'));
|
||||
$this->assertDatabaseMissing('pages', ['id' => $page->id]);
|
||||
}
|
||||
|
||||
public function test_unauthorized_user_cannot_delete_page() {
|
||||
$owner = User::factory()->create();
|
||||
$otherUser = User::factory()->create();
|
||||
$page = Page::factory()->create(['user_id' => $owner->id]);
|
||||
|
||||
$response = $this->actingAs($otherUser)->delete('/dashboard/page-admin/landing-pages/delete', [
|
||||
'id' => $page->id,
|
||||
]);
|
||||
|
||||
$response->assertRedirect(route('page-builder.index'));
|
||||
$response->assertSessionHas('error', 'Du er ikke forfatteren av denne siden og kan derfor ikke slette den.');
|
||||
|
||||
$this->assertDatabaseHas('pages', ['id' => $page->id]);
|
||||
}
|
||||
|
||||
public function test_unauthenticated_user_cannot_access_pagebuilder() {
|
||||
$response = $this->get('/dashboard/page-admin');
|
||||
|
||||
$response->assertRedirect('/login');
|
||||
}
|
||||
|
||||
public function test_user_can_publish_existing_revision() {
|
||||
$user = User::factory()->create();
|
||||
$page = Page::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'is_published' => false,
|
||||
'main' => false,
|
||||
'visibility' => 'public',
|
||||
]);
|
||||
|
||||
$revision = PageRevision::factory()->create([
|
||||
'page_id' => $page->id,
|
||||
'user_id' => $user->id,
|
||||
'active' => true,
|
||||
'label' => 'manual-save',
|
||||
'content' => json_encode(['blocks' => [['type' => 'text', 'data' => 'initial']]]),
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->patch('/dashboard/page-admin/landing-pages/patch', [
|
||||
'revision_uuid' => $revision->uuid,
|
||||
'title' => 'Published Update',
|
||||
'content' => json_encode(['blocks' => [['type' => 'text', 'data' => 'updated']]]),
|
||||
'publish' => true,
|
||||
'mainpage' => false,
|
||||
'linked' => false,
|
||||
'linkorder' => 1,
|
||||
'visibility' => 'public',
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
$this->assertDatabaseHas('pages', [
|
||||
'id' => $page->id,
|
||||
'title' => 'Published Update',
|
||||
'is_published' => true,
|
||||
]);
|
||||
$this->assertDatabaseHas('page_revisions', [
|
||||
'id' => $revision->id,
|
||||
'title' => 'Published Update',
|
||||
'label' => 'published',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_user_can_save_as_new_revision() {
|
||||
$user = User::factory()->create();
|
||||
$page = Page::factory()->create(['user_id' => $user->id]);
|
||||
|
||||
PageRevision::factory()->create([
|
||||
'page_id' => $page->id,
|
||||
'user_id' => $user->id,
|
||||
'active' => true,
|
||||
'label' => 'published',
|
||||
'content' => json_encode(['blocks' => [['type' => 'text', 'data' => 'original']]]),
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->post('/dasboard/page-admin/landing-pages/save-as', [
|
||||
'page_id' => $page->id,
|
||||
'title' => 'Saved As New Revision',
|
||||
'content' => json_encode(['blocks' => [['type' => 'text', 'data' => 'new version']]]),
|
||||
'publish' => false,
|
||||
'mainpage' => false,
|
||||
'linked' => false,
|
||||
'linkorder' => 1,
|
||||
'visibility' => 'public',
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
|
||||
$this->assertDatabaseHas('page_revisions', [
|
||||
'page_id' => $page->id,
|
||||
'title' => 'Saved As New Revision',
|
||||
'label' => 'manual-save',
|
||||
'active' => false,
|
||||
]);
|
||||
|
||||
$this->assertDatabaseCount('page_revisions', 2);
|
||||
}
|
||||
}
|
93
tests/Feature/PageRenderTest.php
Normal file
93
tests/Feature/PageRenderTest.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
use App\Models\Page;
|
||||
use App\Models\PageRevision;
|
||||
|
||||
class PageRenderTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_page_render_view() {
|
||||
$page = Page::factory()->create([
|
||||
'is_published' => true,
|
||||
'main' => false,
|
||||
'visibility' => 'public',
|
||||
]);
|
||||
|
||||
PageRevision::factory()->create([
|
||||
'page_id' => $page->id,
|
||||
'user_id' => $page->user_id,
|
||||
'active' => true,
|
||||
'content' => json_encode(['blocks' => []]),
|
||||
'label' => 'published',
|
||||
]);
|
||||
|
||||
$response = $this->get('/p/' . $page->slug);
|
||||
|
||||
$response->assertStatus(200);
|
||||
$response->assertInertia(fn ($page) => $page->component('Root'));
|
||||
}
|
||||
|
||||
public function test_main_page_returns_404_if_viewed_with_slug() {
|
||||
$page = Page::factory()->create([
|
||||
'is_published' => true,
|
||||
'main' => true,
|
||||
'visibility' => 'public',
|
||||
]);
|
||||
|
||||
PageRevision::factory()->create([
|
||||
'page_id' => $page->id,
|
||||
'user_id' => $page->user_id,
|
||||
'active' => true,
|
||||
'content' => json_encode(['blocks' => []]),
|
||||
'label' => 'published',
|
||||
]);
|
||||
|
||||
$response = $this->get('/p/' . $page->slug);
|
||||
$response->assertStatus(404);
|
||||
}
|
||||
|
||||
public function test_unpublished_page_returns_403() {
|
||||
$page = Page::factory()->create([
|
||||
'is_published' => false,
|
||||
'main' => false,
|
||||
'visibility' => 'public',
|
||||
]);
|
||||
|
||||
PageRevision::factory()->create([
|
||||
'page_id' => $page->id,
|
||||
'user_id' => $page->user_id,
|
||||
'active' => true,
|
||||
'content' => json_encode(['blocks' => []]),
|
||||
'label' => 'manual-save',
|
||||
]);
|
||||
|
||||
$response = $this->get('/p/' . $page->slug);
|
||||
$response->assertStatus(403);
|
||||
}
|
||||
|
||||
public function test_private_page_redirects_for_guests() {
|
||||
$page = Page::factory()->create([
|
||||
'is_published' => true,
|
||||
'main' => false,
|
||||
'visibility' => 'private',
|
||||
]);
|
||||
|
||||
PageRevision::factory()->create([
|
||||
'page_id' => $page->id,
|
||||
'user_id' => $page->user_id,
|
||||
'active' => true,
|
||||
'content' => json_encode(['blocks' => []]),
|
||||
'label' => 'published',
|
||||
]);
|
||||
|
||||
$response = $this->get('/p/' . $page->slug);
|
||||
$response->assertStatus(403);
|
||||
}
|
||||
}
|
23
tests/Unit/PageRevisionTest.php
Normal file
23
tests/Unit/PageRevisionTest.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\PageRevision;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class PageRevisionTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_page_revision_creation() {
|
||||
$revision = PageRevision::factory()->create();
|
||||
$this->assertDatabaseHas('page_revisions', ['id' => $revision->id]);
|
||||
}
|
||||
|
||||
public function test_revision_has_uuid_and_version() {
|
||||
$revision = PageRevision::factory()->create();
|
||||
$this->assertNotNull($revision->uuid);
|
||||
$this->assertGreaterThan(0, $revision->version);
|
||||
}
|
||||
}
|
25
tests/Unit/PageTest.php
Normal file
25
tests/Unit/PageTest.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Page;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
class PageTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_page_creation()
|
||||
{
|
||||
$page = Page::factory()->create();
|
||||
$this->assertDatabaseHas('pages', ['id' => $page->id]);
|
||||
}
|
||||
|
||||
public function test_page_has_author_relationship()
|
||||
{
|
||||
$page = Page::factory()->create();
|
||||
$this->assertInstanceOf(User::class, $page->author);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user