RT 4.0.13
[freeside.git] / rt / t / articles / cfsearch.t
1
2 use strict;
3 use warnings;
4
5 use RT::Test tests => 11;
6
7 my $suffix = '-'. $$;
8
9 use_ok 'RT::Class';
10 use_ok 'RT::Article';
11 use_ok 'RT::CustomField';
12
13 my $classname = 'TestClass';
14 my $class = RT::Class->new( $RT::SystemUser );
15 {
16     $class->Load( $classname );
17     unless ( $class->Id ) {
18         my ($id, $msg) = $class->Create(
19             Name => $classname,
20             Description => 'class for cf tests',
21         );
22         ok $id, "created class '$classname' #$id"
23             or diag "error: $msg";
24     } else {
25         ok 1, "class '$classname' exists";
26     }
27 }
28
29 # create cf
30 my $cfname = 'TestCF'. $suffix;
31 my $cf = RT::CustomField->new( $RT::SystemUser );
32 {
33     my ($id, $msg) = $cf->Create(
34         Name => $cfname,
35         LookupType => 'RT::Class-RT::Article',
36         Type => 'Select', MaxValues => 1,
37         Description => 'singleselect cf for tests',
38     );
39     ok $id, "created cf '$cfname' #$id"
40         or diag "error: $msg";
41 }
42
43 # attach cf to class
44 {
45     my ($status, $msg) = $cf->AddToObject( $class );
46     ok $status, "attached the cf to the class"
47         or diag "error: $msg";
48 }
49   
50 # create two cf-values
51 {
52     my ($status, $msg) = $cf->AddValue( Name => 'Value1' );
53     ok $status, "added a value to the cf" or diag "error: $msg";
54
55     ($status, $msg) = $cf->AddValue( Name => 'Value2' );
56     ok $status, "added a value to the cf" or diag "error: $msg";
57 }
58
59 my $article1name = 'TestArticle1'.$suffix;
60 my $article1 = RT::Article->new($RT::SystemUser);
61 $article1->Create( Name => $article1name, Summary => 'Test', Class => $class->Id);
62 $article1->AddCustomFieldValue(Field => $cf->Id, Value => 'Value1');
63
64 my $article2name = 'TestArticle2'.$suffix;
65 my $article2 = RT::Article->new($RT::SystemUser);
66 $article2->Create( Name => $article2name, Summary => 'Test', Class => $class->Id);
67 $article2->AddCustomFieldValue(Field => $cf->Id, Value => 'Value2');
68
69 # search for articles containing 1st value
70 {
71     my $articles = RT::Articles->new( $RT::SystemUser );
72     $articles->UnLimit;
73     $articles->Limit( FIELD => "Class", SUBCLAUSE => 'ClassMatch', VALUE => $class->Id);
74     $articles->LimitCustomField( FIELD => $cf->Id, VALUE => 'Value1' );
75     is $articles->Count, 1, 'found correct number of articles';
76 }
77
78 {
79     my $articles = RT::Articles->new($RT::SystemUser);
80     $articles->UnLimit;
81     $articles->Limit( FIELD => "Class", SUBCLAUSE => 'ClassMatch', VALUE => $class->Id);
82     $articles->LimitCustomField( FIELD => $cf, VALUE => 'Value1' );    
83     is $articles->Count, 1, 'found correct number of articles';
84 }
85
86 {
87     my $articles = RT::Articles->new($RT::SystemUser);
88     $articles->UnLimit( );
89     $articles->Limit( FIELD => "Class", SUBCLAUSE => 'ClassMatch', VALUE => $class->Id);
90     $articles->LimitCustomField( FIELD => $cf->Name, VALUE => 'Value1' );
91     is $articles->Count, 1, 'found correct number of articles';
92 }
93