I have the following test method in bio.module:

  /**
   * Ensure that the "Use bio for user profiles" option is working properly.
   */
  function testBioProfile() {
    // Enable setting and login as basic user to test.
    $this->drupalVariableSet('bio_profile', 1);    
    $this->drupalLoginUser($this->basic_user);
    $uid = $this->basic_user->uid; # Not 1. 231 or whatever.

    // Check to see if link to edit bio shows up in user profile.
    $this->drupalGet("user/$uid");
    $this->assertWantedRaw("user/$uid/bio", t('Checking for link to edit bio on user profile'));

    // Fill in bio form.
    $edit['title'] = $this->randomName(32);
    $edit['body'] = $this->randomName(32);
    $this->drupalPostRequest("user/$uid/bio", $edit, t('Submit'));
    $content = $this->drupalGetContent();
    $this->assertNoUnwantedRaw('This user already has a Biography.', t('Checking for incorrect error message')); # FAIL
    
    // Check for bio information on user profile page.
    $html = $this->drupalGet("user/$uid");
    $this->assertText($edit['body'], t('Checking for bio content on user profile')); # FAIL
  }

The two lines marked # FAIL fail because this check in bio_nodeapi is getting fired. It thinks my user ID is still 1.

    case 'validate':
      // Ensure this user doesn't already have a bio node.
      $account = user_load(array('name' => $node->name));
      $nid = bio_for_user($account->uid);
      if ($nid && ($nid != $node->nid)) {
        form_set_error('name', t('This user already has a @bio. Edit it <a href="@link">here</a> or assign this entry to another user.', array('@bio' => node_get_types('name', $node), '@link' => url('node/'. $nid .'/edit'))));
      } 

Any ideas what could be happening?

Comments

webchick’s picture

Ah, ok.

bio_for_user uses the global $user variable's $uid when $uid is not otherwise set. global $user; within the context of a SimpleTest test, is still user 1.

Not sure if this is by design or not. Seems like it probably is, so if a test fails in the middle, you don't end up logged in as simpletest_x763.

webchick’s picture

Title: drupalLoginUser not fooling Bio module » drupalLoginUser does not change global $user

Ok. I think I figured out what's going on.

When the simpletest_XXXX user attempts to access user/123/bio, they invoke this check in bio_menu:

      else { # if user doesn't have a bio yet...
        $node = (object) array('type' => $type, 'uid' => arg(1));
        # arg(1) is something like 343. $user->uid is 1. this is the 'basic user' so doesn't have
        # access to administer nodes. ergo, access check fails.
        $access = (($user->uid == arg(1)) && node_access('create', $type)) || user_access('administer nodes');
      }

...and they end up at a big fat "Access denied" screen. Because Drupal doesn't care what kind of $_POST values you throw at an access denied screen, the drupalPostRequest() line doesn't fail.

So now, I guess the question is... is this behaviour by design or not? And can you recommend any better access checking solutions for bio module? In the meantime, I can work around this by creating the bio in a different way.

Rok Žlender’s picture

Hm so this doesnt make any sense to me. You are using drupalGet which uses internal SimpleTest browser which has no idea about your uid. So there must be another problem with code or your tests. Which module is this test for? Maybe I can have a look.

webchick’s picture

The test file is located at http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/bio/tests/b...

And the module file is located at http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/bio/bio.mod...

What I'm trying to test is heading to user/X/bio and creating the bio there, rather than node/add/bio. But it's erroneously telling me that my simpletest_XXXX user already has a bio, when it is in fact user 1 (the user I'm logged in as while running the tests) who does.

Rok Žlender’s picture

Ok wild shot but, if I see correctly you write this for D5 and drupalGet patch which removes need for url call just went it today so maybe this is why your tests were failing. I just tested testBioProfile with every other test commented out and I also uncommented/commented proper code so now

    $edit['title'] = $this->randomName(32);
    $edit['body'] = $this->randomName(32);
    $this->drupalPostRequest("user/$uid/bio", $edit, 'Submit');
    $this->drupalGet("user/$uid");
    $this->assertText($edit['body'], t('Checking for bio content on user profile'));

Produces only passes.

boombatower’s picture

Status: Active » Postponed (maintainer needs more info)

This needs a response from webchick.

boombatower’s picture

Status: Postponed (maintainer needs more info) » Closed (fixed)

Closing due to inactivity.