Unit Test: After Yii::app()->user->logout(), user is still logged in, kind of

I cleard up this post since the second one describes the problem better. pleas scroll down to the third post.

hello,

is there a way to test against a logged-out user? what I mean is, you could test like




$this->assertTrue( Yii::app()->user->isGuest ) // this should return TRUE if you are logged out



just to see if that’s correct or not.

–i

I looked a bit closer to the problem and isolated the login/logout-section:

[list=1]

[*]Login with admin

[*][list=1]Check Yii::app()->user (see seperate function)[/list]

[*]Logout admin

[*][list=1]Check Yii::app()->user (see seperate function)[/list]

[*]Login with user

[*][list=1]Check Yii::app()->user (see seperate function)[/list]

[*]Logout user

[*][list=1]Check Yii::app()->user (see seperate function)[/list]

[/list]

This is the new test-method:




	public function testLoginLogout()

	{

		$identity = new UserIdentity('admin', 'admin');

		$identity->authenticate();

		Yii::app()->user->login($identity);

		

		$this->checkUser();

		

		Yii::app()->user->logout();  echo "logout()";


		$this->checkUser();

		

		$identity = new UserIdentity('user', 'user');

		$identity->authenticate();

		Yii::app()->user->login($identity);

		

		$this->checkUser();

		

		Yii::app()->user->logout();  echo "logout()";


		$this->checkUser();

		

	}


	private function checkUser()

	{

		echo "\n\nStatus of current user:\n";

		echo "--------------------------\n";

		echo "User ID: ".Yii::app()->user->id."\n";

		echo "User Name: ".Yii::app()->user->name."\n";

		if (Yii::app()->user->isGuest)

			echo "There is NO user logged in.\n\n";

		else 

			echo "The user is logged in.\n\n";

	}



This is what i get when i run the test:

[font="Courier New"]stefan@stefan-laptop:~/public_html/agroLog/trunk/src/protected/tests$ phpunit unit/AccessControlTest.php

PHPUnit 3.4.13 by Sebastian Bergmann.

Status of current user:


User ID: 1

User Name: admin

The user is logged in.

logout()

[color="#008000"]Status of current user:


User ID:

User Name: Guest

There is NO user logged in.[/color]

Status of current user:


User ID: 2

User Name: user

The user is logged in.

logout()

[color="#FF0000"]

Status of current user:


User ID: 2

User Name: user

The user is logged in.[/color]

Time: 2 seconds, Memory: 12.25Mb

OK (2 tests, 0 assertions)[/font]

I marked the important sections:

[color="#008000"]Green[/color]: I can logout the first user normally.

[color="#FF0000"]Red[/color]: The second user stays logged in.

So to anwer your question: The user ist still logged in, after i logout…

If i first login ‘user’ and then admin, admin stays logged in. So the user iself has nothing to do with this problem.

My Problem really is, that i want to test different users against my RBAC rues. When i now first test admin, an then a user with less authority, this users still has the rights of the admin. So the assert-Tests will fail.

can no one help me?

I’m not sure I’m understanding your question but surely if (Yii::app()->user->isGuest == true), then user is logged out?

Does it need to more complicated?

Yes, the user is logged out, if he is ‘Guest’.

My Problem is: After the second user i can not logout any user anymore. (see console-out, red marked part).

Therefor i can not test multiple roles of my rbac-modul, since i need do test it with different users.

Is this a bug, or am i missing something?

This holds me really back, since i want to test first.

Put this code right after your logout calls:

Yii::app()->user->clearStates();

Peter

is your session file still exist & session directory writeable?

Hmm… interesting…

I just tested it and found that solution is to put




Yii::app()->user->logout();  echo "logout()";

$this->checkUser();

unset($_SESSION);



Also read caution on php.net http://php.net/session_unset

Update:

As this post says, think is better to use


$_SESSION = array();

instead of


unset($_SESSION);

Maybe report this as a bug?

I actually just ran into this same issue, too. What’s funny is I had unit tests that were working fine. I had then added some other code ABOVE my login/logout testing that was setting session variables, and this somehow triggered the problem for the login testing (user not logged out in the unit test).

clearStates did fix it for me, though it doesn’t address what happened in the first place.

The code I added was:

if (empty($strUrl))


	unset(Yii::app()->session['last_url']);


else


	Yii::app()->session['last_url'] =  $strUrl;

this is earlier in my testing (testing the passing $strUrl or not passing).

The only thing in common as far as I can tell is working with sessions. I’ve got other session statements that weren’t an issue. No clue.

Someone opened a github ticket for this, with a link to this forum thread. https://github.com/yiisoft/yii/issues/1792

It really looks like a side effect of the configuration user => array('allowAutoLogin' => true) which uses cookies to reconnect automatically the users.