# Testing and read session in Model Behavior

**URL:** <https://discourse.cakephp.org/t/testing-and-read-session-in-model-behavior/2642>\
**Category:** Need Help\
**Created:** [May 31, 2017, 7:18am UTC](https://discourse.cakephp.org/t/testing-and-read-session-in-model-behavior/2642 "2017-05-31T07:18:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyberbobjr](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/cyberbobjr/32/68_2.png) [@cyberbobjr](https://discourse.cakephp.org/u/cyberbobjr)\
**Post date:** [May 31, 2017, 7:18am UTC](https://discourse.cakephp.org/t/testing-and-read-session-in-model-behavior/2642/1 "2017-05-31T07:18:20Z")

</div>

Hi,  
I write a behavior for Table. This behavior will check variables in session. Since we can’t access to the request-\>session() object in behavior, i read session’s variables directly like this :

```auto

    private function _getSessionId()
    {
        $session = new Session();
        $organisation_id = $session->read($this->_defaultConfig['session']); // read Auth.User.organisations_id
        return $organisation_id;
    }

```

this behavior is register to some tables of my application. This session’s reading is call at every before find for model.

Now, i want to test controllers which use tables with this behavior. So i fix var in session like this before the test of the action :

```auto

    public function testListeStocksMultiSearch()
    {
        Configure::write('debug', 1);
        // placement de la session
        $this->session([
            'Droits' => ['ADMIN'],
            'Auth' => [
                'User' => [
                    'id' => 1,
                    'username' => 'bmarchand',
                    'prenom' => 'Benjamin',
                    'nom' => 'Marchand',
                    'organisations_id' => 1
                ]
            ]
        ]);
        $url = Router::url(['controller' => 'Stocks',
                            'action' => 'recherche',
                            '?' => ['_table' => 'Stocks',
                                             'Produits' => ['couleur' => 'blanc']]]);
        $this->get($url);
        $this->_writeFile($this->_getBodyAsString());
        $this->assertResponseOk();
        $this->viewVariable('produits');
    }

```

But i always have a null value in the behavior when i try to read the variable Auth.User.organisations\_id (it works well when i request the data by the browser).

please someone have an idea about this bug (?).

Many thanks  
regards  
cyb
