From cf5cad1b316ece693587af1f7ab3bcdf419a2930 Mon Sep 17 00:00:00 2001 From: LaetitiaRiffaud Date: Tue, 23 May 2017 15:00:58 +0200 Subject: [PATCH] Add option to use my favorite authors to do a project (#35) --- Features/Context/TranslationContextTrait.php | 3 +- Features/copywriting.feature | 12 ++--- Features/translation.feature | 12 ++--- .../Project/app/config/config_test.yml | 4 ++ .../Service/Project/Author.php | 44 +++++++++++++++++++ .../ProductTestBundle/Service/ProjectApi.php | 6 +++ .../Translation/TranslationManagerTest.php | 11 +++++ Translation/TranslationGenerator.php | 4 +- Translation/TranslationGeneratorInterface.php | 24 +++++----- Translation/TranslationManager.php | 15 ++++++- Translation/TranslationManagerInterface.php | 3 +- composer.json | 2 +- 12 files changed, 108 insertions(+), 32 deletions(-) create mode 100644 Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/Project/Author.php diff --git a/Features/Context/TranslationContextTrait.php b/Features/Context/TranslationContextTrait.php index 4483bd9..86c1100 100644 --- a/Features/Context/TranslationContextTrait.php +++ b/Features/Context/TranslationContextTrait.php @@ -100,7 +100,7 @@ public function createTranslationProject(TableNode $table) json_decode($data['options'], true), isset($data['activity']) ? $data['activity'] : null, isset($data['workTemplate']) ? $data['workTemplate'] : null, - json_decode($data['textmasters'], true) + json_decode($data['useMyTextmasters']) ); if (null !== $project) { @@ -111,7 +111,6 @@ public function createTranslationProject(TableNode $table) PHPUnit_Framework_Assert::assertSame($data['category'], $project->getCategory()); PHPUnit_Framework_Assert::assertSame($data['briefing'], $project->getBriefing()); PHPUnit_Framework_Assert::assertSame(json_decode($data['options'], true), $project->getOptions()); - PHPUnit_Framework_Assert::assertSame(json_decode($data['textmasters'], true), $project->getTextmasters()); } } } diff --git a/Features/copywriting.feature b/Features/copywriting.feature index ac28af8..9b24147 100644 --- a/Features/copywriting.feature +++ b/Features/copywriting.feature @@ -12,8 +12,8 @@ Feature: Copywriting management | Hello NYC | NYC is the big apple | en | Then I should have "0" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale en When I generate a translation batch with the following parameters: - | finder | filter | name | languageFrom | category | briefing | options | activity | textmasters | - | product | {} | PROJECT-1 | en | C054 | Nothing | {"language_level": "premium"} | copywriting | [] | + | finder | filter | name | languageFrom | category | briefing | options | activity | useMyTextmasters | + | product | {} | PROJECT-1 | en | C054 | Nothing | {"language_level": "premium"} | copywriting | false | Then I should have "2" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale en When I receive the request '{ "id": "PROJECT-1", "name": "PROJECT-1", "status": "in_progress"}' Then I should have the following jobs: @@ -33,14 +33,14 @@ Feature: Copywriting management | title | description | locale | | Super title | Super description | en | When I generate a translation batch with the following parameters: - | finder | filter | name | languageFrom | category | briefing | options | activity | textmasters | - | product | {} | PROJECT-1 | en | C054 | Nothing | {"language_level": "premium"} | copywriting | [] | + | finder | filter | name | languageFrom | category | briefing | options | activity | useMyTextmasters | + | product | {} | PROJECT-1 | en | C054 | Nothing | {"language_level": "premium"} | copywriting | false | Then I should have "2" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale en Then I should have the following jobs: | id | translatable | project | document | status | locale | | 1 | 1 | PROJECT-1 | en-copywriting-1 | validated | en | | 2 | 2 | PROJECT-1 | en-copywriting-2 | validated | en | When I generate a translation batch with the following parameters: - | finder | filter | name | languageFrom | category | briefing | options | activity | textmasters | - | product | {} | PROJECT-1 | de | C054 | Nothing | {"language_level": "premium"} | copywriting | [] | + | finder | filter | name | languageFrom | category | briefing | options | activity | useMyTextmasters | + | product | {} | PROJECT-1 | de | C054 | Nothing | {"language_level": "premium"} | copywriting | false | Then I should have "2" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale de diff --git a/Features/translation.feature b/Features/translation.feature index 344f074..b7fbfec 100644 --- a/Features/translation.feature +++ b/Features/translation.feature @@ -12,8 +12,8 @@ Feature: Translation management | Hello NYC | NYC is the big apple | en | Then I should have "0" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale fr When I generate a translation batch with the following parameters: - | finder | filter | name | languageFrom | languageTo | category | briefing | options | activity | textmasters | - | product | {} | PROJECT-1 | en | fr | C054 | Nothing | {"language_level": "premium"} | translation | ["55c3763e656462000b000027"] | + | finder | filter | name | languageFrom | languageTo | category | briefing | options | activity | useMyTextmasters | + | product | {} | PROJECT-1 | en | fr | C054 | Nothing | {"language_level": "premium"} | translation | true | Then I should have "2" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale fr When I receive the request '{ "id": "PROJECT-1", "name": "PROJECT-1", "status": "in_progress"}' Then I should have the following jobs: @@ -35,14 +35,14 @@ Feature: Translation management | Hello NYC | NYC is the big apple | en | | Bonjour NYC | NYC est la grosse pomme | fr | When I generate a translation batch with the following parameters: - | finder | filter | name | languageFrom | languageTo | category | briefing | options | activity | textmasters | - | product | {} | PROJECT-2 | en | fr | C054 | Nothing | {"language_level": "premium"} | translation | ["55c3763e656462000b000027"] | + | finder | filter | name | languageFrom | languageTo | category | briefing | options | activity | useMyTextmasters | + | product | {} | PROJECT-2 | en | fr | C054 | Nothing | {"language_level": "premium"} | translation | true | Then I should have the following jobs: | id | translatable | project | document | status | locale | | 1 | 1 | PROJECT-1 | en-fr-1 | validated | fr | | 2 | 2 | PROJECT-1 | en-fr-2 | validated | fr | When I generate a translation batch with the following parameters: - | finder | filter | name | languageFrom | languageTo | category | briefing | options | activity | textmasters | - | product | {} | PROJECT-1 | en | de | C054 | Nothing | {"language_level": "premium"} | translation | ["55c3763e656462000b000027"] | + | finder | filter | name | languageFrom | languageTo | category | briefing | options | activity | useMyTextmasters | + | product | {} | PROJECT-1 | en | de | C054 | Nothing | {"language_level": "premium"} | translation | true | Then I should have "2" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale de Then I should have "2" translatables with job for class "Worldia\Bundle\ProductTestBundle\Entity\Product" and locale fr diff --git a/Tests/Fixtures/Project/app/config/config_test.yml b/Tests/Fixtures/Project/app/config/config_test.yml index 47daf65..0bd2a7a 100644 --- a/Tests/Fixtures/Project/app/config/config_test.yml +++ b/Tests/Fixtures/Project/app/config/config_test.yml @@ -30,6 +30,10 @@ services: class: Worldia\Bundle\ProductTestBundle\Service\ProjectApi arguments: ['@worldia.textmaster.api.client', '@worldia.textmaster.document.api'] + worldia.textmaster.project_author.api: + class: Worldia\Bundle\ProductTestBundle\Service\Project\AuthorApi + arguments: ['@worldia.textmaster.api.client', 1] + worldia.textmaster.document.api: class: Worldia\Bundle\ProductTestBundle\Service\DocumentApi arguments: ['@worldia.textmaster.api.client'] diff --git a/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/Project/Author.php b/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/Project/Author.php new file mode 100644 index 0000000..b60a418 --- /dev/null +++ b/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/Project/Author.php @@ -0,0 +1,44 @@ +client = $client; + $this->projectId = $projectId; + } + + public function all($status = null) + { + return [ + 'total_pages' => 1, + 'count' => 1, + 'page' => 1, + 'per_page' => 20, + 'previous_page' => null, + 'next_page' => null, + 'my_authors' => [ + [ + 'status' => 'my_textmaster', + 'id' => '58f9febce27d6b5dd2b0b4ec', + 'author_id' => '55c3763e656462000b000027', + ] + ] + ]; + } + + public function setPage($page) + { + $this->page = (null === $page ? $page : (int)$page); + + return $this; + } +} diff --git a/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/ProjectApi.php b/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/ProjectApi.php index 6fe1334..af7b10e 100644 --- a/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/ProjectApi.php +++ b/Tests/Fixtures/Project/src/Worldia/Bundle/ProductTestBundle/Service/ProjectApi.php @@ -4,6 +4,7 @@ use Textmaster\Client; use Textmaster\Model\Project; +use Worldia\Bundle\ProductTestBundle\Service\Project\Author as ProjectAuthors; class ProjectApi { @@ -54,4 +55,9 @@ public function updateProject(array $params) { $this->projects[$params['id']] = array_merge($this->projects[$params['id']], $params); } + + public function authors($projectId) + { + return new ProjectAuthors($this->client, $projectId); + } } diff --git a/Tests/Units/Translation/TranslationManagerTest.php b/Tests/Units/Translation/TranslationManagerTest.php index 1489714..b77e9ed 100644 --- a/Tests/Units/Translation/TranslationManagerTest.php +++ b/Tests/Units/Translation/TranslationManagerTest.php @@ -18,6 +18,7 @@ public function shouldCreateProject() $projectMock = $this->getMockBuilder('Textmaster\Model\Project')->disableOriginalConstructor()->getMock(); $translatableMock = $this->getMock('TranslatableInterface', ['getId']); $documentMock = $this->getMock('Textmaster\Model\DocumentInterface'); + $authorMock = $this->getMock('Textmaster\Model\AuthorInterface'); $textmasterManagerMock->expects($this->once()) ->method('getProject') @@ -56,6 +57,10 @@ public function shouldCreateProject() $projectMock->expects($this->once()) ->method('save') ->willReturn($projectMock); + $projectMock->expects($this->once()) + ->method('getPotentialAuthors') + ->with('my_textmaster') + ->willReturn([$authorMock]); $routerMock->expects($this->exactly(4)) ->method('generate') @@ -75,6 +80,7 @@ public function shouldCreateProject() $this->assertTrue(in_array('Textmaster\Model\ProjectInterface', class_implements($project))); $this->assertSame($projectMock, $project); } + /** * @test */ @@ -87,6 +93,7 @@ public function shouldCreateProjectWithTranslationMemory() $projectMock = $this->getMockBuilder('Textmaster\Model\Project')->disableOriginalConstructor()->getMock(); $translatableMock = $this->getMock('TranslatableInterface', ['getId']); $documentMock = $this->getMock('Textmaster\Model\DocumentInterface'); + $authorMock = $this->getMock('Textmaster\Model\AuthorInterface'); $textmasterManagerMock->expects($this->once()) ->method('getProject') @@ -122,6 +129,10 @@ public function shouldCreateProjectWithTranslationMemory() $projectMock->expects($this->exactly(2)) ->method('save') ->willReturn($projectMock); + $projectMock->expects($this->once()) + ->method('getPotentialAuthors') + ->with('my_textmaster') + ->willReturn([$authorMock]); $routerMock->expects($this->exactly(4)) ->method('generate') diff --git a/Translation/TranslationGenerator.php b/Translation/TranslationGenerator.php index af52e47..1fa6dd6 100644 --- a/Translation/TranslationGenerator.php +++ b/Translation/TranslationGenerator.php @@ -50,7 +50,7 @@ public function generate( array $options = [], $activity = ProjectInterface::ACTIVITY_TRANSLATION, $workTemplate = null, - $textmasters = [] + $useMyTextmasters = true ) { if (null === $locale = $languageTo) { $locale = $languageFrom; @@ -61,7 +61,7 @@ public function generate( return; } - $project = $this->translationManager->create($translatables, $name, $languageFrom, $category, $briefing, $languageTo, $options, $activity, $workTemplate, $textmasters); + $project = $this->translationManager->create($translatables, $name, $languageFrom, $category, $briefing, $languageTo, $options, $activity, $workTemplate, $useMyTextmasters); if (!array_key_exists('translation_memory', $project->getOptions())) { $project->launch(); diff --git a/Translation/TranslationGeneratorInterface.php b/Translation/TranslationGeneratorInterface.php index 0efb3cc..c45fc6f 100644 --- a/Translation/TranslationGeneratorInterface.php +++ b/Translation/TranslationGeneratorInterface.php @@ -9,17 +9,17 @@ interface TranslationGeneratorInterface /** * Generate a textmaster project with documents for eligible objects. * - * @param string $finderCode translatable finder code - * @param string $languageFrom source language identifier (ex: fr, en, de...) - * @param array $filter array of parameters to filter the list of objects - * @param string $name textmaster project name - * @param string $category textmaster project category - * @param string $briefing textmaster project briefing - * @param string $languageTo destination language identifier (ex: fr, en, de...) - * @param array $options textmaster project options - * @param string $activity textmaster project activity - * @param string $workTemplate textmaster project work template - * @param array $textmasters ids of the authors + * @param string $finderCode translatable finder code + * @param string $languageFrom source language identifier (ex: fr, en, de...) + * @param array $filter array of parameters to filter the list of objects + * @param string $name textmaster project name + * @param string $category textmaster project category + * @param string $briefing textmaster project briefing + * @param string $languageTo destination language identifier (ex: fr, en, de...) + * @param array $options textmaster project options + * @param string $activity textmaster project activity + * @param string $workTemplate textmaster project work template + * @param bool $useMyTextmasters use my favorite authors for the project * * @return ProjectInterface|null */ @@ -34,6 +34,6 @@ public function generate( array $options = [], $activity, $workTemplate = null, - $textmasters = [] + $useMyTextmasters = true ); } diff --git a/Translation/TranslationManager.php b/Translation/TranslationManager.php index 4f53355..db07bcd 100644 --- a/Translation/TranslationManager.php +++ b/Translation/TranslationManager.php @@ -4,6 +4,7 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Textmaster\Manager; +use Textmaster\Model\Author; use Textmaster\Model\DocumentInterface; use Textmaster\Model\ProjectInterface; use Textmaster\Translator\TranslatorInterface; @@ -63,7 +64,7 @@ public function create( array $options = [], $activity = ProjectInterface::ACTIVITY_TRANSLATION, $workTemplate = null, - $textmasters = [] + $useMyTextmasters = true ) { $translationMemory = false; if (array_key_exists('translation_memory', $options)) { @@ -82,10 +83,20 @@ public function create( ->setOptions($options) ->setWorkTemplate($workTemplate) ->setCallback($this->generateProjectCallback()) - ->setTextmasters($textmasters) ; $project->save(); + + if ($useMyTextmasters) { + $authors = $project->getPotentialAuthors('my_textmaster'); + + $ids = array_map(function ($author) { + return $author->getAuthorId(); + }, $authors); + + $project->setTextmasters($ids); + } + $project->addDocuments($this->generateDocuments($project, $translatable)); if ($translationMemory) { diff --git a/Translation/TranslationManagerInterface.php b/Translation/TranslationManagerInterface.php index 9399dc7..a9100b3 100644 --- a/Translation/TranslationManagerInterface.php +++ b/Translation/TranslationManagerInterface.php @@ -19,6 +19,7 @@ interface TranslationManagerInterface * @param array $options * @param string $activity * @param string $workTemplate + * @param bool $useMyTextmasters * * @return ProjectInterface */ @@ -32,7 +33,7 @@ public function create( array $options = [], $activity = ProjectInterface::ACTIVITY_TRANSLATION, $workTemplate = null, - $textmasters = [] + $useMyTextmasters = true ); /** diff --git a/composer.json b/composer.json index 7c00d68..f687fd5 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "require": { "php": ">=5.6.0", - "worldia/textmaster-api": "^0.5", + "worldia/textmaster-api": "^0.6", "symfony/framework-bundle": "^3.2", "symfony/property-access": "^3.2", "symfony/validator": "^3.2",