Skip to content

cant set session $this->session->userdata #408

@yaldi4

Description

@yaldi4

hey, i want to make a test on a protected method of a controller
the test:

<?php
require_once '../../application/libraries/Backend_controller.php';
class Welcome_test extends TestCase
{
	public function test_index()
	{
		$output = $this->request('GET', 'ticket/edit');
		printr($output);die;
	}
}

the $output returns null.
the ticket controllers extends backend_controller
the backen_controller:

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Class Backend_Controller
*/
class Backend_controller extends CORE_controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('user/user_m');
		$this->load->model('setting/setting_m');
		$this->load->model('localisation/language_m');
		$this->load->model('vcounter/vcounter_m');

		// Login check
		$exception_uris = array(
			'user/login',
			'user/logout',
			'user/register',
			'verification/email/',
			'verification/test',
			'user/forgotten',
			'user/reset_password',
		);

		if (in_array(uri_string(), $exception_uris) == FALSE) {
			if ($this->user_m->loggedin() == FALSE) {
				// $redirect = current_url();
				redirect(base_url('/user/login').'?url='.uri_string());
			}
		}
        }
       public function validate_modify($controller = NULL) {
           if ($this->user_m->hasPermission('modify', $controller) === TRUE) {
        	return TRUE;
           }

           $this->session->set_flashdata('error', lang('error_permission'));
             return FALSE;
      }
}

when i add 'ticket/edit' to $exception_uris.
the error is: RuntimeException : Trying to access array offset on value of type null on line 189 in file C:\laragon\www\new-smart\application\models\user\User_m.php

│ C:\laragon\www\new-smart\application\tests_ci_phpunit_test\CIPHPUnitTestCase.php:184
│ C:\laragon\www\new-smart\application\models\user\User_m.php:189
│ C:\laragon\www\new-smart\application\libraries\Backend_controller.php:82

user_m snippet:

public function hasPermission($key = NULL, $controller = NULL)
	{
		$this->load->model('user/user_permission_m');
		$permissions = NULL;
		
		foreach (unserialize($this->session->userdata['user_group_id']) as $user_group_id) {
			$where = array(
				'user_group_id' => $user_group_id
			);
			$result = $this->user_permission_m->get_by($where, TRUE);
			if (is_array(unserialize($result['permission']))) {
				$permissions = unserialize($result['permission']);
			}

			if (is_array($permissions[$key])) {
				if (in_array($controller, $permissions[$key])) {
					return TRUE;
				}
			}
		}

		return FALSE;
	}

the ticket snippet:

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Ticket extends Backend_controller {

    public function __construct(){
        parent::__construct();
    }

   public function edit($id = NULL)
    {
        // Load the view
        if ($this->validate_modify('Ticket')) {
            $this->data['subview'] = 'backend/ticket/edit';
        } else {
            $this->session->set_flashdata('error', lang('error_permission'));
            $this->data['subview'] = 'backend/common/error';
        }
        $this->load->view('backend/_layout_main', $this->data);
    }
}

it seems the backend_controller cant get access to $this->session->userdata.
how do i set the $this->session->userdata in my test so i can access my protected controller?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions