CommonComponentTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. App::uses('CommonComponent', 'Tools.Controller/Component');
  3. App::uses('Component', 'Controller');
  4. App::uses('Controller', '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::write('Auth', '');
  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. $is = $this->Controller->Common->getNamedParam('x');
  79. $this->assertNull($is);
  80. $is = $this->Controller->Common->getNamedParam('x', 'y');
  81. $this->assertSame('y', $is);
  82. }
  83. /**
  84. * CommonComponentTest::testGetDefaultUrlParams()
  85. *
  86. * @return void
  87. */
  88. public function testGetDefaultUrlParams() {
  89. $is = $this->Controller->Common->defaultUrlParams();
  90. $this->assertNotEmpty($is);
  91. }
  92. /**
  93. * CommonComponentTest::testcurrentUrl()
  94. *
  95. * @return void
  96. */
  97. public function testCurrentUrl() {
  98. $this->skipIf(php_sapi_name() === 'cli', 'Cannot test session in CLI');
  99. $is = $this->Controller->Common->currentUrl();
  100. $this->assertTrue(is_array($is) && !empty($is));
  101. $is = $this->Controller->Common->currentUrl(true);
  102. $this->assertTrue(!is_array($is) && !empty($is));
  103. }
  104. /**
  105. * CommonComponentTest::testIsForeignReferer()
  106. *
  107. * @return void
  108. */
  109. public function testIsForeignReferer() {
  110. $this->skipIf(php_sapi_name() === 'cli', 'Cannot test session in CLI');
  111. $ref = 'http://www.spiegel.de';
  112. $is = $this->Controller->Common->isForeignReferer($ref);
  113. $this->assertTrue($is);
  114. $ref = HTTP_BASE . '/some/controller/action';
  115. $is = $this->Controller->Common->isForeignReferer($ref);
  116. $this->assertFalse($is);
  117. $ref = '';
  118. $is = $this->Controller->Common->isForeignReferer($ref);
  119. $this->assertFalse($is);
  120. }
  121. /**
  122. * CommonComponentTest::testTransientFlashMessage()
  123. *
  124. * @return void
  125. */
  126. public function testTransientFlashMessage() {
  127. $is = $this->Controller->Common->transientFlashMessage('xyz', 'success');
  128. //$this->assertTrue($is);
  129. $res = Configure::read('messages');
  130. //debug($res);
  131. $this->assertTrue(!empty($res));
  132. $this->assertTrue(isset($res['success'][0]) && $res['success'][0] === 'xyz');
  133. }
  134. /**
  135. * CommonComponentTest::testFlashMessage()
  136. *
  137. * @return void
  138. */
  139. public function testFlashMessage() {
  140. $this->skipIf(php_sapi_name() === 'cli', 'Cannot test session in CLI');
  141. $this->Controller->Session->delete('messages');
  142. $is = $this->Controller->Common->flashMessage('efg');
  143. $res = $this->Controller->Session->read('messages');
  144. $this->assertTrue(!empty($res));
  145. $this->assertTrue(isset($res['info'][0]) && $res['info'][0] === 'efg');
  146. }
  147. /**
  148. * CommonComponentTest::testManualLogin()
  149. *
  150. * @return void
  151. */
  152. public function testManualLogin() {
  153. $user = array(
  154. 'name' => 'foo',
  155. 'password' => 123,
  156. 'role_id' => 1,
  157. );
  158. $User = ClassRegistry::init('MyToolsUser');
  159. $User->create();
  160. $res = $User->save($user);
  161. $this->assertTrue(!empty($res));
  162. $res = CakeSession::read('Auth');
  163. $this->assertNull($res);
  164. $is = $this->Controller->Common->manualLogin(2222);
  165. $this->assertFalse($is);
  166. $is = $this->Controller->Common->manualLogin($User->id);
  167. $this->assertTrue($is);
  168. $res = CakeSession::read('Auth');
  169. $this->assertSame($User->id, $res['User']['id']);
  170. $this->assertTrue(!empty($res['User']['Role']));
  171. }
  172. /**
  173. * CommonComponentTest::testForceLogin()
  174. *
  175. * @return void
  176. */
  177. public function testForceLogin() {
  178. $user = array(
  179. 'name' => 'foo',
  180. 'password' => 123,
  181. 'role_id' => 1,
  182. );
  183. $User = ClassRegistry::init('MyToolsUser');
  184. $User->create();
  185. $res = $User->save($user);
  186. $this->assertTrue(!empty($res));
  187. $res = CakeSession::read('Auth');
  188. $this->assertNull($res);
  189. $is = $this->Controller->Common->forceLogin(2222);
  190. $this->assertFalse($is);
  191. $is = $this->Controller->Common->forceLogin($User->id);
  192. $this->assertTrue($is);
  193. $res = CakeSession::read('Auth');
  194. $this->assertSame($User->id, $res['User']['id']);
  195. $this->assertTrue(!empty($res['User']['Role']));
  196. }
  197. public function testGetGroup() {
  198. $list = array(
  199. 'Models' => array(
  200. '1' => 'Foo',
  201. '2' => 'Bar'
  202. ),
  203. 'Mitarbeiter' => array(
  204. '3' => 'Some',
  205. '4' => 'Thing'
  206. ),
  207. );
  208. $matching = array('Models' => 'Model', 'Mitarbeiter' => 'Contributor');
  209. $res = CommonComponent::getGroup($list, 111);
  210. $this->assertEquals('', $res);
  211. $res = CommonComponent::getGroup($list, 2);
  212. $this->assertEquals('Models', $res);
  213. $res = CommonComponent::getGroup($list, 2, $matching);
  214. $this->assertEquals('Model', $res);
  215. $res = CommonComponent::getGroup($list, 3, $matching);
  216. $this->assertEquals('Contributor', $res);
  217. }
  218. /**
  219. * CommonComponentTest::testDataTrim()
  220. *
  221. * @return void
  222. */
  223. public function testDataTrim() {
  224. $array = array('Some' => array('Deep' => array('array' => ' bla ')));
  225. $this->Controller = new CommonComponentTestController(new CakeRequest, new CakeResponse);
  226. $this->Controller->request->data = $array;
  227. $this->Controller->request->query = $array;
  228. $this->Controller->constructClasses();
  229. $this->Controller->startupProcess();
  230. $expected = array('Some' => array('Deep' => array('array' => 'bla')));
  231. $this->assertSame($expected, $this->Controller->request->data);
  232. $this->assertSame($expected, $this->Controller->request->query);
  233. // Overwrite
  234. Configure::write('DataPreparation.notrim', true);
  235. $this->Controller = new CommonComponentTestController(new CakeRequest, new CakeResponse);
  236. $this->Controller->request->data = $array;
  237. $this->Controller->request->query = $array;
  238. $this->Controller->constructClasses();
  239. $this->Controller->startupProcess();
  240. $this->assertSame($array, $this->Controller->request->data);
  241. $this->assertSame($array, $this->Controller->request->query);
  242. }
  243. }
  244. /*** additional helper classes ***/
  245. class MyToolsUser extends AppModel {
  246. public $useTable = 'tools_users';
  247. public $name = 'MyToolsUser';
  248. public $alias = 'User';
  249. public $belongsTo = array(
  250. 'Role',
  251. );
  252. }
  253. // Use Controller instead of AppController to avoid conflicts
  254. class CommonComponentTestController extends Controller {
  255. public $components = array('Session', 'Tools.Common', 'Auth');
  256. public $failed = false;
  257. public $testHeaders = array();
  258. public function fail() {
  259. $this->failed = true;
  260. }
  261. public function redirect($url, $status = null, $exit = true) {
  262. return $status;
  263. }
  264. public function header($status) {
  265. $this->testHeaders[] = $status;
  266. }
  267. }
  268. class TestComponent extends Component {
  269. public $Controller;
  270. public $isInit = false;
  271. public $isStartup = false;
  272. public function initialize(Controller $Controller) {
  273. //$this->Controller = $Controller;
  274. $this->isInit = true;
  275. }
  276. public function startup(Controller $Controller) {
  277. //$this->Controller = $Controller;
  278. $this->isStartup = true;
  279. }
  280. }
  281. class TestHelper extends Object {
  282. }
  283. class TestLib {
  284. public $hasOptions = false;
  285. public function __construct($options = array()) {
  286. if (!empty($options)) {
  287. $this->hasOptions = true;
  288. }
  289. }
  290. }