First setup: module registry, switchable modules, permissions

This commit is contained in:
Matthijs Iriks committed 2026-12-04 01:18:14 +01:00
commit 936c794597
52 files changed
+1304

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
<?php
use Illuminate\Support\Facades\Schema;
use IriksIT\Manager\Facades\Manager;
use IriksIT\Manager\Models\Group;
use IriksIT\Manager\Tests\Fixtures\User;
use IriksIT\Manager\Tests\TestCase;
uses(TestCase::class);
it('has users and media on and reviews off by default', function () {
expect(Manager::enabled('users'))->toBeTrue()
->and(Manager::enabled('media'))->toBeTrue()
->and(Manager::enabled('reviews'))->toBeFalse();
});
it('adds no routes, tables or permissions for a module that is off', function () {
expect(Route::has('manager.reviews.index'))->toBeFalse()
->and(Schema::hasTable('manager_reviews'))->toBeFalse()
->and(Manager::permissions())->not->toHaveKey('reviews.manage')
->and(collect(Manager::navigation())->pluck('label'))->not->toContain('Reviews');
});
it('adds all of it for a module that is on', function () {
expect(Route::has('manager.posts.index'))->toBeTrue()
->and(Schema::hasTable('manager_posts'))->toBeTrue()
->and(Manager::permissions())->toHaveKey('posts.publish');
});
it('lets a super admin in and keeps others out without the permission', function () {
$admin = User::create(['name' => 'a', 'email' => '[email protected]', 'password' => 'x', 'manager_super_admin' => true]);
$other = User::create(['name' => 'b', 'email' => '[email protected]', 'password' => 'x']);
$this->actingAs($admin)->getJson('/manager/posts')->assertOk();
$this->actingAs($other)->getJson('/manager/posts')->assertForbidden();
});
it('gives a group member the permissions of the group', function () {
$user = User::create(['name' => 'c', 'email' => '[email protected]', 'password' => 'x']);
$group = Group::create(['name' => 'Editors', 'permissions' => ['posts.view']]);
$user->managerGroups()->attach($group);
$this->actingAs($user)->getJson('/manager/posts')->assertOk();
$this->actingAs($user)->getJson('/manager/pages')->assertForbidden();
});
+11
View File
@@ -0,0 +1,11 @@
<?php
use Illuminate\Support\Facades\Schema;
use IriksIT\Manager\Tests\ReviewsOnTestCase;
uses(ReviewsOnTestCase::class);
it('can switch an optional module on', function () {
expect(Route::has('manager.reviews.index'))->toBeTrue()
->and(Schema::hasTable('manager_reviews'))->toBeTrue();
});
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace IriksIT\Manager\Tests\Fixtures;
use Illuminate\Foundation\Auth\User as Authenticatable;
use IriksIT\Manager\Concerns\HasManagerAccess;
class User extends Authenticatable
{
use HasManagerAccess;
protected $guarded = [];
}
@@ -0,0 +1,19 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
});
}
};
+1
View File
@@ -0,0 +1 @@
<?php
+9
View File
@@ -0,0 +1,9 @@
<?php
namespace IriksIT\Manager\Tests;
/** A site that switched the optional reviews module on. */
abstract class ReviewsOnTestCase extends TestCase
{
protected array $modules = ['reviews' => true];
}
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace IriksIT\Manager\Tests;
use Illuminate\Foundation\Testing\RefreshDatabase;
use IriksIT\Manager\ManagerServiceProvider;
use IriksIT\Manager\Tests\Fixtures\User;
use Orchestra\Testbench\TestCase as Orchestra;
abstract class TestCase extends Orchestra
{
use RefreshDatabase;
/** @var array<string, bool> */
protected array $modules = [];
protected function getPackageProviders($app): array
{
return [ManagerServiceProvider::class];
}
protected function defineEnvironment($app): void
{
$app['config']->set('database.default', 'testing');
$app['config']->set('app.key', 'base64:'.base64_encode(str_repeat('a', 32)));
$app['config']->set('manager.user_model', User::class);
$app['config']->set('manager.modules', array_merge($app['config']->get('manager.modules'), $this->modules));
}
protected function defineDatabaseMigrations(): void
{
$this->loadMigrationsFrom(__DIR__.'/Fixtures/migrations');
}
}