Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class 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 Aditya | 502942b | 2012-05-25 07:00:41 +0700 | [diff] [blame] | 16 | |
| 17 | $this->pre = (strpos(DB_DRIVER, 'pgsql') === FALSE) ? '`' : '"'; |
| 18 | $this->esc = (strpos(DB_DRIVER, 'mysql') === FALSE) ? '!' : ''; |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 19 | } |
| 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 Aditya | 502942b | 2012-05-25 07:00:41 +0700 | [diff] [blame] | 29 | |
| 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 Aditya | 98dcac7 | 2012-05-25 05:07:51 +0700 | [diff] [blame] | 31 | |
Taufan Aditya | 6a43244 | 2012-05-25 04:09:38 +0700 | [diff] [blame] | 32 | $res = $this->db->query($sql)->result_array(); |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 33 | |
| 34 | // Check the result |
Taufan Aditya | 3c5abf9 | 2012-05-25 07:17:26 +0700 | [diff] [blame^] | 35 | $this->assertEquals(1, count($res)); |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 36 | } |
| 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 Aditya | 502942b | 2012-05-25 07:00:41 +0700 | [diff] [blame] | 46 | |
| 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 Aditya | 6a43244 | 2012-05-25 04:09:38 +0700 | [diff] [blame] | 49 | $res = $this->db->query($sql)->result_array(); |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 50 | |
| 51 | // Check the result |
Taufan Aditya | 3c5abf9 | 2012-05-25 07:17:26 +0700 | [diff] [blame^] | 52 | $this->assertEquals(2, count($res)); |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 53 | } |
Andrey Andreev | 59d6b4f | 2012-05-25 02:08:00 +0300 | [diff] [blame] | 54 | |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 55 | } |