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