5 use RT::Test tests => 11;
11 use_ok 'RT::CustomField';
13 my $classname = 'TestClass';
14 my $class = RT::Class->new( $RT::SystemUser );
16 $class->Load( $classname );
17 unless ( $class->Id ) {
18 my ($id, $msg) = $class->Create(
20 Description => 'class for cf tests',
22 ok $id, "created class '$classname' #$id"
23 or diag "error: $msg";
25 ok 1, "class '$classname' exists";
30 my $cfname = 'TestCF'. $suffix;
31 my $cf = RT::CustomField->new( $RT::SystemUser );
33 my ($id, $msg) = $cf->Create(
35 LookupType => 'RT::Class-RT::Article',
36 Type => 'Select', MaxValues => 1,
37 Description => 'singleselect cf for tests',
39 ok $id, "created cf '$cfname' #$id"
40 or diag "error: $msg";
45 my ($status, $msg) = $cf->AddToObject( $class );
46 ok $status, "attached the cf to the class"
47 or diag "error: $msg";
50 # create two cf-values
52 my ($status, $msg) = $cf->AddValue( Name => 'Value1' );
53 ok $status, "added a value to the cf" or diag "error: $msg";
55 ($status, $msg) = $cf->AddValue( Name => 'Value2' );
56 ok $status, "added a value to the cf" or diag "error: $msg";
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');
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');
69 # search for articles containing 1st value
71 my $articles = RT::Articles->new( $RT::SystemUser );
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';
79 my $articles = RT::Articles->new($RT::SystemUser);
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';
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';