PageRenderTime 98ms CodeModel.GetById 22ms RepoModel.GetById 7ms app.codeStats 0ms

/phpmyadmin/test/libraries/PMA_SetupIndex_test.php

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