Write unit test for a controller that uses AuthComponent in CakePHP 2.0.3
Hi,
I am trying to test a controller action that allows edition of user
profiles. Among other things I want to test that every logged user can
only edit its own profile and not others. In case of breaking this
restriction the action must redirect to a predefined home page.
With this scenario, I have a fixture that creates a user with ID = 1.
So I was thinking on testing the restriction this way:
$data = $this->Users->User->read(null, 1);
$this->Users->Auth->login($data);
$this->testAction('/users/edit/2', array('method' => 'get'));
$url = parse_url($this->headers['Location']);
$this->assertEquals($url['path'], '/homepage');
The test passes this assert. So the next step is to check if executing
'/users/edit/1', which has the ID of the logged user, shows the form:
$this->testAction('/users/edit/1', array('method' => 'get',
'return' => 'vars'));
$matcher = array(
'tag' => 'form',
'ancestor' => array('tag' => 'div'),
'descendant' => array('tag' => 'fieldset'),
);
$this->assertTag($matcher, $this->vars['content_for_layout'], 'The
edition form was not found');
However this assert fails. After digging around with debug() I've
found that `$this->Auth->user()` returns the whole information but `
$this->Auth->user('id')` returns null. Since I use the latter in a
comparison within the action, it evaluates as false and causes the
test to fail.
The curious thing is that it happens when testing but not when
executing the action in a browser. So, what's the correct way of
testing this action?
Thanks!
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home