/usr/share/php/tests/Horde_Alarm/Horde/Alarm/HandlerTest.php is in php-horde-alarm 2.0.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | <?php
/**
* @author Jan Schneider <jan@horde.org>
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @category Horde
* @package Alarm
* @subpackage UnitTests
*/
class Horde_Alarm_HandlerTest extends PHPUnit_Framework_TestCase
{
protected static $alarm;
protected static $storage;
protected static $mail;
public function setUp()
{
if (!class_exists('Horde_Notification')) {
$this->markTestSkipped('Horde_Notification not installed');
return;
}
if (!class_exists('Horde_Mail_Transport_Mock')) {
$this->markTestSkipped('Horde_Mail not installed');
return;
}
self::$alarm = new Horde_Alarm_Object();
$now = time();
$hash = array('id' => 'personalalarm',
'user' => 'john',
'start' => new Horde_Date($now),
'end' => new Horde_Date($now + 3600),
'methods' => array(),
'params' => array(),
'title' => 'This is a personal alarm.',
'text' => 'Action is required.');
self::$alarm->set($hash);
self::$storage = new Horde_Notification_Storage_Object();
$notification = new Horde_Alarm_HandlerTest_NotificationFactory(self::$storage);
$handler = new Horde_Alarm_Handler_Notify(array('notification' => $notification));
self::$alarm->addHandler('notify', $handler);
$handler = new Horde_Alarm_Handler_Desktop(array('js_notify' => array($this, 'desktopCallback'), 'icon' => 'test.png'));
self::$alarm->addHandler('desktop', $handler);
self::$mail = new Horde_Mail_Transport_Mock();
$factory = new Horde_Alarm_HandlerTest_IdentityFactory();
$handler = new Horde_Alarm_Handler_Mail(array('mail' => self::$mail, 'identity' => $factory, 'charset' => 'us-ascii'));
self::$alarm->addHandler('mail', $handler);
}
public function testNotify()
{
$alarm = self::$alarm->get('personalalarm', 'john');
$alarm['methods'] = array('notify');
self::$alarm->set($alarm);
self::$alarm->notify('john', false);
$this->assertEquals(1, count(self::$storage->notifications['_unattached']));
$this->assertEquals('This is a personal alarm.', self::$storage->notifications['_unattached'][0]->message);
$this->assertEquals('horde.alarm', self::$storage->notifications['_unattached'][0]->type);
}
public function testMail()
{
$header =
'Subject: This is a personal alarm.
To: john@example.com
From: john@example.com
Auto-Submitted: auto-generated
X-Horde-Alarm: This is a personal alarm.
Message-ID: <%d.Horde.%s@%s>
User-Agent: Horde Application Framework 5
Date: %s, %d %s %s %d:%d:%d %s%d
Content-Type: text/plain; charset=UTF-8; format=flowed; DelSp=Yes
MIME-Version: 1.0';
$body = "Action is required.\n";
$alarm = self::$alarm->get('personalalarm', 'john');
$alarm['methods'] = array('mail');
self::$alarm->set($alarm);
self::$alarm->notify('john', false);
$last_sent = end(self::$mail->sentMessages);
$this->assertStringMatchesFormat(
$header,
trim(str_replace("\r\n", "\n", $last_sent['header_text']))
);
$this->assertEquals($body, $last_sent['body']);
self::$mail->sentMessages = array();
self::$alarm->notify('john', false);
$this->assertEquals(self::$mail->sentMessages, array());
/* Test re-sending mails after changing the alarm. */
self::$alarm->set(self::$alarm->get('personalalarm', 'john'));
self::$alarm->notify('john', false);
$last_sent = end(self::$mail->sentMessages);
$this->assertStringMatchesFormat(
$header,
trim(str_replace("\r\n", "\n", $last_sent['header_text']))
);
$this->assertEquals($body, $last_sent['body']);
}
public function testDesktop()
{
$alarm = self::$alarm->get('personalalarm', 'john');
$alarm['methods'] = array('desktop');
self::$alarm->set($alarm);
self::$alarm->notify('john', false);
}
public function desktopCallback($js)
{
$this->assertEquals(
"if(window.webkitNotifications)(function(){function show(){switch(window.webkitNotifications.checkPermission()){case 0:var notify=window.webkitNotifications.createNotification(\"test.png\",\"This is a personal alarm.\",\"Action is required.\");notify.show();(function(){notify.cancel()}).delay(5);break;case 1:window.webkitNotifications.requestPermission(function(){});break}}show()})()",
$js);
}
}
class Horde_Alarm_HandlerTest_IdentityFactory
{
public function create()
{
return new Horde_Alarm_HandlerTest_Identity();
}
}
class Horde_Alarm_HandlerTest_Identity
{
public function getDefaultFromAddress()
{
return 'john@example.com';
}
}
class Horde_Alarm_HandlerTest_NotificationFactory
{
private $storage;
public function __construct($storage)
{
$this->storage = $storage;
}
public function create()
{
return new Horde_Notification_Handler($this->storage);
}
}
|