Passed
Push — master ( edcd19...7b8e38 )
by Felipe
05:02
created

PreconditionSet   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 203
rs 10
c 0
b 0
f 0
wmc 7
1
<?php
2
/**
3
 *  Provides the precondition environment for the test. 
4
 *
5
  * @author     Augmentum SpikeSource Team 
6
  * @copyright  2005 by Augmentum, Inc.
7
  */
8
9
/**
10
 * Provides some base function here.
11
 * 
12
 * Function list now:  login
13
 *                     createDatabase
14
 *                     dropDatabase(can not be run correctly)
15
 *                     createTable
16
 *                     dropTable
17
 *                     logout
18
 */
19
class PreconditionSet extends WebTestCase
20
{
21
    /**
22
     * Logoin phpPgAdmin use the specify user name, password 
23
     * and login home page.
24
     * 
25
     * The user name and password can be replaced.
26
     * But the param $loginPageUrl always equals
27
     * http://localhost:8080/phpPgAdmin/index.php
28
     * 
29
     * @param string $userName  user name of the admin. 
30
     * @param string $password  password of the admin.
31
     * @param string $loginPageUrl  the login home page url.
32
     * 
33
     * @access public
34
     */
35
    function login($userName, $password, $loginPageUrl)
36
    {
37
    	global $lang, $SERVER;
38
39
		$this->setCookie("PHPCOVERAGE_HOME", $PHP_SIMPLETEST_HOME);
40
		$this->get($loginPageUrl, array('server' => $SERVER));
41
		$this->setField('loginUsername', $userName);
42
		$this->setFieldById('loginPassword', $password);
43
		$this->submitFormByid('login_form');
44
		return TRUE;
45
    }
46
     
47
     
48
    /**
49
     * Create a new database by the giving database name and encoding.
50
     * 
51
     * @param string $databaseName The name of the new database.
52
     * @param string $enCoding The encoding mode.
53
     * 
54
     * @access public
55
     */
56
    function createDatabase($databaseName, $enCoding) 
57
    {
58
        global $webUrl;
59
        global $lang, $SERVER;
60
        global $SUPER_USER_NAME;
61
        global $SUPER_USER_PASSWORD;
62
		
63
        $this->login($SUPER_USER_NAME, $SUPER_USER_PASSWORD, 
64
		             "$webUrl/login.php");
65
        $this->get("$webUrl/all_db.php", array('server' => $SERVER));	
66
        $this->clickLink('Create database');
67
         
68
        $this->setField('formName', $databaseName);
69
        $this->setField('formEncoding', $enCoding);
70
        
71
        // Click the button "Create" for creating a database 
72
        // use the specifid name.
73
        $this->clickSubmit($lang['strcreate']);
74
        
75
        return TRUE;
76
    }   
77
     
78
79
    /**
80
     * Drop a exist database by the specify database name.
81
     *
82
     * @param string $databaseName The specify database name which be droped.
83
     * 
84
     * @access public
85
     */
86
    function dropDatabase($databaseName)
87
    {
88
        global $webUrl;
89
        global $lang, $SERVER;
90
        global $SUPER_USER_NAME;
91
        global $SUPER_USER_PASSWORD;
92
		
93
        $this->login($SUPER_USER_NAME, $SUPER_USER_PASSWORD, 
94
                     "$webUrl/login.php");
95
        
96
        $this->get("$webUrl/all_db.php", array('server' => $SERVER));
97
		$this->get("$webUrl/all_db.php", array(
98
			'server' => $SERVER,
99
			'action' => 'confirm_drop',
100
			'subject' => 'database',
101
			'database' => $databaseName,
102
			'dropdatabase' => $databaseName)
103
		);
104
		$this->clickSubmit($lang['strdrop']);
105
        
106
        return TRUE;		
107
    }
108
    
109
    
110
    /**
111
     * Create a new table by the specified conditions.
112
     * 
113
     * Notice: 
114
     * If the value of $fieldNumber equals 3, it would be create a
115
     * table with 3 field like(field name, field type):(field0, text), 
116
     * (field1, text), (field2, text). 
117
     * 
118
     * @param string $databaseName Owner of the new table. paramname.
119
     * @param string $schema the Schema which the new table belong to.
120
     * @param string $tablename The name of the new table.
121
     * @param string $fieldNumber The field number of the new table.
122
     */
123
    function createTable($databaseName, $schema, $tableName, $fieldNumber)
124
    {
125
        global $webUrl;
126
        global $lang, $SERVER;
127
        global $SUPER_USER_NAME;
128
        global $SUPER_USER_PASSWORD;
129
		
130
        $this->login($SUPER_USER_NAME, $SUPER_USER_PASSWORD, 
131
                     "$webUrl/login.php");
132
		
133
		$this->get("$webUrl/tables.php", array(
134
			'server' => $SERVER,
135
			'action' => 'create',
136
			'database' => $databaseName,
137
			'schema' => $schema)
138
		);
139
140
        $this->setField('name', $tableName);
141
        $this->setField('fields', $fieldNumber);
142
        $this->assertTrue($this->setField('spcname', 'pg_default'));
143
        $this->setField('tblcomment', 'Create auto!');
144
145
        // Clicks the button "next >" for inputing the detail information. 
146
        //$this->assertTrue($this->ClickSubmit($lang['strnext']));
147
        // If we do not hardcoded it here, it will cause fail. Encoding issue.
148
		$this->assertTrue($this->ClickSubmit('Next >'));
149
150
        for($ii = 0 ; $ii < $fieldNumber; $ii++) 
151
        {
152
            $field = 'field[' . $ii .']';
153
            $type = 'type[' . $ii . ']';
154
            $array = 'array[' . $ii . ']';
155
            $fieldName = 'field' . $ii;
156
            
157
            // Enter the detail information of the table. 
158
            $this->setField($field, $fieldName);	
159
            $this->setField($type, 'text');
160
            $this->setField($array, '');
161
        }
162
163
        // Click the button "Create" for creating the
164
        // table use the specify conditions.
165
        $this->clickSubmit($lang['strcreate']); 
166
167
        return TRUE;
168
    }
169
    
170
    
171
    /**
172
     * Drop a exist table by the specified table name in a database.
173
     * 
174
     * @param string $databaseName the database which the table content.
175
     * @param string $tableName the table name which wants to delete.
176
     * @param string $schema the schema the table belong.
177
     * 
178
     * @access public
179
     */
180
    function dropTable($databaseName, $tableName, $schema)
181
    {
182
        // Import the global variable.
183
        global $webUrl;
184
        global $lang, $SERVER;
185
        global $SUPER_USER_NAME;
186
        global $SUPER_USER_PASSWORD;
187
		
188
        // Login and trun to the page which list all the 
189
        // table in the database.
190
        $this->login($SUPER_USER_NAME, $SUPER_USER_PASSWORD, 
191
                     "$webUrl/login.php");
192
		
193
		$this->get("$webUrl/tables.php", array(
194
			'server' => $SERVER,
195
			'action' => 'confirm_drop',
196
			'database' => $databaseName,
197
			'schema' => $schema,
198
			'table'	=> $tableName )
199
		); 
200
		           
201
        // Click the button "Drop" for dropping the table from the database.           
202
        $this->clickSubmit($lang['strdrop']);  
203
        
204
        return TRUE;               
205
    }
206
    
207
    
208
    /**
209
     * Logout the system.
210
     */
211
    function logout()
212
    {
213
        global $webUrl;
214
        global $lang;
215
        $this->get("$webUrl/index.php");
216
        
217
        // Select the frame and logout.
218
        $this->setFrameFocus('topbar');
219
        $this->clickLink($lang['strlogout']);
220
        
221
        return TRUE;
222
    }
223
}
224
?>
225