-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check config option before creating/updating items
- Loading branch information
1 parent
d392dfe
commit 25162da
Showing
2 changed files
with
240 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', '[email protected]')->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' => '[email protected]', | ||
'Role' => 'CEO', | ||
]); | ||
|
||
$entry = Entry::query()->where('email', '[email protected]')->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' => '[email protected]', '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' => '[email protected]', | ||
'Role' => 'CEO', | ||
]); | ||
|
||
$entry->fresh(); | ||
|
||
$this->assertNotNull($entry); | ||
$this->assertNull($entry->get('first_name')); | ||
$this->assertNull($entry->get('last_name')); | ||
$this->assertEquals('[email protected]', $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,29 +232,92 @@ 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([ | ||
'destination' => ['type' => 'terms', 'taxonomy' => 'tags'], | ||
'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(); | ||
|
||
$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('[email protected]', $user->email()); | ||
} | ||
|
||
#[Test] | ||
public function it_doesnt_import_a_new_user_when_creation_is_disabled() | ||
{ | ||
$this->assertNull(User::findByEmail('[email protected]')); | ||
|
||
$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' => '[email protected]', | ||
]); | ||
|
||
$this->assertNull(User::findByEmail('[email protected]')); | ||
} | ||
|
||
#[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('[email protected]', $user->email()); | ||
} | ||
|
||
#[Test] | ||
public function it_doesnt_update_an_existing_user_when_updating_is_disabled() | ||
{ | ||
$user = User::make()->email('[email protected]'); | ||
$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' => '[email protected]', | ||
]); | ||
|
||
$user->fresh(); | ||
|
||
$this->assertNotNull($user); | ||
$this->assertNull($user->get('first_name')); | ||
$this->assertNull($user->get('last_name')); | ||
$this->assertEquals('[email protected]', $user->email()); | ||
} | ||
} |