TimeTrex/unit_tests/testcases/other/SystemLogTest.php

217 lines
7.7 KiB
PHP
Raw Normal View History

2022-12-13 07:10:06 +01:00
<?php /** @noinspection PhpMissingDocCommentInspection */
/*********************************************************************************
*
* TimeTrex is a Workforce Management program developed by
* TimeTrex Software Inc. Copyright (C) 2003 - 2021 TimeTrex Software Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY TIMETREX, TIMETREX DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* 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 Affero General Public License for more
* details.
*
*
* You should have received a copy of the GNU Affero General Public License along
* with this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
*
* You can contact TimeTrex headquarters at Unit 22 - 2475 Dobbin Rd. Suite
* #292 West Kelowna, BC V4T 2E9, Canada or at email address info@timetrex.com.
*
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by TimeTrex" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by TimeTrex".
*
********************************************************************************/
/**
* @group SystemLog
*/
class SystemLogTest extends PHPUnit\Framework\TestCase {
protected $company_id = null;
protected $user_id = null;
protected $pay_period_schedule_id = null;
protected $pay_period_objs = null;
protected $pay_stub_account_link_arr = null;
public function setUp(): void {
global $dd;
Debug::text( 'Running setUp(): ', __FILE__, __LINE__, __METHOD__, 10 );
TTDate::setTimeZone( 'America/Vancouver', true ); //Due to being a singleton and PHPUnit resetting the state, always force the timezone to be set.
$dd = new DemoData();
$dd->setEnableQuickPunch( false ); //Helps prevent duplicate punch IDs and validation failures.
$dd->setUserNamePostFix( '_' . uniqid( null, true ) ); //Needs to be super random to prevent conflicts and random failing tests.
$this->company_id = $dd->createCompany();
$this->legal_entity_id = $dd->createLegalEntity( $this->company_id, 10 );
Debug::text( 'Company ID: ' . $this->company_id, __FILE__, __LINE__, __METHOD__, 10 );
//$dd->createPermissionGroups( $this->company_id, 40 ); //Administrator only.
$dd->createCurrency( $this->company_id, 10 );
//$this->branch_id = $dd->createBranch( $this->company_id, 10 ); //NY
//$this->department_id = $dd->createDepartment( $this->company_id, 10 );
$dd->createUserWageGroups( $this->company_id );
$this->user_id = $dd->createUser( $this->company_id, $this->legal_entity_id, 100 );
}
public function tearDown(): void {
Debug::text( 'Running tearDown(): ', __FILE__, __LINE__, __METHOD__, 10 );
}
function createPayPeriodSchedule( $shift_assigned_day = 10 ) {
$ppsf = new PayPeriodScheduleFactory();
$ppsf->setCompany( $this->company_id );
//$ppsf->setName( 'Bi-Weekly'.rand(1000,9999) );
$ppsf->setName( 'Bi-Weekly' );
$ppsf->setDescription( 'Pay every two weeks' );
$ppsf->setType( 20 );
$ppsf->setStartWeekDay( 0 );
$anchor_date = TTDate::getBeginWeekEpoch( TTDate::incrementDate( time(), -42, 'day' ) ); //Start 6 weeks ago
$ppsf->setAnchorDate( $anchor_date );
$ppsf->setStartDayOfWeek( TTDate::getDayOfWeek( $anchor_date ) );
$ppsf->setTransactionDate( 7 );
$ppsf->setTransactionDateBusinessDay( true );
$ppsf->setTimeZone( 'America/Vancouver' );
$ppsf->setDayStartTime( 0 );
$ppsf->setNewDayTriggerTime( ( 4 * 3600 ) );
$ppsf->setMaximumShiftTime( ( 16 * 3600 ) );
$ppsf->setShiftAssignedDay( $shift_assigned_day );
$ppsf->setEnableInitialPayPeriods( false );
if ( $ppsf->isValid() ) {
$insert_id = $ppsf->Save( false );
Debug::Text( 'Pay Period Schedule ID: ' . $insert_id, __FILE__, __LINE__, __METHOD__, 10 );
$ppsf->setUser( [ $this->user_id ] );
$ppsf->Save();
$this->pay_period_schedule_id = $insert_id;
return $insert_id;
}
Debug::Text( 'Failed Creating Pay Period Schedule!', __FILE__, __LINE__, __METHOD__, 10 );
return false;
}
function createPayPeriods( $initial_date = false ) {
$max_pay_periods = 35;
$ppslf = new PayPeriodScheduleListFactory();
$ppslf->getById( $this->pay_period_schedule_id );
if ( $ppslf->getRecordCount() > 0 ) {
$pps_obj = $ppslf->getCurrent();
$end_date = null;
for ( $i = 0; $i < $max_pay_periods; $i++ ) {
if ( $i == 0 ) {
if ( $initial_date !== false ) {
$end_date = $initial_date;
} else {
$end_date = TTDate::getBeginWeekEpoch( TTDate::incrementDate( time(), -42, 'day' ) );
}
} else {
$end_date = TTDate::incrementDate( $end_date, 14, 'day' );
}
Debug::Text( 'I: ' . $i . ' End Date: ' . TTDate::getDate( 'DATE+TIME', $end_date ), __FILE__, __LINE__, __METHOD__, 10 );
$pps_obj->createNextPayPeriod( $end_date, ( 86400 + 3600 ), false ); //Don't import punches, as that causes deadlocks when running tests in parallel.
}
}
return true;
}
function getAllPayPeriods() {
$pplf = new PayPeriodListFactory();
//$pplf->getByCompanyId( $this->company_id );
$pplf->getByPayPeriodScheduleId( $this->pay_period_schedule_id );
if ( $pplf->getRecordCount() > 0 ) {
foreach ( $pplf as $pp_obj ) {
Debug::text( 'Pay Period... Start: ' . TTDate::getDate( 'DATE+TIME', $pp_obj->getStartDate() ) . ' End: ' . TTDate::getDate( 'DATE+TIME', $pp_obj->getEndDate() ), __FILE__, __LINE__, __METHOD__, 10 );
$this->pay_period_objs[] = $pp_obj;
}
}
$this->pay_period_objs = array_reverse( $this->pay_period_objs );
return true;
}
/**
* @group testSystemLogA
*/
function testSystemLogA() {
global $dd;
global $config_vars;
//Populate global variables for current_user.
$ulf = TTnew( 'UserListFactory' ); /** @var UserListFactory $ulf */
$user_obj = $ulf->getById( $this->user_id )->getCurrent();
global $current_user, $current_company;
$current_user = $user_obj;
$current_company = $user_obj->getCompanyObject();
//Enable system log.
$config_vars['other']['disable_audit_log'] = false;
$config_vars['other']['disable_audit_log_detail'] = false;
$user_id = $dd->createUser( $this->company_id, $this->legal_entity_id, 10 );
$llf = new LogListFactory();
$filter_data = [ 'table_name' => 'users', 'object_id' => $user_id ];
$llf->getAPISearchByCompanyIdAndArrayCriteria( $this->company_id, $filter_data );
if ( $llf->getRecordCount() == 1 ) {
foreach ( $llf as $l_obj ) {
$this->assertEquals( $l_obj->getUser(), $this->user_id );
$this->assertEquals( $l_obj->getObject(), $user_id );
$this->assertEquals( 10, $l_obj->getAction() );
$this->assertEquals( 'users', $l_obj->getTableName() );
$this->assertNotEmpty( $l_obj->getDescription() );
}
$this->assertEquals( true, true );
} else {
$this->assertEquals( true, false );
}
//Disable system log again.
$config_vars['other']['disable_audit_log'] = true;
$config_vars['other']['disable_audit_log_detail'] = true;
return true;
}
}
?>