"Fossies" - the Fresh Open Source Software Archive 
As a special service "Fossies" has tried to format the requested text file into HTML format (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
1 use strict;
2 use warnings;
3
4 use lib 'xt/lib';
5 use RT::Extension::Assets::Test tests => undef;
6 use Test::Warn;
7
8 my $catalog;
9
10 diag "Create a catalog";
11 {
12 $catalog = create_catalog( Name => 'Test Catalog', Disabled => 1 );
13 ok $catalog && $catalog->id, "Created catalog";
14 is $catalog->Name, "Test Catalog", "Name is correct";
15 ok $catalog->Disabled, "Disabled";
16
17 my $asset;
18 warning_like {
19 $asset = create_asset( Name => "Test", Catalog => $catalog->id );
20 } qr/^Failed to create asset .* Invalid catalog/i;
21 ok !$asset, "Couldn't create asset in disabled catalog";
22
23 my ($ok, $msg) = $catalog->SetDisabled(0);
24 ok $ok, "Enabled catalog: $msg";
25 ok !$catalog->Disabled, "Enabled";
26 }
27
28 diag "Create basic asset (no CFs)";
29 {
30 my $asset = RT::Asset->new( RT->SystemUser );
31 my ($id, $msg) = $asset->Create(
32 Name => 'Thinkpad T420s',
33 Description => 'Laptop',
34 Catalog => $catalog->Name,
35 );
36 ok $id, "Created: $msg";
37 is $asset->id, $id, "id matches";
38 is $asset->Name, "Thinkpad T420s", "Name matches";
39 is $asset->Description, "Laptop", "Description matches";
40
41 # Create txn
42 my @txns = @{$asset->Transactions->ItemsArrayRef};
43 is scalar @txns, 1, "One transaction";
44 is $txns[0]->Type, "Create", "... of type Create";
45
46 # Update
47 my ($txnid, $txnmsg) = $asset->SetName("Lenovo Thinkpad T420s");
48 ok $txnid, "Updated Name: $txnmsg";
49 is $asset->Name, "Lenovo Thinkpad T420s", "New Name matches";
50
51 # Set txn
52 @txns = @{$asset->Transactions->ItemsArrayRef};
53 is scalar @txns, 2, "Two transactions";
54 is $txns[1]->Type, "Set", "... the second of which is Set";
55 is $txns[1]->Field, "Name", "... Field is Name";
56 is $txns[1]->OldValue, "Thinkpad T420s", "... OldValue is correct";
57
58 # Delete
59 my ($ok, $err) = $asset->Delete;
60 ok !$ok, "Deletes are prevented: $err";
61 $asset->Load($id);
62 ok $asset->id, "Asset not deleted";
63 }
64
65 diag "Create with CFs";
66 {
67 my $height = create_cf( Name => 'Height' );
68 ok $height->id, "Created CF";
69
70 my $material = create_cf( Name => 'Material' );
71 ok $material->id, "Created CF";
72
73 ok apply_cfs($height, $material), "Applied CFs";
74
75 my $asset = RT::Asset->new( RT->SystemUser );
76 my ($id, $msg) = $asset->Create(
77 Name => 'Standing desk',
78 "CustomField-".$height->id => '46"',
79 "CustomField-Material" => 'pine',
80 Catalog => $catalog->Name,
81 );
82 ok $id, "Created: $msg";
83 is $asset->FirstCustomFieldValue('Height'), '46"', "Found height";
84 is $asset->FirstCustomFieldValue('Material'), 'pine', "Found material";
85 is $asset->Transactions->Count, 1, "Only a single txn";
86 }
87
88 note "Create/update with Roles";
89 {
90 my $root = RT::User->new( RT->SystemUser );
91 $root->Load("root");
92 ok $root->id, "Found root";
93
94 my $bps = RT::Test->load_or_create_user( Name => "BPS" );
95 ok $bps->id, "Created BPS user";
96
97 my $asset = RT::Asset->new( RT->SystemUser );
98 my ($id, $msg) = $asset->Create(
99 Name => 'RT server',
100 HeldBy => $root->PrincipalId,
101 Owner => $bps->PrincipalId,
102 Contact => $bps->PrincipalId,
103 Catalog => $catalog->id,
104 );
105 ok $id, "Created: $msg";
106 is $asset->HeldBy->UserMembersObj->First->Name, "root", "root is Holder";
107 is $asset->Owner->Name, "BPS", "BPS is Owner";
108 is $asset->Contacts->UserMembersObj->First->Name, "BPS", "BPS is Contact";
109
110 my $sysadmins = RT::Group->new( RT->SystemUser );
111 $sysadmins->CreateUserDefinedGroup( Name => 'Sysadmins' );
112 ok $sysadmins->id, "Created group";
113 is $sysadmins->Name, "Sysadmins", "Got group name";
114
115 (my $ok, $msg) = $asset->AddRoleMember(
116 Type => 'Contact',
117 Group => 'Sysadmins',
118 );
119 ok $ok, "Added Sysadmins as Contact: $msg";
120 is $asset->Contacts->MembersObj->Count, 2, "Found two members";
121
122 my @txn = grep { $_->Type eq 'AddWatcher' } @{$asset->Transactions->ItemsArrayRef};
123 ok @txn == 1, "Found one AddWatcher txn";
124 is $txn[0]->Field, "Contact", "... of a Contact";
125 is $txn[0]->NewValue, $sysadmins->PrincipalId, "... for the right principal";
126
127 ($ok, $msg) = $asset->DeleteRoleMember(
128 Type => 'Contact',
129 PrincipalId => $bps->PrincipalId,
130 );
131 ok $ok, "Removed BPS user as Contact: $msg";
132 is $asset->Contacts->MembersObj->Count, 1, "Now just one member";
133 is $asset->Contacts->GroupMembersObj(Recursively => 0)->First->Name, "Sysadmins", "... it's Sysadmins";
134
135 @txn = grep { $_->Type eq 'DelWatcher' } @{$asset->Transactions->ItemsArrayRef};
136 ok @txn == 1, "Found one DelWatcher txn";
137 is $txn[0]->Field, "Contact", "... of a Contact";
138 is $txn[0]->OldValue, $bps->PrincipalId, "... for the right principal";
139 }
140
141 diag "Custom Field handling";
142 {
143 diag "Make sure we don't load queue CFs";
144 my $queue_cf = RT::CustomField->new( RT->SystemUser );
145 my ($ok, $msg) = $queue_cf->Create(
146 Name => "Queue CF",
147 Type => "Text",
148 LookupType => RT::Queue->CustomFieldLookupType,
149 );
150 ok( $queue_cf->Id, "Created test CF: " . $queue_cf->Id);
151
152 my $cf1 = RT::CustomField->new( RT->SystemUser );
153 $cf1->LoadByNameAndCatalog ( Name => "Queue CF" );
154
155 ok( (not $cf1->Id), "Queue CF not loaded with LoadByNameAndCatalog");
156
157 my $cf2 = RT::CustomField->new( RT->SystemUser );
158 $cf2->LoadByNameAndCatalog ( Name => "Height" );
159 ok( $cf2->Id, "Loaded CF id: " . $cf2->Id . " with name");
160 ok( $cf2->Name, "Loaded CF name: " . $cf2->Name . " with name");
161
162 my $cf3 = RT::CustomField->new( RT->SystemUser );
163 ($ok, $msg) = $cf3->LoadByNameAndCatalog ( Name => "Height", Catalog => $catalog->Name );
164 ok( (not $cf3->Id), "CF 'Height'"
165 . " not added to catalog: " . $catalog->Name);
166
167 my $color = create_cf( Name => 'Color' );
168 ok $color->Id, "Created CF " . $color->Name;
169 ($ok, $msg) = $color->AddToObject( $catalog );
170
171 ($ok, $msg) = $color->LoadByNameAndCatalog ( Name => "Color", Catalog => $catalog->Name );
172 ok( $color->Id, "Loaded CF id: " . $color->Id
173 . " for catalog: " . $catalog->Name);
174 ok( $color->Name, "Loaded CF name: " . $color->Name
175 . " for catalog: " . $catalog->Name);
176
177 }
178
179
180 done_testing;