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