From 5ddefcaaf039cac26ff911c269b980a271c710fb Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Fri, 5 Jan 2024 17:05:39 +0000 Subject: [PATCH] Adjust expectations for dispatchCommit method --- tests/Git/GitEventTest.php | 136 +++++++++++++++++++------------------ 1 file changed, 69 insertions(+), 67 deletions(-) diff --git a/tests/Git/GitEventTest.php b/tests/Git/GitEventTest.php index 548804eecd..02b43e25df 100644 --- a/tests/Git/GitEventTest.php +++ b/tests/Git/GitEventTest.php @@ -23,6 +23,8 @@ class GitEventTest extends TestCase { use PreventSavingStacheItemsToDisk; + public $user; + public function setUp(): void { parent::setUp(); @@ -32,13 +34,13 @@ public function setUp(): void Config::set('statamic.git', $defaultConfig); Config::set('statamic.git.enabled', true); - $this->actingAs( - User::make() - ->id('chewbacca') - ->email('chew@bacca.com') - ->data(['name' => 'Chewbacca']) - ->makeSuper() - ); + $this->user = User::make() + ->id('chewbacca') + ->email('chew@bacca.com') + ->data(['name' => 'Chewbacca']) + ->makeSuper(); + + $this->actingAs($this->user); Config::set('filesystems.disks.test', [ 'driver' => 'local', @@ -53,8 +55,8 @@ public function setUp(): void /** @test */ public function it_doesnt_commit_when_git_is_disabled() { - Git::shouldReceive('dispatchCommit')->with('Collection saved')->never(); - Git::shouldReceive('dispatchCommit')->with('Collection deleted')->never(); + Git::shouldReceive('dispatchCommit')->with('Collection saved', $this->user)->never(); + Git::shouldReceive('dispatchCommit')->with('Collection deleted', $this->user)->never(); Config::set('statamic.git.enabled', false); @@ -67,8 +69,8 @@ public function it_doesnt_commit_when_git_is_disabled() /** @test */ public function it_doesnt_commit_when_automatic_is_disabled() { - Git::shouldReceive('dispatchCommit')->with('Collection saved')->never(); - Git::shouldReceive('dispatchCommit')->with('Collection deleted')->never(); + Git::shouldReceive('dispatchCommit')->with('Collection saved', $this->user)->never(); + Git::shouldReceive('dispatchCommit')->with('Collection deleted', $this->user)->never(); Config::set('statamic.git.automatic', false); @@ -81,8 +83,8 @@ public function it_doesnt_commit_when_automatic_is_disabled() /** @test */ public function it_doesnt_commit_ignored_events() { - Git::shouldReceive('dispatchCommit')->with('Collection saved')->never(); - Git::shouldReceive('dispatchCommit')->with('Collection deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Collection saved', $this->user)->never(); + Git::shouldReceive('dispatchCommit')->with('Collection deleted', $this->user)->once(); Config::set('statamic.git.ignored_events', [ \Statamic\Events\CollectionSaved::class, @@ -97,8 +99,8 @@ public function it_doesnt_commit_ignored_events() /** @test */ public function it_doesnt_commit_when_event_subscriber_is_disabled() { - Git::shouldReceive('dispatchCommit')->with('Collection saved')->never(); - Git::shouldReceive('dispatchCommit')->with('Collection deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Collection saved', $this->user)->never(); + Git::shouldReceive('dispatchCommit')->with('Collection deleted', $this->user)->once(); $collection = Facades\Collection::make('pages'); @@ -112,7 +114,7 @@ public function it_doesnt_commit_when_event_subscriber_is_disabled() /** @test */ public function it_commits_when_custom_addon_events_are_registered() { - Git::shouldReceive('dispatchCommit')->with('Pun saved')->once(); + Git::shouldReceive('dispatchCommit')->with('Pun saved', $this->user)->once(); Git::makePartial(); Git::listen(PunSaved::class); @@ -127,8 +129,8 @@ public function it_commits_when_custom_addon_events_are_registered() /** @test */ public function it_commits_when_blueprint_is_saved_and_deleted() { - Git::shouldReceive('dispatchCommit')->with('Blueprint saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Blueprint deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Blueprint saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Blueprint deleted', $this->user)->once(); $blueprint = Facades\Blueprint::make('post'); @@ -139,8 +141,8 @@ public function it_commits_when_blueprint_is_saved_and_deleted() /** @test */ public function it_commits_when_fieldset_is_saved_and_deleted() { - Git::shouldReceive('dispatchCommit')->with('Fieldset saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Fieldset deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Fieldset saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Fieldset deleted', $this->user)->once(); $fieldset = Facades\Fieldset::make('address'); @@ -151,8 +153,8 @@ public function it_commits_when_fieldset_is_saved_and_deleted() /** @test */ public function it_commits_when_collection_is_saved_and_deleted() { - Git::shouldReceive('dispatchCommit')->with('Collection saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Collection deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Collection saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Collection deleted', $this->user)->once(); $collection = Facades\Collection::make('pages'); @@ -167,8 +169,8 @@ public function it_commits_when_entry_is_saved_and_deleted() Events\CollectionSaved::class, ]); - Git::shouldReceive('dispatchCommit')->with('Entry saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Entry deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Entry saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Entry deleted', $this->user)->once(); $collection = Facades\Collection::make('pages'); $collection->save(); @@ -190,8 +192,8 @@ public function it_commits_when_tracked_revisions_are_saved_and_deleted() Config::set('statamic.revisions.path', base_path('content/revisions')); - Git::shouldReceive('dispatchCommit')->with('Revision saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Revision deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Revision saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Revision deleted', $this->user)->once(); $collection = Facades\Collection::make('pages'); $collection->save(); @@ -207,8 +209,8 @@ public function it_commits_when_tracked_revisions_are_saved_and_deleted() /** @test */ public function it_commits_when_navigation_is_saved_and_deleted() { - Git::shouldReceive('dispatchCommit')->with('Navigation saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Navigation deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Navigation saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Navigation deleted', $this->user)->once(); $nav = Facades\Nav::make()->handle('footer'); @@ -219,8 +221,8 @@ public function it_commits_when_navigation_is_saved_and_deleted() /** @test */ public function it_commits_when_a_navigation_tree_is_saved_and_deleted() { - Git::shouldReceive('dispatchCommit')->with('Navigation tree saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Navigation tree deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Navigation tree saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Navigation tree deleted', $this->user)->once(); $nav = Facades\Nav::make()->handle('footer'); $tree = $nav->makeTree('en'); @@ -236,8 +238,8 @@ public function it_commits_when_a_collection_tree_is_saved_and_deleted() Events\CollectionSaved::class, ]); - Git::shouldReceive('dispatchCommit')->with('Collection tree saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Collection tree deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Collection tree saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Collection tree deleted', $this->user)->once(); $collection = Facades\Collection::make('pages')->structureContents(['max_depth' => 10])->save(); $tree = $collection->structure()->makeTree('en'); @@ -249,8 +251,8 @@ public function it_commits_when_a_collection_tree_is_saved_and_deleted() /** @test */ public function it_commits_when_taxonomy_is_saved_and_deleted() { - Git::shouldReceive('dispatchCommit')->with('Taxonomy saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Taxonomy deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Taxonomy saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Taxonomy deleted', $this->user)->once(); $taxonomy = Facades\Taxonomy::make('topics'); @@ -265,8 +267,8 @@ public function it_commits_when_term_is_saved_and_deleted() Events\TaxonomySaved::class, ]); - Git::shouldReceive('dispatchCommit')->with('Term saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Term deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Term saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Term deleted', $this->user)->once(); $taxonomy = Facades\Taxonomy::make('topics'); $taxonomy->save(); @@ -283,8 +285,8 @@ public function it_commits_when_term_is_saved_and_deleted() /** @test */ public function it_commits_when_global_set_is_saved_and_deleted() { - Git::shouldReceive('dispatchCommit')->with('Global Set saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Global Set deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Global Set saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Global Set deleted', $this->user)->once(); $set = Facades\GlobalSet::make('main'); $set->addLocalization($set->makeLocalization(Facades\Site::default()->handle())); @@ -296,8 +298,8 @@ public function it_commits_when_global_set_is_saved_and_deleted() /** @test */ public function it_commits_when_form_is_saved_and_deleted() { - Git::shouldReceive('dispatchCommit')->with('Form saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Form deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Form saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Form deleted', $this->user)->once(); $form = Facades\Form::make('main'); @@ -312,8 +314,8 @@ public function it_commits_when_form_submission_is_saved_and_deleted() Events\FormSaved::class, ]); - Git::shouldReceive('dispatchCommit')->with('Submission saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Submission deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Submission saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Submission deleted', $this->user)->once(); $form = Facades\Form::make('contact'); @@ -328,8 +330,8 @@ public function it_commits_when_form_submission_is_saved_and_deleted() /** @test */ public function it_commits_when_user_is_saved_and_deleted() { - Git::shouldReceive('dispatchCommit')->with('User saved')->once(); - Git::shouldReceive('dispatchCommit')->with('User deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('User saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('User deleted', $this->user)->once(); $user = Facades\User::make(); @@ -340,8 +342,8 @@ public function it_commits_when_user_is_saved_and_deleted() /** @test */ public function it_commits_when_user_role_is_saved_and_deleted() { - Git::shouldReceive('dispatchCommit')->with('Role saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Role deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Role saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Role deleted', $this->user)->once(); $role = Facades\Role::make()->handle('author'); @@ -352,8 +354,8 @@ public function it_commits_when_user_role_is_saved_and_deleted() /** @test */ public function it_commits_when_user_group_is_saved_and_deleted() { - Git::shouldReceive('dispatchCommit')->with('User group saved')->once(); - Git::shouldReceive('dispatchCommit')->with('User group deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('User group saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('User group deleted', $this->user)->once(); $group = Facades\UserGroup::make()->handle('admin'); @@ -364,7 +366,7 @@ public function it_commits_when_user_group_is_saved_and_deleted() /** @test */ public function it_commits_when_default_user_preferences_are_saved() { - Git::shouldReceive('dispatchCommit')->with('Default preferences saved')->once(); + Git::shouldReceive('dispatchCommit')->with('Default preferences saved', $this->user)->once(); Facades\Preference::default()->set('foo', 'bar')->save(); @@ -374,8 +376,8 @@ public function it_commits_when_default_user_preferences_are_saved() /** @test */ public function it_commits_when_asset_container_is_saved_and_deleted() { - Git::shouldReceive('dispatchCommit')->with('Asset container saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Asset container deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Asset container saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Asset container deleted', $this->user)->once(); $container = Facades\AssetContainer::make()->handle('assets'); @@ -386,8 +388,8 @@ public function it_commits_when_asset_container_is_saved_and_deleted() /** @test */ public function it_commits_when_asset_is_saved_and_deleted() { - Git::shouldReceive('dispatchCommit')->with('Asset saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Asset deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Asset saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Asset deleted', $this->user)->once(); $asset = $this->makeAsset()->data(['bar' => 'baz']); @@ -398,7 +400,7 @@ public function it_commits_when_asset_is_saved_and_deleted() /** @test */ public function it_commits_when_asset_is_uploaded() { - Git::shouldReceive('dispatchCommit')->with('Asset saved')->once(); + Git::shouldReceive('dispatchCommit')->with('Asset saved', $this->user)->once(); $this->makeAsset()->upload( UploadedFile::fake()->create('asset.txt') @@ -408,7 +410,7 @@ public function it_commits_when_asset_is_uploaded() /** @test */ public function it_commits_when_asset_is_reuploaded() { - Git::shouldReceive('dispatchCommit')->with('Asset reuploaded')->once(); + Git::shouldReceive('dispatchCommit')->with('Asset reuploaded', $this->user)->once(); $file = Mockery::mock(ReplacementFile::class); $file->shouldReceive('extension')->andReturn('txt'); @@ -420,7 +422,7 @@ public function it_commits_when_asset_is_reuploaded() /** @test */ public function it_commits_when_asset_is_moved() { - Git::shouldReceive('dispatchCommit')->with('Asset saved')->once(); + Git::shouldReceive('dispatchCommit')->with('Asset saved', $this->user)->once(); $asset = tap($this->makeAsset()->data(['bar' => 'baz']))->saveQuietly(); @@ -430,7 +432,7 @@ public function it_commits_when_asset_is_moved() /** @test */ public function it_commits_when_asset_is_renamed() { - Git::shouldReceive('dispatchCommit')->with('Asset saved')->once(); + Git::shouldReceive('dispatchCommit')->with('Asset saved', $this->user)->once(); $asset = tap($this->makeAsset()->data(['bar' => 'baz']))->saveQuietly(); @@ -442,7 +444,7 @@ public function it_commits_only_once_when_asset_is_replaced() { $originalAsset = tap($this->makeAsset())->saveQuietly(); - Git::shouldReceive('dispatchCommit')->with('Asset saved')->once(); + Git::shouldReceive('dispatchCommit')->with('Asset saved', $this->user)->once(); $newAsset = $this->makeAsset()->upload( UploadedFile::fake()->create('asset.txt') @@ -456,13 +458,13 @@ public function it_commits_when_replaced_asset_is_deleted() { $originalAsset = tap($this->makeAsset())->saveQuietly(); - Git::shouldReceive('dispatchCommit')->with('Asset saved')->once(); + Git::shouldReceive('dispatchCommit')->with('Asset saved', $this->user)->once(); $newAsset = $this->makeAsset()->upload( UploadedFile::fake()->create('asset.txt') ); - Git::shouldReceive('dispatchCommit')->with('Asset deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Asset deleted', $this->user)->once(); // Replace with `$deleteOriginal = true` $newAsset->replace($originalAsset, true); @@ -471,8 +473,8 @@ public function it_commits_when_replaced_asset_is_deleted() /** @test */ public function it_commits_when_asset_folder_is_saved_and_deleted() { - Git::shouldReceive('dispatchCommit')->with('Asset folder saved')->once(); - Git::shouldReceive('dispatchCommit')->with('Asset folder deleted')->once(); + Git::shouldReceive('dispatchCommit')->with('Asset folder saved', $this->user)->once(); + Git::shouldReceive('dispatchCommit')->with('Asset folder deleted', $this->user)->once(); $folder = $this ->makeAsset() @@ -531,8 +533,8 @@ public function it_batches_term_references_changes_into_one_commit() ->saveQuietly(); } - Git::shouldReceive('dispatchCommit')->with('Term references updated')->once(); // Ensure references updated event gets fired - Git::shouldReceive('dispatchCommit')->with('Entry saved')->never(); // Ensure individual entry saved events do not get fired + Git::shouldReceive('dispatchCommit')->with('Term references updated', $this->user)->once(); // Ensure references updated event gets fired + Git::shouldReceive('dispatchCommit')->with('Entry saved', $this->user)->never(); // Ensure individual entry saved events do not get fired $term->slug('leia-updated')->save(); } @@ -579,15 +581,15 @@ public function it_batches_asset_references_changes_into_one_commit() ->saveQuietly(); } - Git::shouldReceive('dispatchCommit')->with('Asset references updated')->once(); // Ensure references updated event gets fired - Git::shouldReceive('dispatchCommit')->with('Entry saved')->never(); // Ensure individual entry saved events do not get fired + Git::shouldReceive('dispatchCommit')->with('Asset references updated', $this->user)->once(); // Ensure references updated event gets fired + Git::shouldReceive('dispatchCommit')->with('Entry saved', $this->user)->never(); // Ensure individual entry saved events do not get fired $newAsset->replace($originalAsset); } private function makeAsset($path = 'asset.txt') { - Git::shouldReceive('dispatchCommit')->with('Asset container saved')->once(); + Git::shouldReceive('dispatchCommit')->with('Asset container saved', $this->user)->once(); $container = Facades\AssetContainer::make()->handle('assets')->disk('test'); $container->save();