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

89 lines
3.9 KiB
PHP

<?php
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Support\Facades\Schema;
use IriksIT\Manager\Models\Record;
use IriksIT\Manager\Support\ModuleRegistry;
use IriksIT\Manager\Support\Resources;
use IriksIT\Manager\Tests\ResourcesTestCase;
uses(ResourcesTestCase::class);
class Service extends Eloquent
{
protected $table = 'site_services';
protected $guarded = ['secret'];
protected function casts(): array
{
return ['visible' => 'boolean'];
}
}
beforeEach(function () {
Schema::create('site_services', function ($table) {
$table->id();
$table->string('name');
$table->integer('position')->nullable();
$table->boolean('visible')->default(true);
$table->string('secret')->nullable();
$table->timestamps();
});
$this->actingAs(makeUser(['services.manage', 'services.view', 'links.view', 'links.manage']));
});
it('puts a menu item and permissions in the manager for each resource', function () {
$registry = app(ModuleRegistry::class);
expect($registry->permissionValues())->toContain('services.view', 'services.manage', 'links.view')
->and($registry->permissionValues())->not->toContain('posts.view-more')
->and(collect($registry->navigation())->firstWhere('label', 'Services'))->toMatchArray(['route' => 'manager.records.index', 'params' => ['resource' => 'services']]);
$this->getJson('/manager')->assertOk()->assertJsonFragment(['href' => '/manager/records/services']);
});
it('keeps the records of a resource in a model of the site', function () {
$this->postJson('/manager/records/services', ['name' => 'Roofs', 'position' => 2, 'visible' => true, 'secret' => 'x'])->assertCreated();
$this->postJson('/manager/records/services', ['name' => 'Floors', 'position' => 1])->assertCreated();
$rows = $this->getJson('/manager/records/services')->assertOk()->json('rows');
expect(collect($rows)->pluck('values.name')->all())->toBe(['Floors', 'Roofs'])
->and(Service::query()->where('name', 'Roofs')->value('secret'))->toBeNull();
$id = $rows[0]['id'];
$this->putJson("/manager/records/services/{$id}", ['name' => 'Floors and walls', 'position' => 1])->assertOk();
$this->getJson("/manager/records/services/{$id}/edit")->assertOk()->assertJsonPath('record.values.name', 'Floors and walls');
$this->deleteJson("/manager/records/services/{$id}")->assertOk();
expect(Service::query()->count())->toBe(1);
});
it('keeps the records of a resource without a model in the table of the package', function () {
$this->postJson('/manager/records/links', ['label' => 'Home', 'url' => '/', 'new_tab' => true])->assertCreated();
$this->postJson('/manager/records/links', ['label' => 'Bad', 'url' => 'javascript:alert(1)'])->assertJsonValidationErrors(['url']);
$this->postJson('/manager/records/links', ['url' => '/x'])->assertJsonValidationErrors(['label']);
expect(Record::query()->where('resource', 'links')->sole()->data)->toBe(['label' => 'Home', 'url' => '/', 'new_tab' => true]);
$this->getJson('/manager/records/links/create')->assertOk()->assertJsonPath('record.values.new_tab', false)->assertJsonCount(3, 'resource.fields');
});
it('is closed to those without the permission and to what was not defined', function () {
$this->actingAs(makeUser(['services.view']));
$this->getJson('/manager/records/services')->assertOk();
$this->postJson('/manager/records/services', ['name' => 'x'])->assertForbidden();
$this->getJson('/manager/records/links')->assertForbidden();
$this->getJson('/manager/records/posts')->assertNotFound();
$this->getJson('/manager/records/nothing')->assertNotFound();
$this->getJson('/manager/records/services/99/edit')->assertForbidden();
});
it('does not take a name that belongs to a module', function () {
config(['manager.resources.posts' => ['fields' => ['a' => ['type' => 'text']]]]);
expect(array_keys(Resources::all()))->toBe(['services', 'links']);
});