Skip to content

Commit 67e4a2e

Browse files
committed
test: add tests for global blocks feature
1 parent a96af73 commit 67e4a2e

File tree

7 files changed

+213
-14
lines changed

7 files changed

+213
-14
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,20 +589,20 @@ You can configure the plugin directly when registering it:
589589

590590
->plugins([
591591
\Redberry\PageBuilderPlugin\GlobalBlocksPlugin::make()
592-
->enabled(true) // Enable/disable the plugin for this panel
592+
->enableGlobalBlocks(true) // Enable/disable the Global Blocks resource
593593
->resource(\App\Filament\Resources\CustomGlobalBlocksResource::class), // Use custom resource
594594
// ... other plugins
595595
])
596596
```
597597

598598
**Configuration options:**
599-
- `enabled(bool)`: Enable or disable the Global Blocks resource (default: `true`)
599+
- `enableGlobalBlocks(bool)`: Enable or disable the Global Blocks resource (default: `true`)
600600
- `resource(string)`: Specify a custom resource class that extends the package's resource
601601

602602
This approach allows you to:
603603
- **Enable Global Blocks on specific panels only** - Perfect for multi-panel applications
604604
- **Use different resource configurations per panel** - Each panel can have its own customized resource
605-
- **Disable the feature entirely** by not registering the plugin or using `->enabled(false)`
605+
- **Disable the feature entirely** by not registering the plugin or using `->enableGlobalBlocks(false)`
606606

607607
#### How global blocks work
608608

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Redberry\PageBuilderPlugin\Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Redberry\PageBuilderPlugin\Models\GlobalBlockConfig;
7+
use Redberry\PageBuilderPlugin\Tests\Fixtures\Blocks\GlobalViewBlock;
8+
9+
class GlobalBlockConfigFactory extends Factory
10+
{
11+
protected $model = GlobalBlockConfig::class;
12+
13+
public function definition(): array
14+
{
15+
return [
16+
'name' => $this->faker->words(3, true),
17+
'class_name' => GlobalViewBlock::class,
18+
'configuration' => [
19+
'title' => $this->faker->sentence,
20+
'content' => $this->faker->paragraph,
21+
'button_text' => $this->faker->word,
22+
],
23+
];
24+
}
25+
}

src/GlobalBlocksPlugin.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,23 @@
88

99
class GlobalBlocksPlugin implements Plugin
1010
{
11-
protected bool $enabled = true;
12-
13-
protected string $resourceClass = GlobalBlockConfigResource::class;
11+
protected bool $enableGlobalBlocksResource = true;
12+
protected string $globalResourceClass = GlobalBlockConfigResource::class;
1413

1514
public static function make(): static
1615
{
1716
return app(static::class);
1817
}
1918

20-
public function enabled(bool $enabled = true): static
19+
public function enableGlobalBlocks(bool $enable = true): static
2120
{
22-
$this->enabled = $enabled;
23-
21+
$this->enableGlobalBlocksResource = $enable;
2422
return $this;
2523
}
2624

2725
public function resource(string $resourceClass): static
2826
{
29-
$this->resourceClass = $resourceClass;
30-
27+
$this->globalResourceClass = $resourceClass;
3128
return $this;
3229
}
3330

@@ -38,16 +35,16 @@ public function getId(): string
3835

3936
public function register(Panel $panel): void
4037
{
41-
if (! $this->enabled) {
38+
if (!$this->enableGlobalBlocksResource) {
4239
return;
4340
}
4441

45-
if (! class_exists($this->resourceClass)) {
42+
if (!class_exists($this->globalResourceClass)) {
4643
return;
4744
}
4845

4946
$panel->resources([
50-
$this->resourceClass,
47+
$this->globalResourceClass,
5148
]);
5249
}
5350

src/Models/GlobalBlockConfig.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace Redberry\PageBuilderPlugin\Models;
44

5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
56
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Support\Facades\File;
78
use ReflectionClass;
89

910
class GlobalBlockConfig extends Model
1011
{
12+
use HasFactory;
13+
1114
protected $table = 'global_block_configs';
1215

1316
protected $fillable = [
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Redberry\PageBuilderPlugin\Tests\Fixtures\Blocks;
4+
5+
use Filament\Forms\Components\TextInput;
6+
use Filament\Forms\Components\Textarea;
7+
use Redberry\PageBuilderPlugin\Abstracts\BaseBlock;
8+
use Redberry\PageBuilderPlugin\Traits\IsGlobalBlock;
9+
10+
class GlobalViewBlock extends BaseBlock
11+
{
12+
use IsGlobalBlock;
13+
14+
public static function getBlockSchema(?object $record = null): array
15+
{
16+
$schema = static::getBaseBlockSchema($record);
17+
return static::applyGlobalConfiguration($schema);
18+
}
19+
20+
public static function getBaseBlockSchema(?object $record = null): array
21+
{
22+
return [
23+
TextInput::make('title')
24+
->label('Title')
25+
->required(),
26+
27+
Textarea::make('content')
28+
->label('Content')
29+
->required(),
30+
31+
TextInput::make('button_text')
32+
->label('Button Text'),
33+
];
34+
}
35+
}

tests/GlobalBlocksPluginTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
use Filament\Panel;
4+
use Redberry\PageBuilderPlugin\GlobalBlocksPlugin;
5+
use Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource;
6+
7+
it('can be instantiated', function () {
8+
$plugin = GlobalBlocksPlugin::make();
9+
10+
expect($plugin)->toBeInstanceOf(GlobalBlocksPlugin::class);
11+
});
12+
13+
it('has correct plugin ID', function () {
14+
$plugin = GlobalBlocksPlugin::make();
15+
16+
expect($plugin->getId())->toBe('page-builder-global-blocks');
17+
});
18+
19+
it('can configure enable global blocks', function () {
20+
$plugin = GlobalBlocksPlugin::make()
21+
->enableGlobalBlocks(false);
22+
23+
expect($plugin)->toBeInstanceOf(GlobalBlocksPlugin::class);
24+
});
25+
26+
it('can configure custom resource', function () {
27+
$customResourceClass = 'App\\Filament\\Resources\\CustomGlobalBlocksResource';
28+
29+
$plugin = GlobalBlocksPlugin::make()
30+
->resource($customResourceClass);
31+
32+
expect($plugin)->toBeInstanceOf(GlobalBlocksPlugin::class);
33+
});
34+
35+
it('registers resource when enabled', function () {
36+
$panel = Panel::make();
37+
38+
$plugin = GlobalBlocksPlugin::make()
39+
->enableGlobalBlocks(true);
40+
41+
$plugin->register($panel);
42+
43+
expect($panel->getResources())->toContain(GlobalBlockConfigResource::class);
44+
});
45+
46+
it('does not register resource when disabled', function () {
47+
$panel = Panel::make();
48+
49+
$plugin = GlobalBlocksPlugin::make()
50+
->enableGlobalBlocks(false);
51+
52+
$plugin->register($panel);
53+
54+
expect($panel->getResources())->not->toContain(GlobalBlockConfigResource::class);
55+
});
56+
57+
it('registers custom resource when specified', function () {
58+
$customResourceClass = GlobalBlockConfigResource::class; // Using existing class for test
59+
$panel = Panel::make();
60+
61+
$plugin = GlobalBlocksPlugin::make()
62+
->resource($customResourceClass);
63+
64+
$plugin->register($panel);
65+
66+
expect($panel->getResources())->toContain($customResourceClass);
67+
});
68+
69+
it('handles non-existent resource class gracefully', function () {
70+
$panel = Panel::make();
71+
72+
$plugin = GlobalBlocksPlugin::make()
73+
->resource('NonExistentResourceClass');
74+
75+
$plugin->register($panel);
76+
77+
expect($panel->getResources())->toBeEmpty();
78+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
use Redberry\PageBuilderPlugin\Models\GlobalBlockConfig;
4+
use Redberry\PageBuilderPlugin\Tests\Fixtures\Blocks\GlobalViewBlock;
5+
6+
beforeEach(function () {
7+
copy(
8+
__DIR__ . '/../../database/migrations/create_global_block_configs_table.php.stub',
9+
database_path('migrations/create_global_block_configs_table.php')
10+
);
11+
12+
$this->artisan('migrate', ['--database' => 'testing'])->run();
13+
});
14+
15+
afterEach(function () {
16+
unlink(database_path('migrations/create_global_block_configs_table.php'));
17+
});
18+
19+
it('can create a global block config', function () {
20+
$config = GlobalBlockConfig::create([
21+
'name' => 'Test Block',
22+
'class_name' => GlobalViewBlock::class,
23+
'configuration' => [
24+
'title' => 'Test Title',
25+
'content' => 'Test Content',
26+
],
27+
]);
28+
29+
expect($config)->toBeInstanceOf(GlobalBlockConfig::class)
30+
->and($config->name)->toBe('Test Block')
31+
->and($config->class_name)->toBe(GlobalViewBlock::class)
32+
->and($config->configuration)->toBe([
33+
'title' => 'Test Title',
34+
'content' => 'Test Content',
35+
]);
36+
});
37+
38+
it('can get config value', function () {
39+
$config = GlobalBlockConfig::create([
40+
'name' => 'Test Block',
41+
'class_name' => GlobalViewBlock::class,
42+
'configuration' => [
43+
'title' => 'Test Title',
44+
'content' => 'Test Content',
45+
],
46+
]);
47+
48+
expect($config->getConfigValue('title'))->toBe('Test Title')
49+
->and($config->getConfigValue('content'))->toBe('Test Content')
50+
->and($config->getConfigValue('non_existent'))->toBeNull();
51+
});
52+
53+
it('handles missing configuration gracefully', function () {
54+
$config = GlobalBlockConfig::create([
55+
'name' => 'Test Block',
56+
'class_name' => GlobalViewBlock::class,
57+
'configuration' => null,
58+
]);
59+
60+
expect($config->getConfigValue('title'))->toBeNull();
61+
});

0 commit comments

Comments
 (0)