Skip to content

Commit 306a238

Browse files
committed
Move live database related rules into own file
1 parent c592af9 commit 306a238

File tree

2 files changed

+221
-240
lines changed

2 files changed

+221
-240
lines changed
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Validation;
13+
14+
use CodeIgniter\Test\CIUnitTestCase;
15+
use CodeIgniter\Test\DatabaseTestTrait;
16+
use Config\Database;
17+
use Config\Services;
18+
use Tests\Support\Validation\TestRules;
19+
20+
/**
21+
* @internal
22+
*
23+
* @group DatabaseLive
24+
*/
25+
final class DatabaseRelatedRulesTest extends CIUnitTestCase
26+
{
27+
use DatabaseTestTrait;
28+
29+
/**
30+
* @var Validation
31+
*/
32+
private $validation;
33+
34+
private $config = [
35+
'ruleSets' => [
36+
Rules::class,
37+
FormatRules::class,
38+
FileRules::class,
39+
CreditCardRules::class,
40+
TestRules::class,
41+
],
42+
'groupA' => [
43+
'foo' => 'required|min_length[5]',
44+
],
45+
'groupA_errors' => [
46+
'foo' => [
47+
'min_length' => 'Shame, shame. Too short.',
48+
],
49+
],
50+
];
51+
52+
protected function setUp(): void
53+
{
54+
parent::setUp();
55+
$this->validation = new Validation((object) $this->config, Services::renderer());
56+
$this->validation->reset();
57+
}
58+
59+
public function testIsUniqueFalse(): void
60+
{
61+
Database::connect()
62+
->table('user')
63+
->insert([
64+
'name' => 'Derek Travis',
65+
'email' => '[email protected]',
66+
'country' => 'Elbonia',
67+
]);
68+
69+
$data = ['email' => '[email protected]'];
70+
$this->validation->setRules(['email' => 'is_unique[user.email]']);
71+
$this->assertFalse($this->validation->run($data));
72+
}
73+
74+
public function testIsUniqueTrue(): void
75+
{
76+
$data = ['email' => '[email protected]'];
77+
$this->validation->setRules(['email' => 'is_unique[user.email]']);
78+
$this->assertTrue($this->validation->run($data));
79+
}
80+
81+
public function testIsUniqueIgnoresParams(): void
82+
{
83+
$db = Database::connect();
84+
$db
85+
->table('user')
86+
->insert([
87+
'name' => 'Developer A',
88+
'email' => '[email protected]',
89+
'country' => 'Elbonia',
90+
]);
91+
$row = $db->table('user')
92+
->limit(1)
93+
->get()
94+
->getRow();
95+
96+
$data = ['email' => '[email protected]'];
97+
$this->validation->setRules(['email' => "is_unique[user.email,id,{$row->id}]"]);
98+
$this->assertTrue($this->validation->run($data));
99+
}
100+
101+
public function testIsUniqueIgnoresParamsPlaceholders(): void
102+
{
103+
$this->hasInDatabase('user', [
104+
'name' => 'Derek',
105+
'email' => '[email protected]',
106+
'country' => 'GB',
107+
]);
108+
109+
$row = Database::connect()
110+
->table('user')
111+
->limit(1)
112+
->get()
113+
->getRow();
114+
115+
$data = [
116+
'id' => $row->id,
117+
'email' => '[email protected]',
118+
];
119+
120+
$this->validation->setRules(['email' => 'is_unique[user.email,id,{id}]']);
121+
$this->assertTrue($this->validation->run($data));
122+
}
123+
124+
public function testIsUniqueByManualRun(): void
125+
{
126+
Database::connect()
127+
->table('user')
128+
->insert([
129+
'name' => 'Developer A',
130+
'email' => '[email protected]',
131+
'country' => 'Elbonia',
132+
]);
133+
134+
$this->assertFalse((new Rules())->is_unique('[email protected]', 'user.email,id,{id}', []));
135+
}
136+
137+
public function testIsNotUniqueFalse(): void
138+
{
139+
Database::connect()
140+
->table('user')
141+
->insert([
142+
'name' => 'Derek Travis',
143+
'email' => '[email protected]',
144+
'country' => 'Elbonia',
145+
]);
146+
147+
$data = ['email' => '[email protected]'];
148+
$this->validation->setRules(['email' => 'is_not_unique[user.email]']);
149+
$this->assertFalse($this->validation->run($data));
150+
}
151+
152+
public function testIsNotUniqueTrue(): void
153+
{
154+
Database::connect()
155+
->table('user')
156+
->insert([
157+
'name' => 'Derek Travis',
158+
'email' => '[email protected]',
159+
'country' => 'Elbonia',
160+
]);
161+
162+
$data = ['email' => '[email protected]'];
163+
$this->validation->setRules(['email' => 'is_not_unique[user.email]']);
164+
$this->assertTrue($this->validation->run($data));
165+
}
166+
167+
public function testIsNotUniqueIgnoresParams(): void
168+
{
169+
$db = Database::connect();
170+
$db->table('user')
171+
->insert([
172+
'name' => 'Developer A',
173+
'email' => '[email protected]',
174+
'country' => 'Elbonia',
175+
]);
176+
177+
$row = $db->table('user')
178+
->limit(1)
179+
->get()
180+
->getRow();
181+
182+
$data = ['email' => '[email protected]'];
183+
$this->validation->setRules(['email' => "is_not_unique[user.email,id,{$row->id}]"]);
184+
$this->assertFalse($this->validation->run($data));
185+
}
186+
187+
public function testIsNotUniqueIgnoresParamsPlaceholders(): void
188+
{
189+
$this->hasInDatabase('user', [
190+
'name' => 'Derek',
191+
'email' => '[email protected]',
192+
'country' => 'GB',
193+
]);
194+
195+
$row = Database::connect()
196+
->table('user')
197+
->limit(1)
198+
->get()
199+
->getRow();
200+
201+
$data = [
202+
'id' => $row->id,
203+
'email' => '[email protected]',
204+
];
205+
$this->validation->setRules(['email' => 'is_not_unique[user.email,id,{id}]']);
206+
$this->assertTrue($this->validation->run($data));
207+
}
208+
209+
public function testIsNotUniqueByManualRun(): void
210+
{
211+
Database::connect()
212+
->table('user')
213+
->insert([
214+
'name' => 'Developer A',
215+
'email' => '[email protected]',
216+
'country' => 'Elbonia',
217+
]);
218+
219+
$this->assertTrue((new Rules())->is_not_unique('[email protected]', 'user.email,id,{id}', []));
220+
}
221+
}

0 commit comments

Comments
 (0)