Commit f513a5b2 authored by Eduardo Morales Alberti's avatar Eduardo Morales Alberti Committed by Rafael Schally
Browse files

Issue #3265277: Taxonomy unique validator should be take into account the translations

parent 093e1316
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -36,7 +36,11 @@ class TaxonomyUniqueValidator extends ConstraintValidator {
   *   Whether the term is unique or not
   */
  private function isUnique(TermInterface $term) {
    $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $term->getName(), 'vid' => $term->bundle()]);
    $query = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->getQuery();
    $query->condition('name', $term->getName());
    $query->condition('vid', $term->bundle());
    $query->condition('langcode', $term->language()->getId());
    $terms = $query->execute();

    if (empty($terms)) {
      return TRUE;
@@ -44,7 +48,7 @@ class TaxonomyUniqueValidator extends ConstraintValidator {

    if (count($terms) == 1) {
      $found_term = current($terms);
      if ($found_term->id() == $term->id()) {
      if ($found_term == $term->id()) {
        return TRUE;
      }
    }
+35 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\Tests\taxonomy_unique\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;

/**
@@ -19,6 +20,8 @@ class TaxonomyUniqueTest extends KernelTestBase {
    'text',
    'filter',
    'user',
    'language',
    'locale',
  ];

  /**
@@ -28,6 +31,12 @@ class TaxonomyUniqueTest extends KernelTestBase {
    parent::setUp();
    $this->installConfig(['filter']);
    $this->installEntitySchema('taxonomy_term');

    // Create two languages: Spanish and German.
    foreach (['es', 'de'] as $langcode) {
      ConfigurableLanguage::createFromLangcode($langcode)->save();
    }

  }

  /**
@@ -50,6 +59,32 @@ class TaxonomyUniqueTest extends KernelTestBase {
    $this->assertEquals(0, $t3_violations->count());
  }

  /**
   * Tests taxonomy_unique turned on in a vocabulary with translations.
   *
   * The validation should be done between terms with the same language.
   */
  public function testDuplicateTermWithTuEnabledLanguages() {
    $vocabulary = $this->createVocabulary();
    $GLOBALS['config']['taxonomy_unique.settings'] = [$vocabulary->id() => TRUE];

    $t1 = $this->createTerm($vocabulary, ['name' => 'Term 1', 'langcode' => 'en']);
    $t1_violations = $t1->validate();
    $this->assertEquals(0, $t1_violations->count());

    $t2 = $this->createTerm($vocabulary, ['name' => 'Term 1', 'langcode' => 'es']);
    $t2_violations = $t2->validate();
    $this->assertEquals(0, $t2_violations->count());

    $t2 = $this->createTerm($vocabulary, ['name' => 'Term 1', 'langcode' => 'de']);
    $t2_violations = $t2->validate();
    $this->assertEquals(0, $t2_violations->count());

    $t1 = $this->createTerm($vocabulary, ['name' => 'Term 1', 'langcode' => 'en']);
    $t1_violations = $t1->validate();
    $this->assertEquals(1, $t1_violations->count());
  }

  /**
   * Tests the basic functionality with taxonomy_unique turned off in the vocabulary settings.
   */