View source
<?php
define('ERROR_MESSAGE', 'ERROR<br />Expected Path: !expected_path<br />Expected Status Code: !expected_status<br />Location: !location<br />Status: !status');
define('SUCCESS_MESSAGE', 'SUCCESS<br />Expected Path: !expected_path<br />Expected Status Code: !expected_status<br />Location: !location<br />Status: !status');
define('INFO_MESSAGE', 'Running Test: !id<br />Requesting URL: !url');
class GlobalRedirectTestCase extends DrupalWebTestCase {
function setUp() {
parent::setUp('path', 'globalredirect', 'taxonomy', 'forum');
variable_set('clean_url', 1);
$user = $this
->drupalCreateUser(array(
'access content',
'create page content',
'create url aliases',
'access administration pages',
));
$this
->drupalLogin($user);
$node = array(
'type' => 'page',
'title' => 'Test Page Node',
'path' => 'test-node',
);
$node = $this
->drupalCreateNode($node);
path_set_alias('node/add/story', 'add-node-story');
path_set_alias('admin', 'administration');
$forum_term = array(
'vid' => variable_get('forum_nav_vocabulary', 0),
'name' => 'Test Forum Term',
);
taxonomy_save_term($forum_term);
$vocab = array(
'name' => 'test vocab',
'multiple' => 0,
'required' => 0,
'hierarchy' => 0,
'module' => 'taxonomy',
'nodes' => array(
'page',
'story',
),
);
taxonomy_save_vocabulary($vocab);
$vocab = taxonomy_vocabulary_load($vocab['vid'], TRUE);
$term = array(
'vid' => $vocab->vid,
'name' => 'Test Term',
);
taxonomy_save_term($term);
$term = taxonomy_get_term($term['tid']);
path_set_alias(taxonomy_term_path($term), 'test-term');
}
protected function _globalredirect_batch_test() {
$settings = _globalredirect_get_settings();
$this
->assert('pass', '<pre>' . print_r($settings, TRUE) . '</pre>');
$test_paths = array(
array(
'request' => '',
'return-code' => 200,
'expected-path' => '',
),
array(
'request' => 'node',
'return-code' => $settings['frontpage_redirect'] ? 301 : 200,
'expected-path' => $settings['frontpage_redirect'] ? '' : 'node',
),
array(
'request' => 'node/1',
'return-code' => 301,
),
array(
'request' => 'node/add/story',
'return-code' => $settings['menu_check'] ? 403 : 301,
),
array(
'request' => 'user/' . $this->loggedInUser->uid . '/',
'return-code' => $settings['deslash'] ? 301 : 200,
'expected-path' => 'user/' . $this->loggedInUser->uid,
),
array(
'request' => url('<front>', array(
'absolute' => TRUE,
)),
'options' => array(
'query' => array(
'q' => 'node/1',
),
'external' => TRUE,
),
'return-code' => 301,
'expected-path' => 'test-node',
),
array(
'request' => url('<front>', array(
'absolute' => TRUE,
)),
'options' => array(
'query' => array(
'q' => 'node/add/page',
),
'external' => TRUE,
),
'return-code' => $settings['nonclean_to_clean'] ? 301 : 200,
'expected-path' => $settings['nonclean_to_clean'] ? 'node/add/page' : '?q=node/add/page',
),
array(
'request' => url('<front>', array(
'absolute' => TRUE,
)),
'options' => array(
'query' => array(
'q' => 'http://www.example.com',
),
'external' => TRUE,
),
'return-code' => 404,
),
array(
'request' => url('<front>', array(
'absolute' => TRUE,
)),
'options' => array(
'query' => array(
'q' => 'http://www.example.com/',
),
'external' => TRUE,
),
'return-code' => 404,
),
array(
'request' => url('<front>', array(
'absolute' => TRUE,
)) . '?q=http://www.example.com',
'options' => array(
'external' => TRUE,
),
'return-code' => 404,
),
array(
'request' => url('<front>', array(
'absolute' => TRUE,
)) . '?q=http://www.example.com/',
'options' => array(
'external' => TRUE,
),
'return-code' => 404,
),
array(
'request' => 'node/1',
'options' => array(
'query' => array(
'destination' => 'http://www.example.com/',
),
),
'return-code' => 301,
'expected-path' => url('test-node', array(
'absolute' => TRUE,
'query' => array(
'destination' => 'http://www.example.com/',
),
)),
),
array(
'request' => 'Test-Node',
'return-code' => $settings['case_sensitive_urls'] ? 301 : 200,
'expected-path' => $settings['case_sensitive_urls'] ? 'test-node' : 'Test-Node',
),
array(
'request' => 'taxonomy/term/1',
'return-code' => $settings['term_path_handler'] ? 301 : 200,
'expected-path' => $settings['term_path_handler'] ? 'forum/1' : 'taxonomy/term/1',
),
array(
'request' => 'node/1/0',
'return-code' => $settings['trailing_zero'] == 1 ? 301 : 200,
'expected-path' => $settings['trailing_zero'] == 1 ? 'test-node' : 'node/1/0',
),
array(
'request' => 'taxonomy/term/2/0',
'return-code' => $settings['trailing_zero'] > 0 ? 301 : 200,
'expected-path' => $settings['trailing_zero'] > 0 ? 'test-term' : 'taxonomy/term/2/0',
),
array(
'request' => 'taxonomy/term/10/0',
'return-code' => $settings['trailing_zero'] > 0 ? 301 : 404,
'expected-path' => $settings['trailing_zero'] > 0 ? 'taxonomy/term/10' : 'taxonomy/term/10/0',
),
array(
'request' => 'admin',
'return-code' => $settings['ignore_admin_path'] > 0 ? 200 : 301,
'expected-path' => $settings['ignore_admin_path'] > 0 ? 'admin' : 'administration',
),
);
foreach ($test_paths as $id => $path) {
$path += array(
'options' => array(),
);
$path['options'] += array(
'absolute' => TRUE,
'alias' => TRUE,
);
$request_path = url($path['request'], $path['options']);
$info = array(
'!id' => $id,
'!url' => $request_path,
);
$this
->pass(t(INFO_MESSAGE, $info), 'GlobalRedirect');
$this
->drupalHead($request_path, array(
'alias' => TRUE,
));
$headers = $this
->drupalGetHeaders(TRUE);
$result = array(
'!expected_status' => $path['return-code'],
'!location' => isset($headers[0]['location']) ? $headers[0]['location'] : 'N/A',
'!status' => $headers[0][':status'],
);
if ($path['return-code'] != 301) {
$result['!expected_path'] = 'N/A';
}
else {
$url_options = array(
'absolute' => TRUE,
);
if (isset($path['expected-path'])) {
$expected = $path['expected-path'];
$url_options['alias'] = TRUE;
}
else {
$expected = $path['request'];
}
$result['!expected_path'] = url($expected, $url_options);
}
if (strpos($result['!status'], (string) $result['!expected_status']) !== FALSE) {
if ($result['!expected_status'] == 301 && $result['!location'] == $result['!expected_path']) {
$this
->pass(t(SUCCESS_MESSAGE, $result), 'GlobalRedirect');
}
elseif ($result['!expected_status'] != 301) {
$this
->pass(t(SUCCESS_MESSAGE, $result), 'GlobalRedirect');
}
else {
$this
->fail(t(ERROR_MESSAGE, $result), 'GlobalRedirect');
$this
->fail('<pre>' . print_r($headers, TRUE) . '</pre>');
}
}
else {
$this
->fail(t(ERROR_MESSAGE, $result), 'GlobalRedirect');
$this
->fail('<pre>' . print_r($headers, TRUE) . '</pre>');
}
}
}
}
class GlobalRedirectTestCaseDefault extends GlobalRedirectTestCase {
public static function getInfo() {
return array(
'name' => '1. Global Redirect - Default Settings',
'description' => 'Ensure that Global Redirect functions correctly',
'group' => 'Global Redirect',
);
}
function testGlobalRedirect() {
variable_set('globalredirect_settings', array(
'deslash' => 1,
'menu_check' => 0,
'nonclean_to_clean' => 1,
'case_sensitive_urls' => 1,
'term_path_handler' => 0,
'frontpage_redirect' => 1,
'trailing_zero' => 0,
'ignore_admin_path' => 1,
));
$this
->_globalredirect_batch_test();
}
}
class GlobalRedirectTestCaseConfigAlpha extends GlobalRedirectTestCase {
public static function getInfo() {
return array(
'name' => '2. Global Redirect - Config Alpha',
'description' => 'Ensure that Global Redirect functions correctly',
'group' => 'Global Redirect',
);
}
function testGlobalRedirect() {
variable_set('globalredirect_settings', array(
'deslash' => 0,
'menu_check' => 1,
'nonclean_to_clean' => 0,
'case_sensitive_urls' => 0,
'term_path_handler' => 1,
'frontpage_redirect' => 0,
'trailing_zero' => 1,
'ignore_admin_path' => 0,
));
$this
->_globalredirect_batch_test();
}
}