CommonComponentTest.php 5.5 KB

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