2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
38 require_once 'include/SugarEmailAddress/SugarEmailAddress.php';
40 class SugarTestSugarEmailAddressUtilities
42 private static $_createdEmailAddresses = array();
44 private static $_createdContact = null;
46 private function __construct() {} // not an instantiated class.
49 * creates a Parent Bean to hang Emails from
51 * @return Contact|null
53 private function _createContact($time)
55 if (self::$_createdContact === null)
57 $name = 'SugarEmailAddressContact';
59 $contact = new Contact();
60 $contact->first_name = $name . $time;
61 $contact->last_name = 'LastName';
64 $GLOBALS['db']->commit();
65 self::$_createdContact = $contact;
68 return self::$_createdContact;
76 * @return SugarEmailAddress
78 private static function _createEmailAddress($contact,$time,$id,$override)
80 $params['email_address'] = 'semailaddress@'. $time. 'sugar.com';
81 $params['primary'] = true;
82 $params['reply_to'] = false;
83 $params['invalid'] = false;
84 $params['opt_out'] = false;
85 foreach($override as $key => $value) {
86 $params[$key] = $value;
90 $contact->emailAddress->addAddress($params['email_address'], $params['primary'], $params['reply_to'],
91 $params['invalid'], $params['opt_out'], $id);
92 $contact->emailAddress->save($contact->id, $contact->module_dir);
93 self::$_createdEmailAddresses[] = $contact->emailAddress;
94 return $contact->emailAddress;
98 * Create a SugarEmailAddress
99 * - This version doesn't bother attaching a SugarEmailAddress to a parent bean.
100 * - As such, save() doesn't work on the email addresses.
102 * @param string $address - custom address to pass, otherwise pass null.
103 * @param string $id - pass parameter to set a specific uuid for the SugarEmailAddress
104 * @param array $override - pass key => value array of parameters to override the defaults
105 * @return SugarEmailAddress
107 public static function createEmailAddress($address=null,$id = '', $override = array())
110 $contact = self::_createContact($time);
111 if (!empty($address)) {
112 $override['email_address'] = $address;
114 $address = self::_createEmailAddress($contact, $time, $id, $override);
122 public static function removeAllCreatedEmailAddresses()
124 $address_ids = self::getCreatedEmailAddressIds();
125 $GLOBALS['db']->query('DELETE FROM email_addresses WHERE id IN (\'' . implode("', '", $address_ids) . '\')');
129 * clean up the related bean and the relationship table
132 public static function removeCreatedContactAndRelationships(){
133 if (self::$_createdContact === null) {
137 $GLOBALS['db']->query("DELETE FROM contacts WHERE id = '".self::$_createdContact->id."'");
138 $GLOBALS['db']->query('DELETE FROM email_addr_bean_rel WHERE bean_module=\'Contacts\' AND bean_id =\'' . self::$_createdContact->id . '\'');
139 self::$_createdContact = null;
144 * Retrieve a list of all ids of SugarEmailAddresses created through this class
146 * @return array ids of all SugarEmailAddresses created
148 public static function getCreatedEmailAddressIds()
150 $address_ids = array();
151 foreach (self::$_createdEmailAddresses as $address) {
152 $address_ids[] = $address->id;
158 * In case we don't have our bean's UUID - get it via address
159 * @param $address - email address
160 * @return string|null UUID of bean for email address.
162 public static function fetchEmailIdByAddress($address) {
163 $email_caps = strtoupper(trim($address));
164 $rs = $GLOBALS['db']->query("SELECT id from email_addresses where email_address_caps='$email_caps'");
165 $a = $GLOBALS['db']->fetchByAssoc($rs);
167 if (!empty($a['id'])) {
176 * get our parent bean
177 * @return Contact|null
179 public static function getContact() {
180 return self::_createContact(mt_rand());