Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Dec 20, 2024
1 parent a456e97 commit c7bb2ed
Showing 1 changed file with 177 additions and 0 deletions.
177 changes: 177 additions & 0 deletions tests/Jobs/ImportItemJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,91 @@ public function it_imports_a_new_term()
$this->assertEquals('Statamic', $term->get('title'));
}

#[Test]
public function it_imports_a_new_term_in_a_multisite_into_the_default_site()
{
$this->setSites([
'en' => ['locale' => 'en', 'url' => '/'],
'fr' => ['locale' => 'fr', 'url' => '/fr/'],
]);

Taxonomy::find('tags')->sites(['en', 'fr']);

$this->assertNull(Term::query()->where('title', 'Statamic')->first());

$import = Import::make()->config([
'destination' => ['type' => 'terms', 'taxonomy' => 'tags', 'blueprint' => 'tag', 'site' => 'en'],
'unique_field' => 'title',
'mappings' => [
'title' => ['key' => 'Title'],
'slug' => ['key' => 'Slug'],
],
'strategy' => ['create'],
]);

ImportItemJob::dispatch($import, [
'Title' => 'Statamic',
'Slug' => 'statamic',
]);

$term = Term::query()
->where('site', 'en')
->where('title', 'Statamic')
->first();

$this->assertNotNull($term);
$this->assertEquals('statamic', $term->slug());
$this->assertEquals('Statamic', $term->get('title'));
$this->assertEquals('en', $term->site());
}

#[Test]
public function it_imports_a_new_term_in_a_multisite_into_a_specific_site()
{
$this->setSites([
'en' => ['locale' => 'en', 'url' => '/'],
'fr' => ['locale' => 'fr', 'url' => '/fr/'],
]);

Taxonomy::find('tags')->sites(['en', 'fr']);

$this->assertNull(Term::query()->where('title', 'Statamic')->first());

$import = Import::make()->config([
'destination' => ['type' => 'terms', 'taxonomy' => 'tags', 'blueprint' => 'tag', 'site' => 'fr'],
'unique_field' => 'title',
'mappings' => [
'title' => ['key' => 'Title'],
'slug' => ['key' => 'Slug'],
],
'strategy' => ['create'],
]);

ImportItemJob::dispatch($import, [
'Title' => 'Statamic',
'Slug' => 'statamic',
]);

$term = Term::query()
->where('site', 'fr')
->where('title', 'Statamic')
->first();

$this->assertNotNull($term);

// Both the default site and the chosen site should have the same data
// (because of the way taxonomies work).
$en = $term->in('en');
$this->assertEquals('statamic', $en->slug());
$this->assertEquals('Statamic', $en->get('title'));
$this->assertEquals('en', $en->site());

$fr = $term->in('fr');
$this->assertEquals('statamic', $fr->slug());
$this->assertEquals('Statamic', $fr->get('title'));
$this->assertEquals('fr', $fr->site());
}

#[Test]
public function it_imports_a_new_term_with_a_specific_blueprint()
{
Expand Down Expand Up @@ -490,6 +575,98 @@ public function it_updates_an_existing_term()
$this->assertEquals('Baz', $term->get('foo'));
}

#[Test]
public function it_updates_an_existing_term_in_a_multisite_with_the_same_slug()
{
$this->setSites([
'en' => ['locale' => 'en', 'url' => '/'],
'fr' => ['locale' => 'fr', 'url' => '/fr/'],
]);

$term = Term::make()->taxonomy('tags')->slug('statamic')->set('title', 'Statamic')->set('foo', 'bar');
$term->save();

$import = Import::make()->config([
'destination' => ['type' => 'terms', 'taxonomy' => 'tags', 'blueprint' => 'tag', 'site' => 'fr'],
'unique_field' => 'title',
'mappings' => [
'title' => ['key' => 'Title'],
'slug' => ['key' => 'Slug'],
'foo' => ['key' => 'Foo'],
],
'strategy' => ['update'],
]);

ImportItemJob::dispatch($import, [
'Title' => 'Statamic',
'Slug' => 'statamic',
'Foo' => 'Baz',
]);

$term->fresh();

// The importer is updating the French localization of the term, so the English
// localization should stay the same.
$en = $term->in('en');
$this->assertEquals('statamic', $en->slug());
$this->assertEquals('Statamic', $en->get('title'));
$this->assertEquals('bar', $en->get('foo'));
$this->assertEquals('en', $en->site());

$fr = $term->in('fr');
$this->assertEquals('statamic', $fr->slug());
$this->assertEquals('Statamic', $fr->get('title'));
$this->assertEquals('Baz', $fr->get('foo'));
$this->assertEquals('fr', $fr->site());
}

#[Test]
public function it_updates_an_existing_term_in_a_multisite_with_the_default_slug_mapping()
{
$this->setSites([
'en' => ['locale' => 'en', 'url' => '/'],
'fr' => ['locale' => 'fr', 'url' => '/fr/'],
]);

$term = Term::make()->taxonomy('tags')->slug('statamic')->set('title', 'Statamic')->set('foo', 'bar');
$term->save();

$import = Import::make()->config([
'destination' => ['type' => 'terms', 'taxonomy' => 'tags', 'blueprint' => 'tag', 'site' => 'fr'],
'unique_field' => 'title',
'mappings' => [
'title' => ['key' => 'Title'],
'slug' => ['key' => 'Slug'],
'default_slug' => ['key' => 'Default Slug'],
'foo' => ['key' => 'Foo'],
],
'strategy' => ['update'],
]);

ImportItemJob::dispatch($import, [
'Title' => 'Statique Dynamique',
'Slug' => 'statique-dynamique',
'Default Slug' => 'statamic',
'Foo' => 'Baz',
]);

$term->fresh();

// The importer is updating the French localization of the term, so the English
// localization should stay the same.
$en = $term->in('en');
$this->assertEquals('statamic', $en->slug());
$this->assertEquals('Statamic', $en->get('title'));
$this->assertEquals('bar', $en->get('foo'));
$this->assertEquals('en', $en->site());

$fr = $term->in('fr');
$this->assertEquals('statique-dynamique', $fr->slug());
$this->assertEquals('Statique Dynamique', $fr->get('title'));
$this->assertEquals('Baz', $fr->get('foo'));
$this->assertEquals('fr', $fr->site());
}

#[Test]
public function it_doesnt_update_an_existing_term_when_updating_is_disabled()
{
Expand Down

0 comments on commit c7bb2ed

Please sign in to comment.