Skip to content

Commit

Permalink
Check config option before creating/updating items
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Oct 29, 2024
1 parent d392dfe commit 25162da
Show file tree
Hide file tree
Showing 2 changed files with 240 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/Jobs/ImportItemJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand All @@ -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'));
}
Expand All @@ -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'));
}
Expand Down
217 changes: 216 additions & 1 deletion tests/Jobs/ImportItemJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function setUp(): void
'main' => [
'fields' => [
['handle' => 'title', 'field' => ['type' => 'text']],
['handle' => 'foo', 'field' => ['type' => 'text']],
],
],
],
Expand Down Expand Up @@ -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, [
Expand All @@ -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()
{
Expand All @@ -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, [
Expand All @@ -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()
{
Expand All @@ -137,6 +215,10 @@ public function it_imports_a_new_term()
'mappings' => [
'title' => ['key' => 'Title'],
],
'strategy' => [
'create' => true,
'update' => false,
],
]);

ImportItemJob::dispatch($import, [
Expand All @@ -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]
Expand All @@ -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, [
Expand All @@ -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()
{
Expand All @@ -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, [
Expand All @@ -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());
}
}

0 comments on commit 25162da

Please sign in to comment.