generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests to verify syncing and deletion queuing is functioning corre…
…ctly
- Loading branch information
Kurt Friars
committed
Mar 17, 2024
1 parent
a5d3396
commit c18f013
Showing
5 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
use Plank\Publisher\Enums\Status; | ||
use Plank\Publisher\Facades\Publisher; | ||
use Plank\Publisher\Tests\Helpers\Models\Post; | ||
use Plank\Publisher\Tests\Helpers\Models\Section; | ||
|
||
beforeEach(function () { | ||
Publisher::allowDraftContent(); | ||
}); | ||
|
||
it('publishes all dependent content when its parent is published', function () { | ||
$post = Post::factory()->create([ | ||
'status' => Status::DRAFT, | ||
]); | ||
|
||
$sections = Section::factory(3)->create([ | ||
'post_id' => $post->id, | ||
'status' => Status::DRAFT, | ||
]); | ||
|
||
$post->status = Status::PUBLISHED; | ||
$post->save(); | ||
|
||
$sections->each(function ($section) { | ||
expect($section->fresh()->status)->toBe(Status::PUBLISHED); | ||
}); | ||
}); | ||
|
||
it('unpublishes all dependent content when its parent is unpublished', function () { | ||
$post = Post::factory()->create([ | ||
'status' => Status::PUBLISHED, | ||
]); | ||
|
||
$sections = Section::factory(3)->create([ | ||
'post_id' => $post->id, | ||
'status' => Status::PUBLISHED, | ||
]); | ||
|
||
$post->status = Status::DRAFT; | ||
$post->save(); | ||
|
||
$sections->each(function ($section) { | ||
expect($section->fresh()->status)->toBe(Status::DRAFT); | ||
}); | ||
}); | ||
|
||
it('does not delete dependent content when its parent is in draft', function () { | ||
$post = Post::factory()->create([ | ||
'status' => Status::DRAFT, | ||
]); | ||
|
||
$sections = Section::factory(3)->create([ | ||
'post_id' => $post->id, | ||
'status' => Status::DRAFT, | ||
]); | ||
|
||
$section = $sections->first(); | ||
$section->delete(); | ||
expect($post->sections()->withoutQueuedDeletes()->count())->toBe(2); | ||
expect($post->sections()->count())->toBe(3); | ||
expect($section->should_delete)->toBeTrue(); | ||
}); | ||
|
||
it('deletes dependent content when its parent is published', function () { | ||
$post = Post::factory()->create([ | ||
'status' => Status::PUBLISHED, | ||
]); | ||
|
||
$sections = Section::factory(3)->create([ | ||
'post_id' => $post->id, | ||
'status' => Status::PUBLISHED, | ||
]); | ||
|
||
$section = $sections->first(); | ||
$section->delete(); | ||
expect($post->sections()->withoutQueuedDeletes()->count())->toBe(2); | ||
expect($post->sections()->count())->toBe(2); | ||
expect($section->should_delete)->toBeFalse(); | ||
}); | ||
|
||
it('deletes dependent content queued to be deleted when its parent is published', function () { | ||
$post = Post::factory()->create([ | ||
'status' => Status::DRAFT, | ||
]); | ||
|
||
$sections = Section::factory(3)->create([ | ||
'post_id' => $post->id, | ||
'status' => Status::DRAFT, | ||
]); | ||
|
||
$section = $sections->first(); | ||
$section->delete(); | ||
expect($post->sections()->withoutQueuedDeletes()->count())->toBe(2); | ||
expect($post->sections()->count())->toBe(3); | ||
expect($section->should_delete)->toBeTrue(); | ||
|
||
$post->status = Status::PUBLISHED; | ||
$post->save(); | ||
|
||
expect($post->sections()->withoutQueuedDeletes()->count())->toBe(2); | ||
expect($post->sections()->count())->toBe(2); | ||
}); | ||
|
||
it('does not delete dependent content queued to be deleted when its parent is saved in a non-published state', function () { | ||
$post = Post::factory()->create([ | ||
'status' => Status::DRAFT, | ||
]); | ||
|
||
$sections = Section::factory(3)->create([ | ||
'post_id' => $post->id, | ||
'status' => Status::DRAFT, | ||
]); | ||
|
||
$section = $sections->first(); | ||
$section->delete(); | ||
|
||
expect($post->sections()->withoutQueuedDeletes()->count())->toBe(2); | ||
expect($post->sections()->count())->toBe(3); | ||
expect($section->should_delete)->toBeTrue(); | ||
|
||
$post->title .= ' – Updated'; | ||
$post->save(); | ||
|
||
expect($post->sections()->withoutQueuedDeletes()->count())->toBe(2); | ||
expect($post->sections()->count())->toBe(3); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Plank\Publisher\Tests\Helper\Database\Factories; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Plank\Publisher\Enums\Status; | ||
use Plank\Publisher\Tests\Helpers\Models\Post; | ||
use Plank\Publisher\Tests\Helpers\Models\Section; | ||
|
||
class SectionFactory extends Factory | ||
{ | ||
protected $model = Section::class; | ||
|
||
public function definition() | ||
{ | ||
$heading = $this->faker->words($this->faker->numberBetween(2, 4), true); | ||
$status = $this->faker->randomElement(Status::cases()); | ||
|
||
$attributes = [ | ||
'post_id' => Post::query()->inRandomOrder()->first()?->id ?? Post::factory(), | ||
'heading' => $heading, | ||
'text' => $this->faker->paragraphs($this->faker->numberBetween(1, 3), true), | ||
]; | ||
|
||
return [ | ||
...$attributes, | ||
'draft' => $status === 'draft' ? $attributes : null, | ||
'status' => $status, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Plank\Publisher\Tests\Helpers\Models; | ||
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Plank\Publisher\Concerns\IsPublishable; | ||
use Plank\Publisher\Contracts\Publishable; | ||
|
||
class Section extends TestModel implements Publishable | ||
{ | ||
use IsPublishable; | ||
|
||
public string $dependendsOnPublishable = 'post'; | ||
|
||
public function post(): BelongsTo | ||
{ | ||
return $this->belongsTo(Post::class, 'post_id'); | ||
} | ||
} |