]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/SugarTestSugarEmailAddressUtilities.php
Release 6.5.8
[Github/sugarcrm.git] / tests / SugarTestSugarEmailAddressUtilities.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37
38 require_once 'include/SugarEmailAddress/SugarEmailAddress.php';
39
40 class SugarTestSugarEmailAddressUtilities
41 {
42     private static $_createdEmailAddresses = array();
43
44     private static $_createdContact = null;
45
46     private function __construct() {} // not an instantiated class.
47
48     /**
49      * creates a Parent Bean to hang Emails from
50      * @param $time
51      * @return Contact|null
52      */
53     private function _createContact($time)
54     {
55         if (self::$_createdContact === null)
56         {
57             $name = 'SugarEmailAddressContact';
58             $lname = 'LastName';
59             $contact = new Contact();
60             $contact->first_name = $name . $time;
61             $contact->last_name = 'LastName';
62             $contact->save();
63
64             $GLOBALS['db']->commit();
65             self::$_createdContact = $contact;
66         }
67
68         return self::$_createdContact;
69     }
70
71     /**
72      * @param $contact
73      * @param $time
74      * @param $id
75      * @param $override
76      * @return SugarEmailAddress
77      */
78     private static function _createEmailAddress($contact,$time,$id,$override)
79     {
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;
87         }
88
89
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;
95     }
96
97     /**
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.
101      * @access public
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
106      */
107     public static function createEmailAddress($address=null,$id = '', $override = array())
108     {
109         $time = mt_rand();
110         $contact = self::_createContact($time);
111         if (!empty($address)) {
112             $override['email_address'] = $address;
113         }
114         $address = self::_createEmailAddress($contact, $time, $id, $override);
115         return $address;
116     }
117
118     /**
119      * Clean up after use
120      * @access public
121      */
122     public static function removeAllCreatedEmailAddresses()
123     {
124         $address_ids = self::getCreatedEmailAddressIds();
125         $GLOBALS['db']->query('DELETE FROM email_addresses WHERE id IN (\'' . implode("', '", $address_ids) . '\')');
126     }
127
128     /**
129      * clean up the related bean and the relationship table
130      * @access public
131      */
132     public static function removeCreatedContactAndRelationships(){
133         if (self::$_createdContact === null) {
134             return;
135         }
136
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;
140     }
141
142
143     /**
144      * Retrieve a list of all ids of SugarEmailAddresses created through this class
145      * @access public
146      * @return array ids of all SugarEmailAddresses created
147      */
148     public static function getCreatedEmailAddressIds()
149     {
150         $address_ids = array();
151         foreach (self::$_createdEmailAddresses as $address) {
152             $address_ids[] = $address->id;
153         }
154         return $address_ids;
155     }
156
157     /**
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.
161      */
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);
166
167         if (!empty($a['id'])) {
168             return $a['id'];
169         }
170         else {
171             return null;
172         }
173     }
174
175     /**
176      * get our parent bean
177      * @return Contact|null
178      */
179     public static function getContact() {
180         return self::_createContact(mt_rand());
181     }
182
183 }