blob: de6cd0a9de7949027167dd7bef629ba1b477e2b0 [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();
16 }
17
18 // ------------------------------------------------------------------------
19
20 /**
21 * @see ./mocks/schema/skeleton.php
22 */
23 public function test_escape_like_percent_sign()
24 {
Taufan Aditya1af5b472012-05-26 16:04:39 +070025 // Escape the like string
Taufan Aditya2d574452012-05-25 04:03:56 +070026 $string = $this->db->escape_like_str('\%foo');
Taufan Aditya502942b2012-05-25 07:00:41 +070027
Taufan Aditya1af5b472012-05-26 16:04:39 +070028 if (strpos(DB_DRIVER, 'mysql') !== FALSE)
29 {
Andrey Andreev13be5572012-10-04 12:43:03 +030030 $sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%' ESCAPE '!';";
Taufan Aditya1af5b472012-05-26 16:04:39 +070031 }
32 else
33 {
34 $sql = 'SELECT "value" FROM "misc" WHERE "key" LIKE \''.$string.'%\' ESCAPE \'!\';';
35 }
Taufan Aditya98dcac72012-05-25 05:07:51 +070036
Taufan Aditya6a432442012-05-25 04:09:38 +070037 $res = $this->db->query($sql)->result_array();
Andrey Andreevc1862882012-06-09 23:16:58 +030038
Taufan Aditya2d574452012-05-25 04:03:56 +070039 // Check the result
Andrey Andreev20d9b0a2017-12-20 19:57:39 +020040 $this->assertCount(1, $res);
Taufan Aditya2d574452012-05-25 04:03:56 +070041 }
42
43 // ------------------------------------------------------------------------
44
45 /**
46 * @see ./mocks/schema/skeleton.php
47 */
48 public function test_escape_like_backslash_sign()
49 {
Taufan Aditya1af5b472012-05-26 16:04:39 +070050 // Escape the like string
Taufan Aditya2d574452012-05-25 04:03:56 +070051 $string = $this->db->escape_like_str('\\');
Taufan Aditya502942b2012-05-25 07:00:41 +070052
Taufan Aditya1af5b472012-05-26 16:04:39 +070053 if (strpos(DB_DRIVER, 'mysql') !== FALSE)
54 {
Andrey Andreev13be5572012-10-04 12:43:03 +030055 $sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%' ESCAPE '!';";
Taufan Aditya1af5b472012-05-26 16:04:39 +070056 }
57 else
58 {
59 $sql = 'SELECT "value" FROM "misc" WHERE "key" LIKE \''.$string.'%\' ESCAPE \'!\';';
60 }
Taufan Aditya502942b2012-05-25 07:00:41 +070061
Taufan Aditya6a432442012-05-25 04:09:38 +070062 $res = $this->db->query($sql)->result_array();
Andrey Andreevc1862882012-06-09 23:16:58 +030063
Taufan Aditya2d574452012-05-25 04:03:56 +070064 // Check the result
Andrey Andreev20d9b0a2017-12-20 19:57:39 +020065 $this->assertCount(2, $res);
Taufan Aditya2d574452012-05-25 04:03:56 +070066 }
Andrey Andreevc1862882012-06-09 23:16:58 +030067
Andrey Andreev20d9b0a2017-12-20 19:57:39 +020068}