QloginControllerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. App::uses('QloginController', 'Tools.Controller');
  3. App::uses('ComponentCollection', 'Controller');
  4. class QloginControllerTest extends ControllerTestCase {
  5. public $fixtures = array('plugin.tools.code_key', 'plugin.tools.token', 'core.cake_session', 'plugin.tools.user', 'plugin.tools.role');
  6. public $QloginController;
  7. public function setUp() {
  8. parent::setUp();
  9. $this->QloginController = new TestQloginController(new CakeRequest(), new CakeResponse());
  10. $this->QloginController->constructClasses();
  11. $this->QloginController->startupProcess();
  12. $Auth = $this->getMock('AuthComponent', array('login'), array(new ComponentCollection()));
  13. $Auth->expects($this->any())
  14. ->method('login')
  15. ->will($this->returnValue(true));
  16. $this->QloginController->Auth = $Auth;
  17. }
  18. /**
  19. * QloginControllerTest::testObject()
  20. *
  21. * @return void
  22. */
  23. public function testObject() {
  24. $this->assertTrue(is_object($this->QloginController));
  25. $this->assertInstanceOf('QloginController', $this->QloginController);
  26. }
  27. /**
  28. * QloginControllerTest::testGo()
  29. *
  30. * @return void
  31. */
  32. public function testGo() {
  33. $this->Qlogin = ClassRegistry::init('Tools.Qlogin');
  34. $key = $this->Qlogin->generate(array('controller' => 'test', 'action' => 'foo', 'bar'), 1);
  35. $res = $this->Qlogin->translate($key);
  36. $this->assertTrue(is_array($res) && !empty($res));
  37. $this->QloginController->go($key);
  38. $this->assertContains('/test/foo/bar', $this->QloginController->redirectUrl);
  39. }
  40. /**
  41. * QloginControllerTest::_testGoDeprecated()
  42. *
  43. * @return void
  44. */
  45. public function _testGoDeprecated() {
  46. Configure::write('Qlogin.generator', 'CodeKey');
  47. $this->Qlogin = ClassRegistry::init('Tools.Qlogin');
  48. $key = $this->Qlogin->generate(array('controller' => 'test', 'action' => 'foo', 'bar'), 1);
  49. $res = $this->Qlogin->translate($key);
  50. $this->assertTrue(is_array($res) && !empty($res));
  51. $this->QloginController->go($key);
  52. debug($this->QloginController->redirectUrl);
  53. }
  54. /**
  55. * QloginControllerTest::testAdminIndex()
  56. *
  57. * @return void
  58. */
  59. public function testAdminIndex() {
  60. $user = array(
  61. 'id' => 1,
  62. 'role_id' => 1
  63. );
  64. CakeSession::write('Auth.User', $user);
  65. $url = Router::url(array('admin' => true, 'plugin' => 'tools', 'controller' => 'qlogin', 'action' => 'index'));
  66. $result = $this->testAction($url, array(
  67. 'method' => 'get',
  68. 'return' => 'contents'
  69. ));
  70. $this->assertNotEmpty($result);
  71. }
  72. /**
  73. * QloginControllerTest::testAdminIndex()
  74. *
  75. * @return void
  76. */
  77. public function testAdminReset() {
  78. $_SERVER['HTTP_REFERER'] = Router::url('/foo/bar', true);
  79. $user = array(
  80. 'id' => 1,
  81. 'role_id' => 1
  82. );
  83. CakeSession::write('Auth.User', $user);
  84. $url = Router::url(array('admin' => true, 'plugin' => 'tools', 'controller' => 'qlogin', 'action' => 'reset'));
  85. $result = $this->testAction($url, array(
  86. 'return' => 'contents'
  87. ));
  88. $this->assertNull($result);
  89. $this->assertTextContains('admin/tools/qlogin', $this->headers['Location']);
  90. }
  91. }
  92. class TestQloginController extends QloginController {
  93. public $uses = array('Tools.Qlogin');
  94. public $redirectUrl = null;
  95. public function redirect($url, $status = null, $exit = true) {
  96. $this->redirectUrl = $url;
  97. }
  98. }