Hello all,
I am working in drupal 6.I want to make unit testing for below code.I used profile and alter that using hook_form_alter
to change that form.
function reference_number_form_alter(&$form, $form_state, $form_id) {
if ($form_id == "user_profile_form") {
if (isset($form['Personal information']['profile_birthday'])) {
$form['Personal information']['profile_birthday']['#type'] = 'date_popup';
$form['Personal information']['profile_birthday']['#date_format'] =
variable_get('date_format', 'd.m.Y');
$form['Personal information']['profile_birthday']
['#date_year_range'] = '-20:+0';
if (empty($form['Personal information']['profile_birthday']
['#default_value'])) {
$s__old_date = mktime(0, 0, 0, date("m"), date("d"), date("Y") - 20);
$s__date = date('Y-m-d', $s__old_date);
$form['Personal information']['profile_birthday']
['#default_value'] = $s__date;
}
$sm__ref_id = db_result(db_query("SELECT id_ref FROM {reference_number}
WHERE uid = %d", $user->uid));
$form['Personal information']['ref_no'] = array(
'#type' => 'item',
'#title' => t('Reference Number'),
'#value' => $sm__ref_id,
'#weight' => -2,
);
}
}
}
=>unit testing code for above code
class profileDisplayCalTestCase extends DrupalWebTestCase {
/**
* Implementation of getInfo().
*/
function getInfo() {
return array(
'name' => t('Display calendar'),
'description' => t('Test to display a Reference Number.'),
'group' => t('reference_number'),
);
}
function setUp() {
global $db_prefix;
parent::setUp('reference_number', 'profile');
$sm__table_nm = $db_prefix ."profile_fields";
db_query("insert into $sm__table_nm(fid, title, name, explanation, category,
page, type, weight, required, register, visibility, autocomplete, options)
SELECT * FROM profile_fields");
/*$sm__table_nm = $db_prefix ."profile_values";
db_query("insert into $sm__table_nm(fid, title, name, explanation, category,
page, type, weight, required, register, visibility, autocomplete, options)
SELECT * FROM profile_fields");*/
}
function tearDown() {
parent::tearDown();
}
function testDisplayProfileNumber() {
variable_set('user_register', 1);
$name = $this->randomName();
$mail = "$name@example.com";
$edit = array('name' => $name,
'mail' => $mail);
$edit = array();
$edit['name'] = $name = $this->randomName();
$edit['mail'] = $mail = $edit['name'] .'@example.com';
$this->drupalPost('user/register', $edit, t('Create new account'));
$this->assertText(t('Your password and further instructions have been sent to your e-mail address.'), t('User registered successfully.'));
$this->assertText(t('Your password and further instructions have been sent to your e-mail address.'), 'Your password and further instructions ... found');
$this->assertNoText(t('The name %name has been denied access.', array('%name' => $name)), 'not denied access');
// now we check database fields
// we can use an 'edit' array to load user variable
// Check database for created user.
$user = user_load($edit);
$this->assertTrue($user, t('User found in database.'));
$this->assertTrue($user->uid > 0, t('User has valid user id = %id.', array('%id' => $user->uid)));
//$this->drupalPost('user/'. $user->uid .'/edit/Personal information', $edit, t('Personal information'));
//$this->drupalPost('user/'. $user->uid .'/edit', $edit, t('Edit user'));
$this->drupalGet('user/'. $user->uid .'/edit');
//$this->drupalPost('user/'. $user->uid .'/edit/Personal information', $edit, t('Personal information'));
//$this->clickLink('Personal information', '0');
$this->drupalGet('user/'. $user->uid .'/edit/Personal information');
$this->assertRaw(t('Personal information'), t('Personal information'));
$this->assertRaw(t('Reference Number: '), t('Reference Number: '));
$id_ref = generate_reference_number($user->uid);
$this->assertRaw($id_ref, $id_ref);
$this->assertRaw(t('Name: '), t('Name: '));
$this->assertRaw('*', '*');
$this->assertRaw(t('Title:'), t('Title:'));
$this->assertRaw('*', '*');
$this->assertRaw('Address : ', 'Address : ');
$this->assertRaw('Switch to plain text editor', 'Switch to plain text editor');
$this->assertRaw('Place of residence: ', 'Place of residence: ');
$this->assertRaw('*', '*');
$this->assertRaw('Phone number: ', 'Phone number: ');
$this->assertRaw('Birthday: ', 'Birthday: ');
$this->assertRaw(' Format: 16.03.2009', ' Format: 16.03.2009');
$this->assertRaw('pragna', "Page title");
$edit = array (
'profile_name' => 'aaaa',
'profile_title' => 'aaaa',
'profile_address' => '
aaaaaa
',
'profile_place_of_residence' => 'aaaa',
'profile_phone_number' => '3434343',
'profile_birthday[date]' => '22.03.2000',
'op' => 'Save',
);
//$this->drupalPost('user/'. $user->uid .'/edit/Personal information', $edit, 'Save');
$this->drupalPost('user/'. $user->uid .'/edit/Personal information', $edit, 'Save');
$this->drupalGet('user/'. $user->uid .'/edit/Personal information');
$this->assertRaw('The changes have been saved.', 'The changes have been saved.');
}
}
=> Result of simpletest, there is fail test case is
GET to http://localhost/suomen/user/3/edit/Personal%20information, response is 4108 bytes. Browser drupal_web_test_case.inc 997 DrupalWebTestCaseCore->curlExec()
Valid HTML found on "http://localhost/suomen/user/3/edit/Personal%20information" Browser drupal_web_test_case.inc 369 DrupalWebTestCaseCore->pass()
Personal information Other reference_number.test 254 profileDisplayCalTestCase->testDisplayProfileNumber()
Reference Number: Other reference_number.test 255 profileDisplayCalTestCase->testDisplayProfileNumber()
0039 Other reference_number.test 258 profileDisplayCalTestCase->testDisplayProfileNumber()
Name: Other reference_number.test 259 profileDisplayCalTestCase->testDisplayProfileNumber()
* Other reference_number.test 260 profileDisplayCalTestCase->testDisplayProfileNumber()
Title: Other reference_number.test 261 profileDisplayCalTestCase->testDisplayProfileNumber()
* Other reference_number.test 262 profileDisplayCalTestCase->testDisplayProfileNumber()
Address : Other reference_number.test 263 profileDisplayCalTestCase->testDisplayProfileNumber()
Switch to plain text editor Other reference_number.test 264 profileDisplayCalTestCase->testDisplayProfileNumber()
Place of residence: Other reference_number.test 265 profileDisplayCalTestCase->testDisplayProfileNumber()
* Other reference_number.test 266 profileDisplayCalTestCase->testDisplayProfileNumber()
Phone number: Other reference_number.test 267 profileDisplayCalTestCase->testDisplayProfileNumber()
Birthday: Other reference_number.test 268 profileDisplayCalTestCase->testDisplayProfileNumber()
Format: 16.03.2009 Other reference_number.test 269 profileDisplayCalTestCase->testDisplayProfileNumber()
Page title Other reference_number.test 270 profileDisplayCalTestCase->testDisplayProfileNumber()
Comments
Comment #1
boombatower commentedCan you summarize, very hard to read/get the gist of.
Also when posting PHP code please put
<?php ?>around the code so it is highlighted.Comment #2
pragna commentedI solved above problem