CommonComponentTest.php 9.7 KB

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