Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions system/Validation/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ public function greater_than_equal_to(?string $str, string $min): bool
public function is_not_unique(?string $str, string $field, array $data): bool
Copy link

@jlopes90 jlopes90 Jan 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yesterday I was trying something else, I removed "?string" and it worked.

{
// Grab any data for exclusion of a single row.
[$field, $whereField, $whereValue] = array_pad(explode(',', $field), 3, null);
[$field, $whereField, $whereValue] = array_pad(
explode(',', $field),
3,
null
);

// Break the table and field apart
sscanf($field, '%[^.].%[^.]', $table, $field);
Expand All @@ -97,7 +101,10 @@ public function is_not_unique(?string $str, string $field, array $data): bool
->where($field, $str)
->limit(1);

if (! empty($whereField) && ! empty($whereValue) && ! preg_match('/^\{(\w+)\}$/', $whereValue)) {
if (
! empty($whereField) && ! empty($whereValue)
&& ! preg_match('/^\{(\w+)\}$/', $whereValue)
) {
$row = $row->where($whereField, $whereValue);
}

Expand Down Expand Up @@ -125,7 +132,11 @@ public function in_list(?string $value, string $list): bool
*/
public function is_unique(?string $str, string $field, array $data): bool
{
[$field, $ignoreField, $ignoreValue] = array_pad(explode(',', $field), 3, null);
[$field, $ignoreField, $ignoreValue] = array_pad(
explode(',', $field),
3,
null
);

sscanf($field, '%[^.].%[^.]', $table, $field);

Expand All @@ -135,7 +146,10 @@ public function is_unique(?string $str, string $field, array $data): bool
->where($field, $str)
->limit(1);

if (! empty($ignoreField) && ! empty($ignoreValue) && ! preg_match('/^\{(\w+)\}$/', $ignoreValue)) {
if (
! empty($ignoreField) && ! empty($ignoreValue)
&& ! preg_match('/^\{(\w+)\}$/', $ignoreValue)
) {
$row = $row->where("{$ignoreField} !=", $ignoreValue);
}

Expand Down
56 changes: 54 additions & 2 deletions system/Validation/StrictRules/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,34 @@ public function greater_than_equal_to($str, string $min): bool
*/
public function is_not_unique($str, string $field, array $data): bool
{
return $this->nonStrictRules->is_not_unique($str, $field, $data);
if (is_object($str) || is_array($str)) {
return false;
}

// Grab any data for exclusion of a single row.
[$field, $whereField, $whereValue] = array_pad(
explode(',', $field),
3,
null
);

// Break the table and field apart
sscanf($field, '%[^.].%[^.]', $table, $field);

$row = Database::connect($data['DBGroup'] ?? null)
->table($table)
->select('1')
->where($field, $str)
->limit(1);

if (
! empty($whereField) && ! empty($whereValue)
&& ! preg_match('/^\{(\w+)\}$/', $whereValue)
) {
$row = $row->where($whereField, $whereValue);
}

return $row->get()->getRow() !== null;
}

/**
Expand Down Expand Up @@ -151,7 +178,32 @@ public function in_list($value, string $list): bool
*/
public function is_unique($str, string $field, array $data): bool
{
return $this->nonStrictRules->is_unique($str, $field, $data);
if (is_object($str) || is_array($str)) {
return false;
}

[$field, $ignoreField, $ignoreValue] = array_pad(
explode(',', $field),
3,
null
);

sscanf($field, '%[^.].%[^.]', $table, $field);

$row = Database::connect($data['DBGroup'] ?? null)
->table($table)
->select('1')
->where($field, $str)
->limit(1);

if (
! empty($ignoreField) && ! empty($ignoreValue)
&& ! preg_match('/^\{(\w+)\}$/', $ignoreValue)
) {
$row = $row->where("{$ignoreField} !=", $ignoreValue);
}

return $row->get()->getRow() === null;
}

/**
Expand Down
181 changes: 5 additions & 176 deletions tests/system/Validation/DatabaseRelatedRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,17 @@

namespace CodeIgniter\Validation;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Config\Database;
use Config\Services;
use CodeIgniter\Validation\StrictRules\DatabaseRelatedRulesTest as StrictDatabaseRelatedRulesTest;
use Tests\Support\Validation\TestRules;

/**
* @internal
*
* @group DatabaseLive
*/
final class DatabaseRelatedRulesTest extends CIUnitTestCase
final class DatabaseRelatedRulesTest extends StrictDatabaseRelatedRulesTest
{
use DatabaseTestTrait;

private Validation $validation;
private array $config = [
protected array $config = [
'ruleSets' => [
Rules::class,
FormatRules::class,
Expand All @@ -45,173 +39,8 @@ final class DatabaseRelatedRulesTest extends CIUnitTestCase
],
];

protected function setUp(): void
{
parent::setUp();
$this->validation = new Validation((object) $this->config, Services::renderer());
$this->validation->reset();
}

public function testIsUniqueFalse(): void
{
Database::connect()
->table('user')
->insert([
'name' => 'Derek Travis',
'email' => '[email protected]',
'country' => 'Elbonia',
]);

$data = ['email' => '[email protected]'];
$this->validation->setRules(['email' => 'is_unique[user.email]']);
$this->assertFalse($this->validation->run($data));
}

public function testIsUniqueTrue(): void
{
$data = ['email' => '[email protected]'];
$this->validation->setRules(['email' => 'is_unique[user.email]']);
$this->assertTrue($this->validation->run($data));
}

public function testIsUniqueIgnoresParams(): void
{
$db = Database::connect();
$db
->table('user')
->insert([
'name' => 'Developer A',
'email' => '[email protected]',
'country' => 'Elbonia',
]);
$row = $db->table('user')
->limit(1)
->get()
->getRow();

$data = ['email' => '[email protected]'];
$this->validation->setRules(['email' => "is_unique[user.email,id,{$row->id}]"]);
$this->assertTrue($this->validation->run($data));
}

public function testIsUniqueIgnoresParamsPlaceholders(): void
{
$this->hasInDatabase('user', [
'name' => 'Derek',
'email' => '[email protected]',
'country' => 'GB',
]);

$row = Database::connect()
->table('user')
->limit(1)
->get()
->getRow();

$data = [
'id' => $row->id,
'email' => '[email protected]',
];

$this->validation->setRules(['email' => 'is_unique[user.email,id,{id}]']);
$this->assertTrue($this->validation->run($data));
}

public function testIsUniqueByManualRun(): void
{
Database::connect()
->table('user')
->insert([
'name' => 'Developer A',
'email' => '[email protected]',
'country' => 'Elbonia',
]);

$this->assertFalse((new Rules())->is_unique('[email protected]', 'user.email,id,{id}', []));
}

public function testIsNotUniqueFalse(): void
{
Database::connect()
->table('user')
->insert([
'name' => 'Derek Travis',
'email' => '[email protected]',
'country' => 'Elbonia',
]);

$data = ['email' => '[email protected]'];
$this->validation->setRules(['email' => 'is_not_unique[user.email]']);
$this->assertFalse($this->validation->run($data));
}

public function testIsNotUniqueTrue(): void
{
Database::connect()
->table('user')
->insert([
'name' => 'Derek Travis',
'email' => '[email protected]',
'country' => 'Elbonia',
]);

$data = ['email' => '[email protected]'];
$this->validation->setRules(['email' => 'is_not_unique[user.email]']);
$this->assertTrue($this->validation->run($data));
}

public function testIsNotUniqueIgnoresParams(): void
protected function createRules()
{
$db = Database::connect();
$db->table('user')
->insert([
'name' => 'Developer A',
'email' => '[email protected]',
'country' => 'Elbonia',
]);

$row = $db->table('user')
->limit(1)
->get()
->getRow();

$data = ['email' => '[email protected]'];
$this->validation->setRules(['email' => "is_not_unique[user.email,id,{$row->id}]"]);
$this->assertFalse($this->validation->run($data));
}

public function testIsNotUniqueIgnoresParamsPlaceholders(): void
{
$this->hasInDatabase('user', [
'name' => 'Derek',
'email' => '[email protected]',
'country' => 'GB',
]);

$row = Database::connect()
->table('user')
->limit(1)
->get()
->getRow();

$data = [
'id' => $row->id,
'email' => '[email protected]',
];
$this->validation->setRules(['email' => 'is_not_unique[user.email,id,{id}]']);
$this->assertTrue($this->validation->run($data));
}

public function testIsNotUniqueByManualRun(): void
{
Database::connect()
->table('user')
->insert([
'name' => 'Developer A',
'email' => '[email protected]',
'country' => 'Elbonia',
]);

$this->assertTrue((new Rules())->is_not_unique('[email protected]', 'user.email,id,{id}', []));
return new Rules();
}
}
32 changes: 27 additions & 5 deletions tests/system/Validation/StrictRules/DatabaseRelatedRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
/**
* @internal
*
* @no-final
*
* @group DatabaseLive
*/
final class DatabaseRelatedRulesTest extends CIUnitTestCase
class DatabaseRelatedRulesTest extends CIUnitTestCase
{
use DatabaseTestTrait;

private Validation $validation;
private array $config = [
protected Validation $validation;
protected array $config = [
'ruleSets' => [
Rules::class,
FormatRules::class,
Expand All @@ -53,6 +55,11 @@ protected function setUp(): void
$this->validation->reset();
}

protected function createRules()
{
return new Rules();
}

public function testIsUniqueFalse(): void
{
Database::connect()
Expand Down Expand Up @@ -128,7 +135,22 @@ public function testIsUniqueByManualRun(): void
'country' => 'Elbonia',
]);

$this->assertFalse((new Rules())->is_unique('[email protected]', 'user.email,id,{id}', []));
$this->assertFalse($this->createRules()->is_unique('[email protected]', 'user.email,id,{id}', []));
}

public function testIsUniqueIntValueByManualRun(): void
{
Database::connect()
->table('user')
->insert([
'name' => 'Developer A',
'email' => '[email protected]',
'country' => 'Elbonia',
]);

$result = $this->createRules()->is_unique(1, 'user.id', []);

$this->assertFalse($result);
}

public function testIsNotUniqueFalse(): void
Expand Down Expand Up @@ -213,6 +235,6 @@ public function testIsNotUniqueByManualRun(): void
'country' => 'Elbonia',
]);

$this->assertTrue((new Rules())->is_not_unique('[email protected]', 'user.email,id,{id}', []));
$this->assertTrue($this->createRules()->is_not_unique('[email protected]', 'user.email,id,{id}', []));
}
}