#!/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::CompanyId';
use Capture::Tiny;

use Cpanel::Plugins::Components::Rules::CompanyId;

my $mock_companyid = 100;
my $mock           = Test::MockModule->new("Cpanel::License::CompanyID");
$mock->redefine(
    get_company_id => sub {
        return $mock_companyid;
    }
);

subtest 'Check company id when passing one company id' => sub {
    my $phrase = 1;
    my $got    = Cpanel::Plugins::Components::Rules::CompanyId->new($phrase)->is_allowed();
    is $got, !!0, "got true value when companyid $phrase is the NOT the current company id $mock_companyid.";

    $phrase = 100;
    $got    = Cpanel::Plugins::Components::Rules::CompanyId->new($phrase)->is_allowed();
    is $got, !!1, "got true value when companyid $phrase is the current company id $mock_companyid.";
};

subtest 'Check company id when passing list of companies' => sub {
    my $phrase = [ 1, 2, 4 ];
    my $got    = Cpanel::Plugins::Components::Rules::CompanyId->new($phrase)->is_allowed();
    is $got, !!0, "got true value when companyid list, $phrase->@*, is the NOT the current company id $mock_companyid.";

    $phrase = [ 50, 100, 105 ];
    $got    = Cpanel::Plugins::Components::Rules::CompanyId->new($phrase)->is_allowed();
    is $got, !!1, "got true value when companyid list, $phrase->@*, is the current company id $mock_companyid.";
};

subtest 'Run check when server companyid is 0' => sub {
    $mock_companyid = 0;

    my $phrase = [ 1, 2, 4 ];

    my $got = Cpanel::Plugins::Components::Rules::CompanyId->new($phrase)->is_allowed();
    is $got, !!0, "got false value when companyid is 0.";
};

subtest "Run check when server companyid is ''" => sub {
    $mock_companyid = '';

    my $phrase = [ 1, 2, 4 ];

    my $got = Cpanel::Plugins::Components::Rules::CompanyId->new($phrase)->is_allowed();
    is $got, !!0, "got false value when companyid is ''.";
};

done_testing;
