Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6055491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:16:13+00:00 2026-05-23T08:16:13+00:00

I currently trying to test a symfony app that has secured pages, using functional

  • 0

I currently trying to test a symfony app that has secured pages, using functional tests.

Scenario

  1. Log in as a user that is a superadmin (thus bypassing any credential check)
  2. Browse to a secured page (here displaying a list of documents)
  3. Make some various tests to check various requirements.

Problem

When I browse the secured page, instead of a 200 HTTP code, I get a 401 "Unauthorized" HTTP code, meaning I’m trying to access a page without being authenticated/without having the right credentials.

Even if I log as a normal user, that have the proper symfony credentials, I still get a 401 error.

Code used

//CapWebndfTestFunctional.class.php

class CapWebndfTestFunctional extends sfTestFunctional
{
  /**
* Login as a user
* @param string $username User's username
*/
  public function signInAs($username)
  {
    $user = Doctrine_Core::getTable('sfGuardUser')->findOneByUsername($username);

    //force the context to be created
    $this->browser->getContext(true)->getUser()->signIn($user);

    $this->info(sprintf('Signin user using username "%s"', $username));

    if ($user->getIsSuperAdmin())
    {
      $this->info('Beware, you are logged as a superadmin!');
    }

    return $this;
  }

}

Test suite:

<?php

$browser = new CapWebndfTestFunctional(new sfBrowser());

$browser->
  signInAs('superadmin')->
  with('user')->begin()->
    isAuthenticated()-> //ensure that I'm well authenticated
  end()->

  get('/document')-> //a secured page
  with('request')->begin()->
    isParameter('module', 'document')->
    isParameter('action', 'index')->
  end()->

  with('response')->begin()->
    isStatusCode(200)-> //as superadmin is a sfGuard superadmin, this should be 200;
  end()->
  // Here some other tests...

/* Outputs :
clem@ubuntu:/var/www/cap_webndf/trunk$ php symfony test:functional backend documentActions
> Signin user using username "superadmin"
> Beware, you are logged as a superadmin!
ok 1 - user is authenticated
# get /document
ok 2 - request parameter module is document
ok 3 - request parameter action is index
not ok 4 - status code is 200
# Failed test (./lib/vendor/symfony/lib/test/sfTesterResponse.class.php at line 412)
# got: 401
# expected: 200
*/

I’m really stuck on this one, so thank you for any help.

Edit: If you prefer, here is the used code on a gist

EDIT: What is the difference between going to the login page and sending a (fake) post request, and directly signin in using the dedicated sfGuard method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:16:14+00:00Added an answer on May 23, 2026 at 8:16 am

    I don’t think you can use SignIn() like that as it would not set the cookie within the browser so on the next page you would be logged out again.

    Whenever I have done functional testing with users I have gone to the login page and entered the username/password of the user and logged in as them.

    I haven’t tested this but something like this should work, I normally use test users and know their password

    //CapWebndfTestFunctionnal.class.php

    class CapWebndfTestFunctionnal extends sfTestFunctional
    {
      /**
    * Login as a user
    * @param string $username User's username
    */
      public function signInAs($username)
      {
        $tempPassword = rand(1000, 9999999);
    
        $user = Doctrine_Core::getTable('sfGuardUser')->findOneByUsername($username);
        $oldPassword = $user->getPassword();
    
        $user->setPassword($tempPassword);
        $user->save();
    
        $this->info(sprintf('Signin user using username "%s"', $username));
    
        $this->post('/login', array('username' => $user->getUsername(), 'password' => $tempPassword))->isRedirected();
    
        if ($user->getIsSuperAdmin())
        {
          $this->info('Beware, you are logged as a superadmin!');
        }
    
        $user->setPasswordHash($oldPassword);
        $user->save();
    
        return $this;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently trying to find a solution, how to ensure that a test fails
I have a DataAnnotationValidator that I created. I am currently trying to test it
I'm currently trying to test a strcat() function that I wrote myself. Instead of
I am currently trying to build some test code that uses Google C++ Test
I'm currently trying to use a certain SDK that has me loading functions off
I'm currently trying to automate a test case using the White UI-Automation framework .
I'm currently trying to learn proper unit-test. So now I'm trying to write unit-tests
I am currently trying to make an Android App that communicates with Google App
I'm currently debugging some fairly complex persistence code, and trying to increase test coverage
i'm trying to write an unit test for my Android's RelativeLayout. Currently, my testcode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.