Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error to test login with phpunit: setAuthorization() #157

Closed
dgnovo opened this issue Nov 22, 2020 · 2 comments
Closed

Error to test login with phpunit: setAuthorization() #157

dgnovo opened this issue Nov 22, 2020 · 2 comments

Comments

@dgnovo
Copy link

dgnovo commented Nov 22, 2020

I add authentication plugin and authorization plugin to my CakePHP App.

On web broswer login works right.

I'm try to test it with PHPUnit and i receive some errors.

On my test i run this:

$config = TableRegistry::getTableLocator()->exists('Users') ? [] : ['className' => UsersTable::class];
$users = TableRegistry::getTableLocator()->get('Users', $config);
		
$user = $users->get($userId);
$this->session(['Auth' => $user]);

$this->enableCsrfToken();
$this->enableSecurityToken();

// call
$this->get('Users/login');

$this->assertResponseSuccess(); // Check for a 2xx/3xx response code

In my Application.php

new AuthorizationMiddleware($this, [
	'identityDecorator' => function (AuthorizationService $auth, ArrayAccess $user) {
		debug($user);die;
		return $user->setAuthorization($auth);
	},
	'requireAuthorizationCheck' => false,
	'unauthorizedHandler' => [
		'className' => 'Authorization.Redirect',
		'url' => '/users/login',
		'queryParam' => 'redirectUrl',
		'exceptions' => [
			MissingIdentityException::class,
			OtherException::class
		]
	]
])

debug print on test show:

########## DEBUG ##########
object(Authentication\Identity) id:0 {
  'config' => [
    'fieldMap' => [
      'id' => 'id'
    ]
  ]
  'data' => object(Cake\ORM\Entity) id:1 {
    'id' => (int) 1
    'role_id' => (int) 1
    'status_id' => (int) 1 ...
 

On Web Browser show:

object(App\Model\Entity\User) id:0 {
	'id' => (int) 1
	'role_id' => (int) 2
	'status_id' => (int) 1 ...

Error, in testCase on cli-error.log is

Call to undefined method Authentication\Identity::setAuthorization()

Which configuration options am I missing to get things to work together correctly?

@markstory
Copy link
Member

Seems like you might be getting an auto-model in your tests. You should check if $users is the correct class name.

@dgnovo
Copy link
Author

dgnovo commented Nov 22, 2020

Yes it works.
I change on my test this:

$config = TableRegistry::getTableLocator()->exists('Users') ? [] : ['className' => UsersTable::class];
$users = TableRegistry::getTableLocator()->get('Users', $config);

with this

$users = TableRegistry::getTableLocator()->get('Users');

Now it works correctly.
Thanks a lot.

@dgnovo dgnovo closed this as completed Nov 22, 2020
@markstory markstory reopened this Nov 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants