RT 4.0.13
[freeside.git] / rt / t / articles / basic-api.t
1
2 use warnings;
3 use strict;
4
5 use RT::Test tests => 37;
6
7 use_ok('RT::Class');
8
9 my $class = RT::Class->new($RT::SystemUser);
10 isa_ok($class, 'RT::Class');
11 isa_ok($class, 'RT::Record');
12 isa_ok($class, 'RT::Record');
13
14
15 my $name = 'test-'.$$;
16 my ($id,$msg) = $class->Create( Name =>$name, Description => 'Test class');
17 ok($id,$msg);
18 is ($class->Name, $name);
19 is ($class->Description, 'Test class');
20
21
22
23 # Test custom fields.
24
25 can_ok($class, 'CustomFields');
26 can_ok($class, 'AddCustomFieldValue');
27 can_ok($class, 'DeleteCustomFieldValue');
28 can_ok($class, 'FirstCustomFieldValue');
29 can_ok($class, 'CustomFieldValues');
30 can_ok($class, 'CurrentUserHasRight');
31
32
33 # Add a custom field to our class
34 my $cf = RT::CustomField->new($RT::SystemUser);
35 isa_ok($cf, 'RT::CustomField');
36
37 ($id,$msg) = $cf->Create( Name => 'Articles::Sample-'.$$,
38              Description => 'Test text cf',
39              LookupType => RT::Article->CustomFieldLookupType,
40              Type => 'Text'
41              );
42
43
44
45 ok($id,$msg);
46
47
48 ($id,$msg) = $cf->AddToObject($class);
49 ok ($id,$msg);
50
51
52 # Does our class have a custom field?
53
54 my $cfs = $class->ArticleCustomFields;
55 isa_ok($cfs, 'RT::CustomFields');
56 is($cfs->Count, 1, "We only have one custom field");
57 my $found_cf = $cfs->First;
58 is ($cf->id, $found_cf->id, "it's the right one");
59
60 ($id,$msg) = $cf->RemoveFromObject($class);
61
62 is($class->ArticleCustomFields->Count, 0, "All gone!");
63
64 # Put it back. we want to go forward.
65
66 ($id,$msg) = $cf->AddToObject($class);
67 ok ($id,$msg);
68
69
70
71
72 use_ok('RT::Article');
73
74 my $art = RT::Article->new($RT::SystemUser);
75 ($id,$msg) =$art->Create(Class => $class->id,
76              Name => 'Sample'.$$,
77              Description => 'A sample article');
78
79 ok($id,"Created article ".$id." - " .$msg);
80
81 # make sure there is one transaction.
82
83 my $txns = $art->Transactions;
84
85 is($txns->Count, 1, "One txn");
86 my $txn = $txns->First;
87 is ($txn->ObjectType, 'RT::Article');
88 is ($txn->ObjectId , $id ,  "It's the right article");
89 is ($txn->Type, 'Create', "It's a create!");
90
91
92 my $art_cfs = $art->CustomFields();
93 is ($art_cfs->Count, 1, "It has a custom field");
94 my $values = $art->CustomFieldValues($art_cfs->First);
95 is ($values->Count, 0);
96
97 $art->AddCustomFieldValue(Field => $cf->id, Value => 'Testing');
98 $values = $art->CustomFieldValues($art_cfs->First->id);
99
100 # We added the custom field
101 is ($values->Count, 1);
102 is ($values->First->Content, 'Testing', "We added the CF");
103
104 is ($art->Transactions->Count,2, "We added a second transaction for the custom field addition");
105 my $txn2 = $art->Transactions->Last;
106 is ($txn2->ObjectId, $art->id);
107 is ($txn2->id, ($txn->id +1));
108 is ($txn2->Type, 'CustomField');
109 is($txn2->NewValue, 'Testing');
110 ok (!$txn2->OldValue, "It had no old value");
111
112 1;
113