1--TEST--
2mysqli_change_user() - SET NAMES
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8?>
9--FILE--
10<?php
11    require_once 'connect.inc';
12
13    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
14        printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
15
16    if (!$res = mysqli_query($link, "SHOW CHARACTER SET LIKE 'latin%'"))
17        printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
18
19    $charsets = array();
20    while ($row = mysqli_fetch_assoc($res))
21        $charsets[$row['Charset']] = $row['Default collation'];
22
23    mysqli_free_result($res);
24    if (!mysqli_query($link, 'SET NAMES DEFAULT'))
25        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
26
27    if (!$res = mysqli_query($link, 'SELECT
28        @@character_set_client AS charset_client,
29        @@character_set_connection AS charset_connection,
30        @@character_set_results AS charset_results,
31        @@collation_connection AS collation_connection,
32        @@collation_database AS collation_database,
33        @@collation_server AS collation_server'))
34        printf("[004] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
35
36    if (!$defaults = mysqli_fetch_assoc($res))
37        printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
38    mysqli_free_result($res);
39
40    $not_changed = $defaults;
41    foreach ($charsets as $charset => $collation) {
42
43        if (isset($not_changed['charset_client']) &&
44                $charset != $not_changed['charset_client'] &&
45                mysqli_query($link, sprintf("SET @@character_set_client = '%s'", $charset)))
46            unset($not_changed['charset_client']);
47
48        if (isset($not_changed['charset_connection']) &&
49                $charset != $not_changed['charset_connection'] &&
50                mysqli_query($link, sprintf("SET @@character_connection = '%s'", $charset)))
51            unset($not_changed['charset_connection']);
52
53        if (isset($not_changed['charset_results']) &&
54                $charset != $not_changed['charset_results'] &&
55                mysqli_query($link, sprintf("SET @@character_set_results = '%s'", $charset)))
56            unset($not_changed['charset_results']);
57
58        if (isset($not_changed['collation_connection']) &&
59                $collation != $not_changed['collation_connection'] &&
60                mysqli_query($link, sprintf("SET @@collation_connection = '%s'", $collation)))
61            unset($not_changed['collation_connection']);
62
63        if (isset($not_changed['collation_database']) &&
64                $collation != $not_changed['collation_database'] &&
65                mysqli_query($link, sprintf("SET @@collation_database = '%s'", $collation)))
66            unset($not_changed['collation_database']);
67
68        if (isset($not_changed['collation_server']) &&
69                $collation != $not_changed['collation_server'] &&
70                mysqli_query($link, sprintf("SET @@collation_server = '%s'", $collation)))
71            unset($not_changed['collation_server']);
72
73        if (empty($not_changed))
74            break;
75    }
76
77    if (!$res = mysqli_query($link, 'SELECT
78        @@character_set_client AS charset_client,
79        @@character_set_connection AS charset_connection,
80        @@character_set_results AS charset_results,
81        @@collation_connection AS collation_connection,
82        @@collation_database AS collation_database,
83        @@collation_server AS collation_server'))
84        printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
85
86    if (!$modified = mysqli_fetch_assoc($res))
87        printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
88    mysqli_free_result($res);
89
90    if ($modified == $defaults)
91        printf("[008] Not all settings have been changed\n");
92
93    // LAST_INSERT_ID should be reset
94    mysqli_change_user($link, $user, $passwd, $db);
95
96    if (!$res = mysqli_query($link, 'SELECT
97        @@character_set_client AS charset_client,
98        @@character_set_connection AS charset_connection,
99        @@character_set_results AS charset_results,
100        @@collation_connection AS collation_connection,
101        @@collation_database AS collation_database,
102        @@collation_server AS collation_server'))
103        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
104
105    if (!$new = mysqli_fetch_assoc($res))
106        printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
107    mysqli_free_result($res);
108
109    if ($new == $modified) {
110        printf("[011] Charsets/collations have not been reset.\n");
111        printf("Got:\n");
112        var_dump($new);
113        printf("Expected:\n");
114        var_dump($defaults);
115    }
116
117    if ($new != $defaults) {
118        printf("[012] Charsets/collations have not been reset to their defaults.\n");
119        printf("Got:\n");
120        var_dump($new);
121        printf("Expected:\n");
122        var_dump($defaults);
123    }
124
125    if (!is_object($charset = mysqli_get_charset($link)))
126        printf("[013] Expecting object/std_class, got %s/%s\n", gettype($charset), $charset);
127
128    if ($charset->charset != $defaults['charset_connection'])
129        printf("[014] Expecting connection charset to be %s got %s\n",
130            $defaults['charset_connection'],
131            $charset->charset);
132
133    if ($charset->collation != $defaults['collation_connection'])
134        printf("[015] Expecting collation to be %s got %s\n",
135            $defaults['collation_connection'],
136            $charset->collation);
137
138    mysqli_close($link);
139    print "done!";
140?>
141--EXPECT--
142done!
143