diff --git a/src/View/SSViewer.php b/src/View/SSViewer.php index f49ad4738..2aa03f866 100644 --- a/src/View/SSViewer.php +++ b/src/View/SSViewer.php @@ -165,7 +165,10 @@ class SSViewer implements Flushable public static function add_themes($themes = []) { - SSViewer::config()->merge('themes', $themes); + $currentThemes = SSViewer::get_themes(); + $finalThemes = array_merge($themes, $currentThemes); + // array_values is used to ensure sequential array keys as array_unique can leave gaps + SSViewer::set_themes(array_values(array_unique($finalThemes))); } public static function get_themes() diff --git a/tests/php/View/SSViewerTest.php b/tests/php/View/SSViewerTest.php index de6a2fa3e..18fea83b5 100644 --- a/tests/php/View/SSViewerTest.php +++ b/tests/php/View/SSViewerTest.php @@ -80,6 +80,25 @@ class SSViewerTest extends SapphireTest ); } + /** + * Tests for themes helper functions, ensuring they behave as defined in the RFC at + * https://github.com/silverstripe/silverstripe-framework/issues/5604 + */ + public function testThemesHelpers() + { + // Test set_themes() + SSViewer::set_themes(['mytheme', '$default']); + $this->assertEquals(['mytheme', '$default'], SSViewer::get_themes()); + + // Ensure add_themes() prepends + SSViewer::add_themes(['my_more_important_theme']); + $this->assertEquals(['my_more_important_theme', 'mytheme', '$default'], SSViewer::get_themes()); + + // Ensure add_themes() on theme already in cascade promotes it to the top + SSViewer::add_themes(['mytheme']); + $this->assertEquals(['mytheme', 'my_more_important_theme', '$default'], SSViewer::get_themes()); + } + /** * Test that a template without a tag still renders. */