summaryrefslogtreecommitdiff
path: root/rt/t/articles/cfsearch.t
blob: 49420e581a9101a3e0a17495018cfc29349154a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env perl

use strict;
use warnings;

use RT::Test tests => 11;

my $suffix = '-'. $$;

use_ok 'RT::Class';
use_ok 'RT::Article';
use_ok 'RT::CustomField';

my $classname = 'TestClass';
my $class = RT::Class->new( $RT::SystemUser );
{
    $class->Load( $classname );
    unless ( $class->Id ) {
        my ($id, $msg) = $class->Create(
            Name => $classname,
            Description => 'class for cf tests',
        );
        ok $id, "created class '$classname' #$id"
            or diag "error: $msg";
    } else {
        ok 1, "class '$classname' exists";
    }
}

# create cf
my $cfname = 'TestCF'. $suffix;
my $cf = RT::CustomField->new( $RT::SystemUser );
{
    my ($id, $msg) = $cf->Create(
        Name => $cfname,
        LookupType => 'RT::Class-RT::Article',
        Type => 'Select', MaxValues => 1,
        Description => 'singleselect cf for tests',
    );
    ok $id, "created cf '$cfname' #$id"
        or diag "error: $msg";
}

# attach cf to class
{
    my ($status, $msg) = $cf->AddToObject( $class );
    ok $status, "attached the cf to the class"
        or diag "error: $msg";
}
  
# create two cf-values
{
    my ($status, $msg) = $cf->AddValue( Name => 'Value1' );
    ok $status, "added a value to the cf" or diag "error: $msg";

    ($status, $msg) = $cf->AddValue( Name => 'Value2' );
    ok $status, "added a value to the cf" or diag "error: $msg";
}

my $article1name = 'TestArticle1'.$suffix;
my $article1 = RT::Article->new($RT::SystemUser);
$article1->Create( Name => $article1name, Summary => 'Test', Class => $class->Id);
$article1->AddCustomFieldValue(Field => $cf->Id, Value => 'Value1');

my $article2name = 'TestArticle2'.$suffix;
my $article2 = RT::Article->new($RT::SystemUser);
$article2->Create( Name => $article2name, Summary => 'Test', Class => $class->Id);
$article2->AddCustomFieldValue(Field => $cf->Id, Value => 'Value2');

# search for articles containing 1st value
{
    my $articles = RT::Articles->new( $RT::SystemUser );
    $articles->UnLimit;
    $articles->Limit( FIELD => "Class", SUBCLAUSE => 'ClassMatch', VALUE => $class->Id);
    $articles->LimitCustomField( FIELD => $cf->Id, VALUE => 'Value1' );
    is $articles->Count, 1, 'found correct number of articles';
}

{
    my $articles = RT::Articles->new($RT::SystemUser);
    $articles->UnLimit;
    $articles->Limit( FIELD => "Class", SUBCLAUSE => 'ClassMatch', VALUE => $class->Id);
    $articles->LimitCustomField( FIELD => $cf, VALUE => 'Value1' );    
    is $articles->Count, 1, 'found correct number of articles';
}

{
    my $articles = RT::Articles->new($RT::SystemUser);
    $articles->UnLimit( );
    $articles->Limit( FIELD => "Class", SUBCLAUSE => 'ClassMatch', VALUE => $class->Id);
    $articles->LimitCustomField( FIELD => $cf->Name, VALUE => 'Value1' );
    is $articles->Count, 1, 'found correct number of articles';
}