| 1 |
<?php
|
| 2 |
// $Id: path_redirect.test,v 1.4 2009/03/18 02:44:34 davereid Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Tests for the path_redirect module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
class PathRedirectUnitTest extends DrupalWebTestCase {
|
| 10 |
public static function getInfo() {
|
| 11 |
return array(
|
| 12 |
'name' => t('Path redirect functionality'),
|
| 13 |
'description' => t('Test basic redirection.'),
|
| 14 |
'group' => t('Path redirect'),
|
| 15 |
);
|
| 16 |
}
|
| 17 |
|
| 18 |
function setUp() {
|
| 19 |
parent::setUp('path_redirect');
|
| 20 |
}
|
| 21 |
|
| 22 |
function testRedirects() {
|
| 23 |
// Test a basic redirect (with and without a trailing slash requested).
|
| 24 |
path_redirect_save(array('path' => '/test/', 'redirect' => 'node'));
|
| 25 |
$this->assertRedirect('test', 'node');
|
| 26 |
$this->assertRedirect('test/', 'node');
|
| 27 |
|
| 28 |
// Test a unicode URL.
|
| 29 |
path_redirect_save(array('path' => 'FrançAIS', 'redirect' => 'http://example.com/'));
|
| 30 |
$this->assertRedirect('FrançAIS', 'http://example.com/');
|
| 31 |
|
| 32 |
// Test an URL with special characters.
|
| 33 |
path_redirect_save(array('path' => 'foo_/ferzle-foo.bar', 'redirect' => '<front>'));
|
| 34 |
$this->assertRedirect('foo_/ferzle-foo.bar', '<front>');
|
| 35 |
}
|
| 36 |
|
| 37 |
function testNoCleanURLs() {
|
| 38 |
variable_set('clean_url', 0);
|
| 39 |
$this->testRedirects();
|
| 40 |
variable_set('clean_url', 1);
|
| 41 |
}
|
| 42 |
|
| 43 |
private function assertRedirect($request, $redirect, $code = FALSE) {
|
| 44 |
$this->drupalGet($request);
|
| 45 |
if ($code) {
|
| 46 |
$this->assertResponse($code);
|
| 47 |
}
|
| 48 |
$this->assertEqual($this->getUrl(), url($redirect, array('absolute' => TRUE)), t('Redirected from %request to %redirect.', array('%request' => $request, '%redirect' => $redirect)));
|
| 49 |
}
|
| 50 |
}
|
| 51 |
|
| 52 |
/*class PathRedirectPathautoUnitTest extends DrupalWebTestCase {
|
| 53 |
public static function getInfo() {
|
| 54 |
return array(
|
| 55 |
'name' => t('Pathauto integration'),
|
| 56 |
'description' => t('Test integration with the pathauto module.'),
|
| 57 |
'group' => t('Path redirect'),
|
| 58 |
'dependencies' => array('pathauto', 'token'),
|
| 59 |
);
|
| 60 |
}
|
| 61 |
|
| 62 |
function setUp() {
|
| 63 |
parent::setUp('path', 'token', 'pathauto', 'path_redirect');
|
| 64 |
|
| 65 |
$this->admin_user = $this->drupalCreateUser(array('administer nodes', 'administer redirects', 'create url aliases'));
|
| 66 |
$this->drupalLogin($this->admin_user);
|
| 67 |
variable_set('pathauto_update_action', 3);
|
| 68 |
//variable_set('pathauto_verbose', FALSE);
|
| 69 |
}
|
| 70 |
|
| 71 |
function testPathautoIntegration() {
|
| 72 |
$node = $this->drupalCreateNode(array('title' => 'test1'));
|
| 73 |
$this->assertIdentical('content/test1', drupal_get_path_alias('node/' . $node->nid));
|
| 74 |
|
| 75 |
$edit = array(
|
| 76 |
'title' => 'test2',
|
| 77 |
'pathauto_perform_alias' => TRUE,
|
| 78 |
);
|
| 79 |
$this->drupalPost("node/{$node->nid}/edit", $edit, t('Save'));
|
| 80 |
$this->assertRaw(t('%title has been updated.', array('%title' => $edit['title'])), t('Node saved.'));
|
| 81 |
$this->assertTrue(path_redirect_load(array('path' => 'content/test1', 'redirect' => 'node/' . $node->nid)), t('Redirect created.'));
|
| 82 |
|
| 83 |
$edit = array(
|
| 84 |
'title' => 'test3',
|
| 85 |
'pathauto_perform_alias' => TRUE,
|
| 86 |
);
|
| 87 |
$this->drupalPost("node/{$node->nid}/edit", $edit, t('Save'));
|
| 88 |
$this->assertRaw(t('%title has been updated.', array('%title' => $edit['title'])), t('Node saved.'));
|
| 89 |
$this->assertTrue(path_redirect_load(array('path' => 'content/test1', 'redirect' => 'node/' . $node->nid)), t('Redirect created.'));
|
| 90 |
$this->assertTrue(path_redirect_load(array('path' => 'content/test2', 'redirect' => 'node/' . $node->nid)), t('Redirect created.'));
|
| 91 |
}
|
| 92 |
}*/
|
| 93 |
|
| 94 |
class PathRedirectFunctionalTest extends DrupalWebTestCase {
|
| 95 |
public static function getInfo() {
|
| 96 |
return array(
|
| 97 |
'name' => t('Path redirect administration'),
|
| 98 |
'description' => t('Test redirect administration interface.'),
|
| 99 |
'group' => t('Path redirect'),
|
| 100 |
);
|
| 101 |
}
|
| 102 |
|
| 103 |
function setUp() {
|
| 104 |
parent::setUp('path', 'path_redirect');
|
| 105 |
|
| 106 |
$admin_user = $this->drupalCreateUser(array('administer redirects', 'bypass node access', 'create url aliases'));
|
| 107 |
$this->drupalLogin($admin_user);
|
| 108 |
}
|
| 109 |
|
| 110 |
function testAliasRedirection() {
|
| 111 |
$node = $this->drupalCreateNode(array('title' => 'test1'));
|
| 112 |
$edit = array(
|
| 113 |
'path' => 'test1',
|
| 114 |
);
|
| 115 |
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
| 116 |
$this->assertRaw(t('@type %title has been updated.', array('@type' => 'Page', '%title' => $node->title)), t('Node saved.'));
|
| 117 |
$this->assertFalse(path_redirect_load(array('redirect' => 'node/' . $node->nid)), t('No redirects created yet.'));
|
| 118 |
|
| 119 |
$edit = array(
|
| 120 |
'path' => 'test2',
|
| 121 |
);
|
| 122 |
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
| 123 |
$this->assertRaw(t('@type %title has been updated.', array('@type' => 'Page', '%title' => $node->title)), t('Node saved.'));
|
| 124 |
$this->assertTrue(path_redirect_load(array('path' => 'test1', 'redirect' => 'node/' . $node->nid)), t('Redirect created.'));
|
| 125 |
|
| 126 |
$this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
|
| 127 |
$this->assertFalse(path_redirect_load(array('redirect' => 'node/' . $node->nid)), t("Deleted node's redirects deleted."));
|
| 128 |
}
|
| 129 |
|
| 130 |
function testInactivePurging() {
|
| 131 |
// Add two inactive redirects to the database.
|
| 132 |
path_redirect_save(array('path' => 'test1', 'redirect' => 'node', 'last_used' => REQUEST_TIME - 620000));
|
| 133 |
path_redirect_save(array('path' => 'test2', 'redirect' => 'node', 'last_used' => REQUEST_TIME - 620000));
|
| 134 |
|
| 135 |
// Purging is disabled by default, so after running cron both redirects should not be removed.
|
| 136 |
$this->drupalGet('cron.php', array('query' => 'cron_key=' . variable_get('cron_key', 'drupal')));
|
| 137 |
$this->assertTrue(path_redirect_load(array('path' => 'test1')), t('Inactive redirect not removed.'));
|
| 138 |
$this->assertTrue(path_redirect_load(array('path' => 'test2')), t('Inactive redirect not removed.'));
|
| 139 |
|
| 140 |
// Enable purging of inactive redirects.
|
| 141 |
$edit = array('path_redirect_purge_inactive' => 604800);
|
| 142 |
$this->drupalPost('admin/build/path-redirect/settings', $edit, t('Save configuration'));
|
| 143 |
$this->assertText(t('The configuration options have been saved.'));
|
| 144 |
|
| 145 |
// Run one of the redirects and test that the last used timestamp was updated.
|
| 146 |
$this->drupalGet('test1');
|
| 147 |
$this->assertTrue(db_result(db_query("SELECT last_used FROM {path_redirect} WHERE path = 'test1'")) >= REQUEST_TIME, t('Last used timestamp was updated.'));
|
| 148 |
|
| 149 |
// Run cron and test that the inactive redirect was removed.
|
| 150 |
$this->drupalGet('cron.php', array('query' => 'cron_key=' . variable_get('cron_key', 'drupal')));
|
| 151 |
$this->assertTrue(path_redirect_load(array('path' => 'test1')), t('Active redirect not removed.'));
|
| 152 |
$this->assertFalse(path_redirect_load(array('path' => 'test2')), t('Inactive redirect removed.'));
|
| 153 |
}
|
| 154 |
|
| 155 |
function testAutocomplete() {
|
| 156 |
// Create 404 errors.
|
| 157 |
$this->drupalGet('test1');
|
| 158 |
$this->drupalGet('test2');
|
| 159 |
$this->drupalGet('test2');
|
| 160 |
$this->drupalGet('tst3');
|
| 161 |
|
| 162 |
// Create a 404 and then create a valid redirect for it.
|
| 163 |
$this->drupalGet('test3');
|
| 164 |
path_redirect_save(array('path' => 'test3', 'redirect' => 'node'));
|
| 165 |
|
| 166 |
// Create a 404 and then create a valid path for it.
|
| 167 |
$this->drupalGet('node/1');
|
| 168 |
$node = $this->drupalCreateNode();
|
| 169 |
$this->assertEqual($node->nid, 1, t('Node 1 created'));
|
| 170 |
|
| 171 |
// Test that only the valid 404s are found in the autocomplete.
|
| 172 |
$this->drupalGet('admin/build/path-redirect/autocomplete/e');
|
| 173 |
$this->assertText('test1');
|
| 174 |
$this->assertText('test2');
|
| 175 |
$this->assertNoText('node');
|
| 176 |
$this->assertNoText('test3');
|
| 177 |
$this->assertNoText('tst3');
|
| 178 |
|
| 179 |
// Test that the 404 with the greater hits is listed first.
|
| 180 |
$this->assertTrue(strpos($this->drupalGetContent(), 'test1') > strpos($this->drupalGetContent(), 'test2'), t('Paths ordered correctly.'));
|
| 181 |
}
|
| 182 |
}
|