diff --git a/lib/Textmaster/CallbackHandler.php b/lib/Textmaster/CallbackHandler.php index 2b8b759..3dd4d0c 100644 --- a/lib/Textmaster/CallbackHandler.php +++ b/lib/Textmaster/CallbackHandler.php @@ -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'; } diff --git a/lib/Textmaster/Model/Document.php b/lib/Textmaster/Model/Document.php index eb79792..2e5783c 100644 --- a/lib/Textmaster/Model/Document.php +++ b/lib/Textmaster/Model/Document.php @@ -196,6 +196,10 @@ public function getTranslatedContent() return $authorWork['free_text']; } + if (ProjectInterface::ACTIVITY_COPYWRITING === $this->getProject()->getActivity()) { + return $authorWork; + } + return $this->formatTranslatedContent(); } diff --git a/lib/Textmaster/Translator/Adapter/AbstractAdapter.php b/lib/Textmaster/Translator/Adapter/AbstractAdapter.php index 038222d..fca61fd 100644 --- a/lib/Textmaster/Translator/Adapter/AbstractAdapter.php +++ b/lib/Textmaster/Translator/Adapter/AbstractAdapter.php @@ -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 { @@ -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); @@ -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 ); @@ -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); @@ -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. * @@ -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(); @@ -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) { @@ -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); } diff --git a/lib/Textmaster/Translator/Adapter/GedmoTranslatableAdapter.php b/lib/Textmaster/Translator/Adapter/GedmoTranslatableAdapter.php index 94fa3a4..aa35965 100644 --- a/lib/Textmaster/Translator/Adapter/GedmoTranslatableAdapter.php +++ b/lib/Textmaster/Translator/Adapter/GedmoTranslatableAdapter.php @@ -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(); diff --git a/lib/Textmaster/Translator/Adapter/SyliusTranslatableAdapter.php b/lib/Textmaster/Translator/Adapter/SyliusTranslatableAdapter.php index c34111c..1bb6704 100644 --- a/lib/Textmaster/Translator/Adapter/SyliusTranslatableAdapter.php +++ b/lib/Textmaster/Translator/Adapter/SyliusTranslatableAdapter.php @@ -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); } diff --git a/test/Textmaster/Functional/Api/ProjectTest.php b/test/Textmaster/Functional/Api/ProjectTest.php index ac14d75..e3eaadc 100644 --- a/test/Textmaster/Functional/Api/ProjectTest.php +++ b/test/Textmaster/Functional/Api/ProjectTest.php @@ -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; @@ -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(); @@ -134,7 +133,7 @@ public function shouldCreateProject() return $result['id']; } - + /** * @test */ @@ -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\)"\]/'); @@ -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) @@ -423,7 +422,7 @@ private static function spinCall(Project $api, $method, $projectId) } } sleep(self::WAIT_TIME); - $retry++; + ++$retry; } } @@ -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)); diff --git a/test/Textmaster/Unit/Model/DocumentTest.php b/test/Textmaster/Unit/Model/DocumentTest.php index d154e9c..b9bf7ae 100644 --- a/test/Textmaster/Unit/Model/DocumentTest.php +++ b/test/Textmaster/Unit/Model/DocumentTest.php @@ -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 */ diff --git a/test/Textmaster/Unit/Translator/Adapter/GedmoTranslatableAdapterTest.php b/test/Textmaster/Unit/Translator/Adapter/GedmoTranslatableAdapterTest.php index 81453d5..5b4cece 100644 --- a/test/Textmaster/Unit/Translator/Adapter/GedmoTranslatableAdapterTest.php +++ b/test/Textmaster/Unit/Translator/Adapter/GedmoTranslatableAdapterTest.php @@ -11,6 +11,7 @@ namespace Textmaster\Unit\Translator\Adapter; +use Textmaster\Model\ProjectInterface; use Textmaster\Translator\Adapter\GedmoTranslatableAdapter; class GedmoTranslatableAdapterTest extends \PHPUnit_Framework_TestCase @@ -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') @@ -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'); diff --git a/test/Textmaster/Unit/Translator/Adapter/SyliusTranslatableAdapterTest.php b/test/Textmaster/Unit/Translator/Adapter/SyliusTranslatableAdapterTest.php index 3762259..90e2687 100644 --- a/test/Textmaster/Unit/Translator/Adapter/SyliusTranslatableAdapterTest.php +++ b/test/Textmaster/Unit/Translator/Adapter/SyliusTranslatableAdapterTest.php @@ -11,6 +11,7 @@ namespace Textmaster\Unit\Translator\Adapter; +use Textmaster\Model\ProjectInterface; use Textmaster\Translator\Adapter\SyliusTranslatableAdapter; use Textmaster\Unit\Mock\MockTranslation; @@ -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); @@ -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'); @@ -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); @@ -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')