CommonComponentTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. App::uses('CommonComponent', 'Tools.Controller/Component');
  3. App::uses('Component', 'Controller');
  4. App::uses('AppController', 'Controller');
  5. App::uses('AppModel', 'Model');
  6. /**
  7. * 2010-11-10 ms
  8. */
  9. class CommonComponentTest extends CakeTestCase {
  10. public $fixtures = array('core.cake_session', 'plugin.tools.tools_user', 'plugin.tools.role');
  11. public function setUp() {
  12. parent::setUp();
  13. CakeSession::delete('Auth');
  14. $this->Controller = new CommonComponentTestController(new CakeRequest, new CakeResponse);
  15. $this->Controller->constructClasses();
  16. $this->Controller->startupProcess();
  17. }
  18. public function tearDown() {
  19. parent::tearDown();
  20. unset($this->Controller->Common);
  21. unset($this->Controller);
  22. }
  23. /**
  24. * CommonComponentTest::testLoadHelper()
  25. *
  26. * @return void
  27. */
  28. public function testLoadHelper() {
  29. $this->assertTrue(!in_array('Text', $this->Controller->helpers));
  30. $this->Controller->Common->loadHelper('Text');
  31. $this->assertTrue(in_array('Text', $this->Controller->helpers));
  32. }
  33. /**
  34. * CommonComponentTest::testLoadComponent()
  35. *
  36. * @return void
  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. /**
  56. * CommonComponentTest::testLoadLib()
  57. *
  58. * @return void
  59. */
  60. public function testLoadLib() {
  61. $this->assertTrue(!isset($this->Controller->RandomLib));
  62. $this->Controller->Common->loadLib('Tools.RandomLib');
  63. $this->assertTrue(isset($this->Controller->RandomLib));
  64. $res = $this->Controller->RandomLib->pwd(null, 10);
  65. $this->assertTrue(!empty($res));
  66. # with options
  67. $this->assertTrue(!isset($this->Controller->TestLib));
  68. $this->Controller->Common->loadLib(array('Tools.RandomLib', 'TestLib'=>array('x'=>'y')));
  69. $this->assertTrue(isset($this->Controller->TestLib));
  70. $this->assertTrue($this->Controller->TestLib->hasOptions);
  71. }
  72. /**
  73. * CommonComponentTest::testGetParams()
  74. *
  75. * @return void
  76. */
  77. public function testGetParams() {
  78. if (php_sapi_name() !== 'cli') {
  79. $is = $this->Controller->Common->getQueryParam('case');
  80. $this->assertTrue(strpos($is, 'CommonComponent') > 0 || $is === 'AllComponentTests' || $is === 'AllTools');
  81. }
  82. $is = $this->Controller->Common->getQueryParam('x');
  83. $this->assertSame(null, $is);
  84. $is = $this->Controller->Common->getQueryParam('x', 'y');
  85. $this->assertSame($is, 'y');
  86. $is = $this->Controller->Common->getNamedParam('plugin');
  87. $this->assertSame(null, $is);
  88. $is = $this->Controller->Common->getNamedParam('x');
  89. $this->assertSame(null, $is);
  90. $is = $this->Controller->Common->getNamedParam('x', 'y');
  91. $this->assertSame($is, 'y');
  92. }
  93. /**
  94. * CommonComponentTest::testGetDefaultUrlParams()
  95. *
  96. * @return void
  97. */
  98. public function testGetDefaultUrlParams() {
  99. $is = $this->Controller->Common->defaultUrlParams();
  100. $this->assertNotEmpty($is);
  101. }
  102. /**
  103. * CommonComponentTest::testcurrentUrl()
  104. *
  105. * @return void
  106. */
  107. public function testCurrentUrl() {
  108. $this->skipIf(php_sapi_name() === 'cli', 'Cannot test session in CLI');
  109. $is = $this->Controller->Common->currentUrl();
  110. $this->assertTrue(is_array($is) && !empty($is));
  111. $is = $this->Controller->Common->currentUrl(true);
  112. $this->assertTrue(!is_array($is) && !empty($is));
  113. }
  114. /**
  115. * CommonComponentTest::testIsForeignReferer()
  116. *
  117. * @return void
  118. */
  119. public function testIsForeignReferer() {
  120. $this->skipIf(php_sapi_name() === 'cli', 'Cannot test session in CLI');
  121. $ref = 'http://www.spiegel.de';
  122. $is = $this->Controller->Common->isForeignReferer($ref);
  123. $this->assertTrue($is);
  124. $ref = HTTP_BASE . '/some/controller/action';
  125. $is = $this->Controller->Common->isForeignReferer($ref);
  126. $this->assertFalse($is);
  127. $ref = '';
  128. $is = $this->Controller->Common->isForeignReferer($ref);
  129. $this->assertFalse($is);
  130. }
  131. /**
  132. * CommonComponentTest::testTransientFlashMessage()
  133. *
  134. * @return void
  135. */
  136. public function testTransientFlashMessage() {
  137. $is = $this->Controller->Common->transientFlashMessage('xyz', 'success');
  138. //$this->assertTrue($is);
  139. $res = Configure::read('messages');
  140. //debug($res);
  141. $this->assertTrue(!empty($res));
  142. $this->assertTrue(isset($res['success'][0]) && $res['success'][0] === 'xyz');
  143. }
  144. /**
  145. * CommonComponentTest::testFlashMessage()
  146. *
  147. * @return void
  148. */
  149. public function testFlashMessage() {
  150. $this->skipIf(php_sapi_name() === 'cli', 'Cannot test session in CLI');
  151. $this->Controller->Session->delete('messages');
  152. $is = $this->Controller->Common->flashMessage('efg');
  153. $res = $this->Controller->Session->read('messages');
  154. $this->assertTrue(!empty($res));
  155. $this->assertTrue(isset($res['info'][0]) && $res['info'][0] === 'efg');
  156. }
  157. /**
  158. * CommonComponentTest::testManualLogin()
  159. *
  160. * @return void
  161. */
  162. public function testManualLogin() {
  163. $user = array(
  164. 'name' => 'foo',
  165. 'password' => 123,
  166. 'role_id' => 1,
  167. );
  168. $User = ClassRegistry::init('ToolsUser');
  169. $User->create();
  170. $res = $User->save($user);
  171. $this->assertTrue(!empty($res));
  172. $res = CakeSession::read('Auth');
  173. $this->assertNull($res);
  174. $is = $this->Controller->Common->manualLogin(2222);
  175. $this->assertFalse($is);
  176. $is = $this->Controller->Common->manualLogin($User->id);
  177. $this->assertTrue($is);
  178. $res = CakeSession::read('Auth');
  179. $this->assertSame($User->id, $res['User']['id']);
  180. $this->assertTrue(!empty($res['User']['Role']));
  181. }
  182. /**
  183. * CommonComponentTest::testForceLogin()
  184. *
  185. * @return void
  186. */
  187. public function testForceLogin() {
  188. $user = array(
  189. 'name' => 'foo',
  190. 'password' => 123,
  191. 'role_id' => 1,
  192. );
  193. $User = ClassRegistry::init('ToolsUser');
  194. $User->create();
  195. $res = $User->save($user);
  196. $this->assertTrue(!empty($res));
  197. $res = CakeSession::read('Auth');
  198. $this->assertNull($res);
  199. $is = $this->Controller->Common->forceLogin(2222);
  200. $this->assertFalse($is);
  201. $is = $this->Controller->Common->forceLogin($User->id);
  202. $this->assertTrue($is);
  203. $res = CakeSession::read('Auth');
  204. $this->assertSame($User->id, $res['User']['id']);
  205. $this->assertTrue(!empty($res['User']['Role']));
  206. }
  207. public function testGetGroup() {
  208. $list = array(
  209. 'Models' => array(
  210. '1' => 'Foo',
  211. '2' => 'Bar'
  212. ),
  213. 'Mitarbeiter' => array(
  214. '3' => 'Some',
  215. '4' => 'Thing'
  216. ),
  217. );
  218. $matching = array('Models' => 'Model', 'Mitarbeiter' => 'Contributor');
  219. $res = CommonComponent::getGroup($list, 111);
  220. $this->assertEquals('', $res);
  221. $res = CommonComponent::getGroup($list, 2);
  222. $this->assertEquals('Models', $res);
  223. $res = CommonComponent::getGroup($list, 2, $matching);
  224. $this->assertEquals('Model', $res);
  225. $res = CommonComponent::getGroup($list, 3, $matching);
  226. $this->assertEquals('Contributor', $res);
  227. }
  228. }
  229. /*** additional helper classes ***/
  230. class ToolsUser extends AppModel {
  231. public $name = 'ToolsUser';
  232. public $alias = 'User';
  233. public $belongsTo = array(
  234. 'Role',
  235. );
  236. }
  237. class CommonComponentTestController extends AppController {
  238. public $components = array('Tools.Common');
  239. public $failed = false;
  240. public $testHeaders = array();
  241. public function fail() {
  242. $this->failed = true;
  243. }
  244. public function redirect($url, $status = null, $exit = true) {
  245. return $status;
  246. }
  247. public function header($status) {
  248. $this->testHeaders[] = $status;
  249. }
  250. }
  251. class TestComponent extends Component {
  252. public $Controller;
  253. public $isInit = false;
  254. public $isStartup = false;
  255. public function initialize(Controller $Controller) {
  256. //$this->Controller = $Controller;
  257. $this->isInit = true;
  258. }
  259. public function startup(Controller $Controller) {
  260. //$this->Controller = $Controller;
  261. $this->isStartup = true;
  262. }
  263. }
  264. class TestHelper extends Object {
  265. }
  266. class TestLib {
  267. public $hasOptions = false;
  268. public function __construct($options = array()) {
  269. if (!empty($options)) {
  270. $this->hasOptions = true;
  271. }
  272. }
  273. }