Skip to content

Commit

Permalink
feat: add administrator as default recipient in notification
Browse files Browse the repository at this point in the history
  • Loading branch information
MyvTsv authored and cedric-anne committed Jan 6, 2025
1 parent da45dff commit 1acbd83
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 4 deletions.
172 changes: 171 additions & 1 deletion install/empty_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -4382,7 +4382,177 @@ public function getEmptyData(): array
'items_id' => '37',
'type' => '1',
'notifications_id' => '40',
],
], [
'id' => '159',
'items_id' => '1',
'type' => '1',
'notifications_id' => '1',
], [
'id' => '160',
'items_id' => '1',
'type' => '1',
'notifications_id' => '12',
], [
'id' => '161',
'items_id' => '1',
'type' => '1',
'notifications_id' => '16',
], [
'id' => '162',
'items_id' => '1',
'type' => '1',
'notifications_id' => '17',
], [
'id' => '163',
'items_id' => '1',
'type' => '1',
'notifications_id' => '18',
], [
'id' => '164',
'items_id' => '1',
'type' => '1',
'notifications_id' => '20',
], [
'id' => '165',
'items_id' => '1',
'type' => '1',
'notifications_id' => '21',
], [
'id' => '166',
'items_id' => '1',
'type' => '1',
'notifications_id' => '22',
], [
'id' => '167',
'items_id' => '1',
'type' => '1',
'notifications_id' => '23',
], [
'id' => '168',
'items_id' => '1',
'type' => '1',
'notifications_id' => '24',
], [
'id' => '169',
'items_id' => '1',
'type' => '1',
'notifications_id' => '26',
], [
'id' => '170',
'items_id' => '1',
'type' => '1',
'notifications_id' => '27',
], [
'id' => '171',
'items_id' => '1',
'type' => '1',
'notifications_id' => '37',
], [
'id' => '172',
'items_id' => '1',
'type' => '1',
'notifications_id' => '38',
], [
'id' => '173',
'items_id' => '1',
'type' => '1',
'notifications_id' => '39',
], [
'id' => '174',
'items_id' => '1',
'type' => '1',
'notifications_id' => '50',
], [
'id' => '175',
'items_id' => '1',
'type' => '1',
'notifications_id' => '59',
], [
'id' => '176',
'items_id' => '1',
'type' => '1',
'notifications_id' => '60',
], [
'id' => '177',
'items_id' => '1',
'type' => '1',
'notifications_id' => '61',
], [
'id' => '178',
'items_id' => '1',
'type' => '1',
'notifications_id' => '62',
], [
'id' => '179',
'items_id' => '1',
'type' => '1',
'notifications_id' => '63',
], [
'id' => '180',
'items_id' => '1',
'type' => '1',
'notifications_id' => '64',
], [
'id' => '181',
'items_id' => '1',
'type' => '1',
'notifications_id' => '65',
], [
'id' => '183',
'items_id' => '1',
'type' => '1',
'notifications_id' => '67',
], [
'id' => '184',
'items_id' => '1',
'type' => '1',
'notifications_id' => '68',
], [
'id' => '185',
'items_id' => '1',
'type' => '1',
'notifications_id' => '69',
], [
'id' => '186',
'items_id' => '1',
'type' => '1',
'notifications_id' => '70',
], [
'id' => '187',
'items_id' => '1',
'type' => '1',
'notifications_id' => '71',
], [
'id' => '188',
'items_id' => '1',
'type' => '1',
'notifications_id' => '72',
], [
'id' => '189',
'items_id' => '1',
'type' => '1',
'notifications_id' => '74',
], [
'id' => '190',
'items_id' => '1',
'type' => '1',
'notifications_id' => '75',
], [
'id' => '191',
'items_id' => '1',
'type' => '1',
'notifications_id' => '80',
], [
'id' => '192',
'items_id' => '1',
'type' => '1',
'notifications_id' => '81',
], [
'id' => '193',
'items_id' => '1',
'type' => '1',
'notifications_id' => '82',
]
];

$tables['glpi_notificationtemplates'] = [
Expand Down
6 changes: 6 additions & 0 deletions phpunit/functional/NotificationTargetKnowbaseItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class NotificationTargetKnowbaseItemTest extends DbTestCase
{
public function testgetDataForNotifKnowbaseItem()
{
/** @var \DBmysql $DB */
global $DB;

$this->login();

Config::setConfigurationValues('core', ['use_notifications' => 1]);
Expand Down Expand Up @@ -94,6 +97,9 @@ public function testgetDataForNotifKnowbaseItem()

//test add group for notification
foreach ($knowbasenotifs as $kbnotif) {
// remove default targets
$DB->delete(NotificationTarget::getTable(), ['notifications_id' => $kbnotif['id']]);

$ntarget = $this->createItem(
NotificationTarget::class,
[
Expand Down
48 changes: 45 additions & 3 deletions phpunit/functional/NotificationTargetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@
use DbTestCase;
use Entity;
use Generator;
use Monolog\Logger;
use Notification;
use NotificationTarget;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Log\LogLevel;
use Session;

/* Test for inc/notificationtarget.class.php */

class NotificationTargetTest extends DbTestCase
{
public function testGetSubjectPrefix()
Expand Down Expand Up @@ -638,4 +637,47 @@ public function testGetTargetsWithExclusions()
$target = reset($targets);
$this->assertEquals(getItemByTypeName('User', TU_USER, true), $target['users_id']);
}

public function testDefaultTargets()
{
/** @var \DBmysql $DB */
global $DB;

$this->login();

$notification = new Notification();
$notifications = $notification->find();

foreach ($notifications as $notification) {
// Ensure that there is at least one default target
$iterator = $DB->request([
'FROM' => NotificationTarget::getTable(),
'WHERE' => ['notifications_id' => $notification['id']],
]);
$this->assertGreaterThan(0, count($iterator));

// Ensure that the Administrator is one of the default targets, unless it is not a valid target
// for the current notification.
$has_admin_target = true;
if ($notification['itemtype'] === 'PlanningRecall' && $notification['event'] === 'planningrecall') {
// Only the user is able to receive its planning recall
$has_admin_target = false;
} elseif ($notification['itemtype'] === 'ObjectLock' && $notification['event'] === 'unlock') {
// Only the user is able to receive notification of fields that he has unlocked
$has_admin_target = false;
} elseif ($notification['itemtype'] === 'User' && $notification['event'] === 'passwordforget') {
// Only the user is able to receive its password recovery token
$has_admin_target = false;
} elseif ($notification['itemtype'] === 'SavedSearch_Alert' && $notification['event'] === 'alert') {
// Only the user is able to receive its save search alert
$has_admin_target = false;
} elseif ($notification['itemtype'] === 'User' && $notification['event'] === 'passwordinit') {
// Only the user is able to receive its first password
$has_admin_target = false;
}
$notification_target = NotificationTarget::getInstanceByType($notification['itemtype'], $notification['event']);
$notification_target->addNotificationTargets(0);
$this->assertSame($has_admin_target, array_key_exists('1_1', $notification_target->notification_targets));
}
}
}
3 changes: 3 additions & 0 deletions src/NotificationTargetKnowbaseItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function getEvents()

public function addNotificationTargets($entity)
{
if (Session::haveRight("config", UPDATE)) {
$this->addTarget(Notification::GLOBAL_ADMINISTRATOR, __('Administrator'));
}
$this->addGroupsToTargets($entity);
}

Expand Down

0 comments on commit 1acbd83

Please sign in to comment.