Code improve noalyss_user: isolate repository from db

This commit is contained in:
sparkyx 2025-08-10 18:29:07 +02:00
parent 64502d568a
commit 972641f7c5
8 changed files with 510 additions and 102 deletions

View file

@ -39,5 +39,7 @@ $_SESSION[SESSION_KEY.'csv_encoding']='utf8';
$_SESSION[SESSION_KEY.'access_mode']='PC';
$g_user=new Noalyss_user($g_connection);
$_ENV['TMP']="/tmp/";
$_SERVER["REMOTE_ADDR"]="cmd";
$_SERVER['REQUEST_URI']="cmd";
require_once __DIR__.'/facility.class.php';

View file

@ -0,0 +1,200 @@
<?php
/*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* Author : Dany De Bontridder danydb@noalyss.eu
* Copyright (C) 2025 Dany De Bontridder <dany@alchimerys.be>
*
*/
/**
* @file
* @brief noalyss
*/
use PHPUnit\Framework\TestCase;
require DIRTEST . '/global.php';
/**
* @testdox Class xxx : used for ...
* @backupGlobals enabled
* @coversDefaultClass
*/
class AccountRepositoryTest extends TestCase {
/**
* @var Fiche
*/
protected $object;
protected $connection;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test method is executed.
*/
protected function setUp(): void {
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test method is executed.
*/
protected function tearDown(): void {
/**
* example
* if ( ! is_object($this->object->fiche_def)) return;
* include_once DIRTEST.'/global.php';
* $g_connection=Dossier::connect();
* $sql=new ArrayObject();
* $sql->append("delete from fiche_detail where f_id in (select f_id from fiche where fd_id =\$1 )");
* $sql->append("delete from fiche where f_id not in (select f_id from fiche_detail where \$1=\$1)");
* $sql->append("delete from jnt_fic_attr where fd_id = \$1 ");
* $sql->append("delete from fiche_def where fd_id = \$1");
* foreach ($sql as $s) {
* $g_connection->exec_sql($s,[$this->object->fiche_def->id]);
* }
*/
}
/**
* the setUpBeforeClass() template methods is called before the first test of the test case
* class is run
*/
public static function setUpBeforeClass(): void {
// include 'global.php';
}
/**
* @brief Data simulation
* @return array
*/
function dataExample() {
return array(
['0A', 4]
, ['0B', 6]
, [6, 6]
, ['NONE', -1]
, [14, -1]
, [" ", -1]
, [null, -1]
);
}
/**
* tearDownAfterClass() template methods is calleafter the last test of the test case class is run,
*
*/
static function tearDownAfterClass(): void {
$cn = new \Database(0);
// $cn->exec_sql('drop database phpunit_account_repository');
}
//
// public function dataExample()
// {
// return array([1], [2], [3]);
// }
/**
* @testdox description of the test
* @covers Acc_Balance::summary_add
* @depend Acc_Balance::summary_init
* @backupGlobals enabled
* @dataProvider dataExample
* @global $g_connection
*/
// function testFunction() {
//
// }
private function make_conx() {
$db = new \DatabaseCore(
p_user: noalyss_user,
p_password: noalyss_password,
p_dbname: 'phpunit_account_repository',
p_host: '127.0.0.1',
p_port: noalyss_psql_port
);
return $db;
}
/**
* @testdox create a repository
*/
function testCreate() {
try {
$repo = new \Database(0);
$repo->exec_sql("create database phpunit_account_repository encoding='utf8'");
$db = $this->make_conx();
/**
* create repository
*/
$db->start();
$db->execute_script(NOALYSS_INCLUDE . "/sql/account_repository/schema.sql");
$db->execute_script(NOALYSS_INCLUDE . "/sql/account_repository/data.sql");
$db->execute_script(NOALYSS_INCLUDE . "/sql/account_repository/constraint.sql");
$db->commit();
$db->close();
$this->assertTrue(true, __CLASS__ . " cannot create DB");
$db = $this->make_conx();
$db->start();
$nb_table = $db->get_value("select count(*) from information_schema.tables where table_schema='public'");
$db->commit();
$db->close();
$this->assertEquals(11, $nb_table, " number of table incorrect $nb_table");
} catch (\Exception $ex) {
$db->close();
echo $ex->getMessage();
echo $ex->getTraceAsString();
$this->assertTrue(false, __CLASS__ . " cannot create DB");
}
}
/**
* @testdox patching the repository
*/
function testPatching() {
try {
$db = $this->make_conx();
$db->start();
$MaxVersion = DBVERSIONREPO - 1;
for ($i = 4; $i <= $MaxVersion; $i++) {
if ($db->get_value(' select val from version') <= $i) {
$db->execute_script(NOALYSS_INCLUDE . '/sql/patch/ac-upgrade' . $i . '.sql');
}
}
$this->assertTrue(true, __CLASS__ . " cannot patch DB");
$nb_table = $db->get_value("select count(*) from information_schema.tables where table_schema='public'");
// close first
$db->commit();
$db->close();
//
$this->assertEquals(12, $nb_table, " number of table incorrect $nb_table");
} catch (\Exception $ex) {
echo $ex->getMessage();
echo $ex->getTraceAsString();
$this->assertTrue(false, __CLASS__ . " cannot create DB");
}
}
}

View file

@ -198,6 +198,7 @@ class DocumentTest extends TestCase {
/**
* @testdox Generate Document::generate(), Document::parseDocument(),Document::replace(); require unoconv -l in another session
* @covers Document::generate(), Document::parseDocument(),Document::replace();
* @backupGlobals disabled
*/
function testGenerate() {
require_once 'global.php';

View file

@ -10,6 +10,20 @@ define('USE_PASS', md5('password+123P'));
define('USE_ADMIN', 0);
define('USE_EMAIL', 'none@dev.null.eu');
class Repository extends \Database
{
function __construct()
{
\DatabaseCore::__construct(
p_user : noalyss_user,
p_password: noalyss_password,
p_dbname: 'phpunit_account_repository',
p_host:'127.0.0.1',
p_port: noalyss_psql_port
);
}
}
/**
* @backupGlobals enabled
* @coversDefaultClass \Noalyss_User
@ -24,7 +38,63 @@ class NoalyssUserTest extends TestCase
*/
protected $object;
private $cn;
/**
* @testdox create a new repository
* @return void
*/
public static function setUpBeforeClass(): void
{
$repo = new \Database(0);
if ($repo->exist_database('phpunit_account_repository') == 1) return;
$repo->exec_sql("create database phpunit_account_repository encoding='utf8'");
$db=new \DatabaseCore(
p_user : noalyss_user,
p_password: noalyss_password,
p_dbname: 'phpunit_account_repository',
p_host:'127.0.0.1',
p_port: noalyss_psql_port
);
/**
* create repository
*/
$db->start();
$db->execute_script(NOALYSS_INCLUDE."/sql/account_repository/schema.sql");
$db->execute_script(NOALYSS_INCLUDE."/sql/account_repository/data.sql");
$db->execute_script(NOALYSS_INCLUDE."/sql/account_repository/constraint.sql");
$db->commit($cn);
$db->close();
$db=new \DatabaseCore(
p_user : noalyss_user,
p_password: noalyss_password,
p_dbname: 'phpunit_account_repository',
p_host:'127.0.0.1',
p_port: noalyss_psql_port
);
$MaxVersion=DBVERSIONREPO-1;
for ($i=4;$i<= $MaxVersion;$i++)
{
if ( $db->get_value (' select val from version') <= $i ) {
$db->execute_script(NOALYSS_INCLUDE.'/sql/patch/ac-upgrade'.$i.'.sql');
}
}
}
/**
* @testdox drop the created repository
* @return void
*/
public static function tearDownAfterClass(): void
{
$repo = new \Database(0);
// $repo->exec_sql("drop database phpunit_account_repository ");
}
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
@ -32,7 +102,7 @@ class NoalyssUserTest extends TestCase
protected function setUp():void
{
// create database connx :
$this->cn=new Database();
$this->cn=new \Database();
// create a user
$this->cn->exec_sql('delete from jnt_use_dos where use_id=$1',
array(USE_ID));
@ -256,5 +326,68 @@ class NoalyssUserTest extends TestCase
}
}
/**
* @testdox noalyss_user use another repository
* @param type $param
* @covers \Noalyss_User->load()
*/
public function testSecondRepository() {
$repo2=new Repository();
$repo2->exec_sql('insert into ac_users (use_id,use_first_name,use_name,use_login,use_active,use_pass,use_admin,use_email) values ($1,$2,$3,$4,$5,$6,$7,$8)
on conflict do nothing',
array(USE_ID, USE_FIRST_NAME, "REPO2".USE_NAME, USE_LOGIN, USE_ACTIVE, USE_PASS,
USE_ADMIN, USE_EMAIL));
$demo=new \Noalyss_User(new \Database(DOSSIER), USE_ID,$repo2);
$demo->load();
$this->object->load();
$demo_name= $demo->getName() ;
$name = $this->object->getName();
$repository_name=$this->object->get_repository()->get_name();
$repository_name2=$demo->get_repository()->get_name();
$this->assertEquals('phpunit_account_repository',$repo2->get_name(),"repo2 not connected properly");
$this->assertEquals('phpunit_account_repository',$repository_name2,"user not connected to $repository_name2 2nd repository");
$this->assertTrue($demo_name != $name ,"ERROR: Connected to same repo $name $demo_name" );
$this->assertEquals($demo_name,"REPO2".USE_NAME,"user not created in REPO2");
}
/**
* @testdox check the can_connect from both repository
* @covers \Noalyss_User->load(), \Noalyss_User::__construct,\Noalyss_User::can_connect(),\Noalyss_User->save()
*/
public function testConnectSecond()
{
$repo=new Repository();
$demo=new \Noalyss_User(new Database(DOSSIER), USE_ID,$repo);
$demo->setActive(1);
$demo->save();
$this->assertEquals(1,$demo->can_connect(),' admin is supposed to connect');
$demo->setActive(0);
$demo->save();
$this->assertEquals(0,$demo->can_connect(),' admin is not supposed to connect');
$this->assertEquals(1,$this->object->can_connect(),'admin is supposed to connect from REPO1');
$demo->setActive(1);
$demo->save();
}
public function testFolder_Access()
{
$repo=new Repository();
$demo=new \Noalyss_User(new Database(DOSSIER),USE_ID, repository:$repo);
$user=new \Noalyss_User(new Database(DOSSIER));
$this->assertEquals('X',$demo->get_folder_access(DOSSIER),"2nd repository : can access");
$this->assertEquals('R',$user->get_folder_access(DOSSIER),"1st repository : cannot access");
}
}