AjaxComponentTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. App::uses('AjaxComponent', 'Tools.Controller/Component');
  3. App::uses('Controller', 'Controller');
  4. App::uses('AppModel', 'Model');
  5. App::uses('CakeRequest', 'Network');
  6. App::uses('CakeResponse', 'Network');
  7. /**
  8. */
  9. class AjaxComponentTest extends CakeTestCase {
  10. public $fixtures = array('core.cake_session', 'plugin.tools.tools_user', 'plugin.tools.role');
  11. public function setUp() {
  12. parent::setUp();
  13. Configure::delete('Ajax');
  14. $this->Controller = new AjaxComponentTestController(new CakeRequest(), new CakeResponse());
  15. $this->Controller->constructClasses();
  16. }
  17. /**
  18. * AjaxComponentTest::testNonAjax()
  19. *
  20. * @return void
  21. */
  22. public function testNonAjax() {
  23. $this->Controller->startupProcess();
  24. $this->assertFalse($this->Controller->Components->Ajax->respondAsAjax);
  25. }
  26. /**
  27. * AjaxComponentTest::testDefaults()
  28. *
  29. * @return void
  30. */
  31. public function testDefaults() {
  32. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  33. $this->Controller->startupProcess();
  34. $this->assertTrue($this->Controller->Components->Ajax->respondAsAjax);
  35. $this->Controller->Session->setFlash('A message', 'custom');
  36. $session = $this->Controller->Session->read('Message.flash');
  37. $expected = array(
  38. 'message' => 'A message',
  39. 'element' => 'custom',
  40. 'params' => array()
  41. );
  42. $this->assertEquals($expected, $session);
  43. $this->Controller->Components->Ajax->beforeRender($this->Controller);
  44. $this->assertEquals('Tools.Ajax', $this->Controller->viewClass);
  45. $this->assertEquals($expected, $this->Controller->viewVars['_message']);
  46. $session = $this->Controller->Session->read('Message.flash');
  47. $this->assertNull($session);
  48. $this->Controller->redirect('/');
  49. $this->assertSame(array(), $this->Controller->response->header());
  50. $expected = array(
  51. 'url' => Router::url('/', true),
  52. 'status' => null,
  53. 'exit' => true
  54. );
  55. $this->assertEquals($expected, $this->Controller->viewVars['_redirect']);
  56. }
  57. /**
  58. * AjaxComponentTest::testAutoDetectOnFalse()
  59. *
  60. * @return void
  61. */
  62. public function testAutoDetectOnFalse() {
  63. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  64. $this->Controller->Components->unload('Ajax');
  65. $this->Controller->Components->load('Tools.Ajax', array('autoDetect' => false));
  66. $this->Controller->startupProcess();
  67. $this->assertFalse($this->Controller->Components->Ajax->respondAsAjax);
  68. }
  69. /**
  70. * AjaxComponentTest::testAutoDetectOnFalseViaConfig()
  71. *
  72. * @return void
  73. */
  74. public function testAutoDetectOnFalseViaConfig() {
  75. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  76. Configure::write('Ajax.autoDetect', false);
  77. $this->Controller->Components->unload('Ajax');
  78. $this->Controller->Components->load('Tools.Ajax');
  79. $this->Controller->startupProcess();
  80. $this->assertFalse($this->Controller->Components->Ajax->respondAsAjax);
  81. }
  82. /**
  83. * AjaxComponentTest::testToolsMultiMessages()
  84. *
  85. * @return void
  86. */
  87. public function testToolsMultiMessages() {
  88. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  89. Configure::write('Ajax.flashKey', 'messages');
  90. $this->Controller->Components->unload('Ajax');
  91. $this->Controller->Components->load('Tools.Ajax');
  92. $this->Controller->startupProcess();
  93. $this->assertTrue($this->Controller->Components->Ajax->respondAsAjax);
  94. $this->Controller->Flash->message('A message', 'success');
  95. $session = $this->Controller->Session->read('messages');
  96. $expected = array(
  97. 'success' => array('A message')
  98. );
  99. $this->assertEquals($expected, $session);
  100. $this->Controller->Components->Ajax->beforeRender($this->Controller);
  101. $this->assertEquals('Tools.Ajax', $this->Controller->viewClass);
  102. $this->assertEquals($expected, $this->Controller->viewVars['_message']);
  103. $session = $this->Controller->Session->read('messages');
  104. $this->assertNull($session);
  105. }
  106. /**
  107. * AjaxComponentTest::testSetVars()
  108. *
  109. * @return void
  110. */
  111. public function testSetVars() {
  112. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  113. $this->Controller->Components->unload('Ajax');
  114. $content = array('id' => 1, 'title' => 'title');
  115. $this->Controller->set(compact('content'));
  116. $this->Controller->set('_serialize', array('content'));
  117. $this->Controller->Components->load('Tools.Ajax');
  118. $this->assertNotEmpty($this->Controller->viewVars);
  119. $this->assertNotEmpty($this->Controller->viewVars['_serialize']);
  120. $this->assertEquals('content', $this->Controller->viewVars['_serialize'][0]);
  121. }
  122. /**
  123. * AjaxComponentTest::testSetVarsWithRedirect()
  124. *
  125. * @return void
  126. */
  127. public function testSetVarsWithRedirect() {
  128. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  129. $this->Controller->startupProcess();
  130. $content = array('id' => 1, 'title' => 'title');
  131. $this->Controller->set(compact('content'));
  132. $this->Controller->set('_serialize', array('content'));
  133. $this->Controller->redirect('/');
  134. $this->assertSame(array(), $this->Controller->response->header());
  135. $expected = array(
  136. 'url' => Router::url('/', true),
  137. 'status' => null,
  138. 'exit' => true
  139. );
  140. $this->assertEquals($expected, $this->Controller->viewVars['_redirect']);
  141. $this->Controller->set(array('_message' => 'test'));
  142. $this->Controller->redirect('/');
  143. $this->assertArrayHasKey('_message', $this->Controller->viewVars);
  144. $this->assertNotEmpty($this->Controller->viewVars);
  145. $this->assertNotEmpty($this->Controller->viewVars['_serialize']);
  146. $this->assertTrue(in_array('content', $this->Controller->viewVars['_serialize']));
  147. }
  148. }
  149. // Use Controller instead of AppController to avoid conflicts
  150. class AjaxComponentTestController extends Controller {
  151. public $components = array('Session', 'Tools.Ajax', 'Tools.Common', 'Tools.Flash');
  152. }