Files
laravel-manager-packagist/tests/Feature/PagesTest.php
T

83 lines
4.7 KiB
PHP

<?php
use IriksIT\Manager\Models\Page;
use IriksIT\Manager\Support\Fields;
use IriksIT\Manager\Tests\AllModulesTestCase;
uses(AllModulesTestCase::class);
beforeEach(function () {
config(['manager.pages.definitions' => ['home' => ['title' => 'Home', 'fields' => [
'hero_title' => ['label' => 'Title', 'type' => 'text', 'default' => 'Welcome', 'required' => true, 'max' => 20],
'intro' => ['type' => 'markdown'],
'photo' => ['type' => 'media'],
'show_news' => ['type' => 'toggle'],
'columns' => ['type' => 'select', 'options' => ['2' => 'Two', '3' => 'Three']],
'years' => ['type' => 'number'],
'Bad Key' => ['type' => 'text'],
]]]]);
$this->actingAs(makeUser(['pages.edit']));
});
it('lists defined pages next to saved ones, and gives defaults before a page is saved', function () {
Page::create(['slug' => 'privacy', 'title' => 'Privacy', 'body' => 'Text']);
$pages = collect($this->getJson('/manager/pages')->assertOk()->json('pages'));
expect($pages->pluck('slug')->all())->toBe(['home', 'privacy'])
->and($pages->firstWhere('slug', 'home'))->toMatchArray(['defined' => true, 'saved' => false]);
$page = $this->getJson('/manager/pages/home')->assertOk()->json('page');
expect($page['content']['hero_title'])->toBe('Welcome')
->and(collect($page['fields'])->pluck('key')->all())->toBe(['hero_title', 'intro', 'photo', 'show_news', 'columns', 'years'])
->and(Page::contentFor('home')['hero_title'])->toBe('Welcome');
});
it('saves the fields of a defined page, the first time too', function () {
$this->putJson('/manager/pages/home', ['title' => 'Home', 'content' => ['hero_title' => 'Hello', 'intro' => '**Hi**', 'show_news' => true, 'columns' => '3', 'years' => '12', 'unknown' => 'x']])->assertOk();
$page = Page::query()->where('slug', 'home')->sole();
expect($page->content)->toMatchArray(['hero_title' => 'Hello', 'intro' => '**Hi**', 'show_news' => true, 'columns' => '3', 'years' => 12])
->and($page->content)->not->toHaveKey('unknown')
->and(Page::contentFor('home')['photo'])->toBe('');
});
it('validates the values by the type of the field', function () {
$this->putJson('/manager/pages/home', ['title' => 'Home', 'content' => ['hero_title' => '']])->assertJsonValidationErrors(['content.hero_title']);
$this->putJson('/manager/pages/home', ['title' => 'Home', 'content' => ['hero_title' => str_repeat('a', 21)]])->assertJsonValidationErrors(['content.hero_title']);
$this->putJson('/manager/pages/home', ['title' => 'Home', 'content' => ['hero_title' => 'a', 'columns' => '9']])->assertJsonValidationErrors(['content.columns']);
$this->putJson('/manager/pages/home', ['title' => 'Home', 'content' => ['hero_title' => 'a', 'photo' => 'javascript:alert(1)']])->assertJsonValidationErrors(['content.photo']);
$this->putJson('/manager/pages/home', ['title' => 'Home', 'content' => ['hero_title' => 'a', 'photo' => '/media/a.jpg']])->assertOk();
});
it('makes, changes and removes a free page, and keeps defined pages', function () {
$this->postJson('/manager/pages', ['slug' => 'privacy', 'title' => 'Privacy'])->assertCreated();
$this->postJson('/manager/pages', ['slug' => 'privacy', 'title' => 'Again'])->assertJsonValidationErrors(['slug']);
$this->postJson('/manager/pages', ['slug' => 'home', 'title' => 'Again'])->assertJsonValidationErrors(['slug']);
$this->postJson('/manager/pages', ['slug' => 'Bad Slug', 'title' => 'x'])->assertJsonValidationErrors(['slug']);
$this->putJson('/manager/pages/privacy', ['title' => 'Privacy policy', 'body' => 'We keep little.', 'is_published' => true, 'meta_description' => 'About privacy'])->assertOk();
expect(Page::query()->where('slug', 'privacy')->sole())->toMatchArray(['title' => 'Privacy policy', 'body' => 'We keep little.', 'is_published' => true, 'meta_description' => 'About privacy']);
$this->deleteJson('/manager/pages/privacy')->assertOk();
$this->deleteJson('/manager/pages/home')->assertUnprocessable();
$this->putJson('/manager/pages/nothing', ['title' => 'x'])->assertNotFound();
$this->getJson('/manager/pages/nothing')->assertNotFound();
});
it('needs the permission', function () {
$this->actingAs(makeUser(['posts.view']));
$this->getJson('/manager/pages')->assertForbidden();
$this->putJson('/manager/pages/home', ['title' => 'Home'])->assertForbidden();
});
it('cleans values by their type', function () {
$fields = Fields::normalize(['a' => ['type' => 'toggle'], 'b' => ['type' => 'number'], 'c' => ['type' => 'lines']]);
expect(Fields::clean($fields, ['a' => '0', 'b' => '', 'c' => " one\ntwo "]))->toBe(['a' => false, 'b' => null, 'c' => "one\ntwo"]);
});