Skip to content

Commit

Permalink
Update for copywriting project (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Ducoudray authored and cdaguerre committed Sep 16, 2016
1 parent 1c20ded commit 80660c0
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/Textmaster/CallbackHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private function getEvent(array $data)
{
if (array_key_exists('name', $data)) {
$type = 'project';
} elseif (array_key_exists('original_content', $data)) {
} elseif (array_key_exists('original_content', $data) || array_key_exists('author_work', $data)) {
$type = 'document';
}

Expand Down
4 changes: 4 additions & 0 deletions lib/Textmaster/Model/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ public function getTranslatedContent()
return $authorWork['free_text'];
}

if (ProjectInterface::ACTIVITY_COPYWRITING === $this->getProject()->getActivity()) {
return $authorWork;
}

return $this->formatTranslatedContent();
}

Expand Down
56 changes: 41 additions & 15 deletions lib/Textmaster/Translator/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\PropertyAccess\PropertyAccess;
use Textmaster\Exception\UnexpectedTypeException;
use Textmaster\Model\DocumentInterface;
use Textmaster\Model\ProjectInterface;

abstract class AbstractAdapter implements AdapterInterface
{
Expand All @@ -36,9 +37,10 @@ public function supports($subject)
public function create($subject, array $properties, DocumentInterface $document)
{
$this->failIfDoesNotSupport($subject);
$project = $document->getProject();

$language = $document->getProject()->getLanguageFrom();
$content = $this->getProperties($subject, $properties, $language);
$language = $project->getLanguageFrom();
$content = $this->getProperties($subject, $properties, $language, $project->getActivity());

$this->setSubjectOnDocument($subject, $document);

Expand All @@ -54,18 +56,22 @@ public function compare(DocumentInterface $document)
{
$subject = $this->getSubjectFromDocument($document);
$this->failIfDoesNotSupport($subject);

$original = $this->compareContent(
$subject,
$document->getOriginalContent(),
$document->getProject()->getLanguageFrom(),
true
);
$project = $document->getProject();

$original = array();
if (ProjectInterface::ACTIVITY_COPYWRITING !== $project->getActivity()) {
$original = $this->compareContent(
$subject,
$document->getOriginalContent(),
$project->getLanguageFrom(),
true
);
}

$translated = $this->compareContent(
$subject,
$document->getTranslatedContent(),
$document->getProject()->getLanguageTo(),
$this->getLanguageTo($project),
false
);

Expand All @@ -85,7 +91,9 @@ public function complete(DocumentInterface $document, $satisfaction = null, $mes

/** @var array $properties */
$properties = $document->getTranslatedContent();
$language = $document->getProject()->getLanguageTo();

$project = $document->getProject();
$language = $this->getLanguageTo($project);

$this->setProperties($subject, $properties, $language);

Expand All @@ -94,6 +102,22 @@ public function complete(DocumentInterface $document, $satisfaction = null, $mes
return $subject;
}

/**
* Get project language to.
*
* @param ProjectInterface $project
*
* @return string
*/
protected function getLanguageTo(ProjectInterface $project)
{
if (ProjectInterface::ACTIVITY_TRANSLATION === $project->getActivity()) {
return $project->getLanguageTo();
}

return $project->getLanguageFrom();
}

/**
* Compare given content with the subject's one in the given language.
*
Expand All @@ -107,7 +131,7 @@ public function complete(DocumentInterface $document, $satisfaction = null, $mes
protected function compareContent($subject, array $content, $language, $original = true)
{
$properties = array_keys($content);
$values = $this->getProperties($subject, $properties, $language, false);
$values = $this->getProperties($subject, $properties, $language);

$diffs = [];
$renderer = new \Diff_Renderer_Html_SideBySide();
Expand Down Expand Up @@ -148,13 +172,14 @@ protected function setProperties($subject, array $properties, $language)
* @param object $subject
* @param array $properties Array of 'properties'
* @param string $language
* @param string $activity
*
* @return array
*/
protected function getProperties($subject, array $properties, $language)
protected function getProperties($subject, array $properties, $language, $activity = null)
{
$accessor = PropertyAccess::createPropertyAccessor();
$holder = $this->getPropertyHolder($subject, $language);
$holder = $this->getPropertyHolder($subject, $language, $activity);

$data = [];
foreach ($properties as $property) {
Expand Down Expand Up @@ -203,8 +228,9 @@ abstract protected function setSubjectOnDocument($subject, DocumentInterface $do
*
* @param object $subject
* @param string $language
* @param string $activity
*
* @return mixed
*/
abstract protected function getPropertyHolder($subject, $language);
abstract protected function getPropertyHolder($subject, $language, $activity = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(ManagerRegistry $registry, TranslatableListener $lis
/**
* {@inheritdoc}
*/
protected function getPropertyHolder($subject, $language)
protected function getPropertyHolder($subject, $language, $activity = null)
{
$listenerLocale = $this->listener->getListenerLocale();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SyliusTranslatableAdapter extends AbstractDoctrineAdapter
/**
* {@inheritdoc}
*/
protected function getPropertyHolder($subject, $language)
protected function getPropertyHolder($subject, $language, $activity = null)
{
return $subject->translate($language);
}
Expand Down
19 changes: 9 additions & 10 deletions test/Textmaster/Functional/Api/ProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ProjectTest extends \PHPUnit_Framework_TestCase
{
// wait time between calls because the sandbox environment is not as fast as prod
const WAIT_TIME = 3;

// Unique ID used for created project name
private static $testId;

Expand All @@ -32,15 +32,14 @@ class ProjectTest extends \PHPUnit_Framework_TestCase
*/
protected $api;


/**
* Generate a unique ID when tests starts
* Generate a unique ID when tests starts.
*/
public static function setUpBeforeClass()
{
self::$testId = uniqid();
}
}

public function setUp()
{
parent::setUp();
Expand Down Expand Up @@ -134,7 +133,7 @@ public function shouldCreateProject()

return $result['id'];
}

/**
* @test
*/
Expand All @@ -148,7 +147,7 @@ public function shouldNotCreateInvalidProject()
],
'language_from' => 'en',
'language_to' => 'fr',
'category' => 'C021'
'category' => 'C021',
];

$this->setExpectedExceptionRegExp(\LogicException::class, '/"level_name":\["doit être rempli\(e\)"\]/');
Expand Down Expand Up @@ -404,7 +403,7 @@ public function shouldQuoteProject()

/**
* @param Project $api
* @param string $method method to call
* @param string $method method to call
* @param string $projectId
*/
private static function spinCall(Project $api, $method, $projectId)
Expand All @@ -423,7 +422,7 @@ private static function spinCall(Project $api, $method, $projectId)
}
}
sleep(self::WAIT_TIME);
$retry++;
++$retry;
}
}

Expand All @@ -443,7 +442,7 @@ private function waitForStatus($projectId, $status)
}
printf('[Expected status %s, found %s] ', $status, $result['status']);
sleep(self::WAIT_TIME);
$retry++;
++$retry;
}

throw new \RuntimeException(sprintf('Status %s not found for project %s', $status, $projectId));
Expand Down
37 changes: 37 additions & 0 deletions test/Textmaster/Unit/Model/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,43 @@ public function shouldGetTranslatedContentForKeyValue()
$this->assertSame($values['author_work'], $document->getTranslatedContent());
}

/**
* @test
*/
public function shouldGetTranslatedContentForCopywriting()
{
$projectId = '654321';
$values = [
'id' => '123456',
'title' => 'Document 1',
'type' => DocumentInterface::TYPE_KEY_VALUE,
'status' => DocumentInterface::STATUS_IN_CREATION,
'original_content' => [
'key1' => ['original_phrase' => 'Text to translate.', 'completed_phrase' => 'Translated text.'],
'key2' => ['original_phrase' => 'Text to translate.', 'completed_phrase' => 'Translated text.'],
],
'instructions' => 'Translating instructions.',
'project_id' => $projectId,
'author_work' => [
'key1' => 'Translated text.',
'key2' => 'Translated text.',
],
];

$projectMock = $this->getMock('Textmaster\Model\Project', ['getActivity'], [$this->clientMock]);
$projectMock->method('getActivity')
->willReturn(ProjectInterface::ACTIVITY_COPYWRITING);

$document = new Document($this->clientMock, $values);

$this->assertSame('123456', $document->getId());
$this->assertSame('Document 1', $document->getTitle());
$this->assertSame(DocumentInterface::STATUS_IN_CREATION, $document->getStatus());
$this->assertSame($values['original_content'], $document->getOriginalContent());
$this->assertSame('Translating instructions.', $document->getInstructions());
$this->assertSame($values['author_work'], $document->getTranslatedContent());
}

/**
* @test
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Textmaster\Unit\Translator\Adapter;

use Textmaster\Model\ProjectInterface;
use Textmaster\Translator\Adapter\GedmoTranslatableAdapter;

class GedmoTranslatableAdapterTest extends \PHPUnit_Framework_TestCase
Expand All @@ -25,7 +26,7 @@ public function shouldCreateSameLocale()

$translatableMock = $this->getMock('Gedmo\Translatable\Translatable', ['getName', 'getId']);
$documentMock = $this->getMock('Textmaster\Model\Document', ['getProject', 'save'], [], '', false);
$projectMock = $this->getMock('Textmaster\Model\Project', ['getLanguageFrom'], [], '', false);
$projectMock = $this->getMock('Textmaster\Model\Project', ['getLanguageFrom', 'getActivity'], [], '', false);

$translatableMock->expects($this->once())
->method('getName')
Expand All @@ -42,6 +43,10 @@ public function shouldCreateSameLocale()
->method('getLanguageFrom')
->willReturn('en');

$projectMock->expects($this->once())
->method('getActivity')
->willReturn(ProjectInterface::ACTIVITY_TRANSLATION);

$listenerMock->expects($this->once())
->method('getListenerLocale')
->willReturn('en');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Textmaster\Unit\Translator\Adapter;

use Textmaster\Model\ProjectInterface;
use Textmaster\Translator\Adapter\SyliusTranslatableAdapter;
use Textmaster\Unit\Mock\MockTranslation;

Expand Down Expand Up @@ -119,6 +120,10 @@ public function shouldComplete()
->method('getLanguageTo')
->willReturn('fr');

$projectMock->expects($this->once())
->method('getActivity')
->willReturn(ProjectInterface::ACTIVITY_TRANSLATION);

$adapter = new SyliusTranslatableAdapter($managerRegistryMock);
$subject = $adapter->complete($documentMock);

Expand All @@ -137,7 +142,6 @@ public function shouldCompare()
$objectRepositoryMock = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
$documentMock = $this->getMock('Textmaster\Model\DocumentInterface');
$translatableMock = $this->getMock('Sylius\Component\Resource\Model\TranslatableInterface');
$translatableMock = $this->getMock('Sylius\Component\Resource\Model\TranslatableInterface');
$projectMock = $this->getMock('Textmaster\Model\ProjectInterface');
$enTranslationMock = new MockTranslation();
$enTranslationMock->setName('Name to translate');
Expand All @@ -152,7 +156,7 @@ public function shouldCompare()
$documentMock->expects($this->once())
->method('getTranslatedContent')
->willReturn(['name' => 'Le nom à traduire']);
$documentMock->expects($this->exactly(2))
$documentMock->expects($this->once())
->method('getProject')
->willReturn($projectMock);

Expand All @@ -162,6 +166,9 @@ public function shouldCompare()
$projectMock->expects($this->once())
->method('getLanguageTo')
->willReturn('fr');
$projectMock->expects($this->exactly(2))
->method('getActivity')
->willReturn(ProjectInterface::ACTIVITY_TRANSLATION);

$managerRegistryMock->expects($this->once())
->method('getManagerForClass')
Expand Down

0 comments on commit 80660c0

Please sign in to comment.