CommonComponentTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. //$this->assertTrue($is);
  98. $res = $this->Controller->Session->read('messages');
  99. //debug($res);
  100. $this->assertTrue(!empty($res));
  101. $this->assertTrue(isset($res['info'][0]) && $res['info'][0] === 'efg');
  102. }
  103. }
  104. /*** additional helper classes ***/
  105. /**
  106. * Short description for class.
  107. *
  108. * @package cake.tests
  109. * @subpackage cake.tests.cases.libs.controller.components
  110. */
  111. class CommonComponentTestController extends AppController {
  112. /**
  113. * name property
  114. *
  115. * @var string 'SecurityTest'
  116. * @access public
  117. */
  118. /**
  119. * components property
  120. *
  121. * @var array
  122. * @access public
  123. */
  124. public $components = array('Tools.Common');
  125. /**
  126. * failed property
  127. *
  128. * @var bool false
  129. * @access public
  130. */
  131. public $failed = false;
  132. /**
  133. * Used for keeping track of headers in test
  134. *
  135. * @var array
  136. * @access public
  137. */
  138. public $testHeaders = array();
  139. /**
  140. * fail method
  141. *
  142. * @access public
  143. * @return void
  144. */
  145. public function fail() {
  146. $this->failed = true;
  147. }
  148. /**
  149. * redirect method
  150. *
  151. * @param mixed $option
  152. * @param mixed $code
  153. * @param mixed $exit
  154. * @access public
  155. * @return void
  156. */
  157. public function redirect($url, $status = null, $exit = true) {
  158. return $status;
  159. }
  160. /**
  161. * Conveinence method for header()
  162. *
  163. * @param string $status
  164. * @return void
  165. * @access public
  166. */
  167. public function header($status) {
  168. $this->testHeaders[] = $status;
  169. }
  170. }
  171. class TestComponent extends Component {
  172. public $Controller;
  173. public $isInit = false;
  174. public $isStartup = false;
  175. public function initialize(Controller $Controller) {
  176. //$this->Controller = $Controller;
  177. $this->isInit = true;
  178. }
  179. public function startup(Controller $Controller) {
  180. //$this->Controller = $Controller;
  181. $this->isStartup = true;
  182. }
  183. }
  184. class TestHelper extends Object {
  185. }
  186. class TestLib {
  187. public $hasOptions = false;
  188. public function __construct($options = array()) {
  189. if (!empty($options)) {
  190. $this->hasOptions = true;
  191. }
  192. }
  193. }