Skip to content

Commit

Permalink
Remove direct calls to ->save() method on entries (#31)
Browse files Browse the repository at this point in the history
* Write a test to confirm the fix

* And write a similar test for updating an entry

* should be 2 since we're creating the entry before updating it

* we should only be 'touching' entries, not saving them directly
  • Loading branch information
duncanmcclean authored Oct 17, 2022
1 parent bfefd58 commit db84eba
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/Http/Controllers/GuestEntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public function store(StoreRequest $request)
$entry->set($key, $value);
}

$entry->save();
$entry->touch();

event(new GuestEntryCreated($entry));
Expand Down Expand Up @@ -161,7 +160,6 @@ public function update(UpdateRequest $request)
$entry->date($request->get('date'));
}

$entry->save();
$entry->touch();
}

Expand Down
62 changes: 62 additions & 0 deletions tests/Http/Controllers/GuestEntryControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\File;
use Spatie\TestTime\TestTime;
use Statamic\Events\EntrySaved;
use Statamic\Facades\AssetContainer;
use Statamic\Facades\Blueprint;
use Statamic\Facades\Collection;
Expand Down Expand Up @@ -745,6 +746,31 @@ public function can_store_entry_and_ensure_created_in_correct_site_by_current_si
$this->assertSame($entry->locale(), 'two');
}

/** @test */
public function can_store_entry_and_ensure_entry_is_only_saved_once()
{
Event::fake();

Collection::make('comments')->save();

$this
->post(route('statamic.guest-entries.store'), [
'_collection' => 'comments',
'title' => 'This is great',
'slug' => 'this-is-great',
])
->assertRedirect();

$entry = Entry::all()->last();

$this->assertNotNull($entry);
$this->assertSame($entry->collectionHandle(), 'comments');
$this->assertSame($entry->get('title'), 'This is great');
$this->assertSame($entry->slug(), 'this-is-great');

Event::assertDispatchedTimes(EntrySaved::class, 1);
}

/** @test */
public function can_update_entry()
{
Expand Down Expand Up @@ -1395,6 +1421,42 @@ public function can_update_entry_and_date_and_ensure_date_is_saved_normally_if_n
$this->assertStringContainsString('allo-mate.md', $entry->path());
}

/** @test */
public function can_update_entry_and_ensure_entry_is_only_saved_once()
{
Event::fake();

Collection::make('albums')->save();

Entry::make()
->id('allo-mate-idee')
->collection('albums')
->slug('allo-mate')
->data([
'title' => 'Allo Mate!',
'artist' => 'Guvna B',
])
->save();

$this
->post(route('statamic.guest-entries.update'), [
'_collection' => 'albums',
'_id' => 'allo-mate-idee',
'record_label' => 'Unknown',
])
->assertRedirect();

$entry = Entry::find('allo-mate-idee');

$this->assertNotNull($entry);
$this->assertSame($entry->collectionHandle(), 'albums');
$this->assertSame($entry->get('title'), 'Allo Mate!');
$this->assertSame($entry->get('record_label'), 'Unknown');
$this->assertSame($entry->slug(), 'allo-mate');

Event::assertDispatchedTimes(EntrySaved::class, 2);
}

/** @test */
public function can_destroy_entry()
{
Expand Down

0 comments on commit db84eba

Please sign in to comment.