diff --git a/src/Jobs/ImportItemJob.php b/src/Jobs/ImportItemJob.php index 01e8e30..4811e91 100644 --- a/src/Jobs/ImportItemJob.php +++ b/src/Jobs/ImportItemJob.php @@ -76,9 +76,17 @@ protected function findOrCreateEntry(array $data): void ->first(); if (! $entry) { + if (! $this->import->get('strategy.create', true)) { + return; + } + $entry = Entry::make()->collection($this->import->get('destination.collection')); } + if ($entry->id() && ! $this->import->get('strategy.update', true)) { + return; + } + if (isset($data['slug'])) { $entry->slug(Arr::pull($data, 'slug')); } @@ -103,9 +111,17 @@ protected function findOrCreateTerm(array $data): void ->first(); if (! $term) { + if (! $this->import->get('strategy.create', true)) { + return; + } + $term = Term::make()->taxonomy($this->import->get('destination.taxonomy')); } + if (Term::find($term->id()) && ! $this->import->get('strategy.update', true)) { + return; + } + if (isset($data['slug'])) { $term->slug(Arr::pull($data, 'slug')); } @@ -126,9 +142,17 @@ protected function findOrCreateUser(array $data): void ->first(); if (! $user) { + if (! $this->import->get('strategy.create', true)) { + return; + } + $user = User::make(); } + if ($user->id() && ! $this->import->get('strategy.update', true)) { + return; + } + if (isset($data['email'])) { $user->email(Arr::pull($data, 'email')); } diff --git a/tests/Jobs/ImportItemJobTest.php b/tests/Jobs/ImportItemJobTest.php index 3597213..1e6c4d5 100644 --- a/tests/Jobs/ImportItemJobTest.php +++ b/tests/Jobs/ImportItemJobTest.php @@ -43,6 +43,7 @@ public function setUp(): void 'main' => [ 'fields' => [ ['handle' => 'title', 'field' => ['type' => 'text']], + ['handle' => 'foo', 'field' => ['type' => 'text']], ], ], ], @@ -75,6 +76,10 @@ public function it_imports_a_new_entry() 'email' => ['key' => 'Email'], 'role' => ['key' => 'Role'], ], + 'strategy' => [ + 'create' => true, + 'update' => false, + ], ]); ImportItemJob::dispatch($import, [ @@ -93,6 +98,38 @@ public function it_imports_a_new_entry() $this->assertEquals('CEO', $entry->get('role')); } + #[Test] + public function it_doesnt_import_a_new_entry_when_creation_is_disabled() + { + $this->assertNull(Entry::query()->where('email', 'john.doe@example.com')->first()); + + $import = Import::make()->config([ + 'destination' => ['type' => 'entries', 'collection' => 'team'], + 'unique_field' => 'email', + 'mappings' => [ + 'first_name' => ['key' => 'First Name'], + 'last_name' => ['key' => 'Last Name'], + 'email' => ['key' => 'Email'], + 'role' => ['key' => 'Role'], + ], + 'strategy' => [ + 'create' => false, + 'update' => false, + ], + ]); + + ImportItemJob::dispatch($import, [ + 'First Name' => 'John', + 'Last Name' => 'Doe', + 'Email' => 'john.doe@example.com', + 'Role' => 'CEO', + ]); + + $entry = Entry::query()->where('email', 'john.doe@example.com')->first(); + + $this->assertNull($entry); + } + #[Test] public function it_updates_an_existing_entry() { @@ -108,6 +145,10 @@ public function it_updates_an_existing_entry() 'email' => ['key' => 'Email'], 'role' => ['key' => 'Role'], ], + 'strategy' => [ + 'create' => false, + 'update' => true, + ], ]); ImportItemJob::dispatch($import, [ @@ -126,6 +167,43 @@ public function it_updates_an_existing_entry() $this->assertEquals('CEO', $entry->get('role')); } + #[Test] + public function it_doesnt_update_an_existing_entry_when_updating_is_disabled() + { + $entry = Entry::make()->collection('team')->data(['email' => 'john.doe@example.com', 'role' => 'CTO']); + $entry->save(); + + $import = Import::make()->config([ + 'destination' => ['type' => 'entries', 'collection' => 'team'], + 'unique_field' => 'email', + 'mappings' => [ + 'first_name' => ['key' => 'First Name'], + 'last_name' => ['key' => 'Last Name'], + 'email' => ['key' => 'Email'], + 'role' => ['key' => 'Role'], + ], + 'strategy' => [ + 'create' => false, + 'update' => false, + ], + ]); + + ImportItemJob::dispatch($import, [ + 'First Name' => 'John', + 'Last Name' => 'Doe', + 'Email' => 'john.doe@example.com', + 'Role' => 'CEO', + ]); + + $entry->fresh(); + + $this->assertNotNull($entry); + $this->assertNull($entry->get('first_name')); + $this->assertNull($entry->get('last_name')); + $this->assertEquals('john.doe@example.com', $entry->get('email')); + $this->assertEquals('CTO', $entry->get('role')); + } + #[Test] public function it_imports_a_new_term() { @@ -137,6 +215,10 @@ public function it_imports_a_new_term() 'mappings' => [ 'title' => ['key' => 'Title'], ], + 'strategy' => [ + 'create' => true, + 'update' => false, + ], ]); ImportItemJob::dispatch($import, [ @@ -150,10 +232,34 @@ public function it_imports_a_new_term() $this->assertEquals('Statamic', $term->get('title')); } + #[Test] + public function it_doesnt_import_a_new_term_when_creation_is_disabled() + { + $this->assertNull(Term::query()->where('title', 'Statamic')->first()); + + $import = Import::make()->config([ + 'destination' => ['type' => 'terms', 'taxonomy' => 'tags'], + 'unique_field' => 'title', + 'mappings' => [ + 'title' => ['key' => 'Title'], + ], + 'strategy' => [ + 'create' => false, + 'update' => false, + ], + ]); + + ImportItemJob::dispatch($import, [ + 'Title' => 'Statamic', + ]); + + $this->assertNull(Term::query()->where('title', 'Statamic')->first()); + } + #[Test] public function it_updates_an_existing_term() { - $term = Term::make()->taxonomy('tags')->slug('statamic')->set('title', 'Statamic'); + $term = Term::make()->taxonomy('tags')->slug('statamic')->set('title', 'Statamic')->set('foo', 'bar'); $term->save(); $import = Import::make()->config([ @@ -161,11 +267,49 @@ public function it_updates_an_existing_term() 'unique_field' => 'title', 'mappings' => [ 'title' => ['key' => 'Title'], + 'foo' => ['key' => 'Foo'], + ], + 'strategy' => [ + 'create' => false, + 'update' => true, + ], + ]); + + ImportItemJob::dispatch($import, [ + 'Title' => 'Statamic', + 'Foo' => 'Baz', + ]); + + $term->fresh(); + + $this->assertNotNull($term); + $this->assertEquals('statamic', $term->slug()); + $this->assertEquals('Statamic', $term->get('title')); + $this->assertEquals('Baz', $term->get('foo')); + } + + #[Test] + public function it_doesnt_update_an_existing_term_when_updating_is_disabled() + { + $term = Term::make()->taxonomy('tags')->slug('statamic')->set('title', 'Statamic')->set('foo', 'bar'); + $term->save(); + + $import = Import::make()->config([ + 'destination' => ['type' => 'terms', 'taxonomy' => 'tags'], + 'unique_field' => 'title', + 'mappings' => [ + 'title' => ['key' => 'Title'], + 'foo' => ['key' => 'Foo'], + ], + 'strategy' => [ + 'create' => false, + 'update' => false, ], ]); ImportItemJob::dispatch($import, [ 'Title' => 'Statamic', + 'Foo' => 'Baz', ]); $term->fresh(); @@ -173,6 +317,7 @@ public function it_updates_an_existing_term() $this->assertNotNull($term); $this->assertEquals('statamic', $term->slug()); $this->assertEquals('Statamic', $term->get('title')); + $this->assertEquals('bar', $term->get('foo')); } #[Test] @@ -188,6 +333,10 @@ public function it_imports_a_new_user() 'last_name' => ['key' => 'Last Name'], 'email' => ['key' => 'Email'], ], + 'strategy' => [ + 'create' => true, + 'update' => false, + ], ]); ImportItemJob::dispatch($import, [ @@ -204,6 +353,34 @@ public function it_imports_a_new_user() $this->assertEquals('john.doe@example.com', $user->email()); } + #[Test] + public function it_doesnt_import_a_new_user_when_creation_is_disabled() + { + $this->assertNull(User::findByEmail('john.doe@example.com')); + + $import = Import::make()->config([ + 'destination' => ['type' => 'users'], + 'unique_field' => 'email', + 'mappings' => [ + 'first_name' => ['key' => 'First Name'], + 'last_name' => ['key' => 'Last Name'], + 'email' => ['key' => 'Email'], + ], + 'strategy' => [ + 'create' => false, + 'update' => false, + ], + ]); + + ImportItemJob::dispatch($import, [ + 'First Name' => 'John', + 'Last Name' => 'Doe', + 'Email' => 'john.doe@example.com', + ]); + + $this->assertNull(User::findByEmail('john.doe@example.com')); + } + #[Test] public function it_updates_an_existing_user() { @@ -218,6 +395,10 @@ public function it_updates_an_existing_user() 'last_name' => ['key' => 'Last Name'], 'email' => ['key' => 'Email'], ], + 'strategy' => [ + 'create' => false, + 'update' => true, + ], ]); ImportItemJob::dispatch($import, [ @@ -233,4 +414,38 @@ public function it_updates_an_existing_user() $this->assertEquals('Doe', $user->get('last_name')); $this->assertEquals('john.doe@example.com', $user->email()); } + + #[Test] + public function it_doesnt_update_an_existing_user_when_updating_is_disabled() + { + $user = User::make()->email('john.doe@example.com'); + $user->save(); + + $import = Import::make()->config([ + 'destination' => ['type' => 'users'], + 'unique_field' => 'email', + 'mappings' => [ + 'first_name' => ['key' => 'First Name'], + 'last_name' => ['key' => 'Last Name'], + 'email' => ['key' => 'Email'], + ], + 'strategy' => [ + 'create' => false, + 'update' => false, + ], + ]); + + ImportItemJob::dispatch($import, [ + 'First Name' => 'John', + 'Last Name' => 'Doe', + 'Email' => 'john.doe@example.com', + ]); + + $user->fresh(); + + $this->assertNotNull($user); + $this->assertNull($user->get('first_name')); + $this->assertNull($user->get('last_name')); + $this->assertEquals('john.doe@example.com', $user->email()); + } }