diff --git a/testing/tests/NotificationsTest.php b/testing/tests/NotificationsTest.php index 931454e6c40bb425c3e9c1177020392bccb6ed88..067ad3244fb7b91ce19a92f87d1420e934b3a2cd 100644 --- a/testing/tests/NotificationsTest.php +++ b/testing/tests/NotificationsTest.php @@ -255,4 +255,96 @@ class NotificationsTest extends TestCase $result = $notifications->processEndPointPost(); $this->assertEmpty($result); } + + public function testProcessNotificationsOneTaskToProcessOneMatchingAttributes () + { + $taskData = [ + 'status' => 1, + 'fdtasksgranularmaster' => ['main task'], + 'maintask' => [ + [ + 'fdtasksnotificationslistofrecipientsmails' => ['abc@yahoo.com', 'def@gmail.com'], + 'fdtasksnotificationsemailsender' => ['sender@fd.com', 'sender@email.com'], + 'fdtasksnotificationsmailtemplate' => ['template1'], + 'mailInfos' => [], + 'fdtasksnotificationsattributes' => ['resource2', 'resource3'], + 'fdtasksnotificationsresource' => ['test resource'], + 'fdtasksnotificationsstate' => ['test state'], + 'fdtasksnotificationssubstate' => ['test substate'], + ] + ], + 'fdtasksgranularref' => ['resource1'], + 'fdtasksgranulardn' => ['granulardn'], + 'dn' => 'testdn', + 'cn' => ['mycn'], + 'monitoredattrs' => [ + [ + 'fdauditattributes' => [json_encode(['resource2' => 'value2'])], + ] + ] + ]; + $taskGateway = $this->createMock(TaskGateway::class); + $taskGateway->expects($this->once())->method('statusAndScheduleCheck')->willReturnCallback(function ($task) { + return $task['status'] == 1; + }); + $taskGateway->expects($this->exactly(2))->method('getLdapTasks')->willReturnCallback(function ($filter, $attrs, $attachmentsCN, $dn) use ($taskData) { + if ($dn == 'resource1') { + return $taskData['monitoredattrs']; + } + return $taskData['maintask'][0]['mailInfos']; + }); + $notifications = $this->getMockBuilder(Notifications::class)->setConstructorArgs([$taskGateway])->onlyMethods(['getNotificationsMainTask', 'sendNotificationsMail'])->getMock(); + $notifications->expects($this->once())->method('getNotificationsMainTask')->willReturn($taskData['maintask']); + $notifications->expects($this->once())->method('sendNotificationsMail')->willReturn([TRUE]); + $task = new NotificationsTask($taskData); + $result = $notifications->processNotifications([$task->toArray()]); + $expectedOutput = [[TRUE]]; + $this->assertEquals($expectedOutput, $result); + } + + public function testProcessNotificationsOneTaskToProcessSupannMatchingAttributes () + { + $taskData = [ + 'status' => 1, + 'fdtasksgranularmaster' => ['main task'], + 'maintask' => [ + [ + 'fdtasksnotificationslistofrecipientsmails' => ['abc@yahoo.com', 'def@gmail.com'], + 'fdtasksnotificationsemailsender' => ['sender@fd.com', 'sender@email.com'], + 'fdtasksnotificationsmailtemplate' => ['template1'], + 'mailInfos' => [], + 'fdtasksnotificationsattributes' => ['resource2', 'resource3'], + 'fdtasksnotificationsresource' => ['test resource'], + 'fdtasksnotificationsstate' => ['test state'], + 'fdtasksnotificationssubstate' => ['test substate'], + ] + ], + 'fdtasksgranularref' => ['resource1'], + 'fdtasksgranulardn' => ['granulardn'], + 'dn' => 'testdn', + 'cn' => ['mycn'], + 'monitoredattrs' => [ + [ + 'fdauditattributes' => [json_encode(['resource2' => '{test resource}test state:test substate'])], + ] + ] + ]; + $taskGateway = $this->createMock(TaskGateway::class); + $taskGateway->expects($this->once())->method('statusAndScheduleCheck')->willReturnCallback(function ($task) { + return $task['status'] == 1; + }); + $taskGateway->expects($this->exactly(2))->method('getLdapTasks')->willReturnCallback(function ($filter, $attrs, $attachmentsCN, $dn) use ($taskData) { + if ($dn == 'resource1') { + return $taskData['monitoredattrs']; + } + return $taskData['maintask'][0]['mailInfos']; + }); + $notifications = $this->getMockBuilder(Notifications::class)->setConstructorArgs([$taskGateway])->onlyMethods(['getNotificationsMainTask', 'sendNotificationsMail'])->getMock(); + $notifications->expects($this->once())->method('getNotificationsMainTask')->willReturn($taskData['maintask']); + $notifications->expects($this->once())->method('sendNotificationsMail')->willReturn([TRUE]); + $task = new NotificationsTask($taskData); + $result = $notifications->processNotifications([$task->toArray()]); + $expectedOutput = [[TRUE]]; + $this->assertEquals($expectedOutput, $result); + } } \ No newline at end of file