-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TTK-13154 #293 SchemaBundle: add Special Translation Service
- Loading branch information
Showing
6 changed files
with
157 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/Pumukit/SchemaBundle/Services/SpecialTranslationService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/Pumukit/SchemaBundle/Tests/Services/SpecialTranslationServiceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |