PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/test/classes/controllers/ServerVariablesControllerTest.php

https://gitlab.com/trungthao379/phpmyadmin
PHP | 325 lines | 228 code | 41 blank | 56 comment | 0 complexity | 7d088c47c54a94e4b5bc8419f1c7191a MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds ServerVariablesControllerTest class
  5. *
  6. * @package PhpMyAdmin-test
  7. */
  8. use PMA\libraries\di\Container;
  9. use PMA\libraries\Theme;
  10. use PMA\libraries\URL;
  11. require_once 'libraries/database_interface.inc.php';
  12. require_once 'test/libraries/stubs/ResponseStub.php';
  13. require_once 'test/PMATestCase.php';
  14. /**
  15. * Tests for ServerVariablesController class
  16. *
  17. * @package PhpMyAdmin-test
  18. */
  19. class ServerVariablesControllerTest extends PMATestCase
  20. {
  21. /**
  22. * @var \PMA\Test\Stubs\Response
  23. */
  24. private $_response;
  25. /**
  26. * Test for setUp
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. //$_REQUEST
  33. $_REQUEST['log'] = "index1";
  34. $_REQUEST['pos'] = 3;
  35. //$GLOBALS
  36. $GLOBALS['PMA_PHP_SELF'] = PMA_getenv('PHP_SELF');
  37. $GLOBALS['server'] = 1;
  38. $GLOBALS['table'] = "table";
  39. //$_SESSION
  40. //Mock DBI
  41. $dbi = $this->getMockBuilder('PMA\libraries\DatabaseInterface')
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. //this data is needed when ServerStatusData constructs
  45. $server_session_variable = array(
  46. "auto_increment_increment" => "1",
  47. "auto_increment_offset" => "13",
  48. "automatic_sp_privileges" => "ON",
  49. "back_log" => "50",
  50. "big_tables" => "OFF",
  51. );
  52. $server_global_variables = array(
  53. "auto_increment_increment" => "0",
  54. "auto_increment_offset" => "12"
  55. );
  56. $fetchResult = array(
  57. array(
  58. "SHOW SESSION VARIABLES;",
  59. 0,
  60. 1,
  61. null,
  62. 0,
  63. $server_session_variable
  64. ),
  65. array(
  66. "SHOW GLOBAL VARIABLES;",
  67. 0,
  68. 1,
  69. null,
  70. 0,
  71. $server_global_variables
  72. )
  73. );
  74. $dbi->expects($this->any())->method('fetchResult')
  75. ->will($this->returnValueMap($fetchResult));
  76. $GLOBALS['dbi'] = $dbi;
  77. $container = Container::getDefaultContainer();
  78. $container->set('dbi', $GLOBALS['dbi']);
  79. $this->_response = new \PMA\Test\Stubs\Response();
  80. $container->set('PMA\libraries\Response', $this->_response);
  81. $container->alias('response', 'PMA\libraries\Response');
  82. }
  83. /**
  84. * Test for _formatVariable()
  85. *
  86. * @return void
  87. */
  88. public function testFormatVariable()
  89. {
  90. $class = new ReflectionClass(
  91. '\PMA\libraries\controllers\server\ServerVariablesController'
  92. );
  93. $method = $class->getMethod('_formatVariable');
  94. $method->setAccessible(true);
  95. $container = Container::getDefaultContainer();
  96. $container->factory(
  97. 'PMA\libraries\controllers\server\ServerVariablesController'
  98. );
  99. $container->alias(
  100. 'ServerVariablesController',
  101. 'PMA\libraries\controllers\server\ServerVariablesController'
  102. );
  103. $ctrl = $container->get('ServerVariablesController');
  104. //Call the test function
  105. $name_for_value_byte = "binlog_cache_size";
  106. $name_for_value_not_byte = "auto_increment_increment";
  107. $name_for_value_not_num = "PMA_key";
  108. //name is_numeric and the value type is byte
  109. $args = array($name_for_value_byte, "3");
  110. list($formattedValue, $isHtmlFormatted) = $method->invokeArgs($ctrl, $args);
  111. $this->assertEquals(
  112. '<abbr title="3">3 B</abbr>',
  113. $formattedValue
  114. );
  115. $this->assertEquals(true, $isHtmlFormatted);
  116. //name is_numeric and the value type is not byte
  117. $args = array($name_for_value_not_byte, "3");
  118. list($formattedValue, $isHtmlFormatted) = $method->invokeArgs($ctrl, $args);
  119. $this->assertEquals(
  120. '3',
  121. $formattedValue
  122. );
  123. $this->assertEquals(false, $isHtmlFormatted);
  124. //value is not a number
  125. $args = array($name_for_value_not_byte, "value");
  126. list($formattedValue, $isHtmlFormatted) = $method->invokeArgs($ctrl, $args);
  127. $this->assertEquals(
  128. 'value',
  129. $formattedValue
  130. );
  131. $this->assertEquals(false, $isHtmlFormatted);
  132. }
  133. /**
  134. * Test for _getHtmlForLinkTemplates()
  135. *
  136. * @return void
  137. */
  138. public function testGetHtmlForLinkTemplates()
  139. {
  140. $class = new ReflectionClass(
  141. '\PMA\libraries\controllers\server\ServerVariablesController'
  142. );
  143. $method = $class->getMethod('_getHtmlForLinkTemplates');
  144. $method->setAccessible(true);
  145. $container = Container::getDefaultContainer();
  146. $container->factory(
  147. 'PMA\libraries\controllers\server\ServerVariablesController'
  148. );
  149. $container->alias(
  150. 'ServerVariablesController',
  151. 'PMA\libraries\controllers\server\ServerVariablesController'
  152. );
  153. $ctrl = $container->get('ServerVariablesController');
  154. //Call the test function
  155. $html = $method->invoke($ctrl);
  156. $url = 'server_variables.php' . URL::getCommon(array());
  157. //validate 1: URL
  158. $this->assertContains(
  159. $url,
  160. $html
  161. );
  162. //validate 2: images
  163. $this->assertContains(
  164. PMA\libraries\Util::getIcon('b_save.png', __('Save')),
  165. $html
  166. );
  167. $this->assertContains(
  168. PMA\libraries\Util::getIcon('b_close.png', __('Cancel')),
  169. $html
  170. );
  171. }
  172. /**
  173. * Test for PMA_getHtmlForServerVariables()
  174. *
  175. * @return void
  176. */
  177. public function testPMAGetHtmlForServerVariables()
  178. {
  179. $class = new ReflectionClass(
  180. '\PMA\libraries\controllers\server\ServerVariablesController'
  181. );
  182. $method = $class->getMethod('_getHtmlForServerVariables');
  183. $method->setAccessible(true);
  184. $container = Container::getDefaultContainer();
  185. $container->factory(
  186. 'PMA\libraries\controllers\server\ServerVariablesController'
  187. );
  188. $container->alias(
  189. 'ServerVariablesController',
  190. 'PMA\libraries\controllers\server\ServerVariablesController'
  191. );
  192. $ctrl = $container->get('ServerVariablesController');
  193. $_REQUEST['filter'] = "auto-commit";
  194. $serverVarsSession
  195. = $GLOBALS['dbi']->fetchResult('SHOW SESSION VARIABLES;', 0, 1);
  196. $serverVars = $GLOBALS['dbi']->fetchResult('SHOW GLOBAL VARIABLES;', 0, 1);
  197. $html = $method->invoke($ctrl, $serverVars, $serverVarsSession);
  198. //validate 1: Filters
  199. $this->assertContains(
  200. '<legend>' . __('Filters') . '</legend>',
  201. $html
  202. );
  203. $this->assertContains(
  204. __('Containing the word:'),
  205. $html
  206. );
  207. $this->assertContains(
  208. $_REQUEST['filter'],
  209. $html
  210. );
  211. //validate 2: Server Variables
  212. $this->assertContains(
  213. '<table id="serverVariables" class="data filteredData noclick">',
  214. $html
  215. );
  216. $this->assertContains(
  217. __('Variable'),
  218. $html
  219. );
  220. $this->assertContains(
  221. __('Global value'),
  222. $html
  223. );
  224. }
  225. /**
  226. * Test for _getHtmlForServerVariablesItems()
  227. *
  228. * @return void
  229. */
  230. public function testGetHtmlForServerVariablesItems()
  231. {
  232. $class = new ReflectionClass(
  233. '\PMA\libraries\controllers\server\ServerVariablesController'
  234. );
  235. $method = $class->getMethod('_getHtmlForServerVariablesItems');
  236. $method->setAccessible(true);
  237. $container = Container::getDefaultContainer();
  238. $container->factory(
  239. 'PMA\libraries\controllers\server\ServerVariablesController'
  240. );
  241. $container->alias(
  242. 'ServerVariablesController',
  243. 'PMA\libraries\controllers\server\ServerVariablesController'
  244. );
  245. $ctrl = $container->get('ServerVariablesController');
  246. $serverVarsSession
  247. = $GLOBALS['dbi']->fetchResult('SHOW SESSION VARIABLES;', 0, 1);
  248. $serverVars = $GLOBALS['dbi']->fetchResult('SHOW GLOBAL VARIABLES;', 0, 1);
  249. $html = $method->invoke($ctrl, $serverVars, $serverVarsSession);
  250. //validate 1: variable: auto_increment_increment
  251. $name = "auto_increment_increment";
  252. $value = htmlspecialchars(str_replace('_', ' ', $name));
  253. $this->assertContains(
  254. $value,
  255. $html
  256. );
  257. //validate 2: variable: auto_increment_offset
  258. $name = "auto_increment_offset";
  259. $value = htmlspecialchars(str_replace('_', ' ', $name));
  260. $this->assertContains(
  261. $value,
  262. $html
  263. );
  264. $formatVariable = $class->getMethod('_formatVariable');
  265. $formatVariable->setAccessible(true);
  266. $args = array($name, "12");
  267. list($value, $isHtmlFormatted) = $formatVariable->invokeArgs($ctrl, $args);
  268. $this->assertContains(
  269. $value,
  270. $html
  271. );
  272. //validate 3: variables
  273. $this->assertContains(
  274. __('Session value'),
  275. $html
  276. );
  277. $args = array($name, "13");
  278. list($value, $isHtmlFormatted) = $formatVariable->invokeArgs($ctrl, $args);
  279. $this->assertContains(
  280. $value,
  281. $html
  282. );
  283. }
  284. }