Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into 4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Sep 2, 2022
2 parents 428057a + 924b3c4 commit 7c610bf
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 17 deletions.
6 changes: 2 additions & 4 deletions system/Cache/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,15 @@ public function increment(string $key, int $offset = 1)
{
$key = static::validateKey($key, $this->prefix);

return $this->redis->hIncrBy($key, 'data', $offset);
return $this->redis->hIncrBy($key, '__ci_value', $offset);
}

/**
* {@inheritDoc}
*/
public function decrement(string $key, int $offset = 1)
{
$key = static::validateKey($key, $this->prefix);

return $this->redis->hIncrBy($key, 'data', -$offset);
return $this->increment($key, -$offset);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions system/Cookie/CloneableCookieInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public function withExpired();
* Creates a new Cookie that will virtually never expire from the browser.
*
* @return static
*
* @deprecated See https://github.com/codeigniter4/CodeIgniter4/pull/6413
*/
public function withNeverExpiring();

Expand Down
2 changes: 1 addition & 1 deletion system/Cookie/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public function withExpired()
}

/**
* {@inheritDoc}
* @deprecated See https://github.com/codeigniter4/CodeIgniter4/pull/6413
*/
public function withNeverExpiring()
{
Expand Down
2 changes: 1 addition & 1 deletion system/Debug/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function shutdownHandler()
['type' => $type, 'message' => $message, 'file' => $file, 'line' => $line] = $error;

if (in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE], true)) {
$this->exceptionHandler(new ErrorException($message, $type, 0, $file, $line));
$this->exceptionHandler(new ErrorException($message, 0, $type, $file, $line));
}
}

Expand Down
30 changes: 29 additions & 1 deletion system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ protected function processRules(
if ($passed === false) {
// if the $value is an array, convert it to as string representation
if (is_array($value)) {
$value = '[' . implode(', ', $value) . ']';
$value = $this->isStringList($value)
? '[' . implode(', ', $value) . ']'
: json_encode($value);
} elseif (is_object($value)) {
$value = json_encode($value);
}
Expand All @@ -345,6 +347,32 @@ protected function processRules(
return true;
}

/**
* Is the array a string list `list<string>`?
*/
private function isStringList(array $array): bool
{
$expectedKey = 0;

foreach ($array as $key => $val) {
// Note: also covers PHP array key conversion, e.g. '5' and 5.1 both become 5
if (! is_int($key)) {
return false;
}

if ($key !== $expectedKey) {
return false;
}
$expectedKey++;

if (! is_string($val)) {
return false;
}
}

return true;
}

/**
* Takes a Request object and grabs the input data to use from its
* array values.
Expand Down
5 changes: 5 additions & 0 deletions tests/_support/Validation/TestRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public function check_object_rule(object $value, ?string $fields, array $data =

return $find;
}

public function array_count($value, $count): bool
{
return is_array($value) && count($value) === (int) $count;
}
}
24 changes: 16 additions & 8 deletions tests/system/Cache/Handlers/RedisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,22 @@ public function testDeleteMatchingSuffix()
$this->assertSame('keys=90', $dbInfo[0]);
}

// FIXME: I don't like all Hash logic very much. It's wasting memory.
// public function testIncrement()
// {
// }

// public function testDecrement()
// {
// }
public function testIncrementAndDecrement()
{
$this->handler->save('counter', 100);

foreach (range(1, 10) as $step) {
$this->handler->increment('counter', $step);
}

$this->assertSame(155, $this->handler->get('counter'));

$this->handler->decrement('counter', 20);
$this->assertSame(135, $this->handler->get('counter'));

$this->handler->increment('counter', 5);
$this->assertSame(140, $this->handler->get('counter'));
}

public function testClean()
{
Expand Down
36 changes: 36 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,42 @@ public function testJsonInput(): void
unset($_SERVER['CONTENT_TYPE']);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/6466
*/
public function testJsonInputObjectArray(): void
{
$json = <<<'EOL'
{
"p": [
{
"id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
]
}
EOL;

$_SERVER['CONTENT_TYPE'] = 'application/json';

$config = new App();
$config->baseURL = 'http://example.com/';

$request = new IncomingRequest($config, new URI(), $json, new UserAgent());

$rules = [
'p' => 'required|array_count[2]',
];
$validated = $this->validation
->withRequest($request->withMethod('patch'))
->setRules($rules)
->run();

$this->assertFalse($validated);
$this->assertSame(['p' => 'Validation.array_count'], $this->validation->getErrors());

unset($_SERVER['CONTENT_TYPE']);
}

public function testHasRule(): void
{
$this->validation->setRuleGroup('groupA');
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/changelogs/v4.2.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ none.
Deprecations
************

none.
- :php:meth:`CodeIgniter\\Cookie\\Cookie::withNeverExpiring()` is deprecated.

Bugs Fixed
**********
Expand Down
4 changes: 3 additions & 1 deletion user_guide_src/source/libraries/cookies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ In runtime, you can manually supply a new default using the ``Cookie::setDefault
Class Reference
***************

.. php:namespace:: CodeIgniter\HTTP\Cookie
.. php:namespace:: CodeIgniter\Cookie
.. php:class:: Cookie
Expand Down Expand Up @@ -326,6 +326,8 @@ Class Reference

.. php:method:: withNeverExpiring()
.. important:: This method is deprecated.

:param string $name:
:rtype: ``Cookie``
:returns: new ``Cookie`` instance
Expand Down

0 comments on commit 7c610bf

Please sign in to comment.