1

I want to test a module that adds an additional login validation clause. Only users whitelisted somewhere else are allowed to log in to the system.

In order to test this behaviour with Simpletest, I would like to assert that a random user has no access to the system.

I have tried to create this random user and then to call drupalLogin(). Unfortunately, my test fails because drupalLogin() is checking whether the log in succeeded, by calling assertLink() internally.

This is what I have tried:

    $user = $this->drupalCreateUser(array());
    $user->roles[] = 'authenticated user';
    user_save($user);

    // Try to login with it
    // It fails, because of the additional validation performed by my custom module
    $this->drupalLogin($user);

    // I would like to assert it here instead:
    $this->assertFalse(isset($this->loggedInUser->uid));

What should I do in order to avoid the assertion to fail at drupalLogin(), so that I can assert it myself?

Any help will be appreciated.

1

In case someone else wonders about this... It wasn't difficult, but I didn't realise at first that it can be done by copying part of the DrupalWebTestCase::drupalLogin() source code:

$edit = array(
  'name' => $user->name,
  'pass' => $user->pass_raw,
);
$this->drupalPost('user', $edit, t('Log in'));

And then asserting that the user could not log in:

// Assert that the user is not logged in
$this->assertNoLink(t('Log out'), 0, t('User not logged in.'), t('User login'));
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.