89 lines
3.8 KiB
PHP
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);
|
|
});
|