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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
<?php
/**
* Test cases for Ingo_Script:: and derived classes
*
* $Horde: ingo/lib/tests/ScriptTest.php,v 1.1.2.3 2009/12/22 01:52:54 jan Exp $
*
* See the enclosed file LICENSE for license information (ASL). If you
* did not receive this file, see http://www.horde.org/licenses/asl.php.
*
* @author Jason M. Felice <jason.m.felice@gmail.com>
* @package Ingo
* @subpackage UnitTests
*/
require_once dirname(__FILE__) . '/TestBase.php';
require_once INGO_BASE . '/lib/Ingo.php';
require_once INGO_BASE . '/lib/Script.php';
require_once INGO_BASE . '/lib/Script/imap.php';
require_once INGO_BASE . '/lib/Storage.php';
class Ingo_ScriptTest extends Ingo_TestBase {
function test_blacklist_rule_without_folder_will_discard_matching_message()
{
$runner = ScriptTester::factory('all', $this);
$ob = new Ingo_Storage_blacklist();
$ob->setBlacklist(array('spammer@example.com'));
$ob->setBlacklistFolder('');
$runner->addRule($ob);
$runner->assertDeletesMessage('from_spammer');
$runner->assertKeepsMessage('not_from_spammer');
}
function test_whitelist_rule_will_prevent_deletion_of_blacklisted_message()
{
$runner = ScriptTester::factory('all', $this);
$bl = new Ingo_Storage_blacklist();
$bl->setBlacklist(array('spammer@example.com'));
$bl->setBlacklistFolder('');
$runner->addRule($bl);
$wl = new Ingo_Storage_whitelist();
$wl->setWhitelist(array('spammer@example.com'));
$runner->addRule($wl);
$runner->assertKeepsMessage('from_spammer');
$runner->assertKeepsMessage('not_from_spammer');
}
function test_blacklist_rule_with_folder_will_move_matching_messages()
{
$runner = ScriptTester::factory('all', $this);
$ob = new Ingo_Storage_blacklist();
$ob->setBlacklist(array('spammer@example.com'));
$ob->setBlacklistFolder('Junk');
$runner->addRule($ob);
$runner->assertMovesMessage('from_spammer', 'Junk');
}
function test_partial_whitelist_address_should_not_match()
{
$runner = ScriptTester::factory('all', $this);
$bl = new Ingo_Storage_blacklist();
$bl->setBlacklist(array('spammer@example.com'));
$bl->setBlacklistFolder('');
$runner->addRule($bl);
$wl = new Ingo_Storage_whitelist();
$wl->setWhitelist(array('ammer@example.com'));
$runner->addRule($wl);
$runner->assertDeletesMessage('from_spammer');
}
function test_partial_blacklist_address_should_not_match()
{
$runner = ScriptTester::factory('all', $this);
$bl = new Ingo_Storage_blacklist();
$bl->setBlacklist(array('ammer@example.com'));
$bl->setBlacklistFolder('');
$runner->addRule($bl);
$runner->assertKeepsMessage('from_spammer');
}
}
/**
* Abstract base class for strategies for testing different Script backends
*/
class ScriptTester {
var $test;
var $rules = array();
function ScriptTester($test)
{
$this->test = $test;
}
function addRule($rule)
{
$this->rules[] = $rule;
}
function assertDeletesMessage($fixture)
{
return PEAR::raiseError('Not implemented.');
}
function assertKeepsMessage($fixture)
{
return PEAR::raiseError('Not implemented.');
}
function assertMovesMessage($fixture, $to_folder)
{
return PEAR::raiseError('Not implemented.');
}
function factory($type, $test)
{
$class = 'ScriptTester_' . $type;
$ob = new $class($test);
return $ob;
}
function _setupStorage()
{
$_SESSION['ingo']['change'] = 0;
$GLOBALS['ingo_storage'] = Ingo_Storage::factory('mock', array());
foreach ($this->rules as $ob) {
$GLOBALS['ingo_storage']->store($ob);
}
}
}
/**
* Implementation of ScriptTester:: for testing 'imap' scripts
*/
class ScriptTester_imap extends ScriptTester {
var $imap;
var $api;
function _setup()
{
$this->_setupStorage();
$this->api = Ingo_Script_imap_api::factory('mock', array());
$result = $this->api->loadFixtures(dirname(__FILE__) . '/_data/');
$this->test->assertNotType('PEAR_Error', $result);
$GLOBALS['notification'] = new Ingo_Test_Notification;
$params = array('api' => $this->api,
'charset' => 'UTF-8',
'spam_compare' => 'string',
'spam_header' => 'X-Spam-Level',
'spam_char' => '*');
$this->imap = Ingo_Script::factory('imap', $params);
}
function _run()
{
$params = array('api' => $this->api, 'filter_seen' => 0, 'show_filter_msg' => 1);
$this->imap->perform($params);
}
function assertDeletesMessage($fixture)
{
$this->_setup();
$this->test->assertTrue($this->api->hasMessage($fixture));
$this->_run();
$this->test->assertFalse($this->api->hasMessage($fixture));
}
function assertKeepsMessage($fixture)
{
$this->_setup();
$this->test->assertTrue($this->api->hasMessage($fixture));
$this->_run();
$this->test->assertTrue($this->api->hasMessage($fixture));
}
function assertMovesMessage($fixture, $to_folder)
{
$this->_setup();
$this->test->assertTrue($this->api->hasMessage($fixture));
$this->_run();
$this->test->assertFalse($this->api->hasMessage($fixture));
$this->test->assertTrue($this->api->hasMessage($fixture, $to_folder));
}
}
/**
* This script tester iterates through all enabled backends to verify that
* each one works properly.
*/
class ScriptTester_all extends ScriptTester {
var $backends = array('imap', 'sieve');
function _delegate($method, $params)
{
foreach ($this->backends as $backend) {
$runner = ScriptTester::factory($backend, $this->test);
foreach ($this->rules as $rule) {
$runner->addRule($rule);
}
call_user_func_array(array($runner, $method), $params);
}
}
function assertDeletesMessage($fixture)
{
$this->_delegate('assertDeletesMessage', array($fixture));
}
function assertKeepsMessage($fixture)
{
$this->_delegate('assertKeepsMessage', array($fixture));
}
function assertMovesMessage($fixture, $to_folder)
{
$this->_delegate('assertMovesMessage', array($fixture, $to_folder));
}
}
/**
* Test the sieve Script backend. This uses the command-line `sieve' from
* the GNU mailutils package.
*/
class ScriptTester_sieve extends ScriptTester {
function assertDeletesMessage($fixture)
{
$this->_run();
$this->_assertOutput("DISCARD on msg uid " . $this->uids[$fixture]);
}
function assertKeepsMessage($fixture)
{
$this->_run();
$this->_assertOutput("KEEP on msg uid " . $this->uids[$fixture]);
}
function assertMovesMessage($fixture, $to_folder)
{
$this->_run();
$this->_assertOutput("FILEINTO on msg uid " . $this->uids[$fixture] .
": delivering into " . $to_folder);
}
function _assertOutput($want)
{
$this->test->assertRegExp('/' . preg_quote($want, '/') . '/',
$this->output,
"FAILED SIEVE SCRIPT:\n\n", $this->sieve_text, "\n\n");
}
var $mbox;
var $sieve;
var $script_text;
var $output;
var $uids;
function _run()
{
$this->_buildMailboxFile();
$this->_writeSieveScript();
$this->_runSieve();
@unlink($this->mbox);
@unlink($this->sieve);
}
function _buildMailboxFile()
{
$this->uids = array();
$this->mbox = tempnam('/tmp', 'mbox');
$mh = fopen($this->mbox, 'w');
$uid = 1;
$dh = opendir(dirname(__FILE__) . '/_data');
while (($dent = readdir($dh)) !== false) {
if ($dent == '.' || $dent == '..' || $dent == 'CVS') {
continue;
}
$filespec = dirname(__FILE__) . '/_data/' . $dent;
$fh = fopen($filespec, 'r');
$data = fread($fh, filesize($filespec));
fclose($fh);
fwrite($mh, $data);
if ($data{strlen($data)-1} != "\n") {
fwrite($mh, "\n");
}
$this->uids[$dent] = $uid++;
}
closedir($dh);
fclose($mh);
}
function _writeSieveScript()
{
$params = array('date_format' => '%x',
'time_format' => '%R',
'charset' => 'UTF-8',
'spam_compare' => 'string',
'spam_header' => 'X-Spam-Level',
'spam_char' => '*');
$this->_setupStorage();
$script = Ingo_Script::factory('sieve', $params);
$this->sieve = tempnam('/tmp', 'sieve');
$fh = fopen($this->sieve, 'w');
$this->sieve_text = $script->generate();
fwrite($fh, $this->sieve_text);
fclose($fh);
}
function _runSieve()
{
$this->output = '';
$ph = popen("sieve -vv -n -f " . escapeshellarg($this->mbox) . " " .
escapeshellarg($this->sieve) . ' 2>&1', 'r');
while (!feof($ph)) {
$data = fread($ph, 512);
if (is_string($data)) {
$this->output .= $data;
}
}
pclose($ph);
}
}
|