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

fixing Flash\Session::has() returning always true #15353

Merged
merged 1 commit into from
Mar 31, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
- Fixed `Phalcon\Storage\Adapter` failing to retrieve empty like stored data (such as [], 0, false) [15125](https://github.com/phalcon/cphalcon/issues/15125)
- Fixed declarations for `function getEventsManager()` to allow null return [15010](https://github.com/phalcon/cphalcon/issues/15010)
- Removed underscore from method names (starting) to abide with PSR-12 [15345](https://github.com/phalcon/cphalcon/issues/15345)
- Fixed `Phalcon\Flash\Session::has()` to properly check if any messages are existing [15204](https://github.com/phalcon/cphalcon/issues/15204)
28 changes: 21 additions & 7 deletions phalcon/Flash/Session.zep
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,43 @@ class Session extends AbstractFlash

/**
* Returns the messages in the session flasher
*
* @param string|null $type
* @param bool $remove
*
* @return array
*/
public function getMessages(type = null, bool remove = true) -> array
public function getMessages(var type = null, bool remove = true) -> array
{
return this->getSessionMessages(remove, type);
}

/**
* Checks whether there are messages
*
* @param string|null $type
*
* @return bool
*/
public function has(type = null) -> bool
public function has(var type = null) -> bool
Jeckerson marked this conversation as resolved.
Show resolved Hide resolved
{
var messages;

let messages = this->getSessionMessages(false);

if typeof type != "string" {
return true;
if typeof type == "string" {
return isset messages[type];
}

return isset messages[type];
return count(messages) > 0;
}

/**
* Adds a message to the session flasher
*
* @return null|string|void
*/
public function message(string type, string message) -> string | null
public function message(var type, string message) -> string | null
{
var messages;

Expand Down Expand Up @@ -93,8 +102,13 @@ class Session extends AbstractFlash

/**
* Returns the messages stored in session
*
* @param bool $remove
* @param string|null $type
*
* @return array
*/
protected function getSessionMessages(bool remove, type = null) -> array
protected function getSessionMessages(bool remove, var type = null) -> array
{
var session, messages, returnMessages;

Expand Down
64 changes: 61 additions & 3 deletions tests/unit/Flash/Session/HasCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,78 @@

namespace Phalcon\Test\Unit\Flash\Session;

use Phalcon\Flash\Session;
use Phalcon\Storage\Exception;
use Phalcon\Test\Fixtures\Traits\DiTrait;
use UnitTester;

class HasCest
{

use DiTrait;

public function _before(UnitTester $I): void
{
$this->newDi();

try {
$this->setDiService('escaper');
$this->setDiService('sessionStream');
} catch (Exception $e) {
$I->fail($e->getMessage());
}

if (PHP_SESSION_ACTIVE !== session_status()) {
session_start();
}

if (!isset($_SESSION)) {
$_SESSION = [];
}
}

/**
* Executed after each test
*
* @param UnitTester $I
* @return void
*/
public function _after(UnitTester $I): void
{
session_destroy();
}

/**
* Return flash instance
*/
protected function getFlash(): Session
{
$container = $this->getDi();

$flash = new Session();
$flash->setDI($container);

return $flash;
}

/**
* Tests Phalcon\Flash\Session :: has()
*
* @author Phalcon Team <team@phalcon.io>
* @since 2018-11-13
* @since 2021-03-30
*/
public function flashSessionHas(UnitTester $I)
{
$I->wantToTest('Flash\Session - has()');
$flash = $this->getFlash();

$I->assertFalse($flash->has());

$flash->success("test success");

$I->assertTrue($flash->has());

$I->assertFalse($flash->has("error"));

$I->skipTest('Need implementation');
$I->assertTrue($flash->has("success"));
}
}