Skip to content

Commit

Permalink
TTK-13154 #293 SchemaBundle: add Special Translation Service
Browse files Browse the repository at this point in the history
  • Loading branch information
mreyk committed Feb 1, 2017
1 parent 661e2af commit 93ed392
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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' : '';
Expand All @@ -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,
);

Expand Down
5 changes: 4 additions & 1 deletion src/Pumukit/SchemaBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,12 @@
<argument type="service" id="security.authorization_checker" />
<argument type="service" id="templating" />
<argument type="service" id="router" />
<argument type="service" id="translator" />
<argument>%pumukit_new_admin.disable_broadcast_creation%</argument>
</service>

<service id="pumukitschema.special_translation" class="Pumukit\SchemaBundle\Services\SpecialTranslationService">
<argument type="service" id="translator" />
</service>

</services>
</container>
32 changes: 1 addition & 31 deletions src/Pumukit/SchemaBundle/Services/EmbeddedBroadcastService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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');
Expand All @@ -38,7 +36,6 @@ public function __construct(DocumentManager $documentManager, MultimediaObjectSe
$this->templating = $templating;
$this->router = $router;
$this->disabledBroadcast = $disabledBroadcast;
$this->translator = $translator;
}

/**
Expand Down Expand Up @@ -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;
}
}
46 changes: 46 additions & 0 deletions src/Pumukit/SchemaBundle/Services/SpecialTranslationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Pumukit\SchemaBundle\Services;

use Pumukit\SchemaBundle\Document\EmbeddedBroadcast;
use Symfony\Component\Translation\TranslatorInterface;

class SpecialTranslationService
{
private $translator;

/**
* Constructor.
*/
public function __construct(TranslatorInterface $translator)
{
$this->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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class EmbeddedBroadcastServiceTest extends WebTestCase
private $authorizationChecker;
private $templating;
private $router;
private $translator;

public function setUp()
{
Expand All @@ -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());
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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());
Expand Down Expand Up @@ -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);
Expand All @@ -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, ''));

Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Pumukit\SchemaBundle\Tests\Services;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Pumukit\SchemaBundle\Services\SpecialTranslationService;
use Pumukit\SchemaBundle\Document\EmbeddedBroadcast;
use Pumukit\SchemaBundle\Document\MultimediaObject;
use Pumukit\SchemaBundle\Document\Group;

class SpecialTranslationServiceTest extends WebTestCase
{
private $dm;
private $mmRepo;
private $specialTranslationService;

public function setUp()
{
$options = array('environment' => '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));
}
}

0 comments on commit 93ed392

Please sign in to comment.