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