Ask your Symfony questions! Pay money and get answers fast! (more info)

Can not signin in my functional tests Symfony

  • SOLVED

I've tried every variation I can think on all of the tutorials I've read, and I still can not get this to work.

I'm using Symfony 1.4.

[[LINK href="http://sandbox-ws.com/2009/01/functional-testing-a-secured-module/"]]I thought this bit from Sandbox Web Solutions would work for me, but perhaps it is for an older version of Symfony. [[/LINK]]

Here is what I have right now:

include(dirname(__FILE__).'/../../bootstrap/functional.php');

$username = 'xxx';
$password = 'xxx';

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

$browser->
get('/restaurant_award/index')->
signin($username, $password)->
get('/restaurant_award/index')->
with('response')->begin()->
isStatusCode(200)->
end()
;

$browser->
get('/restaurant_award/index')->

with('request')->begin()->
isParameter('module', 'restaurant_award')->
isParameter('action', 'index')->
end()->

with('response')->begin()->
checkElement('body', '!/This is a temporary page/')->
end()

;


I do not get an outright failure, but I do get a warning, which I do not want:

PHP sent a "warning" error at /home/lkrubner/dev/tastingnotes/lib/vendor/symfony/lib/test/sfTestFunctionalBase.class.php line 433 (call_user_func_array(): First argument is expected to be a valid callback, 'sfBrowser::isRedirected' was given)

I did put this in my lib folder:

<?php

class sfGuardTestFunctional extends sfTestFunctional {

public function signin($username, $password) {
return $this->
info(sprintf('Signin user using username "%s" and password "%s"', $username, $password))->
post('/sfGuardAuth/signin', array('signin' => array('username' => $username, 'password' => $password,)))->
isRedirected()
;
}
}



Any thoughts on what causes this warning?


[UPDATE]

[[LINK href="http://dev.esl.eu/blog/2009/09/02/writing-functional-tests-for-http-auth-secured-actions/"]]I'm trying this code:[[/LINK]]


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

$browser->setAuth($username, $password)->

get('/restaurant_award/some_action')->

isForwardedTo('sfGuardAuth', 'signin')->

// it redirects back to the original uri
isRedirected(true)->

// we have to follow the redirect, otherwise we do not get
// the correct response
followRedirect()->

// did we recieved the correct response?
with('request')->begin()->
isParameter('module', 'restaurant_award')->
isParameter('action', 'index')->
end()->

responseContains('foobar');


Now I get:

Fatal error: Call to a member function getModuleName() on a non-object in /home/lkrubner/dev/tastingnotes/lib/vendor/symfony/lib/test/sfTestFunctional.class.php on line 62


I assume there is some mismatch between the code I'm borrowing and the actual version of Symfony I have installed: 1.4.

Can anyone point to an example of login code that works with 1.4? I can not imagine this is difficult. I assume developers do this every day?

<strong>
[[UPDATE]]</strong>

Please read what I posted below in the comments.

I almost wonder if I'm the sfBrowser is getting the wrong page, perhaps a 404, or the wrong sigin page. Is there a way I can see the raw HTML that is coming back to sfBrowser?

I should note that the whole application is locked down. I know there was a bug regarding that in 2009, but I assume it is long fixed.



[UPDATE]

Okay, I tried this:

$browser->get('/signin')->
with('response')->debug();


and got this:


./symfony test:functional tastingnotes RestaurantAwardsActions
# get /signin


Response debug


# WARNING
# An error occurred when processing this request.
# The real response content has been replaced with the exception message to ease debugging.
HTTP/1.X 404
Content-Type: text/html; charset=utf-8

exception 'sfError404Exception' with message 'Action "signin/index" does not exist.' in /home/lkrubner/dev/tastingnotes/lib/vendor/symfony/lib/controller/sfController.class.php:196

Stack trace:

#0 /home/lkrubner/dev/tastingnotes/lib/vendor/symfony/lib/controller/sfFrontWebController.class.php(48): sfController->forward('signin', 'index')

#1 /home/lkrubner/dev/tastingnotes/lib/vendor/symfony/lib/util/sfBrowser.class.php(43): sfFrontWebController->dispatch()

#2 /home/lkrubner/dev/tastingnotes/lib/vendor/symfony/lib/util/sfBrowserBase.class.php(323): sfBrowser->doCall()

#3 /home/lkrubner/dev/tastingnotes/lib/vendor/symfony/lib/test/sfTestFunctionalBase.class.php(246): sfBrowserBase->call('/signin', 'get', Array, true)

#4 /home/lkrubner/dev/tastingnotes/lib/vendor/symfony/lib/test/sfTestFunctionalBase.class.php(184): sfTestFunctionalBase->call('/signin', 'get', Array, true)

#5 /home/lkrubner/dev/tastingnotes/test/functional/tastingnotes/RestaurantAwardsActionsTest.php(12): sfTestFunctionalBase->get('/signin')

#6 /home/lkrubner/dev/tastingnotes/lib/vendor/symfony/lib/task/test/sfTestFunctionalTask.class.php(89): include('/home/lkrubner/...')

#7 /home/lkrubner/dev/tastingnotes/lib/vendor/symfony/lib/task/sfBaseTask.class.php(68): sfTestFunctionalTask->execute(Array, Array)

#8 /home/lkrubner/dev/tastingnotes/lib/vendor/symfony/lib/task/sfTask.class.php(97): sfBaseTask->doRun(Object(sfCommandManager), NULL)

#9 /home/lkrubner/dev/tastingnotes/lib/vendor/symfony/lib/command/sfSymfonyCommandApplication.class.php(76): sfTask->runFromCLI(Object(sfCommandManager), NULL)

#10 /home/lkrubner/dev/tastingnotes/lib/vendor/symfony/lib/command/cli.php(20): sfSymfonyCommandApplication->run()

#11 /home/lkrubner/dev/tastingnotes/symfony(14): include('/home/lkrubner/...')

#12 {main}

# Looks like everything went fine.



So, '/signin' seems to get a 404 error, even though I can go to in my browser without a problem.



[UPDATE]

Apparently I was dealing with the same bug described here:

[[LINK href="http://sandbox-ws.com/2009/01/functional-testing-a-secured-module/"]]http://sandbox-ws.com/2009/01/functional-testing-a-secured-module/[[/LINK]]

Sam wrote:

<blockquote>I have noticed some strange behaviour. If you enable security globally and tries to access a non existing module/action via browser you get displayed a 404 page correctly, but if you try that as a functional test you will be redirected to the login page. Does this occur with sfGuard too? I wanted to test the security of some module to ensure that the correct actions are permitted and others not. But this incorrect rendering of the scenario is somehow not very helpful</blockquote>

That bug existed in 2009. I'm surprised it was not fixed yet.

Answers (2)

2011-03-16

Francesco Tassi answers:

use followRedirect() instead of isRedirected()


Lawrence Krubner comments:

I get "The request was not redirected. "

See the attached screenshot.