Skip to content

Commit

Permalink
Merge pull request #9 from yokai-php/menu-translation-handling
Browse files Browse the repository at this point in the history
Reworked the way menu labels are built
  • Loading branch information
yann-eugone authored Sep 26, 2019
2 parents b610c43 + 371e860 commit 5add649
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
17 changes: 7 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,20 @@ php:
- 7.0
- 7.1
- 7.2
- 7.3

env:
- SYMFONY_VERSION="3.3.*"
- SYMFONY_VERSION="3.4.*"
- SYMFONY_VERSION="4.0.*"
- SYMFONY_VERSION="4.1.*"
- SYMFONY_VERSION="4.2.x-dev"
- SYMFONY_VERSION="4.2.*"
- SYMFONY_VERSION="4.3.*"

matrix:
exclude:
# Symfony >= 4.0 PHP requirement is ^7.1.3
- php: 7.0
env: SYMFONY_VERSION="4.0.*"
env: SYMFONY_VERSION="4.2.*"
- php: 7.0
env: SYMFONY_VERSION="4.1.*"
- php: 7.0
env: SYMFONY_VERSION="4.2.x-dev"
env: SYMFONY_VERSION="4.3.*"

sudo: false

Expand All @@ -29,10 +26,10 @@ cache:
- $HOME/.composer/cache

before_install:
- composer selfupdate
- rm -rf composer.lock vendor/
- if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/workflow:${SYMFONY_VERSION}" --no-update; fi;

install: composer update --prefer-dist --no-interaction $COMPOSER_FLAGS
install: COMPOSER_MEMORY_LIMIT=-1 travis_retry composer install --prefer-dist --no-interaction

script:
- vendor/bin/phpunit --coverage-clover=build/coverage/clover.xml
Expand Down
16 changes: 13 additions & 3 deletions src/Admin/Extension/WorkflowExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function configureSideMenu(
$transitions = $workflow->getEnabledTransitions($subject);

if (count($transitions) === 0) {
$this->noTransitions($menu);
$this->noTransitions($menu, $admin);
} else {
$this->transitionsDropdown($menu, $admin, $transitions, $subject);
}
Expand Down Expand Up @@ -139,15 +139,19 @@ protected function configureOptions(OptionsResolver $resolver)

/**
* @param MenuItemInterface $menu
* @param AdminInterface $admin
*/
protected function noTransitions(MenuItemInterface $menu)
protected function noTransitions(MenuItemInterface $menu, AdminInterface $admin)
{
if ($this->options['no_transition_display']) {
$menu->addChild($this->options['no_transition_label'], [
'uri' => '#',
'attributes' => [
'icon' => $this->options['no_transition_icon'],
],
'extras' => [
'translation_domain' => $admin->getTranslationDomain(),
],
]);
}
}
Expand All @@ -165,6 +169,9 @@ protected function transitionsDropdown(MenuItemInterface $menu, AdminInterface $
'dropdown' => true,
'icon' => $this->options['dropdown_transitions_icon'],
],
'extras' => [
'translation_domain' => $admin->getTranslationDomain(),
],
]);

foreach ($transitions as $transition) {
Expand All @@ -183,14 +190,17 @@ protected function transitionsItem(MenuItemInterface $menu, AdminInterface $admi
$options = [
'uri' => $this->generateTransitionUri($admin, $transition, $subject),
'attributes' => [],
'extras' => [
'translation_domain' => $admin->getTranslationDomain(),
],
];

if ($icon = $this->getTransitionIcon($transition)) {
$options['attributes']['icon'] = $icon;
}

$menu->addChild(
$admin->getLabelTranslatorStrategy()->getLabel($transition->getName(), 'workflow'),
$admin->getLabelTranslatorStrategy()->getLabel($transition->getName(), 'workflow', 'transition'),
$options
);
}
Expand Down
11 changes: 7 additions & 4 deletions tests/Admin/Extension/WorkflowExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Symfony\Component\Routing\Route;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\StateMachine;
use Symfony\Component\Workflow\SupportStrategy\InstanceOfSupportStrategy;
use Yokai\SonataWorkflow\Admin\Extension\WorkflowExtension;
use Yokai\SonataWorkflow\Controller\WorkflowController;
use Yokai\SonataWorkflow\Tests\PullRequest;
Expand Down Expand Up @@ -106,13 +105,14 @@ public function testConfigureSideMenu($marking, array $transitions)

/** @var AdminInterface|ObjectProphecy $admin */
$admin = $this->prophesize(AdminInterface::class);
$admin->getTranslationDomain()->willReturn('admin');
$admin->getLabelTranslatorStrategy()->willReturn($labelStrategy->reveal());
$admin->getSubject()->willReturn($pullRequest);

foreach ($transitions as $transition) {
$labelStrategy->getLabel($transition, 'workflow')
$labelStrategy->getLabel($transition, 'workflow', 'transition')
->shouldBeCalledTimes(1)
->willReturn('transition.'.$transition);
->willReturn('workflow.transition.'.$transition);
$admin->generateObjectUrl('workflow_apply_transition', $pullRequest, ['transition' => $transition])
->shouldBeCalledTimes(1)
->willReturn('/pull-request/42/workflow/transition/'.$transition.'/apply');
Expand All @@ -135,11 +135,13 @@ public function testConfigureSideMenu($marking, array $transitions)
self::assertNull($child = $menu->getChild('workflow_transitions'));
self::assertNotNull($child = $menu->getChild('workflow_transitions_empty'));
self::assertSame('#', $child->getUri());
self::assertSame('admin', $child->getExtra('translation_domain'));
self::assertFalse($child->hasChildren());
self::assertEmpty($child->getChildren());
} else {
self::assertNull($child = $menu->getChild('workflow_transitions_empty'));
self::assertNotNull($child = $menu->getChild('workflow_transitions'));
self::assertSame('admin', $child->getExtra('translation_domain'));
self::assertTrue($child->getAttribute('dropdown'));
self::assertSame('fa fa-code-fork', $child->getAttribute('icon'));
self::assertTrue($child->hasChildren());
Expand All @@ -150,8 +152,9 @@ public function testConfigureSideMenu($marking, array $transitions)
$icon = 'fa fa-times';
}

self::assertNotNull($item = $child->getChild('transition.'.$transition));
self::assertNotNull($item = $child->getChild('workflow.transition.'.$transition));
self::assertSame('/pull-request/42/workflow/transition/'.$transition.'/apply', $item->getUri());
self::assertSame('admin', $item->getExtra('translation_domain'));
self::assertSame($icon, $item->getAttribute('icon'));
}
}
Expand Down

0 comments on commit 5add649

Please sign in to comment.