Files
laravel-manager/tests/Feature/RevisionsTest.php
T
Matthijs Iriks 6fdb02040b
PR CI / flow (pull_request) Successful in 3s
PR CI / ledger (pull_request) Successful in 3s
PR CI / ui (pull_request) Successful in 17s
PR CI / test (lowest) (pull_request) Successful in 47s
PR CI / test (stable) (pull_request) Successful in 51s
PR CI / security (pull_request) Successful in 53s
PR CI / pr-ci (pull_request) Successful in 1s
PR CI / release-automerge (pull_request) Successful in 3s
Add a history of posts and pages with restore
2026-12-04 05:29:34 +01:00

89 lines
3.8 KiB
PHP

<?php
use IriksIT\Manager\Models\Page;
use IriksIT\Manager\Models\Post;
use IriksIT\Manager\Models\Revision;
use IriksIT\Manager\Tests\AllModulesTestCase;
uses(AllModulesTestCase::class);
function writtenPost(array $values = []): Post
{
return Post::create($values + ['title' => 'First', 'slug' => 'first', 'body' => 'One', 'tags' => ['a']]);
}
it('keeps a state at every save, and nothing when nothing changed', function () {
$post = writtenPost();
$post->update(['body' => 'Two']);
$post->save();
$revisions = Revision::query()->where('type', 'posts')->orderBy('id')->get();
expect($revisions)->toHaveCount(2)
->and($revisions[0]->changed)->toContain('title', 'body')
->and($revisions[1]->changed)->toBe(['body'])
->and($revisions[1]->data)->not->toHaveKey('published_at')->and($revisions[1]->data)->not->toHaveKey('id');
});
it('lists the history to someone who may see it, and only that', function () {
$post = writtenPost();
$post->update(['body' => 'Two']);
$this->actingAs(makeUser(['posts.view', 'revisions.view']));
$list = $this->getJson("/manager/revisions/posts/{$post->id}")->assertOk()->json();
expect($list['revisions'])->toHaveCount(2)
->and($list['revisions'][0])->toMatchArray(['current' => true, 'changed' => ['body']])
->and($list['revisions'][0]['values']['body'])->toBe('Two')
->and($list['can_restore'])->toBeFalse();
$this->actingAs(makeUser(['revisions.view']))->getJson("/manager/revisions/posts/{$post->id}")->assertForbidden();
$this->actingAs(makeUser(['posts.view']))->getJson("/manager/revisions/posts/{$post->id}")->assertForbidden();
$this->actingAs(makeUser(['posts.view', 'revisions.view']))->getJson('/manager/revisions/cars/1')->assertNotFound();
});
it('puts an earlier state back without touching whether the post is on the site', function () {
$post = writtenPost(['published_at' => now()->subDay()]);
$first = Revision::query()->where('type', 'posts')->sole();
$post->update(['body' => 'Two', 'tags' => ['b'], 'published_at' => null]);
$this->actingAs(makeUser(['posts.view', 'posts.write', 'revisions.view', 'revisions.restore']));
$this->postJson("/manager/revisions/posts/{$post->id}/{$first->id}/restore")->assertOk();
$post->refresh();
expect($post->body)->toBe('One')->and($post->tags)->toBe(['a'])->and($post->published_at)->toBeNull()
->and(Revision::query()->where('type', 'posts')->count())->toBe(3);
});
it('keeps a live post for those who may publish', function () {
$post = writtenPost(['published_at' => now()->subDay()]);
$first = Revision::query()->where('type', 'posts')->sole();
$post->update(['body' => 'Two']);
$this->actingAs(makeUser(['posts.view', 'posts.write', 'revisions.view', 'revisions.restore']));
$this->postJson("/manager/revisions/posts/{$post->id}/{$first->id}/restore")->assertForbidden();
$this->actingAs(makeUser(['posts.view', 'posts.write', 'posts.publish', 'revisions.view', 'revisions.restore']));
$this->postJson("/manager/revisions/posts/{$post->id}/{$first->id}/restore")->assertOk();
$this->actingAs(makeUser(['posts.view', 'revisions.view']));
$this->postJson("/manager/revisions/posts/{$post->id}/{$first->id}/restore")->assertForbidden();
});
it('has a history for pages, forgets it with the page, and keeps only the newest', function () {
$page = Page::create(['slug' => 'privacy', 'title' => 'Privacy', 'body' => '1']);
config(['manager.revisions.keep' => 5]);
foreach (range(2, 9) as $n) {
$page->update(['body' => (string) $n]);
}
expect(Revision::query()->where('type', 'pages')->count())->toBe(5)
->and(Revision::query()->where('type', 'pages')->orderByDesc('id')->first()->data['body'])->toBe('9');
$page->delete();
expect(Revision::query()->where('type', 'pages')->count())->toBe(0);
});