Skip to content

Commit

Permalink
Merge pull request #2916 from samsonasik/fix-postgres-is-unique-call-api
Browse files Browse the repository at this point in the history
Fix is_unique/is_not_unique validation called on POST/PUT via API in Postgresql
  • Loading branch information
MGatner authored May 9, 2020
2 parents 85cd56a + c979feb commit 11b7a2b
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 148 deletions.
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.

0 comments on commit 11b7a2b

Please sign in to comment.