CommonComponentTest.php 9.7 KB

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