[TikiWiki-commits] [Git][tikiwiki/tiki][master] [FIX] Update Notifier: convert upgrade objects to strings before display

"luci \(@luciash\) via TikiWiki-cvs" <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <6a8c5c4978d1d_3918d0f879347@gitlab-sidekiq-low-urgency-cpu-bound-v2-959bd5c78-2v77z.mail>

luci pushed to branch master at Tiki Wiki CMS Groupware / Tiki


Commits:
460a4a8c by Bruno Kambere at 2026-08-24T14:21:38+00:00
[FIX] Update Notifier: convert upgrade objects to strings before display
---
* [REF] Update Notifier: extract getUpgradeMessages() and cover it with tests

* [FIX] Update Notifier: convert upgrade objects to strings before display

See merge request tikiwiki/tiki!10959

- - - - -


7 changed files:

- lib/core/Scheduler/Task/TikiCheckerCommandTask.php
- lib/core/Tiki/Suggestion/TikiCheck/Version.php
- lib/core/Tiki/Version/Upgrade.php
- lib/core/Tiki/Version/Utils.php
- + lib/test/Core/Suggestion/TikiCheck/VersionRuleTest.php
- + lib/test/Core/Tiki/Version/UtilsTest.php
- lib/test/TikiVersionTest.php


Changes:

=====================================
lib/core/Scheduler/Task/TikiCheckerCommandTask.php
=====================================
@@ -22,7 +22,7 @@ class Scheduler_Task_TikiCheckerCommandTask extends Scheduler_Task_CommandTask
 
             $tikiVersion = $TWV->version;
             $versionUtils = new Tiki_Version_Utils();
-            $needupdate = $versionUtils->checkUpdatesForVersion($tikiVersion);
+            $needupdate = $versionUtils->getUpgradeMessages($tikiVersion);
             $lastRebuild = $tikilib->get_preference('notified_tiki_version');
 
             if (! empty($needupdate) && $lastRebuild != $tikiVersion) {


=====================================
lib/core/Tiki/Suggestion/TikiCheck/Version.php
=====================================
@@ -10,11 +10,12 @@ use Tiki\Suggestion\SuggestionRulesInterface as SuggestionRules;
 
 class Version implements SuggestionRules
 {
-    public function parser()
+    public function parser(): array
     {
         include_once(__DIR__ . '/../../../../setup/twversion.class.php');
         $TWV = new \TWVersion();
         $versionUtils = new \Tiki_Version_Utils();
-        return $versionUtils->checkUpdatesForVersion($TWV->version);
+        // Suggestions are displayed as plain feedback messages, so only keep the text
+        return $versionUtils->getUpgradeMessages($TWV->version);
     }
 }


=====================================
lib/core/Tiki/Version/Upgrade.php
=====================================
@@ -161,6 +161,16 @@ class Tiki_Version_Upgrade
         }
     }
 
+    /**
+    * Allows the object to be used directly where a message string is expected,
+    * e.g. in templates rendering data restored from an older session.
+    * @return string
+    */
+    public function __toString(): string
+    {
+        return $this->getMessage();
+    }
+
     private function isMinor()
     {
         return $this->old->getMajor() === $this->new->getMajor();


=====================================
lib/core/Tiki/Version/Utils.php
=====================================
@@ -40,4 +40,24 @@ class Tiki_Version_Utils
 
         return $upgrades;
     }
+
+    /**
+     * Same check as checkUpdatesForVersion(), but returns the messages as text.
+     *
+     * For callers handing the result straight to a template, which cannot do
+     * anything with the Tiki_Version_Upgrade objects.
+     *
+     * @param string|Tiki_Version_Version $version
+     * @return string[]
+     * @throws Exception
+     */
+    public static function getUpgradeMessages(string|Tiki_Version_Version $version): array
+    {
+        return array_map(
+            function (Tiki_Version_Upgrade $upgrade): string {
+                return $upgrade->getMessage();
+            },
+            self::checkUpdatesForVersion($version)
+        );
+    }
 }


=====================================
lib/test/Core/Suggestion/TikiCheck/VersionRuleTest.php
=====================================
@@ -0,0 +1,46 @@
+<?php
+
+// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
+//
+// All Rights Reserved. See copyright.txt for details and a complete list of authors.
+// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
+namespace Tiki\Tests\Suggestion\TikiCheck;
+
+use PHPUnit\Framework\TestCase;
+use Tiki\Suggestion\TikiCheck\Version;
+use TikiLib;
+use TWVersion;
+
+class VersionRuleTest extends TestCase
+{
+    private string $cycleUrl;
+
+    protected function setUp(): void
+    {
+        global $TWV;
+        $TWV = new TWVersion();
+
+        $this->cycleUrl = 'https://tiki.org/' . TikiLib::lib('tiki')->get_preference('tiki_release_cycle') . '.cycle';
+        TikiLib::lib('cache')->cacheItem($this->cycleUrl, "99.0\n99.1\n", 'http');
+    }
+
+    protected function tearDown(): void
+    {
+        TikiLib::lib('cache')->invalidate($this->cycleUrl, 'http');
+    }
+
+    /**
+     * Suggestions are rendered as plain feedback messages, so the rule must not
+     * leak the Tiki_Version_Upgrade objects the version check returns.
+     */
+    public function testParserReturnsMessageStringsOnly(): void
+    {
+        $messages = (new Version())->parser();
+
+        $this->assertNotEmpty($messages);
+        foreach ($messages as $message) {
+            $this->assertIsString($message);
+            $this->assertNotSame('', $message);
+        }
+    }
+}


=====================================
lib/test/Core/Tiki/Version/UtilsTest.php
=====================================
@@ -0,0 +1,56 @@
+<?php
+
+// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
+//
+// All Rights Reserved. See copyright.txt for details and a complete list of authors.
+// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
+namespace Tiki\Tests\Version;
+
+use PHPUnit\Framework\TestCase;
+use Tiki_Version_Upgrade;
+use Tiki_Version_Utils;
+use TikiLib;
+use TWVersion;
+
+class UtilsTest extends TestCase
+{
+    private string $cycleUrl;
+
+    protected function setUp(): void
+    {
+        global $TWV;
+        $TWV = new TWVersion();
+
+        $this->cycleUrl = 'https://tiki.org/' . TikiLib::lib('tiki')->get_preference('tiki_release_cycle') . '.cycle';
+        TikiLib::lib('cache')->cacheItem($this->cycleUrl, "99.0\n99.1\n", 'http');
+    }
+
+    protected function tearDown(): void
+    {
+        TikiLib::lib('cache')->invalidate($this->cycleUrl, 'http');
+    }
+
+    public function testCheckUpdatesForVersionReturnsUpgradeObjects(): void
+    {
+        $upgrades = Tiki_Version_Utils::checkUpdatesForVersion('24.0');
+
+        $this->assertNotEmpty($upgrades);
+        $this->assertContainsOnlyInstancesOf(Tiki_Version_Upgrade::class, $upgrades);
+    }
+
+    /**
+     * Callers rendering the result in a template get text, since the upgrade
+     * objects themselves cannot be printed.
+     */
+    public function testGetUpgradeMessagesReturnsTextOnly(): void
+    {
+        $upgrades = Tiki_Version_Utils::checkUpdatesForVersion('24.0');
+        $messages = Tiki_Version_Utils::getUpgradeMessages('24.0');
+
+        $this->assertCount(count($upgrades), $messages);
+        foreach ($messages as $index => $message) {
+            $this->assertIsString($message);
+            $this->assertSame($upgrades[$index]->getMessage(), $message);
+        }
+    }
+}


=====================================
lib/test/TikiVersionTest.php
=====================================
@@ -224,6 +224,18 @@ class TikiVersionTest extends PHPUnit\Framework\TestCase
         $this->assertEquals($expectedMessage, $upgrade->getMessage());
     }
 
+    /**
+     * Casting to string must match getMessage(), so objects reaching a template still render.
+     *
+     * @dataProvider upgradeMessages
+     */
+    public function testUpgradeCastsToItsMessage(string $expectedMessage, Tiki_Version_Upgrade $upgrade, array $ltsMockData = []): void
+    {
+        $this->setupMockTWV($ltsMockData);
+
+        $this->assertEquals($expectedMessage, (string) $upgrade);
+    }
+
     public static function upgradeMessages()
     {
         $providerLink = '<a href="https://tiki.org/Extended-Security-Maintenance" target="_blank" class="alert-link">' . tra("Official Service Providers") . '</a>';



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/460a4a8cf0825f5cb97fff12d848a573de1fb908

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/460a4a8cf0825f5cb97fff12d848a573de1fb908
You're receiving this email because of your account on gitlab.com. Manage all notifications: https://gitlab.com/-/profile/notifications | Help: https://gitlab.com/help

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.