Skip to content

Commit

Permalink
Merge pull request #214 from Frankva/unit_tests
Browse files Browse the repository at this point in the history
Unit tests
  • Loading branch information
Frankva committed Jan 31, 2024
2 parents 9fe3f11 + 1c6ef87 commit b35ad57
Show file tree
Hide file tree
Showing 65 changed files with 4,253 additions and 189 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/codeigniter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: CodeIgniter

on:
push:
# branches: [ "master" ]
pull_request:
# branches: [ "master" ]
release:
types: [published]
workflow_dispatch: # to run manually

permissions:
contents: read

jobs:
codeigniter-test:
environment: unit_test
runs-on: ubuntu-latest
services:
db:
image: mariadb
env:
MYSQL_DATABASE: ci4_test
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
options: --health-cmd="mariadb-admin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout@v3

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress

# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
# Docs: https://getcomposer.org/doc/articles/scripts.md


- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
database.tests.hostname: 127.0.0.1
database.tests.database: ci4_test
database.tests.username: root
database.tests.password: root
database.tests.DBDriver: MySQLi
database.tests.port: 3306
database.tests.DBPrefix:
CLIENT_ID: ${{ secrets.CLIENT_ID }}
TENANT_ID: ${{ secrets.TENANT_ID }}
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
GRAPH_USER_SCOPES: ${{ secrets.GRAPH_USER_SCOPES }}
REDIRECT_URI: ${{ secrets.REDIRECT_URI }}
run: vendor/bin/phpunit
49 changes: 29 additions & 20 deletions app/Controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use CodeIgniter\HTTP\Response;

use Common\Exceptions\AccessDeniedException;

/**
* Class BaseController
Expand Down Expand Up @@ -58,7 +61,8 @@ abstract class BaseController extends Controller
/**
* Constructor.
*/
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
public function initController(RequestInterface $request,
ResponseInterface $response, LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);
Expand All @@ -69,10 +73,7 @@ public function initController(RequestInterface $request, ResponseInterface $res

// Check permission on construct
if (!$this->check_permission()) {
$this->display_view('\User\errors\403error');
exit();
//throw new \Exception("some message here",403);
//show_error(lang('msg_err_access_denied_message'), 403, lang('msg_err_access_denied_header'));
throw AccessDeniedException::forPageAccessDenied();
}
}

Expand All @@ -85,7 +86,8 @@ public function initController(RequestInterface $request, ResponseInterface $res
* @return bool : true if user level is equal or higher than required level,
* false else
*/
protected function check_permission($required_level = NULL)
protected function check_permission(
?int $required_level = NULL): bool|Response
{
if (!isset($_SESSION['logged_in'])) {
// Tests can accidentally delete $_SESSION,
Expand All @@ -106,7 +108,7 @@ protected function check_permission($required_level = NULL)
// check if user is logged in, if not access is not allowed
if ($_SESSION['logged_in'] != true) {
// The usual redirect()->to() doesn't work here. Keep this kind of redirect.
return $this->response->redirect(base_url('user/auth/login'));
return false;
}
// check if page is accessible for all logged in users
elseif ($required_level == "@") {
Expand All @@ -130,39 +132,46 @@ protected function check_permission($required_level = NULL)
* @param $view_parts : single view or array of view parts to display
* $data : data array to send to the view
*/
public function display_view($view_parts, $data = NULL)
public function display_view(string|array $view_parts,
?array $data = NULL): string
{
// The view to be constructed and displayed
$viewToDisplay = '';

// If not defined in $data, set page title to empty string
if (!isset($data['title'])) {
$data['title'] = '';
}

// Display common headers
echo view('Common\header', $data);
// Add common headers to the view
$viewToDisplay .= view('Common\header', $data);

// Display login bar
echo view('Common\login_bar');
// Add login bar to the view
$viewToDisplay .= view('Common\login_bar');

// Display admin menu if appropriate
// Add admin menu to the view if the current url is an admin url
foreach (config('Common\Config\AdminPanelConfig')->tabs as $tab){
if (strstr(current_url(),$tab['pageLink'])) {
echo view('\Common\adminMenu');
$viewToDisplay .= view('\Common\adminMenu');
}
}

if (is_array($view_parts)) {
// Display multiple view parts
// Add multiple parts to the view
foreach ($view_parts as $view_part) {
echo view($view_part, $data);
$viewToDisplay .= view($view_part, $data);
}
}
elseif (is_string($view_parts)) {
// Display unique view part
echo view($view_parts, $data);
// Add unique part to the view
$viewToDisplay .= view($view_parts, $data);
}

// Display common footer
echo view('Common\footer');
// Add common footers to the view
$viewToDisplay .= view('Common\footer');

// Return the complete view to display
return $viewToDisplay;
}

/**
Expand Down
88 changes: 88 additions & 0 deletions app/Views/errors/html/error_403.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?= lang('user_lang.code_error_403') ?></title>

<style>
div.logo {
height: 200px;
width: 155px;
display: inline-block;
opacity: 0.08;
position: absolute;
top: 2rem;
left: 50%;
margin-left: -73px;
}
body {
height: 100%;
background: #fafafa;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #777;
font-weight: 300;
}
h1 {
font-weight: lighter;
letter-spacing: normal;
font-size: 3rem;
margin-top: 0;
margin-bottom: 0;
color: #222;
}
.wrap {
max-width: 1024px;
margin: 5rem auto;
padding: 2rem;
background: #fff;
text-align: center;
border: 1px solid #efefef;
border-radius: 0.5rem;
position: relative;
}
pre {
white-space: normal;
margin-top: 1.5rem;
}
code {
background: #fafafa;
border: 1px solid #efefef;
padding: 0.5rem 1rem;
border-radius: 5px;
display: block;
}
p {
margin-top: 1.5rem;
}
.footer {
margin-top: 2rem;
border-top: 1px solid #efefef;
padding: 1em 2em 0 2em;
font-size: 85%;
color: #999;
}
a:active,
a:link,
a:visited {
color: #dd4814;
}
</style>
</head>
<body>
<div class="wrap">
<h1><?= lang('user_lang.code_error_403')?></h1>

<p>
<?php if (ENVIRONMENT !== 'production') : ?>
<?= nl2br(esc($message)) ?>
<?php else : ?>
<?= nl2br(esc($message)) ?>
<?php endif ?>
</p>
</div>
<?php if (ENVIRONMENT !== 'production') : ?>
<?= d($exception->getTrace()) ?>
<?php endif ?>

</body>
</html>
27 changes: 27 additions & 0 deletions orif/common/Exceptions/AccessDeniedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Common\Exceptions;

use CodeIgniter\Exceptions\HTTPExceptionInterface;
use CodeIgniter\Exceptions\DebugTraceableTrait;
use CodeIgniter\Exceptions\ExceptionInterface;
use RuntimeException;

class AccessDeniedException extends RuntimeException implements
ExceptionInterface, HTTPExceptionInterface
{
use DebugTraceableTrait;

/**
* HTTP status code
*
* @var int
*/
protected $code = 403;

public static function forPageAccessDenied(?string $message = null)
{
return new static($message
?? lang('user_lang.msg_err_access_denied_message'));
}
}
Loading

0 comments on commit b35ad57

Please sign in to comment.