2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * tests for methods under PhpMyAdmin\UserPreferences class
6 * @package PhpMyAdmin-test
8 declare(strict_types
=1);
10 namespace PhpMyAdmin\Tests
;
12 use PhpMyAdmin\Config\ConfigFile
;
13 use PhpMyAdmin\DatabaseInterface
;
14 use PhpMyAdmin\Tests\PmaTestCase
;
15 use PhpMyAdmin\UserPreferences
;
18 * tests for methods under PhpMyAdmin\UserPreferences class
20 * @package PhpMyAdmin-test
22 class UserPreferencesTest
extends PmaTestCase
25 * @var UserPreferences
27 private $userPreferences;
30 * Setup various pre conditions
34 protected function setUp(): void
37 include ROOT_PATH
. 'libraries/config.default.php';
38 $GLOBALS['server'] = 0;
39 $GLOBALS['PMA_PHP_SELF'] = '/phpmyadmin/';
41 $this->userPreferences
= new UserPreferences();
49 public function testPageInit()
52 'Server/hide_db' => 'testval123',
53 'Server/port' => '213',
55 $GLOBALS['cfg']['AvailableCharsets'] = [];
56 $GLOBALS['cfg']['UserprefsDeveloperTab'] = null;
58 $this->userPreferences
->pageInit(new ConfigFile());
64 'hide_db' => 'testval123',
68 $_SESSION['ConfigFile' . $GLOBALS['server']]
77 public function testLoad()
79 $_SESSION['relation'][$GLOBALS['server']]['PMA_VERSION'] = PMA_VERSION
;
81 $_SESSION['relation'][$GLOBALS['server']]['userconfigwork'] = null;
82 unset($_SESSION['userconfig']);
84 $result = $this->userPreferences
->load();
93 $result['config_data']
96 $this->assertEqualsWithDelta(
109 $_SESSION['relation'][$GLOBALS['server']]['userconfigwork'] = 1;
110 $_SESSION['relation'][$GLOBALS['server']]['db'] = "pma'db";
111 $_SESSION['relation'][$GLOBALS['server']]['userconfig'] = "testconf";
112 $_SESSION['relation'][$GLOBALS['server']]['user'] = "user";
114 $dbi = $this->getMockBuilder('PhpMyAdmin\DatabaseInterface')
115 ->disableOriginalConstructor()
118 $query = 'SELECT `config_data`, UNIX_TIMESTAMP(`timevalue`) ts '
119 . 'FROM `pma\'db`.`testconf` WHERE `username` = \'user\'';
121 $dbi->expects($this->once())
122 ->method('fetchSingleRow')
123 ->with($query, 'ASSOC', DatabaseInterface
::CONNECT_CONTROL
)
128 'config_data' => json_encode([1, 2]),
132 $dbi->expects($this->any())
133 ->method('escapeString')
134 ->will($this->returnArgument(0));
136 $GLOBALS['dbi'] = $dbi;
138 $result = $this->userPreferences
->load();
158 public function testSave()
160 $GLOBALS['server'] = 2;
161 $_SESSION['relation'][2]['PMA_VERSION'] = PMA_VERSION
;
162 $_SESSION['relation'][2]['userconfigwork'] = null;
163 unset($_SESSION['userconfig']);
165 $result = $this->userPreferences
->save([1]);
173 $_SESSION['userconfig']
178 $_SESSION['userconfig']['db']
181 /* TODO: This breaks sometimes as there might be time difference! */
182 $this->assertEqualsWithDelta(
184 $_SESSION['userconfig']['ts'],
191 if (isset($_SESSION['cache']['server_2']['userprefs'])) {
200 $_SESSION['relation'][$GLOBALS['server']]['userconfigwork'] = 1;
201 $_SESSION['relation'][$GLOBALS['server']]['db'] = "pmadb";
202 $_SESSION['relation'][$GLOBALS['server']]['userconfig'] = "testconf";
203 $_SESSION['relation'][$GLOBALS['server']]['user'] = "user";
205 $query1 = 'SELECT `username` FROM `pmadb`.`testconf` '
206 . 'WHERE `username` = \'user\'';
208 $query2 = 'UPDATE `pmadb`.`testconf` SET `timevalue` = NOW(), `config_data` = \''
209 . json_encode([1]) . '\' WHERE `username` = \'user\'';
211 $dbi = $this->getMockBuilder('PhpMyAdmin\DatabaseInterface')
212 ->disableOriginalConstructor()
215 $dbi->expects($this->once())
216 ->method('fetchValue')
217 ->with($query1, 0, 0, DatabaseInterface
::CONNECT_CONTROL
)
218 ->will($this->returnValue(true));
220 $dbi->expects($this->once())
222 ->with($query2, DatabaseInterface
::CONNECT_CONTROL
)
223 ->will($this->returnValue(true));
225 $dbi->expects($this->any())
226 ->method('escapeString')
227 ->will($this->returnArgument(0));
229 $GLOBALS['dbi'] = $dbi;
231 $this->userPreferences
->save([1])
236 $query1 = 'SELECT `username` FROM `pmadb`.`testconf` '
237 . 'WHERE `username` = \'user\'';
239 $query2 = 'INSERT INTO `pmadb`.`testconf` (`username`, `timevalue`,`config_data`) '
240 . 'VALUES (\'user\', NOW(), \'' . json_encode([1]) . '\')';
242 $dbi = $this->getMockBuilder('PhpMyAdmin\DatabaseInterface')
243 ->disableOriginalConstructor()
246 $dbi->expects($this->once())
247 ->method('fetchValue')
248 ->with($query1, 0, 0, DatabaseInterface
::CONNECT_CONTROL
)
249 ->will($this->returnValue(false));
251 $dbi->expects($this->once())
253 ->with($query2, DatabaseInterface
::CONNECT_CONTROL
)
254 ->will($this->returnValue(false));
256 $dbi->expects($this->once())
258 ->with(DatabaseInterface
::CONNECT_CONTROL
)
259 ->will($this->returnValue("err1"));
260 $dbi->expects($this->any())
261 ->method('escapeString')
262 ->will($this->returnArgument(0));
264 $GLOBALS['dbi'] = $dbi;
266 $result = $this->userPreferences
->save([1]);
268 $this->assertIsObject($result);
270 'Could not save configuration<br><br>err1',
271 $result->getMessage()
280 public function testApply()
282 $GLOBALS['cfg']['UserprefsDisallow'] = [
286 $GLOBALS['cfg']['UserprefsDeveloperTab'] = null;
287 $result = $this->userPreferences
->apply(
290 'ErrorHandler/display' => true,
291 'ErrorHandler/gather' => false,
292 'Servers/foobar' => '123',
293 'Server/hide_db' => true,
312 public function testApplyDevel()
314 $GLOBALS['cfg']['UserprefsDeveloperTab'] = true;
315 $result = $this->userPreferences
->apply(
323 'DBG' => ['sql' => true],
330 * Test for persistOption
334 public function testPersistOption()
336 $_SESSION['relation'][$GLOBALS['server']]['PMA_VERSION'] = PMA_VERSION
;
337 $_SESSION['relation'][$GLOBALS['server']]['userconfigwork'] = null;
338 $_SESSION['userconfig'] = [];
339 $_SESSION['userconfig']['ts'] = "123";
340 $_SESSION['userconfig']['db'] = [
341 'Server/hide_db' => true,
342 'Server/only_db' => true,
345 $GLOBALS['server'] = 2;
346 $_SESSION['relation'][2]['userconfigwork'] = null;
349 $this->userPreferences
->persistOption('Server/hide_db', 'val', 'val')
353 $this->userPreferences
->persistOption('Server/hide_db', 'val2', 'val')
357 $this->userPreferences
->persistOption('Server/hide_db2', 'val', 'val')
366 public function testRedirect()
368 $GLOBALS['lang'] = '';
369 $GLOBALS['db'] = 'db';
370 $GLOBALS['table'] = 'table';
372 $this->mockResponse('Location: /phpmyadmin/file.html?a=b&saved=1&server=0#h+ash');
374 $GLOBALS['PMA_Config']->set('PmaAbsoluteUri', '');
375 $GLOBALS['PMA_Config']->set('PMA_IS_IIS', false);
377 $this->userPreferences
->redirect(
385 * Test for autoloadGetHeader
389 public function testAutoloadGetHeader()
391 $_SESSION['userprefs_autoload'] = false;
392 $_REQUEST['prefs_autoload'] = 'hide';
396 $this->userPreferences
->autoloadGetHeader()
400 $_SESSION['userprefs_autoload']
403 $_REQUEST['prefs_autoload'] = 'nohide';
404 $GLOBALS['cfg']['ServerDefault'] = 1;
405 $GLOBALS['PMA_PHP_SELF'] = 'phpunit';
406 $result = $this->userPreferences
->autoloadGetHeader();
408 $this->assertStringContainsString(
409 '<form action="prefs_manage.php" method="post" class="disableAjax">',
413 $this->assertStringContainsString(
414 '<input type="hidden" name="token" value="token"',
418 $this->assertStringContainsString(
419 '<input type="hidden" name="json" value="">',
423 $this->assertStringContainsString(
424 '<input type="hidden" name="submit_import" value="1">',
428 $this->assertStringContainsString(
429 '<input type="hidden" name="return_url" value="phpunit?">',