CommonComponentTest.php 8.3 KB

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