]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/DynamicFields/RepairCustomFieldsTest.php
Release 6.2.0
[Github/sugarcrm.git] / tests / modules / DynamicFields / RepairCustomFieldsTest.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 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/DynamicFields/FieldCases.php');
39
40 /**
41  * Test cases for URL Field
42  */
43 class RepairCustomFieldsTest extends Sugar_PHPUnit_Framework_TestCase
44 {
45     protected $modulename = 'Accounts';
46     protected $objectname = 'Account';
47     protected $table_name = 'accounts_cstm';
48     protected $seed;
49
50     protected function repairDictionary()
51     {
52
53         $this->df->buildCache($this->modulename);
54         VardefManager::clearVardef();
55         VardefManager::refreshVardefs($this->modulename, $this->objectname);
56         $this->seed->field_defs = $GLOBALS['dictionary'][$this->objectname]['fields'];
57
58     }
59     
60     public function setUp()
61     {
62         $this->markTestSkipped("Skipping for now...");
63         $this->field = get_widget('varchar');
64         $this->field->id = $this->modulename.'foo_c';
65         $this->field->name = 'foo_c';
66         $this->field->vanme = 'LBL_Foo';
67         $this->field->comments = NULL;
68         $this->field->help = NULL;
69         $this->field->custom_module = $this->modulename;
70         $this->field->type = 'varchar';
71         $this->field->label = 'LBL_FOO';
72         $this->field->len = 255;
73         $this->field->required = 0;
74         $this->field->default_value = NULL;
75         $this->field->date_modified = '2009-09-14 02:23:23';
76         $this->field->deleted = 0;
77         $this->field->audited = 0;
78         $this->field->massupdate = 0;
79         $this->field->duplicate_merge = 0;
80         $this->field->reportable = 1;
81         $this->field->importable = 'true';
82         $this->field->ext1 = NULL;
83         $this->field->ext2 = NULL;
84         $this->field->ext3 = NULL;
85         $this->field->ext4 = NULL;
86         $this->seed = new Account();
87         $this->df = new DynamicField($this->modulename);
88         $this->df->setup ( $this->seed  ) ;
89
90         $this->field->save ( $this->df ) ;
91         $this->db = $GLOBALS['db'];
92
93         $this->repairDictionary();
94
95     }
96     
97     public function tearDown()
98     {
99         if ( isset($this->field) && ( $this->field instanceOf TemplateField ) ) {
100             $this->field->delete ( $this->df ) ;
101         }
102         if ( isset($this->db) && ( $this->db instanceOf DBManager ) && $this->db->tableExists($this->table_name) ) {
103             $this->db->dropTableName($this->table_name);
104         }
105     }
106     
107     public function testRepairRemovedFieldNoExecute()
108     {
109         //Remove the custom column
110         $this->db->query("ALTER TABLE {$this->table_name} DROP COLUMN {$this->field->name}");
111         //Run repair
112         $ret = $this->df->repairCustomFields(false);
113         $this->assertRegExp("/MISSING IN DATABASE - {$this->field->name} -  ROW/", $ret);
114         $compareFieldDefs = $this->db->getHelper()->get_columns($this->table_name);
115         $this->assertArrayNotHasKey($this->field->name, $compareFieldDefs);
116     }
117
118     public function testRepairRemovedFieldExecute()
119     {
120         //Remove the custom column
121         $this->db->query("ALTER TABLE {$this->table_name} DROP COLUMN {$this->field->name}");
122         //Run repair
123         $ret = $this->df->repairCustomFields(true);
124         $this->assertRegExp("/MISSING IN DATABASE - {$this->field->name} -  ROW/", $ret);
125         $compareFieldDefs = $this->db->getHelper()->get_columns($this->table_name);
126         $this->assertArrayHasKey($this->field->name, $compareFieldDefs);
127     }
128
129     public function testCreateTableNoExecute()
130     {
131         //Remove the custom table
132         $this->db->dropTableName($this->table_name);
133         //Run repair
134         $ret = $this->df->repairCustomFields(false);
135         //Test that the table is going to be created.
136         $this->assertRegExp("/Missing Table: {$this->table_name}/", $ret);
137         //Test that the custom field is going to be added.
138         $this->assertRegExp("/MISSING IN DATABASE - {$this->field->name} -  ROW/", $ret);
139         //Assert that the table was NOT created
140         $this->assertFalse($this->db->tableExists($this->table_name),
141             "Asserting that the custom table is not created when repair is run with execute set false");
142     }
143
144     public function testCreateTableExecute()
145     {
146         //Remove the custom table
147         $this->db->dropTableName($this->table_name);
148         //Run repair
149         $ret = $this->df->repairCustomFields();
150         $this->assertRegExp("/MISSING IN DATABASE - {$this->field->name} -  ROW/", $ret);
151         //Test that the table is going to be created.
152         $this->assertRegExp("/Missing Table: {$this->table_name}/", $ret);
153         //Test that the custom field is going to be added.
154         $this->assertRegExp("/MISSING IN DATABASE - {$this->field->name} -  ROW/", $ret);
155         //Assert that the table was created
156         $this->assertTrue($this->db->tableExists($this->table_name),
157             "Asserting that the custom table is created when repair is run with execute not set");
158     }
159 }