4 use RT::Test tests => undef;
5 use JSON qw(decode_json);
7 my ($base, $m) = RT::Test->started_ok;
9 my $cf1 = RT::Test->load_or_create_custom_field(
15 ok $cf1->id, "created cf1";
17 my $cf2 = RT::Test->load_or_create_custom_field(
23 ok $cf2->id, "created cf2";
25 ok( $cf1->AddValue( Name => "cf1 value $_" ) ) for qw(a b c);
26 ok( $cf2->AddValue( Name => "cf2 value $_" ) ) for qw(x y z);
37 $args{term} = delete $args{Term};
39 if (my $obj = delete $args{Context}) {
40 $args{ContextId} = $obj->Id unless defined $args{ContextId};
41 $args{ContextType} = ref($obj) unless defined $args{ContextType};
44 $args{"Object---CustomField-$args{CF}-Values"} = "";
47 delete $args{$_} for grep {not defined $args{$_}} keys %args;
49 my $URI = URI->new("$base/Helpers/Autocomplete/CustomFieldValues");
50 $URI->query_form( %args );
51 $m->get_ok($URI, "GET to autocompleter");
52 return decode_json($m->content);
56 is_deeply ac(CF => 12345, ContextId => 1, ContextType => "RT::Queue"),
57 [], 'nothing for invalid CF';
60 [], "Nothing without a context id";
61 is_deeply ac( ContextId => 12345, ContextType => "RT::Queue"),
62 [], "Nothing with invalid contextid id";
63 is_deeply ac( ContextId => 12, ContextType => "RT::User"),
64 [], "Nothing with invalid contextid type";
68 my $user = RT::Test->load_or_create_user(
70 Password => 'password',
73 my $queue = RT::Test->load_or_create_queue( Name => 'CF Test' );
74 ok $queue->id, 'found or created queue';
75 my $ticket = RT::Test->create_ticket(
77 Subject => "CF application",
79 ok $queue->id, 'created ticket';
82 $m->login('user','password');
84 is_deeply ac( Context => $queue ), [], 'queue context, no permissions, no result';
85 is_deeply ac( Context => $ticket ), [], 'ticket context, no permissions, no result';
87 ok( RT::Test->set_rights(
88 { Principal => $user, Right => [qw(SeeCustomField)], Object => $queue },
89 ), 'add queue level CF viewing rights');
91 my $cfvalues = [ ( map { { value => "cf1 value $_" , label => "cf1 value $_" } } qw(a b c) ) ];
92 is_deeply ac( Context => $queue ), $cfvalues, 'queue context, with permissions get result';
93 is_deeply ac( Context => $ticket ), $cfvalues, 'ticket context, with permissions get result';
96 diag "Switching to non-global CFs";
97 my $globalq = RT::Queue->new( RT->SystemUser );
98 my ($status, $msg) = $cf1->RemoveFromObject( $globalq );
99 ok($status, "Removed CF1 globally: $msg");
100 ($status, $msg) = $cf1->AddToObject( $queue );
101 ok($status, "Added CF1 to queue @{[$queue->id]}: $msg");
102 ($status, $msg) = $cf2->RemoveFromObject( $globalq );
103 ok($status, "Removed CF2 globally: $msg");
106 is_deeply ac( CF => $cf2->id, Context => $queue ), [], 'queue context, but not applied, get no result';
107 is_deeply ac( CF => $cf2->id, Context => $ticket ), [], 'ticket context, but not applied, get no result';
109 is_deeply ac( Context => $queue ), $cfvalues, 'queue context, applied correctly, get result';
110 is_deeply ac( Context => $ticket ), $cfvalues, 'ticket context, applied correctly, get result';
114 diag "Ticket-level rights";
116 ok( RT::Test->set_rights(
117 { Principal => "Owner", Right => [qw(SeeCustomField)], Object => $queue },
118 { Principal => $user, Right => [qw(OwnTicket SeeTicket)], Object => RT->System },
119 ), 'add owner level CF viewing rights');
121 is_deeply ac( Context => $queue ), [], 'queue context, but not owner';
122 is_deeply ac( Context => $ticket ), [], 'ticket context, but not owner';
124 my ($status, $msg) = $ticket->SetOwner( $user->id );
125 ok( $status, "Set owner to user: $msg" );
127 is_deeply ac( Context => $queue ), [], 'queue context is not enough';
128 is_deeply ac( Context => $ticket ), $cfvalues, 'ticket context, get values';