Automatic tests fails (cookies)

Permalink
Hi,

We just submitted our first add-ons to the marketplace.

In our add-on we use cookies. We use the setcookie() for writing the cookie and $_COOKIE[] for retrieval.
We created 2 functions to get and set the cookie, because we need to create the cookie value. The functions are called _getCookie() and _setCookie(). Is this why the automatic tests fails?

Thanx!

4Concrete5

4Concrete5
 
SnefIT replied on at Permalink Best Answer Reply
SnefIT
Hi,

Use the concrete5 way to get/set cookies.

Something like:
[code]
$ch = \Core::make('cookie');

$cookie = $ch->get('my_cookie');
[\code]
goldhat replied on at Permalink Reply
$cookie->eat(ALL);
MrKDilkington replied on at Permalink Reply
MrKDilkington
I haven't used concrete5's cookie helper yet, but this might be helpful.
http://www.concrete5.org/api/class-Concrete.Core.Cookie.Cookie.html...
chatwee replied on at Permalink Reply
Hello, I've problem with my plugin because I want to use cookie but I cannot get cookie. Way to solve this problem shows SneflT in his post but it doesn't work. Error is: "Class cookie does not exist". How can I import valid class and which one to my code ?

My second question is:

how can I use $_SESSION variable? How can I set and get $_SESSION variable? I cannot find clearly answer in concrete5 documentation. Regards,
SnefIT replied on at Permalink Reply
SnefIT
Well,
\Core::make('cookie')

should work. ;)

For sessions, I think:
$session = \Core::make('session');
$session->has('blabla');
$session->get('blabla');
$session->set('blabla', 'foofoo);
chatwee replied on at Permalink Reply 1 Attachment
Thanks, with session it's works perfectly!

but i have still problem with cookies... please see on my code. I try in this way:
public function getPackage()
{
$ch = \Core::make('cookie');

$cookie = $ch->get('my_cookie');
print_R($cookie);
exit;
if (!$this->package) {
$this->package = \Package::getByHandle('chatwee');
}
return $this->package;
}

I get this error (see attachment)
goutnet replied on at Permalink Reply
Well first of all, you want to activate debug output to get more details here.
chatwee replied on at Permalink Reply
Can anyone help me?

Thanks,
SnefIT replied on at Permalink Reply
SnefIT
What C5 version are you using?
chatwee replied on at Permalink Reply 1 Attachment
As seen on screen I use concrete ver. 5.7.2,

I have still problem with Cookie Class.
chatwee replied on at Permalink Reply
No more questions :) I've just resolved my problem with Cookies.
problem was found with a typo. Please look on this:


$session = \Core::make('session');
$session->get("my_session"); // value of my_session
$session = \Core::make('cookie');
$session->get("my_cookie"); // error !!

It is very weird. How to resolve it?

$this->cookies = \Core::make('Cookie');
$session->get("my_cookie"); // value of my_cookie

Small difference but very significant. Thanks for reply!
chatwee replied on at Permalink Reply
No more questions :) I've just resolved my problem with Cookies.
problem was found with a typo. Please look on this:


$session = \Core::make('session');
$session->get("my_session"); // value of my_session
$session = \Core::make('cookie');
$session->get("my_cookie"); // error !!

It is very weird. How to resolve it?

$this->cookies = \Core::make('Cookie');
$session->get("my_cookie"); // value of my_cookie

Small difference but very significant. Thanks for reply!
chatwee replied on at Permalink Reply
I have one question, how to set cookie?

I try on this way:
$sessionId ="43JFA2NCZA1";
$CookieDomain = ".concrete.org"; //for example
$cookies = \Core::make('Cookie');
$cookies->set("chch-SI", $sessionId, time() + 2592000, "/", $CookieDomain);

but this doesn't work
chatwee replied on at Permalink Reply
anyone can help me? What Am I doing right now to done this plugin? It's just finish.. please guys...
SnefIT replied on at Permalink Reply
SnefIT
Is your domain parameter set correctly?

Here's a piece of code (working) from a plugin that I made (and use).

Baking cookie:
// Json encode data.
$data = Json::encode($data);
// Cookie vars.
$ttl = time() + (60 * 60 * 24); // just a timestamp in real app! 
$path = '/';
$domain = $_SERVER['HTTP_HOST'];
// Get cookie 'helper'.
$ch = \Core::make('cookie');
// Set cookie.
$ch->set('my_recipe', $data, $ttl, $path, $domain);


Eating cookie:
// Cookie 'helper'.
$ch = \Core::make('cookie');
// Get cookie.
$cookie = $ch->get('my_recipe');


You should use "\Core::make('cookie');" (lowercase cookie). You can see it in here:
(notice the 'cookie' in the register function).
namespace Concrete\Core\Cookie;
use Concrete\Core\Foundation\Service\Provider as ServiceProvider;
class CookieServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(
            'cookie',
            '\Concrete\Core\Cookie\CookieJar'
        );
    }
}