-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
70 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php namespace CodeIgniter\Database\Live; | ||
|
||
use CodeIgniter\Test\CIDatabaseTestCase; | ||
|
||
/** | ||
* @group DatabaseLive | ||
*/ | ||
class EscapeTest extends CIDatabaseTestCase | ||
{ | ||
protected $refresh = false; | ||
|
||
//-------------------------------------------------------------------- | ||
|
||
/** | ||
* Ensures we don't have escaped - values... | ||
* | ||
* @see https://github.com/codeigniter4/CodeIgniter4/issues/606 | ||
*/ | ||
public function testEscapeProtectsNegativeNumbers() | ||
{ | ||
$this->assertEquals("'-100'", $this->db->escape(-100)); | ||
} | ||
|
||
//-------------------------------------------------------------------- | ||
|
||
public function testEscape() | ||
{ | ||
$expected = "SELECT * FROM brands WHERE name = 'O\'Doules'"; | ||
$sql = "SELECT * FROM brands WHERE name = " . $this->db->escape("O'Doules"); | ||
|
||
$this->assertEquals($expected, $sql); | ||
} | ||
|
||
//-------------------------------------------------------------------- | ||
|
||
public function testEscapeString() | ||
{ | ||
$expected = "SELECT * FROM brands WHERE name = 'O\'Doules'"; | ||
$sql = "SELECT * FROM brands WHERE name = '" . $this->db->escapeString("O'Doules") . "'"; | ||
|
||
$this->assertEquals($expected, $sql); | ||
} | ||
|
||
//-------------------------------------------------------------------- | ||
|
||
public function testEscapeLikeString() | ||
{ | ||
$expected = "SELECT * FROM brands WHERE column LIKE '%10!% more%' ESCAPE '!'"; | ||
$sql = "SELECT * FROM brands WHERE column LIKE '%" . $this->db->escapeLikeString("10% more") . "%' ESCAPE '!'"; | ||
|
||
$this->assertEquals($expected, $sql); | ||
} | ||
|
||
//-------------------------------------------------------------------- | ||
|
||
public function testEscapeLikeStringDirect() | ||
{ | ||
if ($this->db->DBDriver === 'MySQLi') | ||
{ | ||
$expected = "SHOW COLUMNS FROM brands WHERE column LIKE 'wild\_chars%'"; | ||
$sql = "SHOW COLUMNS FROM brands WHERE column LIKE '". $this->db->escapeLikeStringDirect("wild_chars") . "%'"; | ||
|
||
$this->assertEquals($expected, $sql); | ||
} | ||
else | ||
{ | ||
$this->expectNotToPerformAssertions(); | ||
} | ||
} | ||
} |