Skip to content

Commit

Permalink
feat(revisions): Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pboivin committed Jun 30, 2022
1 parent 2da2200 commit d8b4f55
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 6 deletions.
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<file>tests/integration/BrowsersTest.php</file>
<file>tests/integration/NestedModuleTest.php</file>
<file>tests/integration/SingletonModuleTest.php</file>
<file>tests/integration/RevisionsTest.php</file>
</testsuite>
</testsuites>
<filter>
Expand Down
203 changes: 203 additions & 0 deletions tests/integration/RevisionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php

namespace A17\Twill\Tests\Integration;

use App\Repositories\AuthorRepository;
use Carbon\Carbon;

class RevisionsTest extends TestCase
{
protected $allFiles = [
'{$stubs}/modules/authors/Author.php' => '{$app}/Models/',
'{$stubs}/modules/authors/AuthorController.php' => '{$app}/Http/Controllers/Admin/',
'{$stubs}/modules/authors/AuthorRepository.php' => '{$app}/Repositories/',
'{$stubs}/modules/authors/AuthorRequest.php' => '{$app}/Http/Requests/Admin/',
'{$stubs}/modules/authors/AuthorRevision.php' => '{$app}/Models/Revisions/',
'{$stubs}/modules/authors/AuthorSlug.php' => '{$app}/Models/Slugs/',
'{$stubs}/modules/authors/AuthorTranslation.php' => '{$app}/Models/Translations/',
'{$stubs}/modules/authors/2019_10_18_193753_create_authors_tables.php' => '{$database}/migrations/',
'{$stubs}/modules/authors/admin.php' => '{$base}/routes/admin.php',
];

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

$this->copyFiles($this->allFiles);

$this->migrate();

$this->login();
}

public function addMinutes($minutes = 1)
{
Carbon::setTestNow($this->now->addMinutes($minutes));
}

public function createAuthorWithRevisions()
{
$this->addMinutes(1);

$author = $this->createAuthor();

$this->addMinutes(1);

$this->updateAuthor($author, [
'name' => ['en' => 'Bobby', 'fr' => 'Bobby'],
'published' => false,
]);

$this->addMinutes(1);

$this->updateAuthor($author, [
'name' => ['en' => 'Bobby', 'fr' => 'Bobby'],
'published' => true,
]);

$this->addMinutes(1);

return $author;
}

public function createAuthor($fields = [])
{
$defaults = [
'name' => ['en' => 'Bob', 'fr' => 'Bob'],
'published' => false,
];

return app(AuthorRepository::class)->create(
array_merge($defaults, $fields)
);
}

public function updateAuthor($author, $fields = [])
{
app(AuthorRepository::class)->update($author->id, $fields);

return $author->refresh();
}

public function getRevisionLabels($item)
{
return collect($item->refresh()->revisionsArray())
->pluck('label')
->toArray();
}

public function getRevisionPayloads($item)
{
$revisions = $item->refresh()->revisions;

return collect($revisions)->map(function ($revision) {
return json_decode($revision->payload, true);
});
}

public function test_module_has_revisions()
{
$author = $this->createAuthorWithRevisions();

$this->assertEquals(3, $author->revisions()->count());
}

public function test_update_modifies_published_record()
{
$author = $this->createAuthorWithRevisions();

$this->httpRequestAssert("/twill/personnel/authors/{$author->id}", 'PUT', [
'name' => ['en' => 'Test', 'fr' => 'Test'],
'published' => true,
]);

$author->refresh();
$this->assertTrue((bool) $author->published);
$this->assertEquals('Test', $author->name);
}

public function test_latest_revision_is_identified_as_current()
{
$author = $this->createAuthor();

$this->assertEquals(['Current'], $this->getRevisionLabels($author));

$this->updateAuthor($author, [
'name' => ['en' => 'Bobby', 'fr' => 'Bobby'],
'published' => false,
]);

$this->assertEquals(['Current', ''], $this->getRevisionLabels($author));

$this->updateAuthor($author, [
'name' => ['en' => 'Bobby', 'fr' => 'Bobby'],
'published' => true,
]);

$this->assertEquals(['Current', '', ''], $this->getRevisionLabels($author));
}

public function test_can_create_draft_revisions()
{
putenv('ENABLE_DRAFT_REVISIONS=true');

$author = $this->createAuthorWithRevisions();

$this->httpRequestAssert("/twill/personnel/authors/{$author->id}", 'PUT', [
'name' => ['en' => 'Test', 'fr' => 'Test'],
'published' => true,
'cmsSaveType' => 'draft-revision',
]);

$this->assertEquals(4, $author->revisions()->count());
}

public function test_draft_revision_does_not_modify_published_record()
{
putenv('ENABLE_DRAFT_REVISIONS=true');

$author = $this->createAuthorWithRevisions();

$this->httpRequestAssert("/twill/personnel/authors/{$author->id}", 'PUT', [
'name' => ['en' => 'Test', 'fr' => 'Test'],
'published' => true,
'cmsSaveType' => 'draft-revision',
]);

$author->refresh();
$this->assertTrue((bool) $author->published);
$this->assertNotEquals('Test', $author->name);
}

public function test_draft_revision_is_saved_as_draft()
{
putenv('ENABLE_DRAFT_REVISIONS=true');

$author = $this->createAuthorWithRevisions();

$this->httpRequestAssert("/twill/personnel/authors/{$author->id}", 'PUT', [
'name' => ['en' => 'Test', 'fr' => 'Test'],
'published' => true,
'cmsSaveType' => 'draft-revision',
]);

$revisionData = $this->getRevisionPayloads($author)->first();
$this->assertEquals('draft-revision', $revisionData['cmsSaveType']);
$this->assertFalse((bool) $revisionData['published']);
}

public function test_draft_revision_is_not_identified_as_current()
{
putenv('ENABLE_DRAFT_REVISIONS=true');

$author = $this->createAuthorWithRevisions();

$this->httpRequestAssert("/twill/personnel/authors/{$author->id}", 'PUT', [
'name' => ['en' => 'Test', 'fr' => 'Test'],
'published' => true,
'cmsSaveType' => 'draft-revision',
]);

$this->assertEquals(['', 'Current', '', ''], $this->getRevisionLabels($author));
}
}
6 changes: 0 additions & 6 deletions tests/stubs/modules/authors/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use A17\Twill\Models\Behaviors\Sortable;
use A17\Twill\Models\Model;
use App\Models\Slugs\AuthorSlug;
use App\Models\Revisions\AuthorRevision;

class Author extends Model implements Sortable
{
Expand Down Expand Up @@ -77,11 +76,6 @@ public function slugs()
return $this->hasMany(AuthorSlug::class);
}

public function revisions()
{
return $this->hasMany(AuthorRevision::class);
}

public function categories()
{
return $this->belongsToMany(Category::class);
Expand Down
2 changes: 2 additions & 0 deletions tests/stubs/modules/authors/AuthorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function __construct(Application $app, Request $request)
parent::__construct($app, $request);

$this->indexOptions['editInModal'] = env('EDIT_IN_MODAL', false);

$this->enableDraftRevisions = env('ENABLE_DRAFT_REVISIONS', false);
}

public function getIndexOptions()
Expand Down

0 comments on commit d8b4f55

Please sign in to comment.