From 3fd095ebc62c03256b4e895b6db6153104e1640f Mon Sep 17 00:00:00 2001 From: Pierre Ducoudray Date: Fri, 20 May 2016 10:58:48 +0200 Subject: [PATCH] Fix document translated content (#61) * Add type as a document propertie * Automatically set type depending on original content type * Get translated content from author work * Minor fix --- lib/Textmaster/Model/Document.php | 22 ++++++- lib/Textmaster/Model/DocumentInterface.php | 14 +++++ .../Translator/Adapter/AbstractAdapter.php | 6 +- test/Textmaster/Unit/Model/DocumentTest.php | 63 ++++++++++++++++++- .../Adapter/SyliusTranslatableAdapterTest.php | 2 +- 5 files changed, 99 insertions(+), 8 deletions(-) diff --git a/lib/Textmaster/Model/Document.php b/lib/Textmaster/Model/Document.php index c75992f..a23de3b 100644 --- a/lib/Textmaster/Model/Document.php +++ b/lib/Textmaster/Model/Document.php @@ -26,10 +26,11 @@ class Document extends AbstractObject implements DocumentInterface * @var array */ protected $data = array( - 'status' => DocumentInterface::STATUS_IN_CREATION, - 'word_count_rule' => DocumentInterface::WORD_COUNT_RULE_PERCENTAGE, + 'status' => self::STATUS_IN_CREATION, + 'word_count_rule' => self::WORD_COUNT_RULE_PERCENTAGE, 'word_count' => 0, 'custom_data' => array(), + 'type' => self::TYPE_STANDARD, ); /** @@ -146,6 +147,7 @@ public function setOriginalContent($content) { if (is_array($content)) { $this->checkArrayContent($content); + $this->setProperty('type', self::TYPE_KEY_VALUE); } elseif (!is_string($content)) { throw new InvalidArgumentException('Original content must be of type "string" or "array".'); } @@ -157,12 +159,26 @@ public function setOriginalContent($content) return $this; } + /** + * {@inheritdoc} + */ + public function getType() + { + return $this->getProperty('type'); + } + /** * {@inheritdoc} */ public function getTranslatedContent() { - return $this->getProperty('translated_content'); + $authorWork = $this->getProperty('author_work'); + + if (self::TYPE_STANDARD === $this->getType() && !empty($authorWork)) { + $authorWork = $authorWork['free_text']; + } + + return $authorWork; } /** diff --git a/lib/Textmaster/Model/DocumentInterface.php b/lib/Textmaster/Model/DocumentInterface.php index b2e4598..9c4a449 100644 --- a/lib/Textmaster/Model/DocumentInterface.php +++ b/lib/Textmaster/Model/DocumentInterface.php @@ -29,6 +29,9 @@ interface DocumentInterface const SATISFACTION_POSITIVE = 'positive'; const SATISFACTION_NEGATIVE = 'negative'; + const TYPE_STANDARD = 'standard'; + const TYPE_KEY_VALUE = 'key_value'; + const WORD_COUNT_RULE_PERCENTAGE = 0; const WORD_COUNT_RULE_MIN = 1; const WORD_COUNT_RULE_MAX = 2; @@ -112,11 +115,22 @@ public function getOriginalContent(); public function setOriginalContent($content); /** + * Get type. + * + * @return string + */ + public function getType(); + + /** + * Get translated content. + * * @return string|array */ public function getTranslatedContent(); /** + * Get word count. + * * @return int */ public function getWordCount(); diff --git a/lib/Textmaster/Translator/Adapter/AbstractAdapter.php b/lib/Textmaster/Translator/Adapter/AbstractAdapter.php index 6d5239f..27a2b58 100644 --- a/lib/Textmaster/Translator/Adapter/AbstractAdapter.php +++ b/lib/Textmaster/Translator/Adapter/AbstractAdapter.php @@ -68,7 +68,7 @@ public function complete(DocumentInterface $document, $satisfaction = null, $mes * Set properties on given subject. * * @param object $subject - * @param array $properties Array of 'property' => array('translated_phrase' => value') pairs. + * @param array $properties Array of 'property' => 'value' pairs. * @param string $language */ protected function setProperties($subject, array $properties, $language) @@ -76,8 +76,8 @@ protected function setProperties($subject, array $properties, $language) $accessor = PropertyAccess::createPropertyAccessor(); $holder = $this->getPropertyHolder($subject, $language); - foreach ($properties as $property => $content) { - $accessor->setValue($holder, $property, $content['translated_phrase']); + foreach ($properties as $property => $value) { + $accessor->setValue($holder, $property, $value); } } diff --git a/test/Textmaster/Unit/Model/DocumentTest.php b/test/Textmaster/Unit/Model/DocumentTest.php index 5f6c01c..57c20fd 100644 --- a/test/Textmaster/Unit/Model/DocumentTest.php +++ b/test/Textmaster/Unit/Model/DocumentTest.php @@ -91,7 +91,7 @@ public function setUp() public function shouldCreateEmpty() { $title = 'Document 1'; - $originalContent = 'Text to translate.'; + $originalContent = array('key' => array('original_phrase' => 'Text to translate.')); $instructions = 'Translating instructions.'; $customData = array('Custom data can be any type'); @@ -109,6 +109,7 @@ public function shouldCreateEmpty() $this->assertSame($originalContent, $document->getOriginalContent()); $this->assertSame($instructions, $document->getInstructions()); $this->assertSame($customData, $document->getCustomData()); + $this->assertSame(DocumentInterface::TYPE_KEY_VALUE, $document->getType()); } /** @@ -135,6 +136,66 @@ public function shouldCreateFromValues() $this->assertSame('Translating instructions.', $document->getInstructions()); } + /** + * @test + */ + public function shouldGetTranslatedContentForStandard() + { + $projectId = '654321'; + $values = array( + 'id' => '123456', + 'title' => 'Document 1', + 'type' => DocumentInterface::TYPE_STANDARD, + 'status' => DocumentInterface::STATUS_IN_CREATION, + 'original_content' => 'Text to translate.', + 'instructions' => 'Translating instructions.', + 'project_id' => $projectId, + 'author_work' => array('free_text' => 'Translated text.'), + ); + + $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('Text to translate.', $document->getOriginalContent()); + $this->assertSame('Translating instructions.', $document->getInstructions()); + $this->assertSame('Translated text.', $document->getTranslatedContent()); + } + + /** + * @test + */ + public function shouldGetTranslatedContentForKeyValue() + { + $projectId = '654321'; + $values = array( + 'id' => '123456', + 'title' => 'Document 1', + 'type' => DocumentInterface::TYPE_KEY_VALUE, + 'status' => DocumentInterface::STATUS_IN_CREATION, + 'original_content' => array( + 'key1' => array('original_phrase' => 'Text to translate.'), + 'key2' => array('original_phrase' => 'Text to translate.'), + ), + 'instructions' => 'Translating instructions.', + 'project_id' => $projectId, + 'author_work' => array( + 'key1' => 'Translated text.', + 'key2' => 'Translated text.', + ), + ); + + $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/SyliusTranslatableAdapterTest.php b/test/Textmaster/Unit/Translator/Adapter/SyliusTranslatableAdapterTest.php index b84dad1..8a82e95 100644 --- a/test/Textmaster/Unit/Translator/Adapter/SyliusTranslatableAdapterTest.php +++ b/test/Textmaster/Unit/Translator/Adapter/SyliusTranslatableAdapterTest.php @@ -87,7 +87,7 @@ public function shouldComplete() ->willReturn(array('class' => 'My\Class', 'id' => 1)); $documentMock->expects($this->once()) ->method('getTranslatedContent') - ->willReturn(array('name' => array('translated_phrase' => 'my translation'))); + ->willReturn(array('name' => 'my translation')); $documentMock->expects($this->once()) ->method('getProject') ->willReturn($projectMock);