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