2008-06-09 08:11:45 +00:00
<?php
2008-07-19 12:31:14 +00:00
// $Id$
2008-06-09 08:11:45 +00:00
class CommonFormatSizeTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Format size test'),
'description' => t('Parse a predefined amount of bytes and compare the output with the expected value.'),
'group' => t('System')
);
}
/**
* Implementation of setUp().
*/
function setUp() {
$this->exact_test_cases = array(
'1 byte' => 1, // byte
'1 KB' => 1000, // kilobyte
'1 MB' => 1000000, // megabyte
'1 GB' => 1000000000, // gigabyte
'1 TB' => 1000000000000, // terabyte
'1 PB' => 1000000000000000, // petabyte
'1 EB' => 1000000000000000000, // exabyte
'1 ZB' => 1000000000000000000000, // zettabyte
'1 YB' => 1000000000000000000000000, // yottabyte
);
$this->rounded_test_cases = array(
'2 bytes' => 2, // bytes
'1 MB' => 999999, // 1 MB (not 1000 kilobyte!)
'3.62 MB' => 3623651, // megabytes
'67.23 PB' => 67234178751368124, // petabytes
'235.35 YB' => 235346823821125814962843827, // yottabytes
);
parent::setUp();
}
/**
* testCommonFormatSize
*/
function testCommonFormatSize() {
foreach (array($this->exact_test_cases, $this->rounded_test_cases) as $test_cases) {
foreach ($test_cases as $expected => $size) {
$this->assertTrue(
($result = format_size($size, NULL)) == $expected,
$expected . " == " . $result . " (" . $size . " bytes) %s"
);
}
}
}
}
2008-08-03 18:34:21 +00:00
/**
* Test drupal_explode_tags() and drupal_implode_tags().
*/
class DrupalTagsHandlingTestCase extends DrupalWebTestCase {
var $validTags = array(
'Drupal' => 'Drupal',
'Drupal with some spaces' => 'Drupal with some spaces',
'"Legendary Drupal mascot of doom: ""Druplicon"""' => 'Legendary Drupal mascot of doom: "Druplicon"',
'"Drupal, although it rhymes with sloopal, is as awesome as a troopal!"' => 'Drupal, although it rhymes with sloopal, is as awesome as a troopal!',
);
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Drupal tags handling'),
'description' => t("Performs tests on Drupal's handling of tags, both explosion and implosion tactics used."),
'group' => t('System')
);
}
/**
* Explode a series of tags.
*/
function testDrupalExplodeTags() {
$string = implode(', ', array_keys($this->validTags));
$tags = drupal_explode_tags($string);
$this->assertTags($tags);
}
/**
* Implode a series of tags.
*/
function testDrupalImplodeTags() {
$tags = array_values($this->validTags);
// Let's explode and implode to our heart's content.
for ($i = 0; $i < 10; $i++) {
$string = drupal_implode_tags($tags);
$tags = drupal_explode_tags($string);
}
$this->assertTags($tags);
}
/**
* Helper function: asserts that the ending array of tags is what we wanted.
*/
function assertTags($tags) {
$original = $this->validTags;
foreach ($tags as $tag) {
$key = array_search($tag, $original);
$this->_assert($key !== FALSE, t('Make sure tag %tag shows up in the final tags array (originally %original)', array('%tag' => $tag, '%original' => $key)));
unset($original[$key]);
}
foreach ($original as $leftover) {
$this->_assert(FALSE, t('Leftover tag %leftover was left over.', array('%leftover' => $leftover)));
}
}
2008-08-13 07:11:18 +00:00
}
/**
* Test drupal_http_request().
*/
class DrupalHTTPRequestTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Drupal HTTP request'),
'description' => t("Performs tests on Drupal's HTTP request mechanism."),
'group' => t('System')
);
}
2008-09-02 17:00:34 +00:00
/**
* Implementation of setUp().
*/
function setUp() {
parent::setUp('system_test');
}
2008-08-13 07:11:18 +00:00
function testDrupalHTTPRequest() {
2008-08-29 14:45:19 +00:00
// Parse URL schema.
2008-08-13 07:11:18 +00:00
$missing_scheme = drupal_http_request('example.com/path');
2008-09-02 17:00:34 +00:00
$this->assertEqual($missing_scheme->error, 'missing schema', t('Returned with "missing schema" error.'));
2008-08-13 07:11:18 +00:00
$unable_to_parse = drupal_http_request('http:///path');
2008-09-02 17:00:34 +00:00
$this->assertEqual($unable_to_parse->error, 'unable to parse URL', t('Returned with "unable to parse URL" error.'));
2008-08-29 14:45:19 +00:00
// Fetch page.
$result = drupal_http_request(url('node', array('absolute' => TRUE)));
$this->assertEqual($result->code, 200, t('Fetched page successfully.'));
$this->drupalSetContent($result->data);
$this->assertTitle(variable_get('site_name', 'Drupal'), t('Site title matches.'));
2008-08-13 07:11:18 +00:00
}
2008-09-02 17:00:34 +00:00
function testDrupalHTTPRequestBasicAuth() {
$username = $this->randomName();
$password = $this->randomName();
$url = url('system-test/auth', array('absolute' => TRUE));
$auth = str_replace('http://', 'http://' . $username . ':' . $password .'@', $url);
$result = drupal_http_request($auth);
// We use strpos directly.
// assertRaw() cannot be used because we are getting the data
// in a variable instead of $this->_content.
$this->assertTrue(strpos($result->data, $username) !== FALSE, t('$_SERVER[\'PHP_AUTH_USER\'] is passed correctly.'));
$this->assertTrue(strpos($result->data, $password) !== FALSE, t('$_SERVER[\'PHP_AUTH_PW\'] is passed correctly.'));
}
function testDrupalHTTPRequestRedirect() {
$redirect_301 = drupal_http_request(url('system-test/redirect/301', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
$this->assertEqual($redirect_301->redirect_code, 200, t('drupal_http_request follows the 301 redirect.'));
$redirect_301 = drupal_http_request(url('system-test/redirect/301', array('absolute' => TRUE)), array(), 'GET', NULL, 0);
$this->assertFalse(isset($redirect_301->redirect_code), t('drupal_http_request does not follow 301 redirect if $retry = 0.'));
$redirect_invalid = drupal_http_request(url('system-test/redirect-noscheme', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
$this->assertEqual($redirect_invalid->error, 'missing schema', t('301 redirect to invalid URL returned with error "!error".', array('!error' => $redirect_invalid->error)));
$redirect_invalid = drupal_http_request(url('system-test/redirect-noparse', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
$this->assertEqual($redirect_invalid->error, 'unable to parse URL', t('301 redirect to invalid URL returned with error "!error".', array('!error' => $redirect_invalid->error)));
$redirect_invalid = drupal_http_request(url('system-test/redirect-invalid-scheme', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
$this->assertEqual($redirect_invalid->error, 'invalid schema ftp', t('301 redirect to invalid URL returned with error "!error".', array('!error' => $redirect_invalid->error)));
$redirect_302 = drupal_http_request(url('system-test/redirect/302', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
$this->assertEqual($redirect_302->redirect_code, 200, t('drupal_http_request follows the 302 redirect.'));
$redirect_302 = drupal_http_request(url('system-test/redirect/302', array('absolute' => TRUE)), array(), 'GET', NULL, 0);
$this->assertFalse(isset($redirect_302->redirect_code), t('drupal_http_request does not follow 302 redirect if $retry = 0.'));
$redirect_307 = drupal_http_request(url('system-test/redirect/307', array('absolute' => TRUE)), array(), 'GET', NULL, 1);
$this->assertEqual($redirect_307->redirect_code, 200, t('drupal_http_request follows the 307 redirect.'));
$redirect_307 = drupal_http_request(url('system-test/redirect/307', array('absolute' => TRUE)), array(), 'GET', NULL, 0);
$this->assertFalse(isset($redirect_307->redirect_code), t('drupal_http_request does not follow 307 redirect if $retry = 0.'));
}
function testDrupalGetDestination() {
$query = $this->randomName(10);
$url = url('system-test/destination', array('absolute' => TRUE, 'query' => $query));
$this->drupalGet($url);
$this->assertText($query, t('The query passed to the page is correctly represented by drupal_get_detination().'));
}
2008-08-13 07:11:18 +00:00
}