diff --git a/core/core.services.yml b/core/core.services.yml index fc1b515..32bbbd6 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -424,8 +424,7 @@ services: theme.negotiator: class: Drupal\Core\Theme\ThemeNegotiator arguments: ['@access_check.theme'] - calls: - - [setContainer, ['@service_container']] + parent: container.trait tags: - { name: service_id_collector, tag: theme_negotiator } theme.negotiator.default: diff --git a/core/tests/Drupal/Tests/Core/Theme/ThemeNegotiatorTest.php b/core/tests/Drupal/Tests/Core/Theme/ThemeNegotiatorTest.php index 55a6f7f..ceae0ae 100644 --- a/core/tests/Drupal/Tests/Core/Theme/ThemeNegotiatorTest.php +++ b/core/tests/Drupal/Tests/Core/Theme/ThemeNegotiatorTest.php @@ -7,11 +7,10 @@ namespace Drupal\Tests\Core\Theme; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\Routing\RouteMatch; use Drupal\Core\Theme\ThemeNegotiator; use Drupal\Tests\UnitTestCase; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Routing\Route; /** @@ -28,6 +27,13 @@ class ThemeNegotiatorTest extends UnitTestCase { protected $themeAccessCheck; /** + * The container builder. + * + * @var \Drupal\Core\DependencyInjection\ContainerBuilder + */ + protected $container; + + /** * The request stack. * * @var \Symfony\Component\HttpFoundation\RequestStack @@ -41,11 +47,14 @@ class ThemeNegotiatorTest extends UnitTestCase { */ protected $themeNegotiator; + /** + * {@inheritdoc} + */ protected function setUp() { $this->themeAccessCheck = $this->getMockBuilder('\Drupal\Core\Theme\ThemeAccessCheck') ->disableOriginalConstructor() ->getMock(); - $this->themeNegotiator = new ThemeNegotiator($this->themeAccessCheck); + $this->container = new ContainerBuilder(); } /** @@ -62,14 +71,16 @@ public function testDetermineActiveTheme() { ->method('applies') ->will($this->returnValue(TRUE)); - $this->themeNegotiator->addNegotiator($negotiator, 0); + $this->container->set('test_negotiator', $negotiator); + + $negotiators = ['test_negotiator']; $this->themeAccessCheck->expects($this->any()) ->method('checkAccess') ->will($this->returnValue(TRUE)); $route_match = new RouteMatch('test_route', new Route('/test-route'), array(), array()); - $theme = $this->themeNegotiator->determineActiveTheme($route_match); + $theme = $this->createThemeNegotiator($negotiators)->determineActiveTheme($route_match); $this->assertEquals('example_test', $theme); } @@ -80,6 +91,8 @@ public function testDetermineActiveTheme() { * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme() */ public function testDetermineActiveThemeWithPriority() { + $negotiators = []; + $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface'); $negotiator->expects($this->once()) ->method('determineActiveTheme') @@ -88,7 +101,7 @@ public function testDetermineActiveThemeWithPriority() { ->method('applies') ->will($this->returnValue(TRUE)); - $this->themeNegotiator->addNegotiator($negotiator, 10); + $negotiators['test_negotiator_1'] = $negotiator; $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface'); $negotiator->expects($this->never()) @@ -96,14 +109,18 @@ public function testDetermineActiveThemeWithPriority() { $negotiator->expects($this->never()) ->method('applies'); - $this->themeNegotiator->addNegotiator($negotiator, 0); + $negotiators['test_negotiator_2'] = $negotiator; + + foreach ($negotiators as $id => $negotiator) { + $this->container->set($id, $negotiator); + } $this->themeAccessCheck->expects($this->any()) ->method('checkAccess') ->will($this->returnValue(TRUE)); $route_match = new RouteMatch('test_route', new Route('/test-route'), array(), array()); - $theme = $this->themeNegotiator->determineActiveTheme($route_match); + $theme = $this->createThemeNegotiator(array_keys($negotiators))->determineActiveTheme($route_match); $this->assertEquals('example_test', $theme); } @@ -114,6 +131,8 @@ public function testDetermineActiveThemeWithPriority() { * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme() */ public function testDetermineActiveThemeWithAccessCheck() { + $negotiators = []; + $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface'); $negotiator->expects($this->once()) ->method('determineActiveTheme') @@ -122,7 +141,7 @@ public function testDetermineActiveThemeWithAccessCheck() { ->method('applies') ->will($this->returnValue(TRUE)); - $this->themeNegotiator->addNegotiator($negotiator, 10); + $negotiators['test_negotiator_1'] = $negotiator; $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface'); $negotiator->expects($this->once()) @@ -132,7 +151,11 @@ public function testDetermineActiveThemeWithAccessCheck() { ->method('applies') ->will($this->returnValue(TRUE)); - $this->themeNegotiator->addNegotiator($negotiator, 0); + $negotiators['test_negotiator_2'] = $negotiator; + + foreach ($negotiators as $id => $negotiator) { + $this->container->set($id, $negotiator); + } $this->themeAccessCheck->expects($this->at(0)) ->method('checkAccess') @@ -145,7 +168,7 @@ public function testDetermineActiveThemeWithAccessCheck() { ->will($this->returnValue(TRUE)); $route_match = new RouteMatch('test_route', new Route('/test-route'), array(), array()); - $theme = $this->themeNegotiator->determineActiveTheme($route_match); + $theme = $this->createThemeNegotiator(array_keys($negotiators))->determineActiveTheme($route_match); $this->assertEquals('example_test2', $theme); } @@ -156,6 +179,8 @@ public function testDetermineActiveThemeWithAccessCheck() { * @see \Drupal\Core\Theme\ThemeNegotiatorInterface */ public function testDetermineActiveThemeWithNotApplyingNegotiator() { + $negotiators = []; + $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface'); $negotiator->expects($this->never()) ->method('determineActiveTheme'); @@ -163,7 +188,7 @@ public function testDetermineActiveThemeWithNotApplyingNegotiator() { ->method('applies') ->will($this->returnValue(FALSE)); - $this->themeNegotiator->addNegotiator($negotiator, 10); + $negotiators['test_negotiator_1'] = $negotiator; $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface'); $negotiator->expects($this->once()) @@ -173,16 +198,35 @@ public function testDetermineActiveThemeWithNotApplyingNegotiator() { ->method('applies') ->will($this->returnValue(TRUE)); - $this->themeNegotiator->addNegotiator($negotiator, 0); + $negotiators['test_negotiator_2'] = $negotiator; + + foreach ($negotiators as $id => $negotiator) { + $this->container->set($id, $negotiator); + } $this->themeAccessCheck->expects($this->any()) ->method('checkAccess') ->will($this->returnValue(TRUE)); $route_match = new RouteMatch('test_route', new Route('/test-route'), array(), array()); - $theme = $this->themeNegotiator->determineActiveTheme($route_match); + $theme = $this->createThemeNegotiator(array_keys($negotiators))->determineActiveTheme($route_match); $this->assertEquals('example_test2', $theme); } + /** + * Creates a new theme negotiator instance. + * + * @param array $negotiators + * An array of negotiator IDs. + * + * @return \Drupal\Core\Theme\ThemeNegotiator + */ + protected function createThemeNegotiator(array $negotiators) { + $theme_negotiator = new ThemeNegotiator($this->themeAccessCheck, $negotiators); + $theme_negotiator->setContainer($this->container); + + return $theme_negotiator; + } + }