diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d51dee..fe36311 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +- Force closed alerts display after alert update. - Clean display preferences and profile rights table on uninstall ## [1.14.0] - 2026-04-30 diff --git a/composer.json b/composer.json index 8338bce..945d30f 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ }, "autoload-dev": { "psr-4": { - "Glpi\\Tools\\": "../../tools/src/" + "Glpi\\Tools\\": "../../tools/src/", + "GlpiPlugin\\News\\Tests\\": "tests/" } } } diff --git a/inc/alert.class.php b/inc/alert.class.php index a965f63..e9e5188 100644 --- a/inc/alert.class.php +++ b/inc/alert.class.php @@ -227,28 +227,23 @@ public function rawSearchOptions() public function post_updateItem($history = true) { - // if close is not allowed update all user alerts to force display - if ( - isset($this->input['is_close_allowed']) - && !$this->input['is_close_allowed'] - ) { - $alert_user = new PluginNewsAlert_User(); - //get all Alert_User for this alert where state is hidden - $all_alert = $alert_user->find( + // force display of alerts after update regardless of their closable state + $alert_user = new PluginNewsAlert_User(); + //get all Alert_User for this alert where state is hidden + $all_alert = $alert_user->find( + [ + 'plugin_news_alerts_id' => $this->getID(), + 'state' => PluginNewsAlert_User::HIDDEN, + ], + ); + foreach ($all_alert as $alert) { + //update state to force display + $alert_user->update( [ - 'plugin_news_alerts_id' => $this->getID(), - 'state' => PluginNewsAlert_User::HIDDEN, + 'id' => $alert['id'], + 'state' => PluginNewsAlert_User::VISIBLE, ], ); - foreach ($all_alert as $alert) { - //update state to force display - $alert_user->update( - [ - 'id' => $alert['id'], - 'state' => PluginNewsAlert_User::VISIBLE, - ], - ); - } } } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..cd6c7b3 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,11 @@ + + + + tests + + + diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..aed262b --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,44 @@ +. + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2015-2023 by News plugin team. + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html + * @link https://github.com/pluginsGLPI/news + * ------------------------------------------------------------------------- + */ + +require __DIR__ . '/../../../tests/bootstrap.php'; +require dirname(__DIR__) . '/vendor/autoload.php'; + +$plugin = new Plugin(); +$plugin->checkPluginState('news'); +$plugin->getFromDBbyDir('news'); + +if (!$plugin->isInstalled('news')) { + $plugin->install($plugin->getID()); +} + +if (!$plugin->isActivated('news')) { + $plugin->activate($plugin->getID()); +} diff --git a/tests/units/PluginNewsAlertTest.php b/tests/units/PluginNewsAlertTest.php new file mode 100644 index 0000000..0ef3929 --- /dev/null +++ b/tests/units/PluginNewsAlertTest.php @@ -0,0 +1,233 @@ +. + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2015-2023 by News plugin team. + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html + * @link https://github.com/pluginsGLPI/news + * ------------------------------------------------------------------------- + */ + +namespace GlpiPlugin\News\Tests\Units; + +use Glpi\Tests\DbTestCase; +use PluginNewsAlert; +use PluginNewsAlert_User; +use User; + +class PluginNewsAlertTest extends DbTestCase +{ + private function getAlertUserState(int $au_id): int + { + $alert_user = new PluginNewsAlert_User(); + $this->assertTrue($alert_user->getFromDB($au_id)); + return (int) $alert_user->fields['state']; + } + + public function testPostUpdateItemResetsHiddenAlerts(): void + { + $this->login('glpi'); + + $alert = $this->createItem( + PluginNewsAlert::class, + [ + 'name' => 'closable Alert', + 'message' => 'This is a closable alert', + 'type' => 1, + 'is_displayed_onlogin' => 1, + 'is_displayed_oncentral' => 1, + 'is_displayed_onservicecatalog' => 1, + 'display_dates' => 1, + 'background_color' => 'white', + 'emphasis_color' => 'dark', + 'size' => 'medium', + 'icon' => 'settings', + 'is_displayed_onhelpdesk' => 1, + 'is_active' => 1, + 'entities_id' => 0, + 'is_close_allowed' => 1, + ], + ); + $alert_id = $alert->getID(); + + $user_1 = $this->createItem( + User::class, + [ + 'name' => 'user 1', + 'password' => 'test', + 'password2' => 'test', + ], + ['password', 'password2'], + ); + + $user_2 = $this->createItem( + User::class, + [ + 'name' => 'user 2', + 'password' => 'test', + 'password2' => 'test', + ], + ['password', 'password2'], + ); + + $user_3 = $this->createItem( + User::class, + [ + 'name' => 'user 3', + 'password' => 'test', + 'password2' => 'test', + ], + ['password', 'password2'], + ); + + $user_1_id = $user_1->getID(); + $user_2_id = $user_2->getID(); + $user_3_id = $user_3->getID(); + + $alert_user_1 = $this->createItem(PluginNewsAlert_User::class, [ + 'plugin_news_alerts_id' => $alert_id, + 'users_id' => $user_1_id, + 'state' => PluginNewsAlert_User::HIDDEN, + ]); + + $alert_user_2 = $this->createItem(PluginNewsAlert_User::class, [ + 'plugin_news_alerts_id' => $alert_id, + 'users_id' => $user_2_id, + 'state' => PluginNewsAlert_User::HIDDEN, + ]); + + //user 3 has not hidden the alert + $alert_user_3 = $this->createItem(PluginNewsAlert_User::class, [ + 'plugin_news_alerts_id' => $alert_id, + 'users_id' => $user_3_id, + 'state' => PluginNewsAlert_User::VISIBLE, + ]); + + $alert_user_1_id = $alert_user_1->getID(); + $alert_user_2_id = $alert_user_2->getID(); + $alert_user_3_id = $alert_user_3->getID(); + + $this->updateItem(PluginNewsAlert::class, $alert_id, ['name' => 'Alert with hidden users (updated)']); + + //assert that both users are now in VISIBLE state + $this->assertSame(PluginNewsAlert_User::VISIBLE, $this->getAlertUserState($alert_user_1_id)); + $this->assertSame(PluginNewsAlert_User::VISIBLE, $this->getAlertUserState($alert_user_2_id)); + + //assert that user 3 is still in VISIBLE state + $this->assertSame(PluginNewsAlert_User::VISIBLE, $this->getAlertUserState($alert_user_3_id)); + + //re-hide users 1 and 2 alerts + $this->updateItem(PluginNewsAlert_User::class, $alert_user_1_id, ['state' => PluginNewsAlert_User::HIDDEN]); + $this->updateItem(PluginNewsAlert_User::class, $alert_user_2_id, ['state' => PluginNewsAlert_User::HIDDEN]); + + //update the alert with is_close_allowed = 0 + $this->updateItem(PluginNewsAlert::class, $alert_id, ['is_close_allowed' => 0]); + + //assert that alerts are visible again + $this->assertSame(PluginNewsAlert_User::VISIBLE, $this->getAlertUserState($alert_user_1_id)); + $this->assertSame(PluginNewsAlert_User::VISIBLE, $this->getAlertUserState($alert_user_2_id)); + + //assert that user 3 is still in VISIBLE state + $this->assertSame(PluginNewsAlert_User::VISIBLE, $this->getAlertUserState($alert_user_3_id)); + } + + public function testPostUpdateItemDoesNotAffectOtherAlerts(): void + { + $this->login('glpi'); + + $alert_1 = $this->createItem( + PluginNewsAlert::class, + [ + 'name' => 'alert 1', + 'message' => 'This is a closable alert', + 'type' => 1, + 'is_displayed_onlogin' => 1, + 'is_displayed_oncentral' => 1, + 'is_displayed_onservicecatalog' => 1, + 'display_dates' => 1, + 'background_color' => 'white', + 'emphasis_color' => 'dark', + 'size' => 'medium', + 'icon' => 'settings', + 'is_displayed_onhelpdesk' => 1, + 'is_active' => 1, + 'entities_id' => 0, + 'is_close_allowed' => 1, + ], + ); + $alert_1_id = $alert_1->getID(); + + $alert_2 = $this->createItem( + PluginNewsAlert::class, + [ + 'name' => 'alert 2', + 'message' => 'This is a closable alert', + 'type' => 1, + 'is_displayed_onlogin' => 1, + 'is_displayed_oncentral' => 1, + 'is_displayed_onservicecatalog' => 1, + 'display_dates' => 1, + 'background_color' => 'white', + 'emphasis_color' => 'dark', + 'size' => 'medium', + 'icon' => 'settings', + 'is_displayed_onhelpdesk' => 1, + 'is_active' => 1, + 'entities_id' => 0, + 'is_close_allowed' => 1, + ], + ); + $alert_2_id = $alert_2->getID(); + + $user_1 = $this->createItem( + User::class, + [ + 'name' => 'user 1', + 'password' => 'test', + 'password2' => 'test', + ], + ['password', 'password2'], + ); + $user_1_id = $user_1->getID(); + + $alert_user_1 = $this->createItem(PluginNewsAlert_User::class, [ + 'plugin_news_alerts_id' => $alert_1_id, + 'users_id' => $user_1_id, + 'state' => PluginNewsAlert_User::HIDDEN, + ]); + + $alert_user_2 = $this->createItem(PluginNewsAlert_User::class, [ + 'plugin_news_alerts_id' => $alert_2_id, + 'users_id' => $user_1_id, + 'state' => PluginNewsAlert_User::HIDDEN, + ]); + + $alert_user_1_id = $alert_user_1->getID(); + $alert_user_2_id = $alert_user_2->getID(); + + $this->updateItem(PluginNewsAlert::class, $alert_1_id, ['name' => 'Alert 1 (updated)']); + + $this->assertSame(PluginNewsAlert_User::VISIBLE, $this->getAlertUserState($alert_user_1_id)); + $this->assertSame(PluginNewsAlert_User::HIDDEN, $this->getAlertUserState($alert_user_2_id)); + } +}