/**
* @group InstantNotificationsService
*/
public function testCanNotifyUserWithoutAnObject()
{
$from = $this->mocks()->getUser();
$to1 = $this->mocks()->getUser();
$to2 = $this->mocks()->getUser();
create_metadata($to2->guid, 'notification:method:test_method', true, '', $to2->guid, ACCESS_PUBLIC);
$to3 = $this->mocks()->getUser();
$subject = 'Test message';
$body = 'Lorem ipsum';
$event = new InstantNotificationEvent(null, null, $from);
$this->hooks->registerHandler('get', 'subscriptions', function ($hook, $type, $return) use($to3) {
$return[$to3->guid] = ['test_method'];
return $return;
});
$this->hooks->registerHandler('prepare', 'notification', function ($hook, $type, $notification) {
$notification->prepare_hook = true;
return $notification;
});
$this->hooks->registerHandler('prepare', "notification:{$event->getDescription()}", function ($hook, $type, $notification) {
$notification->granular_prepare_hook = true;
return $notification;
});
$this->hooks->registerHandler('format', 'notification:test_method', function ($hook, $type, $notification) {
$notification->format_hook = true;
return $notification;
});
$sent = 0;
$this->hooks->registerHandler('send', 'notification:test_method', function ($hook, $type, $return, $params) use(&$sent, $subject, $body, $event) {
$sent++;
$notification = $params['notification'];
$this->assertInstanceOf(Notification::class, $notification);
$this->assertEquals($notification->subject, $subject);
$this->assertEquals($notification->body, $body);
$this->assertEquals($event, $params['event']);
$this->assertTrue($notification->prepare_hook);
$this->assertTrue($notification->granular_prepare_hook);
$this->assertTrue($notification->format_hook);
return true;
});
$this->setupServices();
$this->notifications->registerMethod('test_method');
$this->notifications->registerMethod('test_method2');
$expected = [$to2->guid => ['test_method' => true], $to3->guid => ['test_method' => true]];
$this->assertEquals($expected, notify_user([$to1->guid, $to2->guid, 0], $from->guid, $subject, $body));
$this->assertEquals(2, $sent);
}