From 5aee7c10d2e885dc6a0252cd96dc3215a0d669af Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Sat, 8 Feb 2014 12:17:37 -0600 Subject: [PATCH] lint: Add first of tests for mail parsing --- setup/test/tests/mockdb.php | 82 ++++++++++++++++++++++++++++ setup/test/tests/test.mail-parse.php | 53 ++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 setup/test/tests/mockdb.php create mode 100644 setup/test/tests/test.mail-parse.php diff --git a/setup/test/tests/mockdb.php b/setup/test/tests/mockdb.php new file mode 100644 index 000000000..b6bc348cf --- /dev/null +++ b/setup/test/tests/mockdb.php @@ -0,0 +1,82 @@ +<?php + +function db_connect($source) { + global $__db; + $__db = $source; +} + +function db_input($what) { + return sprintf("'%8.8s'", md5($what)); +} + +function db_query($sql) { + global $__db; + + return $__db->query($sql); +} + +function db_fetch_row($res) { + return $res->fetch_row(); +} + +function db_fetch_array($res) { + return $res->fetch_array(); +} + +function db_affected_row() { + global $__db; + return $__db->affected_rows; +} +function db_insert_id() { + global $__db; + return $__db->insert_id; +} + +function db_num_rows($res) { + return $res->num_rows(); +} + +class MockDbSource { + var $insert_id = 1; + var $affected_rows = 1; + + var $data; + + function __construct($data=array()) { + $this->data = $data; + } + + function query($sql) { + $hash = md5($sql); + if (!isset($this->data[$sql])) + print ($hash.": No data found:\n".$sql."\n"); + + return new MockDbCursor($this->data[$hash] ?: array()); + } + + function addRecordset($hash, &$data) { + $this->data[$hash] = $data; + } +} + +class MockDbCursor { + var $data; + + function __construct($data) { + $this->data = $data; + } + + function fetch_row() { + list($i, $row) = each($this->data); + return $row; + } + + function fetch_array() { + list($i, $row) = each($this->data); + return $row; + } + + function num_rows() { + return count($this->data); + } +} diff --git a/setup/test/tests/test.mail-parse.php b/setup/test/tests/test.mail-parse.php new file mode 100644 index 000000000..d1d80c225 --- /dev/null +++ b/setup/test/tests/test.mail-parse.php @@ -0,0 +1,53 @@ +<?php + +require_once INCLUDE_DIR.'class.validator.php'; +require_once INCLUDE_DIR.'class.auth.php'; +require_once INCLUDE_DIR.'class.staff.php'; +require_once INCLUDE_DIR.'class.email.php'; +require_once INCLUDE_DIR.'class.format.php'; +require_once INCLUDE_DIR.'class.thread.php'; + +require_once 'mockdb.php'; + +class TestMailParsing extends Test { + var $name = "Mail parsing library tests"; + + function testRecipients() { + db_connect(new MockDbSource()); + $email = <<<EOF +Delivered-To: jared@osticket.com +Received: by 10.60.55.168 with SMTP id t8csp161432oep; + Fri, 7 Feb 2014 22:11:19 -0800 (PST) +X-Received: by 10.182.18.9 with SMTP id s9mr16356699obd.15.1391839879167; + Fri, 07 Feb 2014 22:11:19 -0800 (PST) +Return-Path: <mailer@greezybacon.supportsystem.com> +To: jared@osticket.com +Subject: =?utf-8?Q?System_test_email_=C2=AE?= +Content-Type: multipart/alternative; + boundary="=_28022448a1f58a3af7edf57ff2e3af44" +From: "Support" <help@supportsystem.com> +Date: Sat, 08 Feb 2014 01:11:18 -0500 +Message-ID: <Syke6-g24hwuTu77-help@supportsystem.com> +MIME-Version: 1.0 + +--=_28022448a1f58a3af7edf57ff2e3af44 +Content-Transfer-Encoding: base64 +Content-Type: text/plain; charset=utf-8 + +Q2hlZXJzISE= +--=_28022448a1f58a3af7edf57ff2e3af44 +Content-Transfer-Encoding: base64 +Content-Type: text/html; charset=utf-8 + +Q2hlZXJzISE= +--=_28022448a1f58a3af7edf57ff2e3af44-- +EOF; + + $result = EmailDataParser::parse($email); + $this->assert(count($result['recipients']) == 1, 'Expected 1 recipient'); + $this->assert($result['recipients'][0]['source'] == 'delivered-to', + 'Delivered-To header used as a collaborator'); + } +} +return 'TestMailParsing'; +?> -- GitLab