Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix is_unique/is_not_unique validation called on POST/PUT via API in Postgresql #2916

Merged
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
10 changes: 8 additions & 2 deletions system/Validation/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ public function is_not_unique(string $str = null, string $field, array $data): b

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

return (bool) ($row->get()
Expand Down Expand Up @@ -228,7 +231,10 @@ public function is_unique(string $str = null, string $field, array $data): bool

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

return (bool) ($row->get()
Expand Down
181 changes: 181 additions & 0 deletions tests/system/Validation/RulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CodeIgniter\Test\CIDatabaseTestCase;
use Config\Database;
use CodeIgniter\Validation\Rules;

class RulesTest extends CIDatabaseTestCase
{
Expand Down Expand Up @@ -609,6 +610,186 @@ public function testIsUniqueIgnoresParams()

//--------------------------------------------------------------------

/**
* @group DatabaseLive
*/
public function testIsUniqueIgnoresParamsPlaceholders()
{
$this->hasInDatabase('user', [
'name' => 'Derek',
'email' => '[email protected]',
'country' => 'GB',
]);

$db = Database::connect();
$row = $db->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));
}

//--------------------------------------------------------------------

/**
* @group DatabaseLive
*/
public function testIsUniqueByManualRun()
{
$db = Database::connect();
$db->table('user')
->insert([
'name' => 'Developer A',
'email' => '[email protected]',
'country' => 'Elbonia',
]);

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

//--------------------------------------------------------------------

/**
* @group DatabaseLive
*/
public function testIsNotUniqueFalse()
{
$db = Database::connect();
$db->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));
}

//--------------------------------------------------------------------

/**
* @group DatabaseLive
*/
public function testIsNotUniqueTrue()
{
$db = Database::connect();
$db->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));
}

//--------------------------------------------------------------------

/**
* @group DatabaseLive
*/
public function testIsNotUniqueIgnoresParams()
{
$db = Database::connect();
$user = $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));
}

//--------------------------------------------------------------------

/**
* @group DatabaseLive
*/
public function testIsNotUniqueIgnoresParamsPlaceholders()
{
$this->hasInDatabase('user', [
'name' => 'Derek',
'email' => '[email protected]',
'country' => 'GB',
]);

$db = Database::connect();
$row = $db->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));
}

//--------------------------------------------------------------------

/**
* @group DatabaseLive
*/
public function testIsNotUniqueByManualRun()
{
$db = Database::connect();
$db->table('user')
->insert([
'name' => 'Developer A',
'email' => '[email protected]',
'country' => 'Elbonia',
]);

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

//--------------------------------------------------------------------

public function testMinLengthNull()
{
$data = [
Expand Down
146 changes: 0 additions & 146 deletions tests/system/Validation/UniqueRulesTest.php

This file was deleted.