#!/usr/local/cpanel/3rdparty/bin/perl -w
#                                      Copyright 2025 WebPros International, LLC
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use cPstrict;

use Test2::V0;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;
use Test2::Tools::Subtest;
use Test2::Tools::Exception;

use Test::MockModule qw/strict/;

use Test::Cpanel::Policy testing => 'Cpanel::Plugins::Components::Rules::CpanelVersion';

use Cpanel::Plugins::Components::Rules::CpanelVersion ();

my $mock_version;
my $mock = Test::MockModule->new('Cpanel::Version');
$mock->redefine( 'getversionnumber' => sub { return $mock_version } );

subtest 'for a single version' => sub {

    $mock_version = '11.110';
    my $got = Cpanel::Plugins::Components::Rules::CpanelVersion->new('11.110')->is_allowed();
    is $got, 1, 'The cPanel version check for 110 when system is 110 returned true.';

    $mock_version = '11.118';
    $got          = Cpanel::Plugins::Components::Rules::CpanelVersion->new('11.110')->is_allowed();
    is $got, '', 'The cPanel version check for 110 when system is 118 return false.';

    $mock_version = '11.110';
    $got          = Cpanel::Plugins::Components::Rules::CpanelVersion->new('11.118')->is_allowed();
    is $got, '', 'The cPanel version check for 118 when system is 110 return false.';
};

subtest 'for a list of versions version' => sub {

    $mock_version = '11.110';

    my $check = [ '11.110', '11.118', '11.126' ];
    my $got   = Cpanel::Plugins::Components::Rules::CpanelVersion->new($check)->is_allowed();
    is $got, 1, 'The cPanel version check returns true if the version is in the list.';

    $mock_version = '11.118';
    $got          = Cpanel::Plugins::Components::Rules::CpanelVersion->new($check)->is_allowed();
    is $got, 1, 'The cPanel version check returns true if the version is in the list.';

    $mock_version = '11.126';
    $got          = Cpanel::Plugins::Components::Rules::CpanelVersion->new($check)->is_allowed();
    is $got, 1, 'The cPanel version check returns true if the version is in the list.';

    $mock_version = '11.108';
    $got          = Cpanel::Plugins::Components::Rules::CpanelVersion->new($check)->is_allowed();
    is $got, '', 'The cPanel version check returns false if the version is NOT in the list.';

    $mock_version = '11.128';
    $got          = Cpanel::Plugins::Components::Rules::CpanelVersion->new($check)->is_allowed();
    is $got, '', 'The cPanel version check returns false if the version is NOT in the list.';
};

done_testing;
