Loading diff.services.yml +1 −1 Changes for diff.services.yml: 1 added line, 1 removed line. Original line number Diff line number Diff line Loading @@ -31,6 +31,6 @@ services: theme.negotiator.visual_diff: class: Drupal\diff\VisualDiffThemeNegotiator arguments: ['@current_user', '@config.factory', '@entity.manager', '@router.admin_context'] arguments: ['@config.factory'] tags: - { name: theme_negotiator, priority: 0 } src/VisualDiffThemeNegotiator.php +31 −8 Changes for src/VisualDiffThemeNegotiator.php: 31 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -2,31 +2,53 @@ namespace Drupal\diff; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\user\Theme\AdminNegotiator; use Drupal\Core\Theme\ThemeNegotiatorInterface; /** * Visual inline layout theme negotiator. * * @package Drupal\diff */ class VisualDiffThemeNegotiator extends AdminNegotiator { class VisualDiffThemeNegotiator implements ThemeNegotiatorInterface { /** * The config factory. * * @var \Drupal\Core\Config\ConfigFactoryInterface */ protected $configFactory; /** * VisualDiffThemeNegotiator constructor. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. */ public function __construct(ConfigFactoryInterface $config_factory) { $this->configFactory = $config_factory; } /** * {@inheritdoc} */ public function applies(RouteMatchInterface $routeMatch) { if ($routeMatch->getParameter('filter') === 'visual_inline') { if ($this->isDiffRoute($routeMatch)) { if ($this->configFactory->get('diff.settings')->get('general_settings.visual_inline_theme') === 'default') { return TRUE; } if ($routeMatch->getParameter('filter') !== 'visual_inline') { return FALSE; } if (!$this->isDiffRoute($routeMatch)) { return FALSE; } if ($this->configFactory->get('diff.settings')->get('general_settings.visual_inline_theme') !== 'default') { return FALSE; } return TRUE; } /** * {@inheritdoc} */ Loading @@ -43,9 +65,10 @@ class VisualDiffThemeNegotiator extends AdminNegotiator { * @return bool * Return TRUE if route name is ok. */ public function isDiffRoute(RouteMatchInterface $route_match) { protected function isDiffRoute(RouteMatchInterface $route_match) { $regex_pattern = '/^entity\..*\.revisions_diff$/'; return $route_match->getRouteName() === 'diff.revisions_diff' || preg_match($regex_pattern, $route_match->getRouteName()); } } tests/src/Unit/VisualDiffThemeNegotiatorTest.php +77 −49 Changes for tests/src/Unit/VisualDiffThemeNegotiatorTest.php: 77 added lines, 49 removed lines. Original line number Diff line number Diff line Loading @@ -4,13 +4,9 @@ namespace Drupal\Tests\diff\Unit; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ImmutableConfig; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Routing\AdminContext; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; use Drupal\diff\VisualDiffThemeNegotiator; use Drupal\Tests\UnitTestCase; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Class VisualDiffThemeNegotiatorTest Loading @@ -20,76 +16,108 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class VisualDiffThemeNegotiatorTest extends UnitTestCase { /** * The current user. * * @var \Drupal\Core\Session\AccountInterface */ protected $user; /** * The config factory. * * @var \Drupal\Core\Config\ConfigFactoryInterface * @var \Drupal\Core\Config\ConfigFactoryInterface|\Prophecy\Prophecy\ProphecyInterface */ protected $configFactory; /** * The entity manager. * The class under test. * * @var \Drupal\Core\Entity\EntityManagerInterface * @var \Drupal\diff\VisualDiffThemeNegotiator */ protected $entityManager; /** * The route admin context to determine whether a route is an admin one. * * @var \Drupal\Core\Routing\AdminContext */ protected $adminContext; protected $themeNegotiator; /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->user = $this->prophesize(AccountInterface::class); $this->configFactory = $this->prophesize(ConfigFactoryInterface::class); $this->entityManager = $this->prophesize(EntityManagerInterface::class); $this->adminContext = $this->prophesize(AdminContext::class); $this->themeNegotiator = new VisualDiffThemeNegotiator($this->configFactory->reveal()); } /** * Tests if applies method returns true. * * Check the value returned by the applies method when the theme used for * visual inline plugin is "Standard". * @covers ::determineActiveTheme */ public function testNegotiator() { // Create $theme using mocked class. $theme = new VisualDiffThemeNegotiator($this->user->reveal(), $this->configFactory->reveal(), $this->entityManager->reveal(), $this->adminContext->reveal()); public function testDetermineActiveTheme() { $config = $this->prophesize(ImmutableConfig::class); $config->get('default')->willReturn('the_default_theme'); $this->configFactory->get('system.theme')->willReturn($config->reveal()); // Mocking $route_match and relevant variable. $route_match = $this->prophesize(RouteMatchInterface::class); $route_match->getRouteName()->willReturn('diff.revisions_diff'); $this->assertTrue($theme->isDiffRoute($route_match->reveal())); $route_match->getRouteName()->willReturn('entity.entity_type_id.revisions_diff'); $this->assertTrue($theme->isDiffRoute($route_match->reveal())); $route_match->getParameter('filter')->willReturn('visual_inline'); $result = $this->themeNegotiator->determineActiveTheme($route_match->reveal()); $this->assertSame('the_default_theme', $result); } $container = $this->prophesize(ContainerInterface::class); $container->get('config.factory')->willReturn($this->configFactory); /** * @covers ::applies * @covers ::isDiffRoute * * @dataProvider providerTestApplies */ public function testApplies($filter_parameter, $route_name, $config_value, $expected) { $route_match = $this->prophesize(RouteMatchInterface::class); $route_match->getParameter('filter')->willReturn($filter_parameter); if ($route_name) { $route_match->getRouteName()->willReturn($route_name); } else { $route_match->getRouteName()->shouldNotBeCalled(); } if ($config_value) { $diff_config = $this->prophesize(ImmutableConfig::class); $this->configFactory->get('diff.settings') ->willReturn($diff_config->reveal()); $diff_config->get('general_settings.visual_inline_theme')->willReturn('default'); \Drupal::setContainer($container->reveal()); $diff_config->get('general_settings.visual_inline_theme')->willReturn($config_value); $this->configFactory->get('diff.settings')->willReturn($diff_config->reveal()); } else { $this->configFactory->get('diff.settings')->shouldNotBeCalled(); } $this->assertSame($expected, $this->themeNegotiator->applies($route_match->reveal())); } // Check if applies function returns true. $this->assertTrue($theme->applies($route_match->reveal())); /** * Provides test data to ::testApplies(). */ public function providerTestApplies() { $data = []; $data[] = [ 'unexpected_filter_parameter', NULL, NULL, FALSE, ]; $data[] = [ 'visual_inline', 'unexpected_route_name', NULL, FALSE, ]; $data[] = [ 'visual_inline', 'diff.revisions_diff', 'unexpected_config_value', FALSE, ]; $data[] = [ 'visual_inline', 'diff.revisions_diff', 'default', TRUE, ]; $data[] = [ 'visual_inline', 'entity.foo.revisions_diff', 'default', TRUE, ]; return $data; } } Loading
diff.services.yml +1 −1 Changes for diff.services.yml: 1 added line, 1 removed line. Original line number Diff line number Diff line Loading @@ -31,6 +31,6 @@ services: theme.negotiator.visual_diff: class: Drupal\diff\VisualDiffThemeNegotiator arguments: ['@current_user', '@config.factory', '@entity.manager', '@router.admin_context'] arguments: ['@config.factory'] tags: - { name: theme_negotiator, priority: 0 }
src/VisualDiffThemeNegotiator.php +31 −8 Changes for src/VisualDiffThemeNegotiator.php: 31 added lines, 8 removed lines. Original line number Diff line number Diff line Loading @@ -2,31 +2,53 @@ namespace Drupal\diff; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\user\Theme\AdminNegotiator; use Drupal\Core\Theme\ThemeNegotiatorInterface; /** * Visual inline layout theme negotiator. * * @package Drupal\diff */ class VisualDiffThemeNegotiator extends AdminNegotiator { class VisualDiffThemeNegotiator implements ThemeNegotiatorInterface { /** * The config factory. * * @var \Drupal\Core\Config\ConfigFactoryInterface */ protected $configFactory; /** * VisualDiffThemeNegotiator constructor. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory. */ public function __construct(ConfigFactoryInterface $config_factory) { $this->configFactory = $config_factory; } /** * {@inheritdoc} */ public function applies(RouteMatchInterface $routeMatch) { if ($routeMatch->getParameter('filter') === 'visual_inline') { if ($this->isDiffRoute($routeMatch)) { if ($this->configFactory->get('diff.settings')->get('general_settings.visual_inline_theme') === 'default') { return TRUE; } if ($routeMatch->getParameter('filter') !== 'visual_inline') { return FALSE; } if (!$this->isDiffRoute($routeMatch)) { return FALSE; } if ($this->configFactory->get('diff.settings')->get('general_settings.visual_inline_theme') !== 'default') { return FALSE; } return TRUE; } /** * {@inheritdoc} */ Loading @@ -43,9 +65,10 @@ class VisualDiffThemeNegotiator extends AdminNegotiator { * @return bool * Return TRUE if route name is ok. */ public function isDiffRoute(RouteMatchInterface $route_match) { protected function isDiffRoute(RouteMatchInterface $route_match) { $regex_pattern = '/^entity\..*\.revisions_diff$/'; return $route_match->getRouteName() === 'diff.revisions_diff' || preg_match($regex_pattern, $route_match->getRouteName()); } }
tests/src/Unit/VisualDiffThemeNegotiatorTest.php +77 −49 Changes for tests/src/Unit/VisualDiffThemeNegotiatorTest.php: 77 added lines, 49 removed lines. Original line number Diff line number Diff line Loading @@ -4,13 +4,9 @@ namespace Drupal\Tests\diff\Unit; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ImmutableConfig; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Routing\AdminContext; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; use Drupal\diff\VisualDiffThemeNegotiator; use Drupal\Tests\UnitTestCase; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Class VisualDiffThemeNegotiatorTest Loading @@ -20,76 +16,108 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class VisualDiffThemeNegotiatorTest extends UnitTestCase { /** * The current user. * * @var \Drupal\Core\Session\AccountInterface */ protected $user; /** * The config factory. * * @var \Drupal\Core\Config\ConfigFactoryInterface * @var \Drupal\Core\Config\ConfigFactoryInterface|\Prophecy\Prophecy\ProphecyInterface */ protected $configFactory; /** * The entity manager. * The class under test. * * @var \Drupal\Core\Entity\EntityManagerInterface * @var \Drupal\diff\VisualDiffThemeNegotiator */ protected $entityManager; /** * The route admin context to determine whether a route is an admin one. * * @var \Drupal\Core\Routing\AdminContext */ protected $adminContext; protected $themeNegotiator; /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->user = $this->prophesize(AccountInterface::class); $this->configFactory = $this->prophesize(ConfigFactoryInterface::class); $this->entityManager = $this->prophesize(EntityManagerInterface::class); $this->adminContext = $this->prophesize(AdminContext::class); $this->themeNegotiator = new VisualDiffThemeNegotiator($this->configFactory->reveal()); } /** * Tests if applies method returns true. * * Check the value returned by the applies method when the theme used for * visual inline plugin is "Standard". * @covers ::determineActiveTheme */ public function testNegotiator() { // Create $theme using mocked class. $theme = new VisualDiffThemeNegotiator($this->user->reveal(), $this->configFactory->reveal(), $this->entityManager->reveal(), $this->adminContext->reveal()); public function testDetermineActiveTheme() { $config = $this->prophesize(ImmutableConfig::class); $config->get('default')->willReturn('the_default_theme'); $this->configFactory->get('system.theme')->willReturn($config->reveal()); // Mocking $route_match and relevant variable. $route_match = $this->prophesize(RouteMatchInterface::class); $route_match->getRouteName()->willReturn('diff.revisions_diff'); $this->assertTrue($theme->isDiffRoute($route_match->reveal())); $route_match->getRouteName()->willReturn('entity.entity_type_id.revisions_diff'); $this->assertTrue($theme->isDiffRoute($route_match->reveal())); $route_match->getParameter('filter')->willReturn('visual_inline'); $result = $this->themeNegotiator->determineActiveTheme($route_match->reveal()); $this->assertSame('the_default_theme', $result); } $container = $this->prophesize(ContainerInterface::class); $container->get('config.factory')->willReturn($this->configFactory); /** * @covers ::applies * @covers ::isDiffRoute * * @dataProvider providerTestApplies */ public function testApplies($filter_parameter, $route_name, $config_value, $expected) { $route_match = $this->prophesize(RouteMatchInterface::class); $route_match->getParameter('filter')->willReturn($filter_parameter); if ($route_name) { $route_match->getRouteName()->willReturn($route_name); } else { $route_match->getRouteName()->shouldNotBeCalled(); } if ($config_value) { $diff_config = $this->prophesize(ImmutableConfig::class); $this->configFactory->get('diff.settings') ->willReturn($diff_config->reveal()); $diff_config->get('general_settings.visual_inline_theme')->willReturn('default'); \Drupal::setContainer($container->reveal()); $diff_config->get('general_settings.visual_inline_theme')->willReturn($config_value); $this->configFactory->get('diff.settings')->willReturn($diff_config->reveal()); } else { $this->configFactory->get('diff.settings')->shouldNotBeCalled(); } $this->assertSame($expected, $this->themeNegotiator->applies($route_match->reveal())); } // Check if applies function returns true. $this->assertTrue($theme->applies($route_match->reveal())); /** * Provides test data to ::testApplies(). */ public function providerTestApplies() { $data = []; $data[] = [ 'unexpected_filter_parameter', NULL, NULL, FALSE, ]; $data[] = [ 'visual_inline', 'unexpected_route_name', NULL, FALSE, ]; $data[] = [ 'visual_inline', 'diff.revisions_diff', 'unexpected_config_value', FALSE, ]; $data[] = [ 'visual_inline', 'diff.revisions_diff', 'default', TRUE, ]; $data[] = [ 'visual_inline', 'entity.foo.revisions_diff', 'default', TRUE, ]; return $data; } }