Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions src/Http/Controllers/CP/Taxonomies/TermsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,21 @@ public function update(Request $request, $taxonomy, $term, $site)

$fields = $term->blueprint()->fields()->addValues($request->except('id'));

$fields->validate([
'title' => 'required',
'slug' => [
'required',
new Slug,
new UniqueTermValue(taxonomy: $taxonomy->handle(), except: $term->id(), site: $site->handle()),
],
]);
$fields
->validator()
->withRules([
'title' => 'required',
'slug' => [
'required',
new Slug,
new UniqueTermValue(taxonomy: $taxonomy->handle(), except: $term->id(), site: $site->handle()),
],
])
->withReplacements([
'id' => $term->id(),
'taxonomy' => $taxonomy->handle(),
'site' => $site->handle(),
])->validate();

$values = $fields->process()->values();

Expand Down Expand Up @@ -274,10 +281,16 @@ public function store(Request $request, $taxonomy, $site)

$fields = $blueprint->fields()->addValues($request->all());

$fields->validate([
'title' => 'required',
'slug' => ['required', new UniqueTermValue(taxonomy: $taxonomy->handle(), site: $site->handle())],
]);
$fields
->validator()
->withRules([
'title' => 'required',
'slug' => ['required', new UniqueTermValue(taxonomy: $taxonomy->handle(), site: $site->handle())],
])
->withReplacements([
'taxonomy' => $taxonomy->handle(),
'site' => $site->handle(),
])->validate();

$values = $fields->process()->values()->except(['slug', 'blueprint']);

Expand Down
69 changes: 69 additions & 0 deletions tests/Feature/Taxonomies/StoreTermTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Tests\Feature\Taxonomies;

use Facades\Statamic\Fields\BlueprintRepository;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Blueprint;
use Statamic\Facades\Taxonomy;
use Statamic\Facades\Term;
use Statamic\Facades\User;
use Tests\FakesRoles;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;

class StoreTermTest extends TestCase
{
use FakesRoles, PreventSavingStacheItemsToDisk;

#[Test]
public function term_gets_created()
{
$this->setTestRoles(['test' => ['access cp', 'create tags terms']]);
$user = tap(User::make()->assignRole('test'))->save();

Taxonomy::make('tags')->save();

$this
->actingAs($user)
->store('tags', ['title' => 'Alfa', 'slug' => 'alfa'])
->assertOk();

$this->assertEquals('Alfa', Term::find('tags::alfa')->title);
}

#[Test]
public function it_replaces_placeholders_in_blueprint_validation_rules()
{
$this->setTestRoles(['test' => ['access cp', 'create tags terms']]);
$user = tap(User::make()->assignRole('test'))->save();

Taxonomy::make('tags')->save();
Taxonomy::make('categories')->save();
Term::make()->taxonomy('categories')->inDefaultLocale()->slug('alfa')->data(['title' => 'alfa'])->save();

$blueprint = Blueprint::makeFromFields([
'slug' => ['type' => 'slug', 'validate' => 'new \\Statamic\\Rules\\UniqueTermValue({taxonomy}, {id}, {site})'],
]);

BlueprintRepository::partialMock();
BlueprintRepository::shouldReceive('in')->with('taxonomies/tags')->andReturn(collect([$blueprint]));

$this
->actingAs($user)
->store('tags', ['title' => 'Alfa', 'slug' => 'alfa'])
->assertOk();

$this->assertEquals('Alfa', Term::find('tags::alfa')->title);
}

private function store($taxonomy, $attrs = [])
{
$payload = array_merge([
'title' => 'New term',
'slug' => 'new-term',
], $attrs);

return $this->postJson(cp_route('taxonomies.terms.store', [$taxonomy, 'en']), $payload);
}
}
27 changes: 27 additions & 0 deletions tests/Feature/Taxonomies/UpdateTermTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Tests\Feature\Taxonomies;

use Facades\Statamic\Fields\BlueprintRepository;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Blueprint;
use Statamic\Facades\Taxonomy;
use Statamic\Facades\Term;
use Statamic\Facades\User;
Expand Down Expand Up @@ -77,6 +79,31 @@ public function term_gets_updated()
$this->assertEquals('Updated alfa', $term->title);
}

#[Test]
public function it_replaces_placeholders_in_blueprint_validation_rules()
{
$this->setTestRoles(['test' => ['access cp', 'edit tags terms']]);
$user = tap(User::make()->assignRole('test'))->save();

Taxonomy::make('tags')->save();
$term = tap(Term::make()->taxonomy('tags')->inDefaultLocale()->slug('alfa')->data(['title' => 'alfa']))->save();

$blueprint = Blueprint::makeFromFields([
'slug' => ['type' => 'slug', 'validate' => 'new \\Statamic\\Rules\\UniqueTermValue({taxonomy}, {id}, {site})'],
]);

BlueprintRepository::partialMock();
BlueprintRepository::shouldReceive('in')->with('taxonomies/tags')->andReturn(collect([$blueprint]));

$this
->actingAs($user)
->update($term, ['title' => 'Updated alfa', 'slug' => 'alfa'])
->assertOk();

$term = $term->fresh();
$this->assertEquals('Updated alfa', $term->title);
}

private function update($term, $attrs = [])
{
$payload = array_merge([
Expand Down
Loading