I have this form with an element name 'teacher'
$form['teacher'] = array(
'#type' => 'select',
'#options' => $teachers, //this is an array
'#title' => t('Teacher')
);
and i have a test over here
function testSomething() {
$teacher_mod = array();
$teacher_mod['teacher'] = $teacher->uid;
After i perform a drupalpost, i keep getting a unable to set field teacher to .
I modified the handleForm function a little to do some debugging.
protected function handleForm(&$post, &$edit, &$upload, $submit, $form) {
// Retrieve the form elements.
$elements = $form->xpath('.//input|.//textarea|.//select');
$submit_matches = FALSE;
foreach ($elements as $element) {
// SimpleXML objects need string casting all the time.
$name = (string) $element['name'];
// This can either be the type of or the name of the tag itself
// for
or .
$type = isset($element['type']) ? (string)$element['type'] : $element->getName();
$value = isset($element['value']) ? (string)$element['value'] : '';
/****************** this is the added line ************************/
error_log( "element =" . $name.'|'.$type.'|'.$value);
$done = FALSE;
...
}
The output I got was
[Fri May 15 15:07:24 2009] [error] [client xxx.xxx.xxx.xxx] element =teacher|sele
ct|, referer: http://somehost/drupal/batch?op=start&id=105
What went wrong? I guess it is the way I created the $teacher_mod.
Thanks.
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | 463354-custom-role-name.patch | 2.27 KB | chanhongguan |
| #6 | 463354-custom-role-name.patch | 2.14 KB | boombatower |
| #4 | drupal_web_test_case.php_.patch | 708 bytes | chanhongguan |
Comments
Comment #1
chanhongguan commentedI tried it for 6.x-2.x too and the rest is the same too
Comment #2
boombatower commentedThe value you set the select to is the key used in the options array?
Also try adding the following after the
$this->drupalPost()call and posting the result.Comment #3
boombatower commentedComment #4
chanhongguan commentedThanks boombatower for the help.
was very useful.
The value is used is the key. I figured out where I went wrong. Because I require specially-named role eg 'teacher' for the teacher to be shown in the 'teacher' select element, it did not show up in the list of options during the post. So I modified drupalCreateUser and _drupal_CreateRole in drupal_web_test_case.php so that it will accept a role as a parameter.
So, it can now be used like this.
Simpletest is turning out to be very more handy than I expected.
Comment #5
boombatower commentedThis is something that should be decided and committed to Drupal 7 core SimpleTest and then backported.
Comment #6
boombatower commentedI have created a patch, but it does not contain a test. I would like to get some more opinions on this.
Obviously we have a usecase and would seem like something that the test should be able to control if it needs to.
Comment #7
chanhongguan commentedSorry for the earlier patch. It was made against 6.x-2.x-dev
I made an updated patch. The one boombatower submitted did not pass $role_name to drupalCreateRole.
Comment #8
boombatower commentedHeh, defeats the point. Nice catch.
/me reminds self to check simple patches.
Comment #9
mr.baileys+1. Looks like a clean and simple improvement to me.
Comments should be full sentences, maybe something like:
instead of:
Comment #10
dries commentedThis seems to be a bit of a hack. What if I want to assign multiple roles? What if I want to have control over what permissions are associated with what role.
It almost sounds like we want to be able to pass in:
instead of:
I don't know. I'd like to see us discuss the API a bit more.
Comment #11
mr.baileysI briefly pondered whether drupalCreateUser could be rewritten like:
The reasoning was that some of the currently existing tests bypass drupalCreateUser because they need to set specific properties when creating a user.
In the end it looked like overkill though, since 99% of the tests don't seem to need it, and the others just create users themselves (bypassing drupalCreateUser)
Comment #13
webchickComment #14
chanhongguan commentedSo, where do we go from here? What is the consensus? I hope this doesn't stay stagnant.
Comment #16
dave reidWe should improve the core role and permission API is what we should do. #300993: User roles and permissions API We want to remove the special stuff from drupalCreateRole and drupalCreateUser().