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

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

Open
yaldi4 opened this issue Jun 26, 2023 · 1 comment
Open

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

yaldi4 opened this issue Jun 26, 2023 · 1 comment

Comments

@yaldi4
Copy link

yaldi4 commented Jun 26, 2023

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?

@kenjis
Copy link
Owner

kenjis commented Aug 3, 2023

What is $this->session->userdata['user_group_id']?

As far as I know, $this->session->userdata() is a method, not an array.
https://codeigniter.com/userguide3/libraries/sessions.html#retrieving-session-data

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

I don't get what is a protected method you say.

You do not need to test protect methods in general.
A protect method is tested within a test on public methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants