View source
<?php
namespace Drupal\Tests\taxonomy_unique\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class TaxonomyUniqueTest extends KernelTestBase {
use TaxonomyTestTrait;
public static $modules = [
'taxonomy',
'taxonomy_unique',
'text',
'filter',
'user',
];
public function setUp() {
parent::setUp();
$this
->installConfig([
'filter',
]);
$this
->installEntitySchema('taxonomy_term');
}
public function testDuplicateTermWithTuEnabled() {
$vocabulary = $this
->createVocabulary();
$GLOBALS['config']['taxonomy_unique.settings'] = [
$vocabulary
->id() => TRUE,
];
$t1 = $this
->createTerm($vocabulary, [
'name' => 'Term 1',
]);
$t1_violations = $t1
->validate();
$this
->assertEquals(0, $t1_violations
->count());
$t2 = $this
->createTerm($vocabulary, [
'name' => 'Term 1',
]);
$t2_violations = $t2
->validate();
$this
->assertEquals(1, $t2_violations
->count());
$t3 = $this
->createTerm($vocabulary, [
'name' => 'Term 2',
]);
$t3_violations = $t3
->validate();
$this
->assertEquals(0, $t3_violations
->count());
}
public function testDuplicateTermWithTuDisabled() {
$vocabulary = $this
->createVocabulary();
$t1 = $this
->createTerm($vocabulary, [
'name' => 'Term 1',
]);
$t1_violations = $t1
->validate();
$this
->assertEquals(0, $t1_violations
->count());
$t2 = $this
->createTerm($vocabulary, [
'name' => 'Term 1',
]);
$t2_violations = $t2
->validate();
$this
->assertEquals(0, $t2_violations
->count());
}
public function testCrossVocabulary() {
$vocabulary1 = $this
->createVocabulary();
$vocabulary2 = $this
->createVocabulary();
$GLOBALS['config']['taxonomy_unique.settings'] = [
$vocabulary1
->id() => TRUE,
$vocabulary2
->id() => TRUE,
];
$t1 = $this
->createTerm($vocabulary1, [
'name' => 'Term 1',
]);
$t1_violations = $t1
->validate();
$this
->assertEquals(0, $t1_violations
->count());
$t2 = $this
->createTerm($vocabulary2, [
'name' => 'Term 1',
]);
$t2_violations = $t2
->validate();
$this
->assertEquals(0, $t2_violations
->count());
}
public function testEditTerm() {
$vocabulary = $this
->createVocabulary();
$GLOBALS['config']['taxonomy_unique.settings'] = [
$vocabulary
->id() => TRUE,
];
$t1 = $this
->createTerm($vocabulary, [
'name' => 'Term 1',
]);
$t1_violations = $t1
->validate();
$this
->assertEquals(0, $t1_violations
->count());
$t2 = $this
->createTerm($vocabulary, [
'name' => 'Term 2',
]);
$t2_violations = $t2
->validate();
$this
->assertEquals(0, $t2_violations
->count());
$t2
->setName('Term 1');
$t2
->save();
$violations = $t2
->validate();
$this
->assertEquals(1, $violations
->count());
}
}