Skip to content

Commit

Permalink
wip:
Browse files Browse the repository at this point in the history
  • Loading branch information
sheadawson committed Oct 22, 2024
1 parent 3666e47 commit 951fbdc
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"pest_2.36.0","defects":{"P\\Tests\\ArchTest::__pest_evaluable_it_will_not_use_debugging_functions":8},"times":{"P\\Tests\\PublishedStatusTableColumnTest::__pest_evaluable_it_can_render_page":0.197}}
{"version":"pest_2.36.0","defects":{"P\\Tests\\ArchTest::__pest_evaluable_it_will_not_use_debugging_functions":8},"times":{"P\\Tests\\PublishedStatusTableColumnTest::__pest_evaluable_it_can_render_page":0.372,"P\\Tests\\PublishedStatusTableColumnTest::__pest_evaluable_it_can_renders_correct_published_statuses":0.188,"P\\Tests\\PublishTableActionTest::__pest_evaluable_it_can_publish_pages":0.2,"P\\Tests\\PublishTableActionTest::__pest_evaluable_it_only_shows_on_unpublishedOrRevised_pages":0.087,"P\\Tests\\UnpublishTableActionTest::__pest_evaluable_it_can_unpublish_pages":0.211,"P\\Tests\\UnpublishTableActionTest::__pest_evaluable_it_only_shows_on_published_pages":0.068}}
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@
"php": "^8.3",
"filament/filament": "^3.0",
"spatie/laravel-package-tools": "^1.16",
"indracollective/laravel-revisor" : "^1.0",
"indracollective/laravel-revisor" : "dev-main",
"illuminate/contracts": "^11.0"
},
"require-dev": {
"indracollective/laravel-revisor" : "^1.0",
"larastan/larastan": "^2.9",
"laravel/pint": "^1.0",
"nunomaduro/collision": "^8.1.1||^7.10.0",
"larastan/larastan": "^2.9",
"orchestra/testbench": "^9.0.0||^8.22.0",
"pestphp/pest": "^2.34",
"pestphp/pest-plugin-arch": "^2.7",
"pestphp/pest-plugin-laravel": "^2.3",
"pestphp/pest-plugin-livewire": "^2.1",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-phpunit": "^1.3"
},
"repositories": [
{
"type": "path",
"url": "../laravel-revisor"
}
],
"autoload": {
"psr-4": {
"Indra\\RevisorFilament\\": "src/",
Expand Down
43 changes: 43 additions & 0 deletions src/PublishTableAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Indra\RevisorFilament;

use Filament\Support\Enums\Alignment;
use Filament\Support\Enums\MaxWidth;
use Filament\Support\Facades\FilamentIcon;
use Filament\Tables\Actions\Action;
use Illuminate\Database\Eloquent\Model;
use Indra\Revisor\Contracts\HasRevisor;

class PublishTableAction extends Action
{
public static function getDefaultName(): ?string
{
return 'publish';
}

protected function setUp(): void
{
parent::setUp();

$this
->label(fn (HasRevisor $record) => $record->isPublished() ? 'Publish Changes' : 'Publish')
->icon(FilamentIcon::resolve('heroicon-o-arrow-up-tray') ?? 'heroicon-o-arrow-up-tray')
->color('success')
->deselectRecordsAfterCompletion()
->modalHeading(fn (Model $record) => "Publish '$record->title'")
->modalIcon(FilamentIcon::resolve('heroicon-o-arrow-up-tray') ?? 'heroicon-o-arrow-up-tray')
->modalIconColor('success')
->modalDescription('Are you sure you want to publish this page?')
->modalAlignment(Alignment::Center)
->modalFooterActionsAlignment(Alignment::Center)
->modalSubmitActionLabel(__('filament-actions::modal.actions.confirm.label'))
->modalWidth(MaxWidth::Medium)
->hidden(fn (HasRevisor $record) => ! $record->isUnpublishedOrRevised())
->action(function (HasRevisor $record, array $data) {
$record->publish();
$this->success();
})
->successNotificationTitle(fn () => $this->getModelLabel() . ' published successfully');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Indra\RevisorFilament\Filament;
namespace Indra\RevisorFilament;

use Filament\Tables\Columns\TextColumn;
use Indra\Revisor\Contracts\HasRevisor;
Expand Down
43 changes: 43 additions & 0 deletions src/UnpublishTableAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Indra\RevisorFilament;

use Filament\Support\Enums\Alignment;
use Filament\Support\Enums\MaxWidth;
use Filament\Support\Facades\FilamentIcon;
use Filament\Tables\Actions\Action;
use Illuminate\Database\Eloquent\Model;
use Indra\Revisor\Contracts\HasRevisor;

class UnpublishTableAction extends Action
{
public static function getDefaultName(): ?string
{
return 'unpublish';
}

protected function setUp(): void
{
parent::setUp();

$this
->label('Unpublish')
->icon(FilamentIcon::resolve('heroicon-o-arrow-down-tray') ?? 'heroicon-o-arrow-down-tray')
->color('warning')
->deselectRecordsAfterCompletion()
->modalHeading(fn (Model $record) => "Unpublish '$record->title'")
->modalIcon(FilamentIcon::resolve('heroicon-o-arrow-down-tray') ?? 'heroicon-o-arrow-down-tray')
->modalIconColor('warning')
->modalDescription('Are you sure you want to unpublish this page?')
->modalAlignment(Alignment::Center)
->modalFooterActionsAlignment(Alignment::Center)
->modalSubmitActionLabel(__('filament-actions::modal.actions.confirm.label'))
->modalWidth(MaxWidth::Medium)
->hidden(fn (HasRevisor $record) => ! $record->isPublished())
->action(function (HasRevisor $record, array $data) {
$record->unpublish();
$this->success();
})
->successNotificationTitle(fn (array $data) => $this->getModelLabel() . ' unpublished successfully');
}
}
9 changes: 8 additions & 1 deletion tests/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@

namespace Indra\RevisorFilament\Tests\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Indra\Revisor\Concerns\HasRevisor;
use Indra\Revisor\Contracts\HasRevisor as HasRevisorContract;
use Indra\RevisorFilament\Tests\Database\Factories\PageFactory;

class Page extends Model implements HasRevisorContract
{
use HasFactory;
use HasRevisor;

protected string $baseTable = 'pages';

protected $fillable = [
'title',
'description',
];

protected static function newFactory()
{
return PageFactory::new();
}
}
8 changes: 8 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Indra\RevisorFilament\Tests\Database\Factories\UserFactory;

class User extends Authenticatable implements FilamentUser
{
use HasFactory;
use Notifiable;

/**
Expand Down Expand Up @@ -48,4 +51,9 @@ public function canAccessPanel(Panel $panel): bool
{
return true;
}

protected static function newFactory()
{
return UserFactory::new();
}
}
2 changes: 2 additions & 0 deletions tests/Providers/AdminPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;
use Indra\Revisor\Middleware\DraftMiddleware;
use Indra\RevisorFilament\RevisorFilamentPlugin;
use Indra\RevisorFilament\Tests\Resources\PageResource;

Expand All @@ -30,6 +31,7 @@ public function panel(Panel $panel): Panel
->pages([
Pages\Dashboard::class,
])
->middleware([DraftMiddleware::class], isPersistent: true)
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
Expand Down
34 changes: 34 additions & 0 deletions tests/PublishTableActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Indra\RevisorFilament\PublishTableAction;
use Indra\RevisorFilament\Tests\Models\Page;
use Indra\RevisorFilament\Tests\Resources\PageResource\Pages\ListPages;

use function Pest\Livewire\livewire;

it('only shows on unpublishedOrRevised pages', function () {
$page = Page::create(['title' => 'page']);

livewire(ListPages::class)
->assertTableActionExists(PublishTableAction::class, record: $page);

$page->publish();

livewire(ListPages::class)
->assertTableActionDoesNotExist(PublishTableAction::class, record: $page);

$page->update(['title' => 'updated page']);

livewire(ListPages::class)
->assertTableActionExists(PublishTableAction::class, record: $page);
});

it('can publish pages', function () {
$page = Page::create(['title' => 'draft']);

livewire(ListPages::class)
->callTableAction(PublishTableAction::class, $page)
->assertHasNoTableActionErrors();

expect($page->refresh()->isPublished())->toBeTrue();
});
21 changes: 20 additions & 1 deletion tests/PublishedStatusTableColumnTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
<?php

use Indra\Revisor\Facades\Revisor;
use Indra\RevisorFilament\Tests\Models\Page;
use Indra\RevisorFilament\Tests\Resources\PageResource;
use Indra\RevisorFilament\Tests\Resources\PageResource\Pages\ListPages;

use function Pest\Livewire\livewire;

it('can renders correct published statuses', function () {
Revisor::draftContext();

it('can render page', function () {
$this->get(PageResource::getUrl('index'))->assertSuccessful();

$draft = Page::create(['title' => 'draft']);
$published = Page::create(['title' => 'published'])->publish();
$revised = Page::create(['title' => 'revise'])->publish();

$this->travel(5)->seconds();
$revised->update(['title' => 'revised']);

livewire(ListPages::class)
->assertTableColumnStateSet('published_status', '["draft"]', $draft)
->assertTableColumnStateSet('published_status', '["published"]', $published)
->assertTableColumnStateSet('published_status', '["published","revised"]', $revised);
});
10 changes: 7 additions & 3 deletions tests/Resources/PageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;
use Indra\RevisorFilament\Filament\PublishedStatusTableColumn;
use Indra\RevisorFilament\Tests\Pages\ListPages;
use Indra\RevisorFilament\PublishedStatusTableColumn;
use Indra\RevisorFilament\PublishTableAction;
use Indra\RevisorFilament\Tests\Resources\PageResource\Pages\ListPages;
use Indra\RevisorFilament\UnpublishTableAction;

// use LiveSource\Chord\Filament\Actions\CreateChildPageTableAction;
// use Livesource\Chord\Filament\Actions\PublishBulkAction;
Expand Down Expand Up @@ -47,7 +49,7 @@ public static function table(Table $table): Table
->toggledHiddenByDefault(),
Tables\Columns\TextColumn::make('title')
->searchable(),
PublishedStatusTableColumn::make('publish_status'),
PublishedStatusTableColumn::make('published_status'),
Tables\Columns\TextColumn::make('publisher.name')
->label('Published')
->prefix('By: ')
Expand All @@ -60,6 +62,8 @@ public static function table(Table $table): Table
->actions([
Tables\Actions\ActionGroup::make([
EditAction::make(),
PublishTableAction::make(),
UnpublishTableAction::make(),
// Tables\Actions\Action::make('versions')
// ->label('History')
// ->url(fn (ChordPage $record) => PageResource::getUrl('versions', ['record' => $record->{$record->getRouteKeyName()}]))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Indra\RevisorFilament\Tests\Pages;
namespace Indra\RevisorFilament\Tests\Resources\PageResource\Pages;

use Filament\Resources\Pages\ListRecords;
use Indra\RevisorFilament\Tests\Resources\PageResource;
Expand Down
10 changes: 4 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Filament\Tables\TablesServiceProvider;
use Filament\Widgets\WidgetsServiceProvider;
use Illuminate\Database\Eloquent\Factories\Factory;
use Indra\Revisor\Enums\RevisorContext;
use Indra\Revisor\RevisorServiceProvider;
use Indra\RevisorFilament\RevisorFilamentServiceProvider;
use Indra\RevisorFilament\Tests\Models\User;
Expand All @@ -34,11 +35,7 @@ protected function setUp(): void
fn (string $modelName) => 'Indra\\RevisorFilament\\Database\\Factories\\' . class_basename($modelName) . 'Factory'
);

$this->actingAs(User::create([
'name' => 'test',
'email' => '[email protected]',
'password' => bcrypt('password'),
]));
$this->actingAs(User::factory()->create());
}

protected function getPackageProviders($app)
Expand All @@ -65,7 +62,8 @@ protected function getPackageProviders($app)
public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');
$app['config']->set('auth.providers.users.model', User::class);
config()->set('revisor.default_context', RevisorContext::Draft);
config()->set('auth.providers.users.model', User::class);

$migration = include __DIR__ . '/database/create_test_tables.php';
$migration->up();
Expand Down
31 changes: 31 additions & 0 deletions tests/UnpublishTableActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Indra\Revisor\Facades\Revisor;
use Indra\RevisorFilament\Tests\Models\Page;
use Indra\RevisorFilament\Tests\Resources\PageResource\Pages\ListPages;
use Indra\RevisorFilament\UnpublishTableAction;

use function Pest\Livewire\livewire;

it('only shows on published pages', function () {
Revisor::draftContext();
$page = Page::create(['title' => 'page']);

livewire(ListPages::class)
->assertTableActionDoesNotExist(UnpublishTableAction::class, record: $page);

$page->publish();

livewire(ListPages::class)
->assertTableActionExists(UnpublishTableAction::class, record: $page);
});

it('can unpublish pages', function () {
$page = Page::create(['title' => 'draft'])->publish();

livewire(ListPages::class)
->callTableAction(UnpublishTableAction::class, record: $page)
->assertHasNoTableActionErrors();

expect($page->refresh()->isPublished())->toBeFalse();
});
18 changes: 18 additions & 0 deletions tests/database/factories/PageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Indra\RevisorFilament\Tests\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Indra\RevisorFilament\Tests\Models\Page;

class PageFactory extends Factory
{
protected $model = Page::class;

public function definition(): array
{
return [
'title' => $this->faker->sentence(),
];
}
}
Loading

0 comments on commit 951fbdc

Please sign in to comment.