QloginControllerTest.php 3.2 KB

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