blob: 5df812ca6b3fc5eb16053affa2aaf58b3c45a33f [file] [log] [blame]
Taufan Aditya2d574452012-05-25 04:03:56 +07001<?php
2
3class Escape_test extends CI_TestCase {
4
5 /**
6 * @var object Database/Query Builder holder
7 */
8 protected $db;
9
10 public function set_up()
11 {
12 $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER);
13
14 Mock_Database_Schema_Skeleton::create_tables();
15 Mock_Database_Schema_Skeleton::create_data();
Taufan Aditya502942b2012-05-25 07:00:41 +070016
17 $this->pre = (strpos(DB_DRIVER, 'pgsql') === FALSE) ? '`' : '"';
18 $this->esc = (strpos(DB_DRIVER, 'mysql') === FALSE) ? '!' : '';
Taufan Aditya2d574452012-05-25 04:03:56 +070019 }
20
21 // ------------------------------------------------------------------------
22
23 /**
24 * @see ./mocks/schema/skeleton.php
25 */
26 public function test_escape_like_percent_sign()
27 {
28 $string = $this->db->escape_like_str('\%foo');
Taufan Aditya502942b2012-05-25 07:00:41 +070029
30 $sql = "SELECT {$this->pre}value{$this->pre} FROM {$this->pre}misc{$this->pre} WHERE {$this->pre}key{$this->pre} LIKE '$string%' ESCAPE '$this->esc';";
Taufan Aditya98dcac72012-05-25 05:07:51 +070031
Taufan Aditya6a432442012-05-25 04:09:38 +070032 $res = $this->db->query($sql)->result_array();
Taufan Aditya2d574452012-05-25 04:03:56 +070033
34 // Check the result
Taufan Aditya3c5abf92012-05-25 07:17:26 +070035 $this->assertEquals(1, count($res));
Taufan Aditya2d574452012-05-25 04:03:56 +070036 }
37
38 // ------------------------------------------------------------------------
39
40 /**
41 * @see ./mocks/schema/skeleton.php
42 */
43 public function test_escape_like_backslash_sign()
44 {
45 $string = $this->db->escape_like_str('\\');
Taufan Aditya502942b2012-05-25 07:00:41 +070046
47 $sql = "SELECT {$this->pre}value{$this->pre} FROM {$this->pre}misc{$this->pre} WHERE {$this->pre}key{$this->pre} LIKE '$string%' ESCAPE '$this->esc';";
48
Taufan Aditya6a432442012-05-25 04:09:38 +070049 $res = $this->db->query($sql)->result_array();
Taufan Aditya2d574452012-05-25 04:03:56 +070050
51 // Check the result
Taufan Aditya3c5abf92012-05-25 07:17:26 +070052 $this->assertEquals(2, count($res));
Taufan Aditya2d574452012-05-25 04:03:56 +070053 }
Andrey Andreev59d6b4f2012-05-25 02:08:00 +030054
Taufan Aditya2d574452012-05-25 04:03:56 +070055}