From 7b91207c12405cb42f2f7658fbf1db72be1cc2d6 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Fri, 9 Aug 2024 09:27:38 +1200 Subject: [PATCH] FIX Don't error if template global is null (#11331) --- src/View/SSViewer_Scope.php | 3 +++ tests/php/View/SSViewerTest.php | 6 ++++++ tests/php/View/SSViewerTest/TestGlobalProvider.php | 9 +++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/View/SSViewer_Scope.php b/src/View/SSViewer_Scope.php index f82b84062..3733dd3af 100644 --- a/src/View/SSViewer_Scope.php +++ b/src/View/SSViewer_Scope.php @@ -177,6 +177,9 @@ class SSViewer_Scope public function getObj($name, $arguments = [], $cache = false, $cacheName = null) { $on = $this->itemIterator ? $this->itemIterator->current() : $this->item; + if ($on === null) { + return null; + } return $on->obj($name, $arguments, $cache, $cacheName); } diff --git a/tests/php/View/SSViewerTest.php b/tests/php/View/SSViewerTest.php index ce91527f3..2ce97e2b9 100644 --- a/tests/php/View/SSViewerTest.php +++ b/tests/php/View/SSViewerTest.php @@ -364,6 +364,12 @@ SS; ); } + public function testGlobalVariablesReturnNull() + { + $this->assertEquals('

', $this->render('

$SSViewerTest_GlobalReturnsNull

')); + $this->assertEquals('

', $this->render('

$SSViewerTest_GlobalReturnsNull.Chained.Properties

')); + } + public function testCoreGlobalVariableCalls() { $this->assertEquals( diff --git a/tests/php/View/SSViewerTest/TestGlobalProvider.php b/tests/php/View/SSViewerTest/TestGlobalProvider.php index 9fccbb0c0..f005f24ce 100644 --- a/tests/php/View/SSViewerTest/TestGlobalProvider.php +++ b/tests/php/View/SSViewerTest/TestGlobalProvider.php @@ -18,8 +18,8 @@ class TestGlobalProvider implements TemplateGlobalProvider, TestOnly 'SSViewerTest_GlobalReferencedByString' => 'get_reference', 'SSViewerTest_GlobalReferencedInArray' => ['method' => 'get_reference'], - 'SSViewerTest_GlobalThatTakesArguments' => ['method' => 'get_argmix', 'casting' => 'HTMLFragment'] - + 'SSViewerTest_GlobalThatTakesArguments' => ['method' => 'get_argmix', 'casting' => 'HTMLFragment'], + 'SSViewerTest_GlobalReturnsNull' => 'getNull', ]; } @@ -43,4 +43,9 @@ class TestGlobalProvider implements TemplateGlobalProvider, TestOnly $args = func_get_args(); return 'z' . implode(':', $args) . 'z'; } + + public static function getNull() + { + return null; + } }