use strict; use warnings; use 5.10.1; use lib qw(. lib t); use Test::More tests => 16; use Capture::Tiny ':all'; use Bugzilla; use Bugzilla::Constants; BEGIN { $ENV{PROJECT} = 'tests'; Bugzilla->extensions; } my $e; Bugzilla->usage_mode(USAGE_MODE_CMDLINE); Bugzilla->set_user(Bugzilla::User->new({name => 'myadmin@example.net'})); use_ok('Bugzilla::Extension::RuleEngine::RuleGroup'); use_ok('Bugzilla::Extension::RuleEngine::Rule'); use_ok('Bugzilla::Extension::RuleEngine::RuleDetail'); use_ok('Bugzilla::Extension::RuleEngine::Pages'); my $rule_group; eval { $rule_group = Bugzilla::Extension::RuleEngine::RuleGroup->create({ name => 'BRE Test', description => 'A group for testing the BRE', }); }; $e = $@; ok((not $e), "Create RuleGroup"); diag explain $e if ($e); isa_ok($rule_group, 'Bugzilla::Extension::RuleEngine::RuleGroup'); my $view_grp = Bugzilla::Group->check({name => 'editbugs'}); my $edit_grp = Bugzilla::Group->check({name => 'admin'}); eval { $rule_group->set_view_groups([$view_grp]); $rule_group->set_edit_groups([$edit_grp]); $rule_group->set_user_id('myadmin@example.net'); $rule_group->update; }; $e = $@; ok((not $e), "Update RuleGroup"); diag explain $e if ($e); my $product; eval { $product = Bugzilla::Product->check({name => 'Bugzilla'}); }; $e = $@; ok((not $e), "Load product"); diag explain $e if ($e); eval { $product->set_rule_group($rule_group->id); $product->update(); }; $e = $@; ok((not $e), "Set BRE Group on product"); diag explain $e if ($e); my %args = ( name => 'Test rule', description => 'Test rule is testing', rule_group_id => $rule_group->id, is_periodic => 0, ); my $rule; eval { $rule = Bugzilla::Extension::RuleEngine::Rule->create(\%args); }; $e = $@; ok((not $e), "Create Rule"); diag explain $e if ($e); isa_ok($rule, 'Bugzilla::Extension::RuleEngine::Rule'); my $rule_detail; my $match = { product => {values => ['Bugzilla']}, bug_status => {values => ["All open"]}, keywords => {word => "is not any of", values => ["FutureFeature"]}, }; my $action = {keywords => {action => "add", values => ["FutureFeature"]}}; eval { $rule_detail = Bugzilla::Extension::RuleEngine::RuleDetail->create({ match => $match, action => $action, creator_id => Bugzilla->user->id, rule_id => $rule->id, isactive => 1, }); }; $e = $@; ok((not $e), "Create RuleDetail"); diag explain $e if ($e); isa_ok($rule_detail, 'Bugzilla::Extension::RuleEngine::RuleDetail'); eval { $rule->set_current_detail_id($rule_detail->id); $rule->update; }; $e = $@; ok((not $e), "Set current detail on rule"); diag explain $e if ($e); my @bug_ids; eval { @bug_ids = @{_get_matching_bugs_for_rule($rule->current->match, $rule)}; }; $e = $@; ok((not $e), "Get bugs for rule"); diag explain $e if ($e); is($bug_ids[0], 1, "Matched some bugs");