Skip to content

Commit

Permalink
Add tests to verify syncing and deletion queuing is functioning corre…
Browse files Browse the repository at this point in the history
…ctly
  • Loading branch information
Kurt Friars committed Mar 17, 2024
1 parent a5d3396 commit c18f013
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 1 deletion.
127 changes: 127 additions & 0 deletions tests/Feature/SyncPublishingTest.php
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);
});
3 changes: 2 additions & 1 deletion tests/Helpers/Database/Factories/PostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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\User;

Expand All @@ -13,7 +14,7 @@ class PostFactory extends Factory
public function definition()
{
$title = $this->faker->words($this->faker->numberBetween(2, 4), true);
$status = $this->faker->randomElement(['draft', 'published']);
$status = $this->faker->randomElement(Status::cases());

$attributes = [
'author_id' => User::query()->inRandomOrder()->first()?->id ?? User::factory(),
Expand Down
31 changes: 31 additions & 0 deletions tests/Helpers/Database/Factories/SectionFactory.php
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,
];
}
}
7 changes: 7 additions & 0 deletions tests/Helpers/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class Post extends TestModel implements Publishable
{
use IsPublishable;

protected array $publishingDependents = ['sections'];

public function sections(): HasMany
{
return $this->hasMany(Section::class, 'post_id');
}

public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'author_id');
Expand Down
19 changes: 19 additions & 0 deletions tests/Helpers/Models/Section.php
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');
}
}

0 comments on commit c18f013

Please sign in to comment.