first pass RT4 merge, RT#13852
[freeside.git] / rt / t / articles / articles.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use RT::Test tests => 29;
7
8 use_ok 'RT::Articles';
9 use_ok 'RT::Classes';
10 use_ok 'RT::Class';
11
12 my $class = RT::Class->new($RT::SystemUser);
13 my ( $id, $msg ) = $class->Create( Name => 'CollectionTest-' . $$ );
14 ok( $id, $msg );
15
16 # Add a custom field to our class
17 use_ok('RT::CustomField');
18 my $cf = RT::CustomField->new($RT::SystemUser);
19 isa_ok($cf, 'RT::CustomField');
20
21 ($id,$msg) = $cf->Create( Name => 'Articles::Sample-'.$$,
22              Description => 'Test text cf',
23              LookupType => RT::Article->CustomFieldLookupType,
24              Type => 'Freeform'
25              );
26
27
28
29 ok($id,$msg);
30
31
32 ($id,$msg) = $cf->AddToObject($class);
33 ok ($id,$msg);
34
35
36
37 my $art = RT::Article->new($RT::SystemUser);
38 ( $id, $msg ) = $art->Create(
39     Class   => $class->id,
40     Name    => 'Collection-1-' . $$,
41     Summary => 'Coll-1-' . $$,
42     'CustomField-'.$cf->Name => 'Test-'.$$
43 );
44
45 ok( $id, $msg );
46
47
48
49
50
51
52 my $arts = RT::Articles->new($RT::SystemUser);
53 $arts->LimitName( VALUE => 'Collection-1-' . $$ . 'fake' );
54 is( $arts->Count, 0,
55     "Found no artlcles with names matching something that is not there" );
56
57 my $arts2 = RT::Articles->new($RT::SystemUser);
58 $arts2->LimitName( VALUE => 'Collection-1-' . $$ );
59 is( $arts2->Count, 1, 'Found one with names matching the word "test"' );
60
61 $arts = RT::Articles->new($RT::SystemUser);
62 $arts->LimitSummary( VALUE => 'Coll-1-' . $$ . 'fake' );
63 is( $arts->Count, 0,
64     'Found no artlcles with summarys matching something that is not there' );
65
66 $arts2 = RT::Articles->new($RT::SystemUser);
67 $arts2->LimitSummary( VALUE => 'Coll-1-' . $$ );
68 is( $arts2->Count, 1, 'Found one with summarys matching the word "Coll-1"' );
69
70 my $new_art = RT::Article->new($RT::SystemUser);
71 ( $id, $msg ) = $new_art->Create(
72     Class          => $class->id,
73     Name           => 'CFSearchTest1' . $$,
74     'CustomField-'.$cf->Name  => 'testing' . $$
75 );
76
77 ok( $id, $msg . " Created a second testable article" );
78
79
80 $arts = RT::Articles->new($RT::SystemUser);
81 $arts->LimitCustomField( OPERATOR => 'LIKE', VALUE => "esting".$$ );
82 is( $arts->Count, 1, "Found 1 cf values matching 'esting" . $$ . "' for an unspecified field");
83
84 $arts = RT::Articles->new($RT::SystemUser);
85 $arts->LimitCustomField( OPERATOR => '=', VALUE => "esting".$$ );
86 is( $arts->Count, 0, "Found 0 cf values EXACTLY matching 'esting" . $$ . "' for an unspecified field");
87
88 $arts = RT::Articles->new($RT::SystemUser);
89 $arts->LimitCustomField( OPERATOR => '=', VALUE => "testing".$$ );
90 is( $arts->Count, 1, "Found 0 cf values EXACTLY matching 'testing" . $$ . "' for an unspecified field");
91
92
93 $arts = RT::Articles->new($RT::SystemUser);
94 $arts->LimitCustomField( OPERATOR => 'LIKE', VALUE => $$ );
95 is( $arts->Count, 2, "Found 1 cf values matching '" . $$ . "' for an unspecified field");
96
97
98 # Test searching on named custom fields
99 $arts = RT::Articles->new($RT::SystemUser);
100 $arts->LimitCustomField( OPERATOR => 'LIKE', VALUE => $$, FIELD => $cf->Name );
101 is( $arts->Count, 2, "Found 1 Article with cf values matching '".$$."' for CF named " .$cf->Name);
102
103 $arts = RT::Articles->new($RT::SystemUser);
104 $arts->LimitCustomField( OPERATOR => 'LIKE', VALUE => $$, FIELD => 'NO-SUCH-CF' );
105 is( $arts->Count,0, "Found no cf values matching '".$$."' for CF 'NO-SUCH-CF'  " );
106
107 $arts = RT::Articles->new($RT::SystemUser);
108 $arts->Limit(FIELD =>'Class', VALUE => $class->id);
109         
110 $arts->LimitCustomField(
111     OPERATOR => 'NOT LIKE',
112     VALUE    => 'blah',
113     FIELD    => $cf->id
114 );
115 is(
116     $arts->Count ,2,
117     "Found 1 articles with custom field values not matching blah");
118
119 $arts = RT::Articles->new($RT::SystemUser);
120 $arts->Limit(FIELD =>'Class', VALUE => $class->id);
121 $arts->LimitCustomField( OPERATOR => 'NOT LIKE', VALUE => 'est', FIELD => $cf->id );
122 is( $arts->Count , 0, "Found 0 cf values not matching 'est' for CF  ".$cf->id. " " . join(',', map {$_->id} @{$arts->ItemsArrayRef}));
123 $arts = RT::Articles->new($RT::SystemUser);
124 $arts->Limit(FIELD =>'Class', VALUE => $class->id);
125 $arts->LimitCustomField( OPERATOR => 'NOT LIKE', VALUE => 'BOGUS', FIELD => $cf->id );
126 is( $arts->Count , 2, "Found 2 articles not matching 'BOGUS' for CF  ".$cf->id);
127
128 my $ac = RT::Articles->new($RT::SystemUser);
129 ok( $ac->isa('RT::Articles') );
130 ok( $ac->isa('DBIx::SearchBuilder') );
131 ok( $ac->LimitRefersTo('http://dead.link') );
132 is( $ac->Count, 0 );
133
134 $ac = RT::Articles->new($RT::SystemUser);
135 ok( $ac->LimitReferredToBy('http://dead.link') );
136 is( $ac->Count, 0 );
137