116 lines
4.2 KiB
PHP
116 lines
4.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Feature\Admin;
|
||
|
|
|
||
|
|
use Tests\TestCase;
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Models\Role;
|
||
|
|
use App\Models\Page;
|
||
|
|
use Database\Seeders\RoleSeeder;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
|
||
|
|
class PageManagementTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
protected User $admin;
|
||
|
|
|
||
|
|
protected function setUp(): void
|
||
|
|
{
|
||
|
|
parent::setUp();
|
||
|
|
$this->seed(RoleSeeder::class);
|
||
|
|
$this->admin = User::factory()->create();
|
||
|
|
$this->admin->roles()->attach(Role::where('slug', 'admin')->first());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_admin_can_create_a_page(): void
|
||
|
|
{
|
||
|
|
$adminPath = config('cms.admin_path', 'loom');
|
||
|
|
$response = $this->actingAs($this->admin)->post("/$adminPath/pages", [
|
||
|
|
'title' => 'Test Page',
|
||
|
|
'slug' => 'test-page',
|
||
|
|
'content' => [
|
||
|
|
'en' => [
|
||
|
|
['type' => 'heading', 'data' => ['text' => 'Page Title', 'level' => 2]],
|
||
|
|
['type' => 'paragraph', 'data' => ['text' => 'Some content here.']]
|
||
|
|
],
|
||
|
|
'es' => [
|
||
|
|
['type' => 'heading', 'data' => ['text' => 'Título de la página', 'level' => 2]],
|
||
|
|
['type' => 'paragraph', 'data' => ['text' => 'Algún contenido aquí.']]
|
||
|
|
]
|
||
|
|
],
|
||
|
|
'is_published' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertRedirect("/$adminPath/pages");
|
||
|
|
$this->assertDatabaseHas('pages', ['slug' => 'test-page']);
|
||
|
|
|
||
|
|
$page = Page::where('slug', 'test-page')->first();
|
||
|
|
$this->assertNotNull($page->cached_html);
|
||
|
|
|
||
|
|
// English is default app locale during test
|
||
|
|
$this->assertStringContainsString('<h2>Page Title</h2>', $page->cached_html);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_can_render_page_in_different_locales(): void
|
||
|
|
{
|
||
|
|
$page = Page::factory()->create([
|
||
|
|
'user_id' => $this->admin->id,
|
||
|
|
'slug' => 'multilingual-page',
|
||
|
|
'content' => [
|
||
|
|
'en' => [['type' => 'paragraph', 'data' => ['text' => 'English content']]],
|
||
|
|
'es' => [['type' => 'paragraph', 'data' => ['text' => 'Contenido en español']]]
|
||
|
|
]
|
||
|
|
]);
|
||
|
|
|
||
|
|
$renderer = new \App\Support\PageRenderer();
|
||
|
|
|
||
|
|
// Default locale (en)
|
||
|
|
app()->setLocale('en');
|
||
|
|
$this->assertStringContainsString('English content', $renderer->render($page->content));
|
||
|
|
|
||
|
|
// Spanish locale (es)
|
||
|
|
app()->setLocale('es');
|
||
|
|
$this->assertStringContainsString('Contenido en español', $renderer->render($page->content));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_it_blocks_reserved_slugs(): void
|
||
|
|
{
|
||
|
|
$adminPath = config('cms.admin_path', 'loom');
|
||
|
|
$response = $this->actingAs($this->admin)->post("/$adminPath/pages", [
|
||
|
|
'title' => 'Admin Page',
|
||
|
|
'slug' => 'loom', // Specifically block 'loom' regardless of config for now as per BasePageRequest
|
||
|
|
'content' => [['type' => 'paragraph', 'data' => ['text' => '...']]]
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertSessionHasErrors(['slug']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_admin_can_update_a_page(): void
|
||
|
|
{
|
||
|
|
$adminPath = config('cms.admin_path', 'loom');
|
||
|
|
$page = Page::factory()->create(['user_id' => $this->admin->id, 'content' => [['type' => 'paragraph', 'data' => ['text' => '...']]]]);
|
||
|
|
|
||
|
|
$response = $this->actingAs($this->admin)->put("/$adminPath/pages/{$page->id}", [
|
||
|
|
'title' => 'Updated Title',
|
||
|
|
'slug' => 'updated-slug',
|
||
|
|
'content' => [['type' => 'paragraph', 'data' => ['text' => 'Updated content.']]],
|
||
|
|
'is_published' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertRedirect("/$adminPath/pages");
|
||
|
|
$this->assertDatabaseHas('pages', ['title' => 'Updated Title', 'slug' => 'updated-slug']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_admin_can_delete_a_page(): void
|
||
|
|
{
|
||
|
|
$adminPath = config('cms.admin_path', 'loom');
|
||
|
|
$page = Page::factory()->create(['user_id' => $this->admin->id, 'content' => [['type' => 'paragraph', 'data' => ['text' => '...']]]]);
|
||
|
|
|
||
|
|
$response = $this->actingAs($this->admin)->delete("/$adminPath/pages/{$page->id}");
|
||
|
|
|
||
|
|
$response->assertRedirect("/$adminPath/pages");
|
||
|
|
$this->assertDatabaseMissing('pages', ['id' => $page->id]);
|
||
|
|
}
|
||
|
|
}
|