]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/modules/DynamicFields/RepairCustomFieldsTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / modules / DynamicFields / RepairCustomFieldsTest.php
1 <?php
2 require_once('modules/DynamicFields/FieldCases.php');
3
4 /**
5  * Test cases for URL Field
6  */
7 class RepairCustomFieldsTest extends Sugar_PHPUnit_Framework_TestCase
8 {
9     protected $modulename = 'Accounts';
10     protected $objectname = 'Account';
11     protected $table_name = 'accounts_cstm';
12     protected $seed;
13
14     protected function repairDictionary()
15     {
16
17         $this->df->buildCache($this->modulename);
18         VardefManager::clearVardef();
19         VardefManager::refreshVardefs($this->modulename, $this->objectname);
20         $this->seed->field_defs = $GLOBALS['dictionary'][$this->objectname]['fields'];
21
22     }
23     
24     public function setUp()
25     {
26         $this->markTestSkipped("Skipping for now...");
27         $this->field = get_widget('varchar');
28         $this->field->id = $this->modulename.'foo_c';
29         $this->field->name = 'foo_c';
30         $this->field->vanme = 'LBL_Foo';
31         $this->field->comments = NULL;
32         $this->field->help = NULL;
33         $this->field->custom_module = $this->modulename;
34         $this->field->type = 'varchar';
35         $this->field->label = 'LBL_FOO';
36         $this->field->len = 255;
37         $this->field->required = 0;
38         $this->field->default_value = NULL;
39         $this->field->date_modified = '2009-09-14 02:23:23';
40         $this->field->deleted = 0;
41         $this->field->audited = 0;
42         $this->field->massupdate = 0;
43         $this->field->duplicate_merge = 0;
44         $this->field->reportable = 1;
45         $this->field->importable = 'true';
46         $this->field->ext1 = NULL;
47         $this->field->ext2 = NULL;
48         $this->field->ext3 = NULL;
49         $this->field->ext4 = NULL;
50         $this->seed = new Account();
51         $this->df = new DynamicField($this->modulename);
52         $this->df->setup ( $this->seed  ) ;
53
54         $this->field->save ( $this->df ) ;
55         $this->db = $GLOBALS['db'];
56
57         $this->repairDictionary();
58
59     }
60     
61     public function tearDown()
62     {
63         $this->field->delete ( $this->df ) ;
64         if ($this->db->tableExists($this->table_name))
65         {
66             $this->db->dropTableName($this->table_name);
67         }
68     }
69     
70     public function testRepairRemovedFieldNoExecute()
71     {
72         //Remove the custom column
73         $this->db->query("ALTER TABLE {$this->table_name} DROP COLUMN {$this->field->name}");
74         //Run repair
75         $ret = $this->df->repairCustomFields(false);
76         $this->assertRegExp("/MISSING IN DATABASE - {$this->field->name} -  ROW/", $ret);
77         $compareFieldDefs = $this->db->getHelper()->get_columns($this->table_name);
78         $this->assertArrayNotHasKey($this->field->name, $compareFieldDefs);
79     }
80
81     public function testRepairRemovedFieldExecute()
82     {
83         //Remove the custom column
84         $this->db->query("ALTER TABLE {$this->table_name} DROP COLUMN {$this->field->name}");
85         //Run repair
86         $ret = $this->df->repairCustomFields(true);
87         $this->assertRegExp("/MISSING IN DATABASE - {$this->field->name} -  ROW/", $ret);
88         $compareFieldDefs = $this->db->getHelper()->get_columns($this->table_name);
89         $this->assertArrayHasKey($this->field->name, $compareFieldDefs);
90     }
91
92     public function testCreateTableNoExecute()
93     {
94         //Remove the custom table
95         $this->db->dropTableName($this->table_name);
96         //Run repair
97         $ret = $this->df->repairCustomFields(false);
98         //Test that the table is going to be created.
99         $this->assertRegExp("/Missing Table: {$this->table_name}/", $ret);
100         //Test that the custom field is going to be added.
101         $this->assertRegExp("/MISSING IN DATABASE - {$this->field->name} -  ROW/", $ret);
102         //Assert that the table was NOT created
103         $this->assertFalse($this->db->tableExists($this->table_name),
104             "Asserting that the custom table is not created when repair is run with execute set false");
105     }
106
107     public function testCreateTableExecute()
108     {
109         //Remove the custom table
110         $this->db->dropTableName($this->table_name);
111         //Run repair
112         $ret = $this->df->repairCustomFields();
113         $this->assertRegExp("/MISSING IN DATABASE - {$this->field->name} -  ROW/", $ret);
114         //Test that the table is going to be created.
115         $this->assertRegExp("/Missing Table: {$this->table_name}/", $ret);
116         //Test that the custom field is going to be added.
117         $this->assertRegExp("/MISSING IN DATABASE - {$this->field->name} -  ROW/", $ret);
118         //Assert that the table was created
119         $this->assertTrue($this->db->tableExists($this->table_name),
120             "Asserting that the custom table is created when repair is run with execute not set");
121     }
122 }