From 93ed3927daa650ae0eb6e034c8a10ba7f01a8e96 Mon Sep 17 00:00:00 2001 From: mrey Date: Wed, 1 Feb 2017 17:39:23 +0100 Subject: [PATCH] TTK-13154 #293 SchemaBundle: add Special Translation Service --- .../Controller/MultimediaObjectController.php | 3 +- .../Resources/config/services.xml | 5 +- .../Services/EmbeddedBroadcastService.php | 32 +----- .../Services/SpecialTranslationService.php | 46 +++++++++ .../Services/EmbeddedBroadcastServiceTest.php | 71 ++------------ .../SpecialTranslationServiceTest.php | 98 +++++++++++++++++++ 6 files changed, 157 insertions(+), 98 deletions(-) create mode 100644 src/Pumukit/SchemaBundle/Services/SpecialTranslationService.php create mode 100644 src/Pumukit/SchemaBundle/Tests/Services/SpecialTranslationServiceTest.php diff --git a/src/Pumukit/NewAdminBundle/Controller/MultimediaObjectController.php b/src/Pumukit/NewAdminBundle/Controller/MultimediaObjectController.php index 220b891e0..408684817 100644 --- a/src/Pumukit/NewAdminBundle/Controller/MultimediaObjectController.php +++ b/src/Pumukit/NewAdminBundle/Controller/MultimediaObjectController.php @@ -1049,6 +1049,7 @@ public function updateBroadcastAction(MultimediaObject $multimediaObject, Reques $locale = $request->getLocale(); $dm = $this->get('doctrine_mongodb.odm.document_manager'); $embeddedBroadcastService = $this->get('pumukitschema.embeddedbroadcast'); + $specialTranslationService = $this->get('pumukitschema.special_translation'); $broadcasts = $embeddedBroadcastService->getAllTypes(); $allGroups = $this->getAllGroups(); $template = $multimediaObject->isPrototype() ? '_template' : ''; @@ -1071,7 +1072,7 @@ public function updateBroadcastAction(MultimediaObject $multimediaObject, Reques $embeddedBroadcast = $multimediaObject->getEmbeddedBroadcast(); $jsonResponse = array( 'description' => (string) $embeddedBroadcast, - 'descriptioni18n' => $embeddedBroadcastService->getI18nDescription($embeddedBroadcast), + 'descriptioni18n' => $specialTranslationService->getI18nEmbeddedBroadcast($embeddedBroadcast), 'template' => $template, ); diff --git a/src/Pumukit/SchemaBundle/Resources/config/services.xml b/src/Pumukit/SchemaBundle/Resources/config/services.xml index 217f69ea2..377faa5a5 100644 --- a/src/Pumukit/SchemaBundle/Resources/config/services.xml +++ b/src/Pumukit/SchemaBundle/Resources/config/services.xml @@ -222,9 +222,12 @@ - %pumukit_new_admin.disable_broadcast_creation% + + + + diff --git a/src/Pumukit/SchemaBundle/Services/EmbeddedBroadcastService.php b/src/Pumukit/SchemaBundle/Services/EmbeddedBroadcastService.php index 34ba83219..810a395dd 100644 --- a/src/Pumukit/SchemaBundle/Services/EmbeddedBroadcastService.php +++ b/src/Pumukit/SchemaBundle/Services/EmbeddedBroadcastService.php @@ -11,7 +11,6 @@ use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; use Symfony\Bundle\FrameworkBundle\Routing\Router; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Translation\TranslatorInterface; class EmbeddedBroadcastService { @@ -23,12 +22,11 @@ class EmbeddedBroadcastService private $authorizationChecker; private $router; private $templating; - private $translator; /** * Constructor. */ - public function __construct(DocumentManager $documentManager, MultimediaObjectService $mmsService, MultimediaObjectEventDispatcherService $dispatcher, AuthorizationCheckerInterface $authorizationChecker, EngineInterface $templating, Router $router, TranslatorInterface $translator, $disabledBroadcast) + public function __construct(DocumentManager $documentManager, MultimediaObjectService $mmsService, MultimediaObjectEventDispatcherService $dispatcher, AuthorizationCheckerInterface $authorizationChecker, EngineInterface $templating, Router $router, $disabledBroadcast) { $this->dm = $documentManager; $this->repo = $this->dm->getRepository('PumukitSchemaBundle:MultimediaObject'); @@ -38,7 +36,6 @@ public function __construct(DocumentManager $documentManager, MultimediaObjectSe $this->templating = $templating; $this->router = $router; $this->disabledBroadcast = $disabledBroadcast; - $this->translator = $translator; } /** @@ -375,31 +372,4 @@ public function deleteAllFromGroup(Group $group) } $this->dm->flush(); } - - /** - * Get i18n description. - * - * @param EmbeddedBroadcast $embeddedBroadcast - * @param string $locale - */ - public function getI18nDescription(EmbeddedBroadcast $embeddedBroadcast, $locale = null) - { - $groups = $embeddedBroadcast->getGroups(); - $groupsDescription = ''; - if (($embeddedBroadcast->getType() === EmbeddedBroadcast::TYPE_GROUPS) && ($groups)) { - $groupsDescription = ': '; - foreach ($groups as $group) { - $groupsDescription .= $group->getName(); - if ($group != $groups->last()) { - $groupsDescription .= ', '; - } - } - } - - if ($locale) { - return $this->translator->trans($embeddedBroadcast->getName(), array(), null, $locale).$groupsDescription; - } - - return $this->translator->trans($embeddedBroadcast->getName()).$groupsDescription; - } } diff --git a/src/Pumukit/SchemaBundle/Services/SpecialTranslationService.php b/src/Pumukit/SchemaBundle/Services/SpecialTranslationService.php new file mode 100644 index 000000000..e7d3df89e --- /dev/null +++ b/src/Pumukit/SchemaBundle/Services/SpecialTranslationService.php @@ -0,0 +1,46 @@ +translator = $translator; + } + + /** + * Get I18n EmbeddedBroadcast. + * + * @param EmbeddedBroadcast $embeddedBroadcast + * @param string $locale + */ + public function getI18nEmbeddedBroadcast(EmbeddedBroadcast $embeddedBroadcast, $locale = null) + { + $groups = $embeddedBroadcast->getGroups(); + $groupsDescription = ''; + if (($embeddedBroadcast->getType() === EmbeddedBroadcast::TYPE_GROUPS) && ($groups)) { + $groupsDescription = ': '; + foreach ($groups as $group) { + $groupsDescription .= $group->getName(); + if ($group != $groups->last()) { + $groupsDescription .= ', '; + } + } + } + + if ($locale) { + return $this->translator->trans($embeddedBroadcast->getName(), array(), null, $locale).$groupsDescription; + } + + return $this->translator->trans($embeddedBroadcast->getName()).$groupsDescription; + } +} diff --git a/src/Pumukit/SchemaBundle/Tests/Services/EmbeddedBroadcastServiceTest.php b/src/Pumukit/SchemaBundle/Tests/Services/EmbeddedBroadcastServiceTest.php index 2f5ce0a33..9c2fe8a56 100644 --- a/src/Pumukit/SchemaBundle/Tests/Services/EmbeddedBroadcastServiceTest.php +++ b/src/Pumukit/SchemaBundle/Tests/Services/EmbeddedBroadcastServiceTest.php @@ -22,7 +22,6 @@ class EmbeddedBroadcastServiceTest extends WebTestCase private $authorizationChecker; private $templating; private $router; - private $translator; public function setUp() { @@ -45,8 +44,6 @@ public function setUp() ->get('templating'); $this->router = static::$kernel->getContainer() ->get('router'); - $this->translator = static::$kernel->getContainer() - ->get('translator'); $this->dm->getDocumentCollection('PumukitSchemaBundle:MultimediaObject')->remove(array()); $this->dm->getDocumentCollection('PumukitSchemaBundle:Group')->remove(array()); @@ -65,14 +62,13 @@ public function tearDown() $this->authorizationChecker = null; $this->templating = null; $this->router = null; - $this->translator = null; gc_collect_cycles(); parent::tearDown(); } public function testCreateEmbeddedBroadcastByType() { - $embeddedBroadcastService = new EmbeddedBroadcastService($this->dm, $this->mmsService, $this->dispatcher, $this->authorizationChecker, $this->templating, $this->router, $this->translator, false); + $embeddedBroadcastService = new EmbeddedBroadcastService($this->dm, $this->mmsService, $this->dispatcher, $this->authorizationChecker, $this->templating, $this->router, false); $passwordBroadcast = $embeddedBroadcastService->createEmbeddedBroadcastByType(EmbeddedBroadcast::TYPE_PASSWORD); $ldapBroadcast = $embeddedBroadcastService->createEmbeddedBroadcastByType(EmbeddedBroadcast::TYPE_LOGIN); $groupsBroadcast = $embeddedBroadcastService->createEmbeddedBroadcastByType(EmbeddedBroadcast::TYPE_GROUPS); @@ -158,7 +154,7 @@ public function testCloneResource() public function testGetAllBroadcastTypes() { - $embeddedBroadcastService = new EmbeddedBroadcastService($this->dm, $this->mmsService, $this->dispatcher, $this->authorizationChecker, $this->templating, $this->router, $this->translator, false); + $embeddedBroadcastService = new EmbeddedBroadcastService($this->dm, $this->mmsService, $this->dispatcher, $this->authorizationChecker, $this->templating, $this->router, false); $broadcasts = array( EmbeddedBroadcast::TYPE_PUBLIC => EmbeddedBroadcast::NAME_PUBLIC, EmbeddedBroadcast::TYPE_PASSWORD => EmbeddedBroadcast::NAME_PASSWORD, @@ -167,7 +163,7 @@ public function testGetAllBroadcastTypes() ); $this->assertEquals($broadcasts, $embeddedBroadcastService->getAllTypes()); - $embeddedBroadcastService = new EmbeddedBroadcastService($this->dm, $this->mmsService, $this->dispatcher, $this->authorizationChecker, $this->templating, $this->router, $this->translator, true); + $embeddedBroadcastService = new EmbeddedBroadcastService($this->dm, $this->mmsService, $this->dispatcher, $this->authorizationChecker, $this->templating, $this->router, true); $broadcasts = array( EmbeddedBroadcast::TYPE_PUBLIC => EmbeddedBroadcast::NAME_PUBLIC, EmbeddedBroadcast::TYPE_LOGIN => EmbeddedBroadcast::NAME_LOGIN, @@ -178,7 +174,7 @@ public function testGetAllBroadcastTypes() public function testCreatePublicEmbeddedBroadcast() { - $embeddedBroadcastService = new EmbeddedBroadcastService($this->dm, $this->mmsService, $this->dispatcher, $this->authorizationChecker, $this->templating, $this->router, $this->translator, false); + $embeddedBroadcastService = new EmbeddedBroadcastService($this->dm, $this->mmsService, $this->dispatcher, $this->authorizationChecker, $this->templating, $this->router, false); $publicBroadcast = $embeddedBroadcastService->createPublicEmbeddedBroadcast(); $this->assertEquals(EmbeddedBroadcast::TYPE_PUBLIC, $publicBroadcast->getType()); $this->assertEquals(EmbeddedBroadcast::NAME_PUBLIC, $publicBroadcast->getName()); @@ -519,7 +515,7 @@ public function testCanUserPlayMultimediaObject() ->method('render') ->will($this->returnValue($content)); - $embeddedBroadcastService = new EmbeddedBroadcastService($this->dm, $this->mmsService, $this->dispatcher, $authorizationChecker, $templating, $this->router, $this->translator, false); + $embeddedBroadcastService = new EmbeddedBroadcastService($this->dm, $this->mmsService, $this->dispatcher, $authorizationChecker, $templating, $this->router, false); $response = $embeddedBroadcastService->canUserPlayMultimediaObject($mm, null, ''); $this->assertTrue($response instanceof Response); @@ -538,7 +534,7 @@ public function testCanUserPlayMultimediaObject() ->method('isGranted') ->will($this->returnValue(true)); - $embeddedBroadcastService = new EmbeddedBroadcastService($this->dm, $this->mmsService, $this->dispatcher, $authorizationChecker, $templating, $this->router, $this->translator, false); + $embeddedBroadcastService = new EmbeddedBroadcastService($this->dm, $this->mmsService, $this->dispatcher, $authorizationChecker, $templating, $this->router, false); $this->assertTrue($embeddedBroadcastService->canUserPlayMultimediaObject($mm, $user, '')); @@ -760,59 +756,4 @@ public function testDeleteAllFromGroup() $this->embeddedBroadcastService->deleteAllFromGroup($group); $this->assertEquals(0, count($this->mmRepo->findWithGroupInEmbeddedBroadcast($group)->toArray())); } - - public function testGetI18nDescription() - { - $group = new Group(); - $group->setKey('key'); - $group->setName('group'); - $this->dm->persist($group); - $this->dm->flush(); - - $this->assertEquals(0, count($this->mmRepo->findWithGroupInEmbeddedBroadcast($group)->toArray())); - - $mm1 = new MultimediaObject(); - $mm1->setTitle('mm1'); - $emb1 = new EmbeddedBroadcast(); - $emb1->addGroup($group); - $emb1->setType(EmbeddedBroadcast::TYPE_GROUPS); - $mm1->setEmbeddedBroadcast($emb1); - - $mm2 = new MultimediaObject(); - $mm2->setTitle('mm2'); - $emb2 = new EmbeddedBroadcast(); - $emb2->setType(EmbeddedBroadcast::TYPE_PUBLIC); - $mm2->setEmbeddedBroadcast($emb2); - - $mm3 = new MultimediaObject(); - $mm3->setTitle('mm3'); - $emb3 = new EmbeddedBroadcast(); - $emb3->setType(EmbeddedBroadcast::TYPE_PASSWORD); - $emb3->setPassword('test'); - $mm3->setEmbeddedBroadcast($emb3); - - $mm4 = new MultimediaObject(); - $mm4->setTitle('mm2'); - $emb4 = new EmbeddedBroadcast(); - $emb4->setType(EmbeddedBroadcast::TYPE_LOGIN); - $mm4->setEmbeddedBroadcast($emb4); - - $this->dm->persist($mm1); - $this->dm->persist($mm2); - $this->dm->persist($mm3); - $this->dm->persist($mm4); - $this->dm->flush(); - - $locale = 'en'; - $this->assertEquals((string) $emb1, $this->embeddedBroadcastService->getI18nDescription($emb1, $locale)); - $this->assertEquals((string) $emb2, $this->embeddedBroadcastService->getI18nDescription($emb2, $locale)); - $this->assertEquals((string) $emb3, $this->embeddedBroadcastService->getI18nDescription($emb3, $locale)); - $this->assertEquals((string) $emb4, $this->embeddedBroadcastService->getI18nDescription($emb4, $locale)); - - $locale = 'es'; - $this->assertNotEquals((string) $emb1, $this->embeddedBroadcastService->getI18nDescription($emb1, $locale)); - $this->assertNotEquals((string) $emb2, $this->embeddedBroadcastService->getI18nDescription($emb2, $locale)); - $this->assertNotEquals((string) $emb3, $this->embeddedBroadcastService->getI18nDescription($emb3, $locale)); - $this->assertNotEquals((string) $emb4, $this->embeddedBroadcastService->getI18nDescription($emb4, $locale)); - } } diff --git a/src/Pumukit/SchemaBundle/Tests/Services/SpecialTranslationServiceTest.php b/src/Pumukit/SchemaBundle/Tests/Services/SpecialTranslationServiceTest.php new file mode 100644 index 000000000..77d80cbec --- /dev/null +++ b/src/Pumukit/SchemaBundle/Tests/Services/SpecialTranslationServiceTest.php @@ -0,0 +1,98 @@ + 'test'); + static::bootKernel($options); + + $this->dm = static::$kernel->getContainer() + ->get('doctrine_mongodb')->getManager(); + $this->mmRepo = $this->dm + ->getRepository('PumukitSchemaBundle:MultimediaObject'); + $this->specialTranslationService = static::$kernel->getContainer() + ->get('pumukitschema.special_translation'); + + $this->dm->getDocumentCollection('PumukitSchemaBundle:MultimediaObject')->remove(array()); + $this->dm->getDocumentCollection('PumukitSchemaBundle:Group')->remove(array()); + $this->dm->flush(); + } + + public function tearDown() + { + $this->dm->close(); + $this->dm = null; + $this->mmRepo = null; + $this->specialTranslationService = null; + gc_collect_cycles(); + parent::tearDown(); + } + + public function testGetI18nEmbeddedBroadcast() + { + $group = new Group(); + $group->setKey('key'); + $group->setName('group'); + $this->dm->persist($group); + $this->dm->flush(); + + $this->assertEquals(0, count($this->mmRepo->findWithGroupInEmbeddedBroadcast($group)->toArray())); + + $mm1 = new MultimediaObject(); + $mm1->setTitle('mm1'); + $emb1 = new EmbeddedBroadcast(); + $emb1->addGroup($group); + $emb1->setType(EmbeddedBroadcast::TYPE_GROUPS); + $mm1->setEmbeddedBroadcast($emb1); + + $mm2 = new MultimediaObject(); + $mm2->setTitle('mm2'); + $emb2 = new EmbeddedBroadcast(); + $emb2->setType(EmbeddedBroadcast::TYPE_PUBLIC); + $mm2->setEmbeddedBroadcast($emb2); + + $mm3 = new MultimediaObject(); + $mm3->setTitle('mm3'); + $emb3 = new EmbeddedBroadcast(); + $emb3->setType(EmbeddedBroadcast::TYPE_PASSWORD); + $emb3->setPassword('test'); + $mm3->setEmbeddedBroadcast($emb3); + + $mm4 = new MultimediaObject(); + $mm4->setTitle('mm2'); + $emb4 = new EmbeddedBroadcast(); + $emb4->setType(EmbeddedBroadcast::TYPE_LOGIN); + $mm4->setEmbeddedBroadcast($emb4); + + $this->dm->persist($mm1); + $this->dm->persist($mm2); + $this->dm->persist($mm3); + $this->dm->persist($mm4); + $this->dm->flush(); + + $locale = 'en'; + $this->assertEquals((string) $emb1, $this->specialTranslationService->getI18nEmbeddedBroadcast($emb1, $locale)); + $this->assertEquals((string) $emb2, $this->specialTranslationService->getI18nEmbeddedBroadcast($emb2, $locale)); + $this->assertEquals((string) $emb3, $this->specialTranslationService->getI18nEmbeddedBroadcast($emb3, $locale)); + $this->assertEquals((string) $emb4, $this->specialTranslationService->getI18nEmbeddedBroadcast($emb4, $locale)); + + $locale = 'es'; + $this->assertNotEquals((string) $emb1, $this->specialTranslationService->getI18nEmbeddedBroadcast($emb1, $locale)); + $this->assertNotEquals((string) $emb2, $this->specialTranslationService->getI18nEmbeddedBroadcast($emb2, $locale)); + $this->assertNotEquals((string) $emb3, $this->specialTranslationService->getI18nEmbeddedBroadcast($emb3, $locale)); + $this->assertNotEquals((string) $emb4, $this->specialTranslationService->getI18nEmbeddedBroadcast($emb4, $locale)); + } +}