QloginControllerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. public function tearDown() {
  19. CakeSession::delete('Auth.User');
  20. parent::tearDown();
  21. }
  22. /**
  23. * QloginControllerTest::testObject()
  24. *
  25. * @return void
  26. */
  27. public function testObject() {
  28. $this->assertTrue(is_object($this->QloginController));
  29. $this->assertInstanceOf('QloginController', $this->QloginController);
  30. }
  31. /**
  32. * QloginControllerTest::testGo()
  33. *
  34. * @return void
  35. */
  36. public function testGo() {
  37. $this->Qlogin = ClassRegistry::init('Tools.Qlogin');
  38. $key = $this->Qlogin->generate(array('controller' => 'test', 'action' => 'foo', 'bar'), 1);
  39. $res = $this->Qlogin->translate($key);
  40. $this->assertTrue(is_array($res) && !empty($res));
  41. $this->QloginController->go($key);
  42. $this->assertContains('/test/foo/bar', $this->QloginController->redirectUrl);
  43. }
  44. /**
  45. * QloginControllerTest::_testGoDeprecated()
  46. *
  47. * @return void
  48. */
  49. public function _testGoDeprecated() {
  50. Configure::write('Qlogin.generator', 'CodeKey');
  51. $this->Qlogin = ClassRegistry::init('Tools.Qlogin');
  52. $key = $this->Qlogin->generate(array('controller' => 'test', 'action' => 'foo', 'bar'), 1);
  53. $res = $this->Qlogin->translate($key);
  54. $this->assertTrue(is_array($res) && !empty($res));
  55. $this->QloginController->go($key);
  56. debug($this->QloginController->redirectUrl);
  57. }
  58. /**
  59. * QloginControllerTest::testAdminIndex()
  60. *
  61. * @return void
  62. */
  63. public function testAdminIndex() {
  64. $user = array(
  65. 'id' => 1,
  66. 'role_id' => 1
  67. );
  68. CakeSession::write('Auth.User', $user);
  69. $url = Router::url(array('admin' => true, 'plugin' => 'tools', 'controller' => 'qlogin', 'action' => 'index'));
  70. $result = $this->testAction($url, array(
  71. 'method' => 'get',
  72. 'return' => 'contents'
  73. ));
  74. $this->assertNotEmpty($result);
  75. }
  76. /**
  77. * QloginControllerTest::testAdminIndex()
  78. *
  79. * @return void
  80. */
  81. public function testAdminReset() {
  82. $user = array(
  83. 'id' => 1,
  84. 'role_id' => 1
  85. );
  86. CakeSession::write('Auth.User', $user);
  87. $url = Router::url(array('admin' => true, 'plugin' => 'tools', 'controller' => 'qlogin', 'action' => 'reset'));
  88. $result = $this->testAction($url, array(
  89. 'return' => 'contents'
  90. ));
  91. $this->assertNull($result);
  92. $this->assertTextContains('admin/tools/qlogin', $this->headers['Location']);
  93. }
  94. }
  95. class TestQloginController extends QloginController {
  96. public $uses = array('Tools.Qlogin');
  97. public $redirectUrl = null;
  98. public function redirect($url, $status = null, $exit = true) {
  99. $this->redirectUrl = $url;
  100. }
  101. }