CommonComponentTest.php 9.0 KB

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