Merge pull request #5381 from christianbeeznest/fixes-updates22

Internal: Fix extra fields translation and update tests
pull/5388/head
christianbeeznest 7 months ago committed by GitHub
commit eb159f798e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 20
      public/main/inc/lib/extra_field.lib.php
  2. 12
      src/CoreBundle/Entity/ExtraField.php
  3. 15
      src/CoreBundle/Entity/ExtraFieldOptions.php
  4. 53
      tests/CoreBundle/Repository/ExtraFieldOptionsRepositoryTest.php
  5. 70
      tests/CoreBundle/Repository/ExtraFieldRepositoryTest.php

@ -794,7 +794,7 @@ class ExtraField extends Model
return $extra;
}
/**
* Translate the display text for a extra field.
*
@ -2361,23 +2361,7 @@ class ExtraField extends Model
}
$form->addElement('header', $header);
if ('edit' === $action) {
$translateUrl = api_get_path(WEB_CODE_PATH).'extrafield/translate.php?'.http_build_query(['id' => $id]);
$translateButton = Display::toolbarButton(
get_lang('Translate this term'),
$translateUrl,
'language',
'link'
);
$form->addText(
'display_text',
[get_lang('Name'), $translateButton]
);
} else {
$form->addElement('text', 'display_text', get_lang('Name'));
}
$form->addElement('text', 'display_text', get_lang('Name'));
$form->addHtmlEditor('description', get_lang('Description'), false);

@ -97,8 +97,6 @@ class ExtraField
#[Gedmo\Translatable]
#[ORM\Column(name: 'display_text', type: 'string', length: 255, nullable: true, unique: false)]
protected ?string $displayText = null;
#[Gedmo\Locale]
protected ?string $locale = null;
#[ORM\Column(name: 'helper_text', type: 'text', nullable: true, unique: false)]
protected ?string $helperText = null;
#[ORM\Column(name: 'default_value', type: 'text', nullable: true, unique: false)]
@ -318,16 +316,6 @@ class ExtraField
return $this;
}
public function setTranslatableLocale($locale): static
{
$this->locale = $locale;
return $this;
}
public function getTranslatableLocale(): ?string
{
return $this->locale;
}
public function getAutoRemove(): bool
{

@ -33,9 +33,6 @@ class ExtraFieldOptions
#[ORM\Column(name: 'display_text', type: 'string', length: 255, nullable: true)]
protected ?string $displayText = null;
#[Gedmo\Locale]
protected ?string $locale = null;
#[ORM\Column(name: 'priority', type: 'string', length: 255, nullable: true)]
protected ?string $priority = null;
@ -133,16 +130,4 @@ class ExtraFieldOptions
return $this;
}
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
return $this;
}
public function getTranslatableLocale()
{
return $this->locale;
}
}

@ -12,7 +12,6 @@ use Chamilo\CoreBundle\Repository\ExtraFieldOptionsRepository;
use Chamilo\CoreBundle\Repository\ExtraFieldRepository;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
use Gedmo\Translatable\Entity\Translation;
class ExtraFieldOptionsRepositoryTest extends AbstractApiTest
{
@ -58,56 +57,4 @@ class ExtraFieldOptionsRepositoryTest extends AbstractApiTest
$this->assertSame($defaultCount + 1, $extraFieldRepo->count([]));
$this->assertSame($defaultCountOptions + 1, $extraFieldOptionsRepo->count([]));
}
public function testCreateWithTranslation(): void
{
$this->testCreate();
$em = $this->getEntityManager();
$extraFieldOptionsRepo = static::getContainer()->get(ExtraFieldOptionsRepository::class);
/** @var ExtraFieldOptions $extraFieldOption */
$extraFieldOption = $extraFieldOptionsRepo->findOneBy(['value' => 'value']);
$this->assertNotNull($extraFieldOption);
$this->assertInstanceOf(ExtraFieldOptions::class, $extraFieldOption);
$extraFieldOption
->setTranslatableLocale('fr_FR')
->setDisplayText('test in FRENCH')
;
$em->persist($extraFieldOption);
$em->flush();
/** @var ExtraFieldOptions $extraFieldOption */
$extraFieldOption = $extraFieldOptionsRepo->find($extraFieldOption->getId());
$extraFieldOption
->setTranslatableLocale('pl')
->setDisplayText('test in POLISH')
;
$em->persist($extraFieldOption);
$em->flush();
$em->clear();
/** @var ExtraFieldOptions $extraFieldOption */
$extraFieldOption = $extraFieldOptionsRepo->find($extraFieldOption->getId());
$repository = $em->getRepository(Translation::class);
$translations = $repository->findTranslations($extraFieldOption);
$this->assertCount(2, $translations);
$expected = [
'fr_FR' => [
'displayText' => 'test in FRENCH',
],
'pl' => [
'displayText' => 'test in POLISH',
],
];
$this->assertSame($expected, $translations);
/** @var ExtraFieldOptions $extraFieldOption */
$extraFieldOption = $extraFieldOptionsRepo->find($extraFieldOption->getId());
$this->assertSame('test in ENGLISH', $extraFieldOption->getDisplayText());
}
}

@ -11,7 +11,6 @@ use Chamilo\CoreBundle\Entity\ExtraFieldSavedSearch;
use Chamilo\CoreBundle\Repository\ExtraFieldRepository;
use Chamilo\Tests\AbstractApiTest;
use Chamilo\Tests\ChamiloTestTrait;
use Gedmo\Translatable\Entity\Translation;
class ExtraFieldRepositoryTest extends AbstractApiTest
{
@ -57,75 +56,6 @@ class ExtraFieldRepositoryTest extends AbstractApiTest
$this->assertSame($defaultCount + 1, $repo->count([]));
}
public function testCreateWithTranslation(): void
{
$em = $this->getEntityManager();
// $extraFieldRepo = static::getContainer()->get(ExtraFieldRepository::class);
$extraFieldRepo = $em->getRepository(ExtraField::class);
$translator = static::getContainer()->get('translator');
$defaultLocale = $translator->getLocale();
$this->assertSame('en_US', $defaultLocale);
$defaultCount = $extraFieldRepo->count([]);
$extraField = (new ExtraField())
->setDisplayText('test in ENGLISH')
->setVariable('test in en')
->setItemType(ExtraField::USER_FIELD_TYPE)
->setValueType(\ExtraField::FIELD_TYPE_TEXT)
;
$this->assertHasNoEntityViolations($extraField);
$em->persist($extraField);
$em->flush();
$this->assertSame($defaultCount + 1, $extraFieldRepo->count([]));
/** @var ExtraField $extraField */
$extraField = $extraFieldRepo->find($extraField->getId());
$extraField
->setTranslatableLocale('fr_FR')
->setDisplayText('test in FRENCH')
;
$em->persist($extraField);
$em->flush();
/** @var ExtraField $extraField */
$extraField = $extraFieldRepo->find($extraField->getId());
$extraField
->setTranslatableLocale('it')
->setDisplayText('test in ITALIAN')
;
$em->persist($extraField);
$em->flush();
$em->clear();
$this->assertSame($defaultCount + 1, $extraFieldRepo->count([]));
/** @var ExtraField $extraField */
$extraField = $extraFieldRepo->find($extraField->getId());
$repository = $em->getRepository(Translation::class);
$translations = $repository->findTranslations($extraField);
$this->assertCount(2, $translations);
$expected = [
'fr_FR' => [
'displayText' => 'test in FRENCH',
],
'it' => [
'displayText' => 'test in ITALIAN',
],
];
$this->assertSame($expected, $translations);
/** @var ExtraField $extraField */
$extraField = $extraFieldRepo->find($extraField->getId());
$this->assertSame('test in ENGLISH', $extraField->getDisplayText());
}
public function testGetExtraFields(): void
{
$repo = static::getContainer()->get(ExtraFieldRepository::class);

Loading…
Cancel
Save