xref: /PHP-5.3/ext/pgsql/tests/08escape.phpt (revision 030ebaaa)
1--TEST--
2PostgreSQL escape functions
3--SKIPIF--
4<?php include("skipif.inc"); ?>
5--FILE--
6<?php
7
8include 'config.inc';
9define('FILE_NAME', dirname(__FILE__) . '/php.gif');
10
11// pg_escape_string() test
12$before = "ABC\\ABC\'";
13$expect  = "ABC\\\\ABC\\'";
14$after = pg_escape_string($before);
15if ($expect === $after) {
16	echo "pg_escape_string() is Ok\n";
17}
18else {
19	echo "pg_escape_string() is NOT Ok\n";
20	var_dump($before);
21	var_dump($after);
22	var_dump($expect);
23}
24
25// pg_escape_bytea() test
26$before = "ABC\\ABC";
27$expect  = "ABC\\\\\\\\ABC";
28$after  = pg_escape_bytea($before);
29if ($expect === $after) {
30	echo "pg_escape_bytea() is Ok\n";
31}
32else {
33	echo "pg_escape_byte() is NOT Ok\n";
34	var_dump($before);
35	var_dump($after);
36	var_dump($expect);
37}
38
39// Test using database
40$data = file_get_contents(FILE_NAME);
41$db   = pg_connect($conn_str);
42
43// Insert binary to DB
44$escaped_data = pg_escape_bytea($data);
45pg_query("DELETE FROM ".$table_name." WHERE num = -9999;");
46$sql = "INSERT INTO ".$table_name." (num, bin) VALUES (-9999, CAST ('".$escaped_data."' AS BYTEA));";
47pg_query($db, $sql);
48
49// Retrieve binary from DB
50$sql = "SELECT bin::bytea FROM ".$table_name." WHERE num = -9999";
51$result = pg_query($db, $sql);
52$row = pg_fetch_array($result, 0, PGSQL_ASSOC);
53
54if ($data === pg_unescape_bytea($row['bin'])) {
55	echo "pg_escape_bytea() actually works with database\n";
56}
57else {
58	echo "pg_escape_bytea() is broken\n";
59}
60
61?>
62--EXPECT--
63pg_escape_string() is NOT Ok
64string(9) "ABC\ABC\'"
65string(12) "ABC\\ABC\\''"
66string(10) "ABC\\ABC\'"
67pg_escape_bytea() is Ok
68pg_escape_bytea() actually works with database
69