N°7726 - Fatal error if a newsroom is sent without any message

This commit is contained in:
XavierGR
2024-11-14 09:06:41 +01:00
committed by XGUI
parent 5d8db176f4
commit 6cb1cf7b7e
19 changed files with 241 additions and 8 deletions

View File

@@ -355,14 +355,30 @@
}
}
if ($bIsAsync === true) {
AsyncSendNewsroom::AddToQueue($this->GetKey(), $oTrigger->GetKey(), $aRecipientsIds, $sMessage, $sTitle, $sUrl, $iObjectId, $sObjectClass);
} else {
foreach ($aRecipientsIds as $iRecipientId) {
$oEvent = Combodo\iTop\Service\Notification\Event\EventNotificationNewsroomService::MakeEventFromAction($this, $iRecipientId, $oTrigger->GetKey(), $sMessage, $sTitle, $sUrl, $iObjectId, $sObjectClass);
$oEvent->DBInsertNoReload();
}
}
try {
if ($bIsAsync === true) {
AsyncSendNewsroom::AddToQueue($this->GetKey(), $oTrigger->GetKey(), $aRecipientsIds, $sMessage, $sTitle, $sUrl, $iObjectId, $sObjectClass);
} else {
foreach ($aRecipientsIds as $iRecipientId) {
$oEvent = Combodo\iTop\Service\Notification\Event\EventNotificationNewsroomService::MakeEventFromAction($this, $iRecipientId, $oTrigger->GetKey(), $sMessage, $sTitle, $sUrl, $iObjectId, $sObjectClass);
$oEvent->DBInsertNoReload();
}
}
} catch (CoreCannotSaveObjectException $e) {
ExceptionLog::LogException($e);
foreach($aRecipientsIds as $iRecipientId) {
$oEvent = Combodo\iTop\Service\Notification\Event\EventNotificationNewsroomService::MakeEventFromAction($this,
$iRecipientId,
$oTrigger->GetKey(),
Dict::S('Core:EventNotificationNewsroom:ErrorOnDBInsert'),
Dict::S('Core:EventNotificationNewsroom:ErrorNotificationNotSent'),
$sUrl,
$iObjectId,
$sObjectClass
);
$oEvent->DBInsertNoReload();
}
}
$this->SetNotificationLanguage($sPreviousLanguage, $aPreviousPluginProperties['language_code'] ?? null);
}

View File

@@ -807,6 +807,8 @@ Je-li čas vynechán, bude nastaveno 00:00:00
'Core:Duration_Hours_Minutes_Seconds' => '%1$dh %2$dmin %3$ds',
'Core:Duration_Minutes_Seconds' => '%1$dmin %2$ds',
'Core:Duration_Seconds' => '%1$ds',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent~~',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification~~',
'Core:ExecProcess:Code1' => 'Chybní příkaz, nebo příkaz skončil s chybou (např chybný název skriptu)',
'Core:ExecProcess:Code255' => 'PHP Error (parsing/runtime)',
'Core:ExplainWTC:ElapsedTime' => 'Uplynulý čas ("%1$s")',

View File

@@ -806,6 +806,8 @@ If the time is omitted, it defaults to 00:00:00
'Core:Duration_Hours_Minutes_Seconds' => '%1$dh %2$dmin %3$ds',
'Core:Duration_Minutes_Seconds' => '%1$dmin %2$ds',
'Core:Duration_Seconds' => '%1$ds',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent~~',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification~~',
'Core:ExecProcess:Code1' => 'Forkert kommando eller kommandoen afsluttede med fejl (f.eks. forkert script navn)',
'Core:ExecProcess:Code255' => 'PHP Error (parsing, or runtime)',
'Core:ExplainWTC:ElapsedTime' => 'Time elapsed (stored as "%1$s")~~',

View File

@@ -808,6 +808,8 @@ Falls der Zeit-Wert weggelassen wird, ist der Standardwert 00:00:00
'Core:Duration_Hours_Minutes_Seconds' => '%1$dh %2$dmin %3$ds',
'Core:Duration_Minutes_Seconds' => '%1$dmin %2$ds',
'Core:Duration_Seconds' => '%1$ds',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent~~',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification~~',
'Core:ExecProcess:Code1' => 'Falscher Befehl oder Befehl mit Fehler beendet (z.B. falscher Skriptname).',
'Core:ExecProcess:Code255' => 'PHP-Fehler (Parsing oder Laufzeit)',
'Core:ExplainWTC:ElapsedTime' => 'Abgelaufene Zeit (gespeichert als \"%1$s\")',

View File

@@ -514,6 +514,8 @@ Dict::Add('EN US', 'English', 'English', array(
'Class:EventNotificationNewsroom/Attribute:read_date+' => '',
'Class:EventNotificationNewsroom/Attribute:contact_id' => 'Contact',
'Class:EventNotificationNewsroom/Attribute:contact_id+' => '',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification',
));
//

View File

@@ -792,6 +792,8 @@ Si se omite el tiempo, por omisión es 00:00:00
'Core:Duration_Hours_Minutes_Seconds' => '%1$dh %2$dmin %3$ds',
'Core:Duration_Minutes_Seconds' => '%1$dmin %2$ds',
'Core:Duration_Seconds' => '%1$ds',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent~~',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification~~',
'Core:ExecProcess:Code1' => 'Comando equivocado o comando terminó con errores (ejem. nombre incorrecto de script)',
'Core:ExecProcess:Code255' => 'PHP Error (parsing, or runtime)',
'Core:ExplainWTC:ElapsedTime' => 'Tiempo transcurrido (almacenado como \"%1$s\")',

View File

@@ -835,6 +835,8 @@ Si l\'heure n\'est pas spécifiée, cela revient à 00:00:00 (minuit)
'Core:Duration_Hours_Minutes_Seconds' => '%1$dh %2$dmin %3$ds',
'Core:Duration_Minutes_Seconds' => '%1$dmin %2$ds',
'Core:Duration_Seconds' => '%1$ds',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification non envoyée',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'Une erreur est survenue lors de l\'envoi de la notification',
'Core:ExecProcess:Code1' => 'Mauvaise commande ou commande se finissant avec des erreurs (e.g. mauvais nom de script)',
'Core:ExecProcess:Code255' => 'Erreur PHP (analyse de fichier, ou exécution)',
'Core:ExplainWTC:ElapsedTime' => 'Temps écoulé (enregistré dans "%1$s")',

View File

@@ -801,6 +801,8 @@ Ha az időpontot nem adja meg, akkor az alapértelmezett értéke 00:00:00
'Core:Duration_Hours_Minutes_Seconds' => '%1$d óra %2$d perc %3$d mp',
'Core:Duration_Minutes_Seconds' => '%1$d perc %2$d mp',
'Core:Duration_Seconds' => '%1$d mp',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent~~',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification~~',
'Core:ExecProcess:Code1' => 'Helytelen parancs vagy hibásan befejezett parancs (pl. helytelen szkriptnév)',
'Core:ExecProcess:Code255' => 'PHP hiba (parsing, vagy runtime)',
'Core:ExplainWTC:ElapsedTime' => 'Eltelt idő (tárolva mint %1$s)',

View File

@@ -802,6 +802,8 @@ Se \'oraè omessa, di default è 00:00:00
'Core:Duration_Hours_Minutes_Seconds' => '%1$dh %2$dmin %3$sec',
'Core:Duration_Minutes_Seconds' => '%1$dmin %2$ds',
'Core:Duration_Seconds' => '%1$ds',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent~~',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification~~',
'Core:ExecProcess:Code1' => 'Comando errato o comando finito con errori (es. errato nome dello script)',
'Core:ExecProcess:Code255' => 'Errore PHP (parsing o runtime)',
'Core:ExplainWTC:ElapsedTime' => 'Tempo trascorso (memorizzato come \"%1$s\")',

View File

@@ -812,6 +812,8 @@ Operators:<br/>
'Core:ExplainWTC:StopWatch-TimeSpent' => 'Time spent for "%1$s"~~',
'Core:FriendlyName-Description' => 'Friendly name',
'Core:FriendlyName-Label' => 'Name',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent~~',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification~~',
'Core:Placeholder:CannotBeResolved' => '(%1$s : cannot be resolved)~~',
'Core:SyncDataExchangeComment' => '(データ同期)',
'Core:SyncDataSourceAccessRestriction' => '管理者またはデータ·ソースに指定されたユーザーのみ、この操作を実行することができます。操作はキャンセルされました。',

View File

@@ -804,6 +804,8 @@ Hier wordt standaard \'00:00:00\' van gemaakt als er geen tijd wordt opgegeven.
'Core:Duration_Hours_Minutes_Seconds' => '%1$dh %2$dmin %3$ds',
'Core:Duration_Minutes_Seconds' => '%1$dmin %2$ds',
'Core:Duration_Seconds' => '%1$ds',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent~~',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification~~',
'Core:ExecProcess:Code1' => 'Verkeerde commando of commando beëindigd met fouten (bijvoorbeeld verkeerde scriptnaam)',
'Core:ExecProcess:Code255' => 'PHP Error (parsing, of runtime)',
'Core:ExplainWTC:ElapsedTime' => 'Tijd voorbij (opgeslagen als "%1$s")',

View File

@@ -803,6 +803,8 @@ Jeśli czas zostanie pominięty, domyślnie to 00:00:00
'Core:Duration_Hours_Minutes_Seconds' => '%1$dh %2$dmin %3$ds',
'Core:Duration_Minutes_Seconds' => '%1$dmin %2$ds',
'Core:Duration_Seconds' => '%1$ds',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent~~',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification~~',
'Core:ExecProcess:Code1' => 'Niewłaściwe polecenie lub polecenie zakończone błędami (np. zła nazwa skryptu)',
'Core:ExecProcess:Code255' => 'Błąd PHP (parsowanie lub środowisko uruchomieniowe)',
'Core:ExplainWTC:ElapsedTime' => 'Czas, który upłynął (przechowywany jako "%1$s")',

View File

@@ -801,6 +801,8 @@ Se o tempo for omitido, o padrão é 00:00:00
'Core:Duration_Hours_Minutes_Seconds' => '%1$dh %2$dmin %3$ds',
'Core:Duration_Minutes_Seconds' => '%1$dmin %2$ds',
'Core:Duration_Seconds' => '%1$ds',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent~~',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification~~',
'Core:ExecProcess:Code1' => 'Comando incorreto ou comando terminou com erros (por exemplo, nome do script errado)',
'Core:ExecProcess:Code255' => 'Erro PHP (parsing, ou runtime)',
'Core:ExplainWTC:ElapsedTime' => 'Tempo decorrido (armazenado como \"%1$s\")',

View File

@@ -806,6 +806,8 @@ If omitted the From (label) is used.~~',
'Core:Duration_Hours_Minutes_Seconds' => '%1$d ч %2$d мин %3$d с',
'Core:Duration_Minutes_Seconds' => '%1$d мин %2$d с',
'Core:Duration_Seconds' => '%1$d с',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent~~',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification~~',
'Core:ExecProcess:Code1' => 'Неверная команда или команда завершена с ошибкой (возможно, неверное имя скрипта)',
'Core:ExecProcess:Code255' => 'Ошибка PHP (parsing, or runtime)',
'Core:ExplainWTC:ElapsedTime' => 'Time elapsed (stored as "%1$s")',

View File

@@ -825,6 +825,8 @@ Ak je čas vynechaný, predvolený čas je 00:00:00
'Core:Duration_Hours_Minutes_Seconds' => '%1$dh %2$dmin %3$ds',
'Core:Duration_Minutes_Seconds' => '%1$dmin %2$ds',
'Core:Duration_Seconds' => '%1$ds',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent~~',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification~~',
'Core:ExecProcess:Code1' => 'Nesprávny príkaz alebo príkaz skončil s chybami (napr. nesprávne meno skriptu)',
'Core:ExecProcess:Code255' => 'PHP chyba (pri parsovaní, alebo počas behu)',
'Core:ExplainWTC:ElapsedTime' => 'Time elapsed (stored as "%1$s")~~',

View File

@@ -806,6 +806,8 @@ If the time is omitted, it defaults to 00:00:00
'Core:Duration_Hours_Minutes_Seconds' => '%1$dh %2$dmin %3$ds',
'Core:Duration_Minutes_Seconds' => '%1$dmin %2$ds',
'Core:Duration_Seconds' => '%1$ds',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent~~',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification~~',
'Core:ExecProcess:Code1' => 'Yanlış komut veya komut hataları ile bitti (örneğin, yanlış senaryo adı)',
'Core:ExecProcess:Code255' => 'PHP hatası (ayrıştırma veya çalışma zamanı)',
'Core:ExplainWTC:ElapsedTime' => 'Geçen zaman (\"%1$s\" olarak saklanır)',

View File

@@ -822,6 +822,8 @@ Operators:<br/>
'Core:Duration_Hours_Minutes_Seconds' => '%1$dh %2$dmin %3$ds',
'Core:Duration_Minutes_Seconds' => '%1$dmin %2$ds',
'Core:Duration_Seconds' => '%1$ds',
'Core:EventNotificationNewsroom:ErrorNotificationNotSent' => 'Notification not sent~~',
'Core:EventNotificationNewsroom:ErrorOnDBInsert' => 'An error occurred while saving the notification~~',
'Core:ExecProcess:Code1' => '命令错误或命令执行出错 (例如错误的脚本名称)',
'Core:ExecProcess:Code255' => 'PHP错误 (解析, 或运行时)',
'Core:ExplainWTC:ElapsedTime' => '耗时 (储存为 "%1$s")',

View File

@@ -18,6 +18,8 @@ use CMDBObject;
use CMDBSource;
use Combodo\iTop\Service\Events\EventService;
use Contact;
use CoreException;
use CoreUnexpectedValue;
use DBObject;
use DBObjectSet;
use DBSearch;
@@ -29,6 +31,9 @@ use lnkContactToFunctionalCI;
use lnkContactToTicket;
use lnkFunctionalCIToTicket;
use MetaModel;
use MissingQueryArgument;
use MySQLException;
use MySQLHasGoneAwayException;
use Person;
use PluginManager;
use Server;
@@ -1439,4 +1444,24 @@ abstract class ItopDataTestCase extends ItopTestCase
self::markTestSkipped("Test skipped: module '$sModule' is not present");
}
}
/**
* @throws CoreException
* @throws CoreUnexpectedValue
* @throws ArchivedObjectException
* @throws MissingQueryArgument
* @throws MySQLException
* @throws MySQLHasGoneAwayException
* @throws Exception
*/
protected function AssertUniqueObjectInDB(string $sClass, array $aCriteria, string $sMessage = ''): void
{
$oSearch = new \DBObjectSearch($sClass);
foreach($aCriteria as $sAttCode => $value)
{
$oSearch->AddCondition($sAttCode, $value);
}
$oSet = new DBObjectSet($oSearch);
$this->assertEquals(1, $oSet->Count(), $sMessage);
}
}

View File

@@ -0,0 +1,160 @@
<?php
// Copyright (c) 2010-2024 Combodo SAS
//
// This file is part of iTop.
//
// iTop is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// iTop is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with iTop. If not, see <http://www.gnu.org/licenses/>
//
namespace Combodo\iTop\Test\UnitTest\Core;
use ArchivedObjectException;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use CoreException;
use CoreUnexpectedValue;
use DBObjectSearch;
use DBObjectSet;
use Exception;
use MetaModel;
use MissingQueryArgument;
use MySQLException;
use MySQLHasGoneAwayException;
class ActionNewsroomTest extends ItopDataTestCase
{
const CREATE_TEST_ORG = true;
private int $iTrigger;
private int $iRecipientId;
protected function setUp(): void
{
parent::setUp();
$this->iTrigger = $this->GivenObjectInDB('TriggerOnObjectCreate',
array(
'description' => '[TEST] TriggerOnObjectCreate',
'target_class' => 'UserRequest',
)
);
$this->iRecipientId = $this->GivenPersonInDB(1);
}
private function ActionNewsroomProvider(): array
{
return [
'With Sync ActionNewsroom' => ['bIsAsynchronous' => false],
'With Async ActionNewsroom' => ['bIsAsynchronous' => true]
];
}
/**
* @throws CoreException
* @throws MissingQueryArgument
* @throws CoreUnexpectedValue
* @throws ArchivedObjectException
* @throws MySQLException
* @throws MySQLHasGoneAwayException
* @throws Exception
*/
public function testActionNewsroomRecordsEvent()
{
$iActionNewsroomId = $this->GivenActionNewsroomInDB(false, 'Body of the notification');
$this->CreateUserRequest(1,
[
'title' => '[TEST] ActionNewsroom',
'org_id' => $this->getTestOrgId(),
'caller_id' => $this->iRecipientId,
'description' => 'PHPUnit Test',
]
);
$this->AssertUniqueObjectInDB(
'EventNotificationNewsroom',
[
'action_id' => $iActionNewsroomId,
'message' => 'Body of the notification',
'title' => 'Title'
]
);
}
/**
* @dataProvider ActionNewsroomProvider
* @return void
* @throws Exception
*/
public function testActionNewsroomRecordsSpecificEventIfAMandatoryFieldIsEmpty(bool $bIsAsynchronous)
{
$iActionNewsroomId = $this->GivenActionNewsroomInDB($bIsAsynchronous, '$this->service_name$');
$this->CreateUserRequest(1,
[
'title' => '[TEST] ActionNewsroom',
'org_id' => $this->getTestOrgId(),
'caller_id' => $this->iRecipientId,
'description' => 'PHPUnit Test',
'service_id' => 0
]
);
$this->AssertUniqueObjectInDB(
'EventNotificationNewsroom',
[
'action_id' => $iActionNewsroomId,
'title' => 'Notification not sent',
'message' => 'An error occurred while saving the notification'
]
);
}
/**
* @throws Exception
*/
private function GivenActionNewsroomInDB(bool $bIsAsynchronous, string $sMessage): int
{
$this->GivenObjectInDB('UserLocal', [
'login' => 'demo_test_'.uniqid(__CLASS__, true),
'password' => 'admin_123',
'language' => 'EN US',
'profile_list' => ['profileid:'.self::$aURP_Profiles['Administrator']],
'contactid' => $this->iRecipientId
]);
return $this->GivenObjectInDB('ActionNewsroom', [
'name' => 'ActionNewsroom TriggerOnObjectCreate',
'status' => 'test',
'test_recipient_id' => $this->iRecipientId,
'title' => 'Title',
'message' => $sMessage,
'recipients' => "SELECT Person WHERE id = $this->iRecipientId",
'asynchronous' => $bIsAsynchronous ? 'yes' : 'no',
'trigger_list' => [
"trigger_id:$this->iTrigger"
],
]);
}
/**
* @throws Exception
*/
private function GivenService(string $sName): int
{
return $this->GivenObjectInDB('Service', [
'name' => $sName,
'org_id' => $this->getTestOrgId()
]
);
}
}