Add testspull/4020/head^2
parent
0083c6b58d
commit
c8b87f2afd
@ -0,0 +1,110 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
use Chamilo\CoreBundle\Entity\ExtraField; |
||||
use Chamilo\CoreBundle\Entity\Language; |
||||
use Chamilo\CoreBundle\Framework\Container; |
||||
use Gedmo\Translatable\Entity\Translation; |
||||
|
||||
$cidReset = true; |
||||
|
||||
require_once __DIR__.'/../inc/global.inc.php'; |
||||
|
||||
api_protect_admin_script(); |
||||
|
||||
$em = Database::getManager(); |
||||
|
||||
$extraFieldRepo = Container::getExtraFieldRepository(); |
||||
$extraFieldOptionsRepo = Container::getExtraFieldOptionsRepository(); |
||||
$languageRepo = Container::getLanguageRepository(); |
||||
|
||||
$fieldId = (int) ($_REQUEST['id'] ?? 0); |
||||
|
||||
/** @var \Chamilo\CoreBundle\Entity\ExtraFieldOptions|null $extraFieldOption */ |
||||
$extraFieldOption = $extraFieldOptionsRepo->find($fieldId); |
||||
|
||||
if (null === $extraFieldOption) { |
||||
api_not_allowed(true); |
||||
} |
||||
|
||||
$extraField = $extraFieldOption->getField(); |
||||
|
||||
$currentUrl = api_get_self().'?id='.$fieldId; |
||||
$qb = $languageRepo->getAllAvailable(); |
||||
$languages = $qb->getQuery()->getResult(); |
||||
|
||||
$form = new FormValidator('translate', 'POST', $currentUrl); |
||||
$form->addHidden('id', $fieldId); |
||||
$form->addHeader($extraFieldOption->getDisplayText()); |
||||
|
||||
$repository = $em->getRepository(Translation::class); |
||||
$translations = $repository->findTranslations($extraFieldOption); |
||||
|
||||
$defaults = []; |
||||
/** @var Language $language */ |
||||
foreach ($languages as $language) { |
||||
$iso = $language->getIsocode(); |
||||
$variable = 'variable['.$iso.']'; |
||||
$form->addText($variable, $language->getOriginalName().' ('.$iso.')', false); |
||||
if (isset($translations[$iso]) && $translations[$iso]['displayText']) { |
||||
$defaults['variable['.$iso.']'] = $translations[$iso]['displayText']; |
||||
} |
||||
} |
||||
|
||||
$form->setDefaults($defaults); |
||||
$form->addButtonSave(get_lang('Save')); |
||||
|
||||
$interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH).'admin', 'name' => get_lang('Administration')]; |
||||
|
||||
switch ($extraField->getExtraFieldType()) { |
||||
case ExtraField::USER_FIELD_TYPE: |
||||
$interbreadcrumb[] = [ |
||||
'url' => api_get_path(WEB_CODE_PATH).'admin/extra_fields.php?type=user', |
||||
'name' => get_lang('Profile attributes'), |
||||
]; |
||||
break; |
||||
case ExtraField::COURSE_FIELD_TYPE: |
||||
$interbreadcrumb[] = [ |
||||
'url' => api_get_path(WEB_CODE_PATH).'admin/extra_fields.php?type=course', |
||||
'name' => get_lang('Course fields'), |
||||
]; |
||||
break; |
||||
case ExtraField::SESSION_FIELD_TYPE: |
||||
$interbreadcrumb[] = [ |
||||
'url' => api_get_path(WEB_CODE_PATH).'admin/extra_fields.php?type=session', |
||||
'name' => get_lang('Session fields'), |
||||
]; |
||||
break; |
||||
} |
||||
|
||||
if ($form->validate()) { |
||||
$values = $form->getSubmitValues(); |
||||
foreach ($languages as $language) { |
||||
if (!isset($values['variable'][$language->getIsocode()])) { |
||||
continue; |
||||
} |
||||
$translation = $values['variable'][$language->getIsocode()]; |
||||
if (empty($translation)) { |
||||
continue; |
||||
} |
||||
|
||||
$extraFieldOption = $extraFieldOptionsRepo->find($fieldId); |
||||
$extraFieldOption |
||||
->setTranslatableLocale($language->getIsocode()) |
||||
->setDisplayText($translation) |
||||
; |
||||
$em->persist($extraFieldOption); |
||||
$em->flush(); |
||||
} |
||||
|
||||
Display::addFlash(Display::return_message(get_lang('Updated'))); |
||||
api_location($currentUrl); |
||||
} |
||||
|
||||
$tpl = new Template(get_lang('Translations')); |
||||
$tpl->assign('form', $form->returnForm()); |
||||
$template = $tpl->get_template('extrafield/translate.html.twig'); |
||||
$content = $tpl->fetch($template); |
||||
$tpl->assign('content', $content); |
||||
$tpl->display_one_col_template(); |
@ -0,0 +1,115 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\Tests\CoreBundle\Repository; |
||||
|
||||
use Chamilo\CoreBundle\Entity\ExtraField; |
||||
use Chamilo\CoreBundle\Entity\ExtraFieldOptions; |
||||
use Chamilo\CoreBundle\Entity\ExtraFieldSavedSearch; |
||||
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 |
||||
{ |
||||
use ChamiloTestTrait; |
||||
|
||||
public function testCreate(): void |
||||
{ |
||||
$em = $this->getEntityManager(); |
||||
$extraFieldRepo = static::getContainer()->get(ExtraFieldRepository::class); |
||||
$extraFieldOptionsRepo = static::getContainer()->get(ExtraFieldOptionsRepository::class); |
||||
|
||||
$defaultCount = $extraFieldRepo->count([]); |
||||
$defaultCountOptions = $extraFieldOptionsRepo->count([]); |
||||
|
||||
$extraField = (new ExtraField()) |
||||
->setDisplayText('test') |
||||
->setVariable('test') |
||||
->setDescription('desc') |
||||
->setHelperText('help') |
||||
->setExtraFieldType(ExtraField::USER_FIELD_TYPE) |
||||
->setFieldType(\ExtraField::FIELD_TYPE_TEXT) |
||||
; |
||||
$em->persist($extraField); |
||||
|
||||
$extraFieldOptions = (new ExtraFieldOptions()) |
||||
->setDisplayText('test in ENGLISH') |
||||
->setValue('value') |
||||
->setField($extraField) |
||||
->setOptionOrder(0) |
||||
->setPriority('urgent') |
||||
->setPriorityMessage('is urgent!') |
||||
; |
||||
$this->assertHasNoEntityViolations($extraFieldOptions); |
||||
$em->persist($extraFieldOptions); |
||||
$em->flush(); |
||||
|
||||
$this->assertSame('test in ENGLISH', $extraFieldOptions->getDisplayText()); |
||||
$this->assertSame('value', $extraFieldOptions->getValue()); |
||||
$this->assertSame(0, $extraFieldOptions->getOptionOrder()); |
||||
$this->assertSame('urgent', $extraFieldOptions->getPriority()); |
||||
$this->assertSame('is urgent!', $extraFieldOptions->getPriorityMessage()); |
||||
|
||||
$this->assertSame($defaultCount + 1, $extraFieldRepo->count([])); |
||||
$this->assertSame($defaultCountOptions + 1, $extraFieldOptionsRepo->count([])); |
||||
} |
||||
|
||||
public function testCreateWithTranslation(): void |
||||
{ |
||||
$this->testCreate(); |
||||
$em = $this->getEntityManager(); |
||||
|
||||
$extraFieldRepo = static::getContainer()->get(ExtraFieldRepository::class); |
||||
$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()); |
||||
} |
||||
} |
Loading…
Reference in new issue