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.

Comments

chanhongguan’s picture

Version: 6.x-2.8 » 6.x-2.x-dev

I tried it for 6.x-2.x too and the rest is the same too

boombatower’s picture

The 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.

file_put_contents('output.html', $this->drupalGetContent());
boombatower’s picture

Status: Active » Postponed (maintainer needs more info)
chanhongguan’s picture

Status: Postponed (maintainer needs more info) » Patch (to be ported)
StatusFileSize
new708 bytes

Thanks boombatower for the help.

<?php
file_put_contents('output.html', $this->drupalGetContent());
?>

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.

       $teacher = $this->drupalCreateUser(NULL, 'teacher');

Simpletest is turning out to be very more handy than I expected.

boombatower’s picture

Title: unable to submit value of select element » Support custom role name in drupalCreaseUser()
Project: SimpleTest » Drupal core
Version: 6.x-2.x-dev » 7.x-dev
Component: Code » simpletest.module
Category: support » feature

This is something that should be decided and committed to Drupal 7 core SimpleTest and then backported.

boombatower’s picture

Assigned: Unassigned » boombatower
Status: Patch (to be ported) » Needs review
StatusFileSize
new2.14 KB

I 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.

chanhongguan’s picture

Title: Support custom role name in drupalCreaseUser() » Support custom role name in drupalCreateUser()
StatusFileSize
new2.27 KB

Sorry 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.

boombatower’s picture

Heh, defeats the point. Nice catch.

/me reminds self to check simple patches.

mr.baileys’s picture

+1. Looks like a clean and simple improvement to me.

Comments should be full sentences, maybe something like:

+   *   The name of the role to create. If not set, a random name will be used.

instead of:

+   *   Name of role to create. If NULL then will use randomName().
dries’s picture

This 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:

  array(
    'my role 1' => array('permission 1', 'permission 2'),
    'my role 2' => array('permission 1', 'permission 3'),
  );

instead of:

  array('permission 1', 'permission 2')

I don't know. I'd like to see us discuss the API a bit more.

mr.baileys’s picture

I briefly pondered whether drupalCreateUser could be rewritten like:

drupalCreateUser(
  array(
    'username' => 'alpha',
    'mail' => 'alhpa@example.com',
    'role' =>
      array(
        'role_a' => array('access content');
      )
  )
);

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)

Status: Needs review » Needs work

The last submitted patch failed testing.

webchick’s picture

Status: Needs work » Needs review
chanhongguan’s picture

So, where do we go from here? What is the consensus? I hope this doesn't stay stagnant.

Status: Needs review » Needs work

The last submitted patch failed testing.

dave reid’s picture

Status: Needs work » Closed (won't fix)

We 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().