PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/test/libraries/PMA_SetupIndex_test.php

https://gitlab.com/trungthao379/phpmyadmin
PHP | 395 lines | 282 code | 60 blank | 53 comment | 11 complexity | 1060d3c8fd4462010854971f86c99ec6 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * tests for methods under setup/lib/index.lib.php
  5. *
  6. * @package PhpMyAdmin-test
  7. */
  8. /*
  9. * Include to test
  10. */
  11. use PMA\libraries\config\ConfigFile;
  12. use PMA\libraries\config\ServerConfigChecks;
  13. require_once 'libraries/config/config_functions.lib.php';
  14. require_once 'setup/lib/index.lib.php';
  15. /**
  16. * tests for methods under setup/lib/index.lib.php
  17. *
  18. * @package PhpMyAdmin-test
  19. */
  20. class PMA_SetupIndex_Test extends PHPUnit_Framework_TestCase
  21. {
  22. /**
  23. * SetUp for test cases
  24. *
  25. * @return void
  26. */
  27. public function setup()
  28. {
  29. $GLOBALS['cfg']['ProxyUrl'] = '';
  30. }
  31. /**
  32. * Test for PMA_messagesBegin()
  33. *
  34. * @return void
  35. */
  36. public function testPMAmessagesBegin()
  37. {
  38. $_SESSION['messages'] = array(
  39. array(
  40. array('foo'),
  41. array('bar')
  42. )
  43. );
  44. PMA_messagesBegin();
  45. $this->assertEquals(
  46. array(
  47. array(
  48. array(
  49. 0 => 'foo',
  50. 'fresh' => false,
  51. 'active' => false
  52. ),
  53. array(
  54. 0 => 'bar',
  55. 'fresh' => false,
  56. 'active' => false
  57. )
  58. )
  59. ),
  60. $_SESSION['messages']
  61. );
  62. // case 2
  63. unset($_SESSION['messages']);
  64. PMA_messagesBegin();
  65. $this->assertEquals(
  66. array(
  67. 'error' => array(),
  68. 'notice' => array()
  69. ),
  70. $_SESSION['messages']
  71. );
  72. }
  73. /**
  74. * Test for PMA_messagesSet
  75. *
  76. * @return void
  77. */
  78. public function testPMAmessagesSet()
  79. {
  80. PMA_messagesSet('type', '123', 'testTitle', 'msg');
  81. $this->assertEquals(
  82. array(
  83. 'fresh' => true,
  84. 'active' => true,
  85. 'title' => 'testTitle',
  86. 'message' => 'msg'
  87. ),
  88. $_SESSION['messages']['type']['123']
  89. );
  90. }
  91. /**
  92. * Test for PMA_messagesEnd
  93. *
  94. * @return void
  95. */
  96. public function testPMAmessagesEnd()
  97. {
  98. $_SESSION['messages'] = array(
  99. array(
  100. array('msg' => 'foo', 'active' => false),
  101. array('msg' => 'bar', 'active' => true),
  102. )
  103. );
  104. PMA_messagesEnd();
  105. $this->assertEquals(
  106. array(
  107. array(
  108. '1' => array(
  109. 'msg' => 'bar',
  110. 'active' => 1
  111. )
  112. )
  113. ),
  114. $_SESSION['messages']
  115. );
  116. }
  117. /**
  118. * Test for PMA_messagesShowHtml
  119. *
  120. * @return void
  121. */
  122. public function testPMAMessagesShowHTML()
  123. {
  124. $_SESSION['messages'] = array(
  125. 'type' => array(
  126. array('title' => 'foo', 'message' => '123', 'fresh' => false),
  127. array('title' => 'bar', 'message' => '321', 'fresh' => true),
  128. )
  129. );
  130. ob_start();
  131. PMA_messagesShowHtml();
  132. $result = ob_get_clean();
  133. $this->assertContains(
  134. '<div class="type" id="0"><h4>foo</h4>123</div>',
  135. $result
  136. );
  137. $this->assertContains(
  138. '<div class="type" id="1"><h4>bar</h4>321</div>',
  139. $result
  140. );
  141. $this->assertContains(
  142. '<script type="text/javascript">',
  143. $result
  144. );
  145. $this->assertContains(
  146. "hiddenMessages.push('0');",
  147. $result
  148. );
  149. $this->assertContains(
  150. "</script>",
  151. $result
  152. );
  153. }
  154. /**
  155. * Test for PMA_checkConfigRw
  156. *
  157. * @return void
  158. */
  159. public function testPMACheckConfigRw()
  160. {
  161. if (! PMA_HAS_RUNKIT) {
  162. $this->markTestSkipped('Cannot redefine constant');
  163. }
  164. $redefine = null;
  165. $GLOBALS['cfg']['AvailableCharsets'] = array();
  166. $GLOBALS['server'] = 0;
  167. $GLOBALS['ConfigFile'] = new ConfigFile();
  168. if (!defined('SETUP_CONFIG_FILE')) {
  169. define('SETUP_CONFIG_FILE', 'test/test_data/configfile');
  170. } else {
  171. $redefine = 'SETUP_CONFIG_FILE';
  172. runkit_constant_redefine(
  173. 'SETUP_CONFIG_FILE',
  174. 'test/test_data/configfile'
  175. );
  176. }
  177. $is_readable = false;
  178. $is_writable = false;
  179. $file_exists = false;
  180. PMA_checkConfigRw($is_readable, $is_writable, $file_exists);
  181. $this->assertTrue(
  182. $is_readable
  183. );
  184. $this->assertTrue(
  185. $is_writable
  186. );
  187. $this->assertFalse(
  188. $file_exists
  189. );
  190. runkit_constant_redefine(
  191. 'SETUP_CONFIG_FILE',
  192. 'test/test_data/test.file'
  193. );
  194. PMA_checkConfigRw($is_readable, $is_writable, $file_exists);
  195. $this->assertTrue(
  196. $is_readable
  197. );
  198. $this->assertTrue(
  199. $is_writable
  200. );
  201. $this->assertTrue(
  202. $file_exists
  203. );
  204. if ($redefine !== null) {
  205. runkit_constant_redefine('SETUP_CONFIG_FILE', $redefine);
  206. } else {
  207. runkit_constant_remove('SETUP_CONFIG_FILE');
  208. }
  209. }
  210. /**
  211. * Test for ServerConfigChecks::performConfigChecks
  212. *
  213. * @return void
  214. * @group medium
  215. */
  216. public function testServerConfigChecksPerformConfigChecks()
  217. {
  218. $GLOBALS['cfg']['AvailableCharsets'] = array();
  219. $GLOBALS['cfg']['ServerDefault'] = 0;
  220. $GLOBALS['server'] = 0;
  221. $cf = new ConfigFile();
  222. $GLOBALS['ConfigFile'] = $cf;
  223. $reflection = new \ReflectionProperty('PMA\libraries\config\ConfigFile', '_id');
  224. $reflection->setAccessible(true);
  225. $sessionID = $reflection->getValue($cf);
  226. $_SESSION[$sessionID]['Servers'] = array(
  227. '1' => array(
  228. 'host' => 'localhost',
  229. 'ssl' => false,
  230. 'extension' => 'mysql',
  231. 'auth_type' => 'config',
  232. 'user' => 'username',
  233. 'password' => 'password',
  234. 'AllowRoot' => true,
  235. 'AllowNoPassword' => true,
  236. )
  237. );
  238. $_SESSION[$sessionID]['AllowArbitraryServer'] = true;
  239. $_SESSION[$sessionID]['LoginCookieValidity'] = 5000;
  240. $_SESSION[$sessionID]['LoginCookieStore'] = 4000;
  241. $_SESSION[$sessionID]['SaveDir'] = true;
  242. $_SESSION[$sessionID]['TempDir'] = true;
  243. $_SESSION[$sessionID]['GZipDump'] = true;
  244. $_SESSION[$sessionID]['BZipDump'] = true;
  245. $_SESSION[$sessionID]['ZipDump'] = true;
  246. $noticeArrayKeys = array(
  247. 'TempDir',
  248. 'SaveDir',
  249. 'LoginCookieValidity',
  250. 'AllowArbitraryServer',
  251. 'Servers/1/AllowNoPassword',
  252. 'Servers/1/auth_type',
  253. 'Servers/1/ssl'
  254. );
  255. $errorArrayKeys = array(
  256. 'LoginCookieValidity'
  257. );
  258. if (@!function_exists('gzopen') || @!function_exists('gzencode')) {
  259. $errorArrayKeys[] = 'GZipDump';
  260. }
  261. if (@!function_exists('bzopen') || @!function_exists('bzcompress')) {
  262. $errorArrayKeys[] = 'BZipDump';
  263. }
  264. if (!@function_exists('zip_open')) {
  265. $errorArrayKeys[] = 'ZipDump_import';
  266. }
  267. if (!@function_exists('gzcompress')) {
  268. $errorArrayKeys[] = 'ZipDump_export';
  269. }
  270. $configChecker = new ServerConfigChecks($GLOBALS['ConfigFile']);
  271. $configChecker->performConfigChecks();
  272. foreach ($noticeArrayKeys as $noticeKey) {
  273. $this->assertArrayHasKey(
  274. $noticeKey,
  275. $_SESSION['messages']['notice']
  276. );
  277. }
  278. foreach ($errorArrayKeys as $errorKey) {
  279. $this->assertArrayHasKey(
  280. $errorKey,
  281. $_SESSION['messages']['error']
  282. );
  283. }
  284. // Case 2
  285. unset($_SESSION['messages']);
  286. unset($_SESSION[$sessionID]);
  287. $_SESSION[$sessionID]['Servers'] = array(
  288. '1' => array(
  289. 'host' => 'localhost',
  290. 'ssl' => true,
  291. 'extension' => 'mysqli',
  292. 'auth_type' => 'cookie',
  293. 'AllowRoot' => false
  294. )
  295. );
  296. $_SESSION[$sessionID]['AllowArbitraryServer'] = false;
  297. $_SESSION[$sessionID]['LoginCookieValidity'] = -1;
  298. $_SESSION[$sessionID]['LoginCookieStore'] = 0;
  299. $_SESSION[$sessionID]['SaveDir'] = '';
  300. $_SESSION[$sessionID]['TempDir'] = '';
  301. $_SESSION[$sessionID]['GZipDump'] = false;
  302. $_SESSION[$sessionID]['BZipDump'] = false;
  303. $_SESSION[$sessionID]['ZipDump'] = false;
  304. $configChecker = new ServerConfigChecks($GLOBALS['ConfigFile']);
  305. $configChecker->performConfigChecks();
  306. $this->assertArrayHasKey(
  307. 'blowfish_secret_created',
  308. $_SESSION['messages']['notice']
  309. );
  310. foreach ($noticeArrayKeys as $noticeKey) {
  311. $this->assertArrayNotHasKey(
  312. $noticeKey,
  313. $_SESSION['messages']['notice']
  314. );
  315. }
  316. $this->assertArrayNotHasKey(
  317. 'error',
  318. $_SESSION['messages']
  319. );
  320. // Case 3
  321. $_SESSION[$sessionID]['blowfish_secret'] = 'sec';
  322. $_SESSION[$sessionID]['Servers'] = array(
  323. '1' => array(
  324. 'host' => 'localhost',
  325. 'auth_type' => 'cookie'
  326. )
  327. );
  328. $configChecker = new ServerConfigChecks($GLOBALS['ConfigFile']);
  329. $configChecker->performConfigChecks();
  330. $this->assertArrayHasKey(
  331. 'blowfish_warnings2',
  332. $_SESSION['messages']['error']
  333. );
  334. }
  335. }