CommonComponentTest.php 9.1 KB

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