]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/Import/Bug25736ImportTest.php
Release 6.5.14
[Github/sugarcrm.git] / tests / modules / Import / Bug25736ImportTest.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2013 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('modules/Import/Importer.php');
39 require_once('modules/Import/sources/ImportFile.php');
40 require_once('include/export_utils.php');
41
42 /**
43  *
44  * Test if non-primary emails are being imported properly from a CSV file
45  * on Accounts module, or modules based on Person
46  *
47  * @author avucinic@sugarcrm.com
48  *
49  */
50 class Bug25736ImportTest extends Sugar_PHPUnit_Framework_TestCase
51 {
52     private $_importObject;
53     private $_file;
54     private $_cleanId;
55
56     public function setUp()
57     {
58         $this->_file = $GLOBALS['sugar_config']['upload_dir'] . 'Bug25736Test.csv';
59
60         SugarTestHelper::setUp('beanFiles');
61         SugarTestHelper::setUp('beanList');
62         SugarTestHelper::setUp('current_user');
63     }
64
65     public function tearDown()
66     {
67         $GLOBALS['db']->query("DELETE FROM email_addr_bean_rel WHERE bean_id = '{$this->_cleanId}' AND bean_module = '{$this->_importObject->module_dir}'");
68         $GLOBALS['db']->query("DELETE FROM email_addresses WHERE email_address IN ('testmail1@test.com', 'testmail2@test.com', 'testmail3@test.com')");
69         $GLOBALS['db']->query("DELETE FROM {$this->_importObject->table_name} WHERE created_by = '{$GLOBALS['current_user']->id}'");
70
71         SugarTestHelper::tearDown();
72     }
73
74     /**
75      * Check if semi-colon separated non-primary mails
76      * are being imported properly
77      *
78      * @dataProvider providerEmailImport
79      */
80     public function testEmailImport($module, $lastName, $expected, $test)
81     {
82         $fileCreated = sugar_file_put_contents($this->_file, $test);
83         $this->assertGreaterThan(0, $fileCreated, 'Failed to write to ' . $this->_file);
84
85         // Create the ImportFile the Importer uses from our CSV
86         $importSource = new ImportFile($this->_file, ',', '"');
87
88         // Create the bean type we're importing
89         $this->_importObject = $bean = new $module;
90
91         // Setup needed $_REQUEST data 
92         $_REQUEST['columncount'] = 2;
93         $_REQUEST['colnum_0'] = 'email_addresses_non_primary';
94         $_REQUEST['colnum_1'] = 'last_name';
95         // A few changed for Accounts module
96         if ($module == "Account") {
97             $_REQUEST['columncount'] = 3;
98             $_REQUEST['colnum_1'] = 'name';
99             $_REQUEST['colnum_2'] = 'team_id';
100         }
101
102         $_REQUEST['import_module'] = $bean->module_dir;
103         $_REQUEST['importlocale_charset'] = 'UTF-8';
104         $_REQUEST['importlocale_dateformat'] = "m/d/Y";
105         $_REQUEST['importlocale_timeformat'] = "h:i a";
106         $_REQUEST['importlocale_timezone'] = 'GMT';
107         $_REQUEST['importlocale_default_currency_significant_digits'] = '2';
108         $_REQUEST['importlocale_currency'] = '-99';
109         $_REQUEST['importlocale_dec_sep'] = '.';
110         $_REQUEST['importlocale_currency'] = '-99';
111         $_REQUEST['importlocale_default_locale_name_format'] = 's f l';
112         $_REQUEST['importlocale_num_grp_sep'] = ',';
113
114         // Create the Importer and try importing
115         $importer = new Importer($importSource, $bean);
116         $importer->import();
117
118         // Check if the Lead is created
119         $query = "SELECT id FROM $bean->table_name WHERE {$_REQUEST['colnum_1']} = '$lastName'";
120         $result = $GLOBALS['db']->query($query);
121         $row = $GLOBALS['db']->fetchByAssoc($result);
122
123         $this->assertNotEmpty($row['id'], $module . ' not created');
124         // Save Lead id for easier cleanup after test
125         $this->_cleanId = $row['id'];
126
127         // Check if all of the mails got created and linked properly
128         foreach ($expected as $mail)
129         {
130             // Check if the mail got created
131             $query = "SELECT id FROM email_addresses WHERE email_address = '$mail'";
132             $result = $GLOBALS['db']->query($query);
133             $row = $GLOBALS['db']->fetchByAssoc($result);
134
135             $this->assertNotEmpty($row['id'], 'Mail not created');
136             $mailId = $row['id'];
137
138             // Check if the mail is linked
139             $query = "SELECT id FROM email_addr_bean_rel WHERE email_address_id = '$mailId' AND bean_module = '$bean->module_dir' AND deleted = 0 AND primary_address = 0";
140             $result = $GLOBALS['db']->query($query);
141             $row = $GLOBALS['db']->fetchByAssoc($result);
142
143             $this->assertNotEmpty($row['id'], 'Mail not linked');
144         }
145     }
146
147     public function providerEmailImport()
148     {
149         /*
150         * Last name for getting created Lead
151         * Array of mails that should be created and linked to Lead
152         * CSV data to be used for import
153         */
154         return array(
155             array("Lead", "Random Guy 1", array("testmail1@test.com", "testmail2@test.com"), array('"testmail1@test.com;testmail2@test.com", "Random Guy 1"')),
156             array("Contact", "Random Guy 2", array("testmail2@test.com"), array('"testmail2@test.com", "Random Guy 2"')),
157             array("Account", "Random Guy 3", array("testmail3@test.com", "testmail1@test.com"), array('"testmail3@test.com;testmail1@test.com", "Random Guy 3", "West"')),
158         );
159     }
160
161 }