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