diff --git a/core/modules/tour/lib/Drupal/tour/Entity/Tour.php b/core/modules/tour/lib/Drupal/tour/Entity/Tour.php index 1dd2d20..2821995 100644 --- a/core/modules/tour/lib/Drupal/tour/Entity/Tour.php +++ b/core/modules/tour/lib/Drupal/tour/Entity/Tour.php @@ -60,6 +60,13 @@ class Tour extends ConfigEntityBase implements TourInterface { protected $routes = array(); /** + * The routes on which this tour should be displayed, keyed by route id. + * + * @var array + */ + protected $keyedRoutes; + + /** * Holds the collection of tips that are attached to this tour. * * @var \Drupal\tour\TipsBag @@ -136,22 +143,21 @@ public function getExportProperties() { * {@inheritdoc} */ public function hasMatchingRoute($route_name, $route_params) { - static $keyed_routes; - if (!isset($keyed_routes)) { - $keyed_routes = array(); - foreach ($this->routes as $route) { - $keyed_routes[$route['route_name']] = isset($route['route_params']) ? $route['route_params'] : array(); + if (!isset($this->keyedRoutes)) { + $this->keyedRoutes = array(); + foreach ($this->getRoutes() as $route) { + $this->keyedRoutes[$route['route_name']] = isset($route['route_params']) ? $route['route_params'] : array(); } } - if (!isset($keyed_routes[$route_name])) { + if (!isset($this->keyedRoutes[$route_name])) { // We don't know about this route. return FALSE; } - if (empty($keyed_routes[$route_name])) { + if (empty($this->keyedRoutes[$route_name])) { // We don't need to worry about route params, the route name is enough. return TRUE; } - foreach ($keyed_routes[$route_name] as $key => $value) { + foreach ($this->keyedRoutes[$route_name] as $key => $value) { // If a required param is missing or doesn't match, return FALSE. if (empty($route_params[$key]) || $route_params[$key] !== $value) { return FALSE; @@ -160,4 +166,11 @@ public function hasMatchingRoute($route_name, $route_params) { return TRUE; } + /** + * {@inheritdoc} + */ + public function resetKeyedRoutes() { + unset($this->keyedRoutes); + } + } diff --git a/core/modules/tour/lib/Drupal/tour/TourInterface.php b/core/modules/tour/lib/Drupal/tour/TourInterface.php index c3c1e8d..2aa1459 100644 --- a/core/modules/tour/lib/Drupal/tour/TourInterface.php +++ b/core/modules/tour/lib/Drupal/tour/TourInterface.php @@ -54,4 +54,9 @@ public function getTip($id); */ public function getTips(); + /** + * Resets the statically cached keyed routes. + */ + public function resetKeyedRoutes(); + } diff --git a/core/modules/tour/tests/Drupal/tour/Tests/Entity/TourTest.php b/core/modules/tour/tests/Drupal/tour/Tests/Entity/TourTest.php new file mode 100644 index 0000000..d62ecc4 --- /dev/null +++ b/core/modules/tour/tests/Drupal/tour/Tests/Entity/TourTest.php @@ -0,0 +1,143 @@ + 'Tour entity tests', + 'description' => 'Test \Drupal\tour\Entity\Tour.', + 'group' => 'Tour', + ); + } + + /** + * Tests \Drupal\tour\Entity\Tour::hasMatchingRoute(). + * + * @param array $routes + * Array of routes as per the Tour::routes property. + * @param string $route_name + * The route name to match. + * @param array $route_params + * Array of route params. + * @param bool $result + * Expected result. + * + * @covers \Drupal\tour\Entity\Tour::hasMatchingRoute(). + * @group Tour + * @dataProvider routeProvider + */ + public function testHasMatchingRoute($routes, $route_name, $route_params, $result) { + $tour = $this->getMockBuilder('\Drupal\tour\Entity\Tour') + ->disableOriginalConstructor() + ->setMethods(array('getRoutes')) + ->getMock(); + + $tour->expects($this->any()) + ->method('getRoutes') + ->will($this->returnValue($routes)); + + $this->assertEquals($result, $tour->hasMatchingRoute($route_name, $route_params)); + + $tour->resetKeyedRoutes(); + } + + /* + * Provides sample routes for testing. + */ + public function routeProvider() { + return array( + // Simple match. + array( + array( + array('route_name' => 'some.route') + ), + 'some.route', + array(), + TRUE + ), + // Empty params. + array( + array( + array( + 'route_name' => 'some.route', + 'route_params' => array('foo' => 'bar'), + ), + ), + 'some.route', + array(), + FALSE + ), + // Match on params. + array( + array( + array( + 'route_name' => 'some.route', + 'route_params' => array('foo' => 'bar'), + ), + ), + 'some.route', + array('foo' => 'bar'), + TRUE + ), + // Non-matching params. + array( + array( + array( + 'route_name' => 'some.route', + 'route_params' => array('foo' => 'bar'), + ), + ), + 'some.route', + array('bar' => 'foo'), + FALSE + ), + // One matching, one not. + array( + array( + array( + 'route_name' => 'some.route', + 'route_params' => array('foo' => 'bar'), + ), + array( + 'route_name' => 'some.route', + 'route_params' => array('bar' => 'foo'), + ), + ), + 'some.route', + array('bar' => 'foo'), + TRUE + ), + // One matching, one not. + array( + array( + array( + 'route_name' => 'some.route', + 'route_params' => array('foo' => 'bar'), + ), + array( + 'route_name' => 'some.route', + 'route_params' => array('foo' => 'baz'), + ), + ), + 'some.route', + array('foo' => 'baz'), + TRUE + ), + ); + } + +}