CommonComponentTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. App::uses('CommonComponent', 'Tools.Controller/Component');
  3. App::uses('Component', 'Controller');
  4. App::uses('AppController', 'Controller');
  5. /**
  6. * 2010-11-10 ms
  7. */
  8. class CommonComponentTest extends CakeTestCase {
  9. public $fixtures = array('core.cake_session');
  10. /**
  11. * setUp method
  12. *
  13. * @access public
  14. * @return void
  15. */
  16. public function setUp() {
  17. $this->Controller = new CommonComponentTestController(new CakeRequest, new CakeResponse);
  18. $this->Controller->constructClasses();
  19. $this->Controller->startupProcess();
  20. }
  21. /**
  22. * Tear-down method. Resets environment state.
  23. *
  24. * @access public
  25. * @return void
  26. */
  27. public function tearDown() {
  28. unset($this->Controller->Common);
  29. unset($this->Controller);
  30. }
  31. public function testLoadHelper() {
  32. $this->assertTrue(!in_array('Text', $this->Controller->helpers));
  33. $this->Controller->Common->loadHelper('Text');
  34. $this->assertTrue(in_array('Text', $this->Controller->helpers));
  35. }
  36. public function testLoadComponent() {
  37. $this->assertTrue(!isset($this->Controller->Test));
  38. $this->Controller->Common->loadComponent('Test');
  39. $this->assertTrue(isset($this->Controller->Test));
  40. # with plugin
  41. $this->Controller->Calendar = null;
  42. $this->assertTrue(!isset($this->Controller->Calendar));
  43. $this->Controller->Common->loadComponent('Tools.Calendar');
  44. $this->assertTrue(isset($this->Controller->Calendar));
  45. # with options
  46. $this->Controller->Test = null;
  47. $this->assertTrue(!isset($this->Controller->Test));
  48. $this->Controller->Common->loadComponent(array('RequestHandler', 'Test'=>array('x'=>'y')));
  49. $this->assertTrue(isset($this->Controller->Test));
  50. $this->assertTrue($this->Controller->Test->isInit);
  51. $this->assertTrue($this->Controller->Test->isStartup);
  52. }
  53. public function testLoadLib() {
  54. $this->assertTrue(!isset($this->Controller->RandomLib));
  55. $this->Controller->Common->loadLib('Tools.RandomLib');
  56. $this->assertTrue(isset($this->Controller->RandomLib));
  57. $res = $this->Controller->RandomLib->pwd(null, 10);
  58. $this->assertTrue(!empty($res));
  59. # with options
  60. $this->assertTrue(!isset($this->Controller->TestLib));
  61. $this->Controller->Common->loadLib(array('Tools.RandomLib', 'TestLib'=>array('x'=>'y')));
  62. $this->assertTrue(isset($this->Controller->TestLib));
  63. $this->assertTrue($this->Controller->TestLib->hasOptions);
  64. }
  65. public function testGetParams() {
  66. if (php_sapi_name() !== 'cli') {
  67. $is = $this->Controller->Common->getQueryParam('case');
  68. $this->assertTrue(strpos($is, 'CommonComponent') > 0 || $is === 'AllComponentTests' || $is === 'AllTools');
  69. }
  70. $is = $this->Controller->Common->getQueryParam('x');
  71. $this->assertSame(null, $is);
  72. $is = $this->Controller->Common->getQueryParam('x', 'y');
  73. $this->assertSame($is, 'y');
  74. $is = $this->Controller->Common->getNamedParam('plugin');
  75. $this->assertSame(null, $is);
  76. $is = $this->Controller->Common->getNamedParam('x');
  77. $this->assertSame(null, $is);
  78. $is = $this->Controller->Common->getNamedParam('x', 'y');
  79. $this->assertSame($is, 'y');
  80. }
  81. public function testGetDefaultUrlParams() {
  82. $is = $this->Controller->Common->defaultUrlParams();
  83. //debug($is);
  84. $this->assertNotEmpty($is);
  85. }
  86. public function testTransientFlashMessage() {
  87. $is = $this->Controller->Common->transientFlashMessage('xyz', 'success');
  88. //$this->assertTrue($is);
  89. $res = Configure::read('messages');
  90. //debug($res);
  91. $this->assertTrue(!empty($res));
  92. $this->assertTrue(isset($res['success'][0]) && $res['success'][0] === 'xyz');
  93. }
  94. public function testFlashMessage() {
  95. $this->Controller->Session->delete('messages');
  96. $is = $this->Controller->Common->flashMessage('efg');
  97. $res = $this->Controller->Session->read('messages');
  98. //debug($res);
  99. $this->assertTrue(!empty($res));
  100. $this->assertTrue(isset($res['info'][0]) && $res['info'][0] === 'efg');
  101. }
  102. }
  103. /*** additional helper classes ***/
  104. /**
  105. * Short description for class.
  106. *
  107. * @package cake.tests
  108. * @subpackage cake.tests.cases.libs.controller.components
  109. */
  110. class CommonComponentTestController extends AppController {
  111. /**
  112. * name property
  113. *
  114. * @var string 'SecurityTest'
  115. * @access public
  116. */
  117. /**
  118. * components property
  119. *
  120. * @var array
  121. * @access public
  122. */
  123. public $components = array('Tools.Common');
  124. /**
  125. * failed property
  126. *
  127. * @var bool false
  128. * @access public
  129. */
  130. public $failed = false;
  131. /**
  132. * Used for keeping track of headers in test
  133. *
  134. * @var array
  135. * @access public
  136. */
  137. public $testHeaders = array();
  138. /**
  139. * fail method
  140. *
  141. * @access public
  142. * @return void
  143. */
  144. public function fail() {
  145. $this->failed = true;
  146. }
  147. /**
  148. * redirect method
  149. *
  150. * @param mixed $option
  151. * @param mixed $code
  152. * @param mixed $exit
  153. * @access public
  154. * @return void
  155. */
  156. public function redirect($url, $status = null, $exit = true) {
  157. return $status;
  158. }
  159. /**
  160. * Conveinence method for header()
  161. *
  162. * @param string $status
  163. * @return void
  164. * @access public
  165. */
  166. public function header($status) {
  167. $this->testHeaders[] = $status;
  168. }
  169. }
  170. class TestComponent extends Component {
  171. public $Controller;
  172. public $isInit = false;
  173. public $isStartup = false;
  174. public function initialize(Controller $Controller) {
  175. //$this->Controller = $Controller;
  176. $this->isInit = true;
  177. }
  178. public function startup(Controller $Controller) {
  179. //$this->Controller = $Controller;
  180. $this->isStartup = true;
  181. }
  182. }
  183. class TestHelper extends Object {
  184. }
  185. class TestLib {
  186. public $hasOptions = false;
  187. public function __construct($options = array()) {
  188. if (!empty($options)) {
  189. $this->hasOptions = true;
  190. }
  191. }
  192. }