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(); |
| 16 | } |
| 17 | |
| 18 | // ------------------------------------------------------------------------ |
| 19 | |
| 20 | /** |
| 21 | * @see ./mocks/schema/skeleton.php |
| 22 | */ |
| 23 | public function test_escape_like_percent_sign() |
| 24 | { |
Taufan Aditya | 1af5b47 | 2012-05-26 16:04:39 +0700 | [diff] [blame] | 25 | // Escape the like string |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 26 | $string = $this->db->escape_like_str('\%foo'); |
Taufan Aditya | 502942b | 2012-05-25 07:00:41 +0700 | [diff] [blame] | 27 | |
Taufan Aditya | 1af5b47 | 2012-05-26 16:04:39 +0700 | [diff] [blame] | 28 | if (strpos(DB_DRIVER, 'mysql') !== FALSE) |
| 29 | { |
Andrey Andreev | 13be557 | 2012-10-04 12:43:03 +0300 | [diff] [blame] | 30 | $sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%' ESCAPE '!';"; |
Taufan Aditya | 1af5b47 | 2012-05-26 16:04:39 +0700 | [diff] [blame] | 31 | } |
| 32 | else |
| 33 | { |
| 34 | $sql = 'SELECT "value" FROM "misc" WHERE "key" LIKE \''.$string.'%\' ESCAPE \'!\';'; |
| 35 | } |
Taufan Aditya | 98dcac7 | 2012-05-25 05:07:51 +0700 | [diff] [blame] | 36 | |
Taufan Aditya | 6a43244 | 2012-05-25 04:09:38 +0700 | [diff] [blame] | 37 | $res = $this->db->query($sql)->result_array(); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 38 | |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 39 | // Check the result |
Andrey Andreev | 20d9b0a | 2017-12-20 19:57:39 +0200 | [diff] [blame^] | 40 | $this->assertCount(1, $res); |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // ------------------------------------------------------------------------ |
| 44 | |
| 45 | /** |
| 46 | * @see ./mocks/schema/skeleton.php |
| 47 | */ |
| 48 | public function test_escape_like_backslash_sign() |
| 49 | { |
Taufan Aditya | 1af5b47 | 2012-05-26 16:04:39 +0700 | [diff] [blame] | 50 | // Escape the like string |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 51 | $string = $this->db->escape_like_str('\\'); |
Taufan Aditya | 502942b | 2012-05-25 07:00:41 +0700 | [diff] [blame] | 52 | |
Taufan Aditya | 1af5b47 | 2012-05-26 16:04:39 +0700 | [diff] [blame] | 53 | if (strpos(DB_DRIVER, 'mysql') !== FALSE) |
| 54 | { |
Andrey Andreev | 13be557 | 2012-10-04 12:43:03 +0300 | [diff] [blame] | 55 | $sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%' ESCAPE '!';"; |
Taufan Aditya | 1af5b47 | 2012-05-26 16:04:39 +0700 | [diff] [blame] | 56 | } |
| 57 | else |
| 58 | { |
| 59 | $sql = 'SELECT "value" FROM "misc" WHERE "key" LIKE \''.$string.'%\' ESCAPE \'!\';'; |
| 60 | } |
Taufan Aditya | 502942b | 2012-05-25 07:00:41 +0700 | [diff] [blame] | 61 | |
Taufan Aditya | 6a43244 | 2012-05-25 04:09:38 +0700 | [diff] [blame] | 62 | $res = $this->db->query($sql)->result_array(); |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 63 | |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 64 | // Check the result |
Andrey Andreev | 20d9b0a | 2017-12-20 19:57:39 +0200 | [diff] [blame^] | 65 | $this->assertCount(2, $res); |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 66 | } |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 67 | |
Andrey Andreev | 20d9b0a | 2017-12-20 19:57:39 +0200 | [diff] [blame^] | 68 | } |