Tag Archive: phpunit


Here are some tests functions I wrote for testing the validity of the access control lists of my website. Nothing fancy but it’s an exemple that may be usefull for begginners.

  1. private function CanAccess($url)
  2. {
  3. $this->dispatch($url);
  4. $this->assertNotResponseCode(‘404’);
  5. list($controller, $action) = explode(“/”, ereg_replace(“^/”, “”, $url));
  6. $this->assertController($controller);
  7. $this->assertAction($action);
  8. }
  9. private function CanNotAccess($url)
  10. {
  11. $this->dispatch($url);
  12. // if the user is Guest
  13. if(!Zend_Auth::getInstance()->hasIdentity())
  14. {
  15. $this->assertController(‘user’);
  16. $this->assertAction(‘login’);
  17. // CustomAclManager Should show a Login form
  18. $this->assertQueryCountMin(‘form’, 1);
  19. $this->assertQueryCountMin(‘input[name~=”email”]’, 1);
  20. $this->assertQueryCountMin(‘input[name~=”password”]’, 1);
  21. $this->assertQueryCountMin(‘input[name~=”submit”]’, 1);
  22. }
  23. // If user is logged In he’ll only get redirected
  24. else
  25. $this->assertRedirect;
  26. }
  27. private function LoginRealUser()
  28. {
  29. $this->request->setMethod(‘POST’)
  30. ->setPost(array(
  31. ’email’ => ‘regularuser@nowhere.com’,
  32. ‘password’ => ‘usertests’,
  33. ));
  34. $this->dispatch(‘/user/login’);
  35. $this->assertRedirect;
  36. $this->assertTrue(Zend_Auth::getInstance()->hasIdentity());
  37. }
  38. // ok, now let’s run some tests
  39. public function testGuestAccessRightsAreOk()
  40. {
  41. $this->CanAccess(‘/user/login’);
  42. }
  43. public function testLoggedInUserAccessRightsAreOk()
  44. {
  45. $this->LoginRealUser();
  46. $this->CanAccess(‘/user/logout’);
  47. }
  48. // testing where users can not go
  49. public function testGuestRestrictionsAreOk()
  50. {
  51. $this->CanNotAccess(‘/user/logout’);
  52. $this->CanNotAccess(‘/user/index’);
  53. }
  54. public function testLoggedInRestrictionsAreOk()
  55. {
  56. $this->LoginRealUser();
  57. $this->CanNotAccess(‘/user/login’);
  58. }

A simple way to test your user access rights !

It’s sunday and I’m playing with Zend_Test. Here are some basic things that took me time to find out :
Grep simple text in the page :

  1. $this->assertQueryContentContains(‘body’, “your email is invalid”);

Verify unvalid users can not log in :

  1. $this->request->setMethod(‘POST’)
  2. ->setPost(array(
  3. ’email’ => ‘fakeaddress@fakedomain.com’,
  4. ‘password’ => ‘fakepassw’
  5. ));
  6. $this->dispatch(‘/user/login’);
  7. $this->assertNotRedirect();
  8. $this->assertFalse(Zend_Auth::getInstance()->hasIdentity());

The interesting part is the last part :

  1. $this->assertFalse(Zend_Auth::getInstance()->hasIdentity());

And, let’s test a form :

  1. $this->assertQueryCountMin(‘form’, 1);
  2. $this->assertQueryCountMin(‘input[name~=”email”]’, 1);
  3. $this->assertQueryCountMin(‘input[name~=”password”]’, 1);
  4. $this->assertQueryCountMin(‘input[name~=”submit”]’, 1);

Nothing fancy here, only some tips that can only make your discovering of Zend_Test easyer ! Happy testing !

This morning I wanted to play with the Zend_Dojo Editor in order to allow users to submit content. Honestly from what I saw I don’t really like the looks of it (checkout the microsoft version of a web editor in Sharepoint if you want to compare), but well I thought it’ll do the trick for the moment. So I added this in my form :

  1. $this->addElement(‘editor’, ‘content’,
  2. array(
  3. ‘editActionInterval’ => 2,
  4. ‘focusOnLoad’        => true,
  5. ‘height’             => ‘250px’,
  6. ‘inheritWidth’       => true,
  7. ));

And all I got was an error ! Why, because the Dojo editor exists only in the svn trunk for the moment (it seems to be scheduled for the 1.7 version). The question is : should that really stop you from using it ? Ok, you know the answer, so let’s go on to the second problem : how to get the framework from svn ? Unfortunately I couldn’t get an explanation from zend.com since the corresponding pages were down, so I had to google arround to find what I was looking for. And guess what, a simple command line does the trick :

  1. svn checkout http://framework.zend.com/svn/framework

And here you go…

Here are some tests functions I wrote for testing the validity of the access control lists of my website. Nothing fancy but it’s an exemple that may be usefull for begginners.

  1. private function CanAccess($url)
  2. {
  3. $this->dispatch($url);
  4. $this->assertNotResponseCode(‘404’);
  5. list($controller, $action) = explode(“/”, ereg_replace(“^/”, “”, $url));
  6. $this->assertController($controller);
  7. $this->assertAction($action);
  8. }
  9. private function CanNotAccess($url)
  10. {
  11. $this->dispatch($url);
  12. // if the user is Guest
  13. if(!Zend_Auth::getInstance()->hasIdentity())
  14. {
  15. $this->assertController(‘user’);
  16. $this->assertAction(‘login’);
  17. // CustomAclManager Should show a Login form
  18. $this->assertQueryCountMin(‘form’, 1);
  19. $this->assertQueryCountMin(‘input[name~=”email”]’, 1);
  20. $this->assertQueryCountMin(‘input[name~=”password”]’, 1);
  21. $this->assertQueryCountMin(‘input[name~=”submit”]’, 1);
  22. }
  23. // If user is logged In he’ll only get redirected
  24. else
  25. $this->assertRedirect;
  26. }
  27. private function LoginRealUser()
  28. {
  29. $this->request->setMethod(‘POST’)
  30. ->setPost(array(
  31. ’email’ => ‘regularuser@nowhere.com’,
  32. ‘password’ => ‘usertests’,
  33. ));
  34. $this->dispatch(‘/user/login’);
  35. $this->assertRedirect;
  36. $this->assertTrue(Zend_Auth::getInstance()->hasIdentity());
  37. }
  38. // ok, now let’s run some tests
  39. public function testGuestAccessRightsAreOk()
  40. {
  41. $this->CanAccess(‘/user/login’);
  42. }
  43. public function testLoggedInUserAccessRightsAreOk()
  44. {
  45. $this->LoginRealUser();
  46. $this->CanAccess(‘/user/logout’);
  47. }
  48. // testing where users can not go
  49. public function testGuestRestrictionsAreOk()
  50. {
  51. $this->CanNotAccess(‘/user/logout’);
  52. $this->CanNotAccess(‘/user/index’);
  53. }
  54. public function testLoggedInRestrictionsAreOk()
  55. {
  56. $this->LoginRealUser();
  57. $this->CanNotAccess(‘/user/login’);
  58. }

A simple way to test your user access rights !

Ok, in the wonderfull world of PhpUnit tests I wanted to test a user registration process with Captcha enabled. But, what do you put in the Captcha field since it’s an automated test ? Well, reading the session appears to be the simplest way to get this information. Here’s a simple function I wrote, suggested by Matthew Weier O’Phinney, that will allow you simply to get the Captcha word while in your php tests :

  1. private function testGetCaptcha()
  2. {
  3. $this->dispatch(‘/user/registration’);
  4. foreach ($_SESSION as $key=>$value)
  5. {
  6. if(ereg(“Zend_Form_Captcha.*”, $key))
  7. return  $value[‘word’];
  8. }
  9. }

And here you go… It’s a quick and dirty solution since this will only work if you have a unique Zend_Form_Captcha to test for. Feel free to write a better solution !