1
votes

In order to use the system i'm trying to test, you need to be an authenticated user. After you sign in, a $_SESSION variable is created, containing the user id and user_name, and while you're logged in. This session is always checked and if this session variable doesn't exist, the user is redirected to the login page. I'm able to sign in using behat, but as soon as it enters the system, i'm redirected back to the login page. If i comment the lines responsible for the $_SESSION verification, it works as expected. Do you guys know any way to sign in and keep the session? I've read a lot of solutions using laravel or symfony, but i'm not using any framework.

Here's the feature code:

Feature:
    In order to use the system, the user needs a valid login first

Scenario: Loading the log in page
    Given I am on "/login.php"
    Then I should see "Login"

@javascript
Scenario: Logging in with the right credentials 
    Given I am signed in
    And I am on "/students"
    And I wait 10 seconds
    Then I should see "logout"

And here is part of the mink context code:

/**
 * @Given /^(?:|I )am signed in$/
 */
public function signIn()
{

    session_start();
    $_SESSION['admin']["user_name"] = 'admin';
    $_SESSION['admin']["id"] = '1';
    $this->getSession()->getDriver()->setCookie(session_name(), session_id());
    session_commit();
}
1
So, do you mean the session is over after you run another test suite? - Stan E
No. I mean: if i have a scenario in which i sign in and get redirected to the next page, that checks if i have a admin session, i get kicked out. As if this admin session was never set. - Supwr
That's weird. Cam you shoe yr tests? - Stan E
I continued to look for some solution. No success. - Supwr
Have you found a solution ? - Thomas Fournet

1 Answers

0
votes

The PHP super global variable $_SESSION will be reset after each Scenario. You can call a login in each Sceanrio to minimize your problem.

/* @Given I am logged in as :username with password :password
 *
 * @param $username
 * @param $password
 */
public function iAmLoggedInAs($username, $password)
{
    $session = $this->getSession();

    $session->visit("[URL]");

    $page = $session->getPage();

    $page->find("named", array("id_or_name", "[id]"))->setValue($username);
    $page->find("named", array("id_or_name", "[id]"))->setValue($password);
    $page->find("named", array("id_or_name", "[submit button]"))->click();
}