1--TEST--
2mysqli->change_user()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8
9require_once 'table.inc';
10
11if (mysqli_get_server_version($link) >= 50600)
12    die("SKIP For MySQL < 5.6.0");
13?>
14--FILE--
15<?php
16    require_once 'connect.inc';
17
18    $mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
19
20    if (false !== ($tmp = $mysqli->change_user($user . '_unknown_really', $passwd . 'non_empty', $db)))
21        printf("[006] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
22
23    if (false !== ($tmp = $mysqli->change_user($user, $passwd . '_unknown_really', $db)))
24        printf("[007] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
25
26    if (false !== ($tmp = $mysqli->change_user($user, $passwd, $db . '_unknown_really')))
27        printf("[008] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
28
29    // Reconnect because after 3 failed change_user attempts, the server blocks you off.
30    $mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket);
31
32    if (!$mysqli->query('SET @mysqli_change_user_test_var=1'))
33        printf("[009] Failed to set test variable: [%d] %s\n", $mysqli->errno, $mysqli->error);
34
35    if (!$res = $mysqli->query('SELECT @mysqli_change_user_test_var AS test_var'))
36        printf("[010] [%d] %s\n", $mysqli->errno, $mysqli->error);
37    $tmp = $res->fetch_assoc();
38    $res->free_result();
39    if (1 != $tmp['test_var'])
40        printf("[011] Cannot set test variable\n");
41
42    if (true !== ($tmp = $mysqli->change_user($user, $passwd, $db)))
43        printf("[012] Expecting true, got %s/%s\n", gettype($tmp), $tmp);
44
45    if (!$res = $mysqli->query('SELECT database() AS dbname, user() AS user'))
46        printf("[013] [%d] %s\n", $mysqli->errno, $mysqli->error);
47    $tmp = $res->fetch_assoc();
48    $res->free_result();
49
50    if (substr($tmp['user'], 0, strlen($user)) !== $user)
51        printf("[014] Expecting user %s, got user() %s\n", $user, $tmp['user']);
52    if ($tmp['dbname'] != $db)
53        printf("[015] Expecting database %s, got database() %s\n", $db, $tmp['dbname']);
54
55    if (!$res = $mysqli->query('SELECT @mysqli_change_user_test_var AS test_var'))
56        printf("[016] [%d] %s\n", $mysqli->errno, $mysqli->error);
57    $tmp = $res->fetch_assoc();
58    $res->free_result();
59    if (NULL !== $tmp['test_var'])
60        printf("[017] Test variable is still set!\n");
61
62    $mysqli->close();
63
64    if (false !== ($tmp = @$mysqli->change_user($user, $passwd, $db)))
65        printf("[018] Expecting false, got %s/%s\n", gettype($tmp), $tmp);
66
67    print "done!";
68?>
69--EXPECT--
70done!
71