Newer
Older
<?php
// $Id$
class EnableDisableCoreTestCase extends DrupalWebTestCase {
protected $admin_user;
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Module list functionality'),
'description' => t('Enable/disable core module and confirm table creation/deletion. Enable module without dependecy enabled.'),
'group' => t('System')
);
}
/**
* Implementation of setUp().
*/
function setUp() {
parent::setUp();
$this->admin_user = $this->drupalCreateUser(array('access administration pages', 'administer site configuration'));
$this->drupalLogin($this->admin_user);
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
}
/**
* Enable a module, check the database for related tables, disable module,
* check for related tables, unistall module, check for related tables.
*/
function testEnableDisable() {
// Enable aggregator, and check tables.
$this->assertModules(array('aggregator'), FALSE);
$this->assertTableCount('aggregator', FALSE);
$edit = array();
$edit['status[aggregator]'] = 'aggregator';
$this->drupalPost('admin/build/modules', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
$this->assertModules(array('aggregator'), TRUE);
$this->assertTableCount('aggregator', TRUE);
// Disable aggregator, check tables, uninstall aggregator, check tables.
$edit = array();
$edit['status[aggregator]'] = FALSE;
$this->drupalPost('admin/build/modules', $edit, t('Save configuration'));
$this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
$this->assertModules(array('aggregator'), FALSE);
$this->assertTableCount('aggregator', TRUE);
$edit = array();
$edit['uninstall[aggregator]'] = 'aggregator';
$this->drupalPost('admin/build/modules/uninstall', $edit, t('Uninstall'));
$this->drupalPost(NULL, NULL, t('Uninstall'));
$this->assertText(t('The selected modules have been uninstalled.'), t('Modules status has been updated.'));
$this->assertModules(array('aggregator'), FALSE);
$this->assertTableCount('aggregator', FALSE);
}
/**
* Attempt to enable translation module without locale enabled.
*/
function testEnableWithoutDependency () {
// Attempt to enable content translation without locale enabled.
$edit = array();
$edit['status[translation]'] = 'translation';
$this->drupalPost('admin/build/modules', $edit, t('Save configuration'));
$this->assertText(t('Some required modules must be enabled'), t('Dependecy required.'));
$this->assertModules(array('translation', 'locale'), FALSE);
// Assert that the locale tables weren't enabled.
$this->assertTableCount('languages', FALSE);
$this->assertTableCount('locale', FALSE);
$this->drupalPost(NULL, NULL, t('Continue'));
$this->assertText(t('The configuration options have been saved.'), t('Modules status has been updated.'));
$this->assertModules(array('translation', 'locale'), TRUE);
// Assert that the locale tables were enabled.
$this->assertTableCount('languages', TRUE);
$this->assertTableCount('locale', TRUE);
}
/**
* Assert tables that begin with the specified base table name.
*
* @param string $base_table Begginning of table name to look for.
* @param boolean $count Assert tables that match specified base table.
* @return boolean Tables with specified base table.
*/
function assertTableCount($base_table, $count) {
$match_count = simpletest_get_like_tables($base_table, TRUE);
if ($count) {
return $this->assertTrue($match_count, t('Tables matching "@base_table" found.', array('@base_table' => $base_table)));
}
return $this->assertFalse($match_count, t('Tables matching "@base_table" not found.', array('@base_table' => $base_table)));
}
/**
* Assert the list of modules are enabled or disabled.
*
* @param array $modules Modules to check.
* @param boolean $enabled Module state.
*/
function assertModules(array $modules, $enabled) {
module_list(TRUE, FALSE);
foreach ($modules as $module) {
if ($enabled) {
$this->assertTrue(module_exists($module) == $enabled, t('Module "@module" is enabled.', array('@module' => $module)));
}
else {
$this->assertTrue(module_exists($module) == $enabled, t('Module "@module" not enabled.', array('@module' => $module)));
}
}
}
}

Dries Buytaert
committed

Dries Buytaert
committed
class IPAddressBlockingTestCase extends DrupalWebTestCase {
protected $blocking_user;

Dries Buytaert
committed
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('IP address blocking'),
'description' => t('Test IP address blocking.'),

Dries Buytaert
committed
'group' => t('System')
);
}
/**
* Implementation of setUp().
*/
function setUp() {
parent::setUp();
// Create user.

Dries Buytaert
committed
$this->blocking_user = $this->drupalCreateUser(array('block IP addresses'));
$this->drupalLogin($this->blocking_user);

Dries Buytaert
committed
}
/**
* Test a variety of user input to confirm correct validation and saving of data.

Dries Buytaert
committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
*/
function testIPAddressValidation() {
$this->drupalGet('admin/settings/ip-blocking');
// Block a valid IP address.
$edit = array();
$edit['ip'] = '192.168.1.1';
$this->drupalPost('admin/settings/ip-blocking', $edit, t('Save'));
$ip = db_result(db_query("SELECT iid from {blocked_ips} WHERE ip = '%s'", $edit['ip']));
$this->assertNotNull($ip, t('IP address found in database'));
$this->assertRaw(t('The IP address %ip has been blocked.', array('%ip' => $edit['ip'])), t('IP address was blocked.'));
// Try to block an IP address that's already blocked.
$edit = array();
$edit['ip'] = '192.168.1.1';
$this->drupalPost('admin/settings/ip-blocking', $edit, t('Save'));
$this->assertText(t('This IP address is already blocked.'));
// Try to block a reserved IP address.
$edit = array();
$edit['ip'] = '255.255.255.255';
$this->drupalPost('admin/settings/ip-blocking', $edit, t('Save'));
$this->assertText(t('Please enter a valid IP address.'));
// Try to block a reserved IP address.
$edit = array();
$edit['ip'] = 'test.example.com';
$this->drupalPost('admin/settings/ip-blocking', $edit, t('Save'));
$this->assertText(t('Please enter a valid IP address.'));
// Submit an empty form.
$edit = array();
$edit['ip'] = '';
$this->drupalPost('admin/settings/ip-blocking', $edit, t('Save'));
$this->assertText(t('Please enter a valid IP address.'));
// Submit your own IP address. This fails, although it works when testing manually.
$edit = array();
$edit['ip'] = ip_address();
$this->drupalPost('admin/settings/ip-blocking', $edit, t('Save'));
$this->assertText(t('You may not block your own IP address.'));
}
}

Dries Buytaert
committed
class CronRunTestCase extends DrupalWebTestCase {
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Cron run'),
'description' => t('Test cron run.'),
'group' => t('System')
);
}
/**
* Test cron runs.
*/
function testCronRun() {
// Run cron anonymously without any cron key.
$this->drupalGet('cron.php');
$this->assertResponse(403);
// Run cron anonymously with a random cron key.
$key = $this->randomName(16);
$this->drupalGet('cron.php', array('query' => 'cron_key=' . $key));
$this->assertResponse(403);
// Run cron anonymously with the valid cron key.
$key = variable_get('cron_key', 'drupal');
$this->drupalGet('cron.php', array('query' => 'cron_key=' . $key));
$this->assertResponse(200);
// Execute cron directly.
$this->assertTrue(drupal_cron_run(), t('Cron ran successfully.'));
}

Dries Buytaert
committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
}
class AdminOverviewTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Admin overview'),
'description' => t('Confirm that the admin overview page appears as expected.'),
'group' => t('System')
);
}
/**
* Test the overview page by task.
*/
function testAdminOverview() {
$admin_user1 = $this->drupalCreateUser(array('access administration pages'));
$this->drupalLogin($admin_user1);
$this->drupalGet('admin');
$this->checkOverview();
$this->drupalGet('admin/by-module');
$this->checkOverview();
// Comments on permissions follow the format: [task], [module] that the permission relates to.
$permissions = array();
$permissions[] = 'access administration pages';
$permissions[] = 'administer comments'; // Content management, Comment.
$permissions[] = 'administer blocks'; // Site building, Block.
$permissions[] = 'administer filters'; // Site configuration, Filter.
$permissions[] = 'administer users'; // User management, User.
$permissions[] = 'access site reports'; // Reports, Database logging.
$admin_user2 = $this->drupalCreateUser($permissions);
$this->drupalLogin($admin_user2);
$this->drupalGet('admin');
$this->checkOverview(array(t('Content management'), t('User management'), t('Reports'), t('Site building'), t('Site configuration')));
$this->drupalGet('admin/by-module');
$this->checkOverview(array(t('Comment'), t('Block'), t('Filter'), t('User'), t('Database logging')));
}
/**
* Check the overview page panels.
*
* @param array $panels List of panels to be found.
*/
function checkOverview(array $panels = array()) {
if ($this->parse()) {
$found = 0;
$extra = 0;
$divs = $this->elements->xpath("//div[@class='admin-panel']");
foreach ($divs as $panel) {
if (in_array(trim($panel->h3), $panels)) {
$found++;
}
else {
$extra++;
}
}
$this->assertTrue(count($panels) == $found, t('Required panels found.'));
$this->assertFalse($extra, t('No extra panels found.'));
}
}
}