import rt 3.8.7
[freeside.git] / rt / t / api / cf_pattern.t
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use RT;
6 use RT::Test tests => 17;
7
8
9 sub fails { ok(!$_[0], "This should fail: $_[1]") }
10 sub works { ok($_[0], $_[1] || 'This works') }
11
12 sub new (*) {
13     my $class = shift;
14     return $class->new($RT::SystemUser);
15 }
16
17 my $q = new(RT::Queue);
18 works($q->Create(Name => "CF-Pattern-".$$));
19
20 my $cf = new(RT::CustomField);
21 my @cf_args = (Name => $q->Name, Type => 'Freeform', Queue => $q->id, MaxValues => 1);
22
23 fails($cf->Create(@cf_args, Pattern => ')))bad!regex((('));
24 works($cf->Create(@cf_args, Pattern => 'good regex'));
25
26 my $t = new(RT::Ticket);
27 my ($id,undef,$msg) = $t->Create(Queue => $q->id, Subject => 'CF Test');
28 works($id,$msg);
29
30 # OK, I'm thoroughly brain washed by HOP at this point now...
31 sub cnt { $t->CustomFieldValues($cf->id)->Count };
32 sub add { $t->AddCustomFieldValue(Field => $cf->id, Value => $_[0]) };
33 sub del { $t->DeleteCustomFieldValue(Field => $cf->id, Value => $_[0]) };
34
35 is(cnt(), 0, "No values yet");
36 fails(add('not going to match'));
37 is(cnt(), 0, "No values yet");
38 works(add('here is a good regex'));
39 is(cnt(), 1, "Value filled");
40 fails(del('here is a good regex'));
41 is(cnt(), 1, "Single CF - Value _not_ deleted");
42
43 $cf->SetMaxValues(0);   # Unlimited MaxValues
44
45 works(del('here is a good regex'));
46 is(cnt(), 0, "Multiple CF - Value deleted");
47
48 fails($cf->SetPattern('(?{ "insert evil code here" })'));
49 works($cf->SetPattern('(?!)')); # reject everything
50 fails(add(''));
51 fails(add('...'));
52
53 1;