Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Theme / ThemeNegotiatorTest.php
index 4b33fcbfcd26b7b6345bd60f8423ebf8e7e51ec4..1e2303d7c4142fae3d22800588e723b20ddfacd2 100644 (file)
@@ -2,6 +2,8 @@
 
 namespace Drupal\Tests\Core\Theme;
 
+use Drupal\Core\DependencyInjection\ClassResolver;
+use Drupal\Core\DependencyInjection\ContainerBuilder;
 use Drupal\Core\Routing\RouteMatch;
 use Drupal\Core\Theme\ThemeNegotiator;
 use Drupal\Tests\UnitTestCase;
@@ -20,6 +22,13 @@ class ThemeNegotiatorTest extends UnitTestCase {
    */
   protected $themeAccessCheck;
 
+  /**
+   * The container builder.
+   *
+   * @var \Drupal\Core\DependencyInjection\ContainerBuilder
+   */
+  protected $container;
+
   /**
    * The request stack.
    *
@@ -34,11 +43,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();
   }
 
   /**
@@ -55,14 +67,16 @@ class ThemeNegotiatorTest extends UnitTestCase {
       ->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'), [], []);
-    $theme = $this->themeNegotiator->determineActiveTheme($route_match);
+    $theme = $this->createThemeNegotiator($negotiators)->determineActiveTheme($route_match);
 
     $this->assertEquals('example_test', $theme);
   }
@@ -73,6 +87,8 @@ class ThemeNegotiatorTest extends UnitTestCase {
    * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
    */
   public function testDetermineActiveThemeWithPriority() {
+    $negotiators = [];
+
     $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
     $negotiator->expects($this->once())
       ->method('determineActiveTheme')
@@ -81,7 +97,7 @@ class ThemeNegotiatorTest extends UnitTestCase {
       ->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())
@@ -89,14 +105,18 @@ class ThemeNegotiatorTest extends UnitTestCase {
     $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'), [], []);
-    $theme = $this->themeNegotiator->determineActiveTheme($route_match);
+    $theme = $this->createThemeNegotiator(array_keys($negotiators))->determineActiveTheme($route_match);
 
     $this->assertEquals('example_test', $theme);
   }
@@ -107,6 +127,8 @@ class ThemeNegotiatorTest extends UnitTestCase {
    * @see \Drupal\Core\Theme\ThemeNegotiator::determineActiveTheme()
    */
   public function testDetermineActiveThemeWithAccessCheck() {
+    $negotiators = [];
+
     $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
     $negotiator->expects($this->once())
       ->method('determineActiveTheme')
@@ -115,7 +137,7 @@ class ThemeNegotiatorTest extends UnitTestCase {
       ->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())
@@ -125,7 +147,11 @@ class ThemeNegotiatorTest extends UnitTestCase {
       ->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')
@@ -138,7 +164,7 @@ class ThemeNegotiatorTest extends UnitTestCase {
       ->will($this->returnValue(TRUE));
 
     $route_match = new RouteMatch('test_route', new Route('/test-route'), [], []);
-    $theme = $this->themeNegotiator->determineActiveTheme($route_match);
+    $theme = $this->createThemeNegotiator(array_keys($negotiators))->determineActiveTheme($route_match);
 
     $this->assertEquals('example_test2', $theme);
   }
@@ -149,6 +175,8 @@ class ThemeNegotiatorTest extends UnitTestCase {
    * @see \Drupal\Core\Theme\ThemeNegotiatorInterface
    */
   public function testDetermineActiveThemeWithNotApplyingNegotiator() {
+    $negotiators = [];
+
     $negotiator = $this->getMock('Drupal\Core\Theme\ThemeNegotiatorInterface');
     $negotiator->expects($this->never())
       ->method('determineActiveTheme');
@@ -156,7 +184,7 @@ class ThemeNegotiatorTest extends UnitTestCase {
       ->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())
@@ -166,16 +194,35 @@ class ThemeNegotiatorTest extends UnitTestCase {
       ->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'), [], []);
-    $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) {
+    $resolver = new ClassResolver();
+    $resolver->setContainer($this->container);
+    $theme_negotiator = new ThemeNegotiator($this->themeAccessCheck, $resolver, $negotiators);
+    return $theme_negotiator;
+  }
+
 }