TinyUrlsControllerTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. App::uses('TinyUrlsController', 'Tools.Controller');
  3. class TinyUrlsControllerTest extends ControllerTestCase {
  4. public $fixtures = ['core.cake_session', 'plugin.tools.user', 'plugin.tools.role', 'plugin.tools.tiny_url'];
  5. public $TinyUrlsController;
  6. public function setUp() {
  7. parent::setUp();
  8. $this->TinyUrlsController = new TestTinyUrlsController(new CakeRequest, new CakeResponse);
  9. }
  10. public function tearDown() {
  11. CakeSession::delete('Auth.User');
  12. parent::tearDown();
  13. }
  14. /**
  15. * TinyUrlsControllerTest::testObject()
  16. *
  17. * @return void
  18. */
  19. public function testObject() {
  20. $this->assertTrue(is_object($this->TinyUrlsController));
  21. $this->assertInstanceOf('TinyUrlsController', $this->TinyUrlsController);
  22. }
  23. /**
  24. * QloginControllerTest::testGo()
  25. *
  26. * @return void
  27. */
  28. public function testGo() {
  29. $this->TinyUrl = ClassRegistry::init('Tools.TinyUrl');
  30. $url = $this->TinyUrl->url(['controller' => 'test', 'action' => 'foo', 'bar'], 1);
  31. $this->assertContains('/tools/tiny_urls/go?id=m', $url);
  32. $key = 'm';
  33. $this->TinyUrlsController->request['id'] = 'm';
  34. $this->TinyUrlsController->go();
  35. $this->assertTextContains('/test/foo/bar', $this->TinyUrlsController->redirectUrl);
  36. // Invalid id
  37. $this->expectException('NotFoundException');
  38. $key = 'm';
  39. $this->TinyUrlsController->request['id'] = 's';
  40. $this->TinyUrlsController->go();
  41. }
  42. /**
  43. * QloginControllerTest::testAdminIndex()
  44. *
  45. * @return void
  46. */
  47. public function testAdminIndex() {
  48. $user = [
  49. 'id' => 1,
  50. 'role_id' => 1
  51. ];
  52. CakeSession::write('Auth.User', $user);
  53. $url = Router::url(['admin' => true, 'plugin' => 'tools', 'controller' => 'tiny_urls', 'action' => 'index']);
  54. $result = $this->testAction($url, [
  55. 'method' => 'get',
  56. 'return' => 'contents'
  57. ]);
  58. $this->assertNotEmpty($result);
  59. }
  60. /**
  61. * QloginControllerTest::testAdminIndex()
  62. *
  63. * @return void
  64. */
  65. public function testAdminReset() {
  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' => 'tiny_urls', 'action' => 'reset']);
  72. $result = $this->testAction($url, [
  73. 'return' => 'contents'
  74. ]);
  75. $this->assertNull($result);
  76. $this->assertTextContains('admin/tools/tiny_urls', $this->headers['Location']);
  77. }
  78. }
  79. class TestTinyUrlsController extends TinyUrlsController {
  80. public $redirectUrl = null;
  81. public function redirect($url, $status = null, $exit = true) {
  82. $this->redirectUrl = $url;
  83. }
  84. }