import rt 3.8.8
[freeside.git] / rt / lib / t / create_data.pl
1 #!@PERL@ -w
2 use strict;
3
4 use Test::More qw/no_plan/;
5 use Text::Lorem;
6 use RT;
7 RT::LoadConfig;
8 RT::Init;
9
10 #### Generate some number of RT accounts.  Come up with random
11 #### usernames if requested, otherwise use provided ones.  Take
12 #### $subdomain argument so that we can generate customer accounts,
13 #### etc.  Group memberships should also be provided.
14
15 =head2 create_users
16
17 =over 4
18
19 This subroutine creates a number of RT users, if they don't already
20 exist, and places them in the specified group.  It also creates the
21 group if it needs to.  Returns a ref to a list containing the user
22 objects.
23
24 If a list of names is specified, users with those names are created.
25 Otherwise, it will make names up, checking to be sure that a user with
26 the random name does not yet exist.  Each user will have an email
27 address in "example.com".
28
29 Takes a hash of the following arguments:
30 number => How many users to create.  Default is 1.
31 names => A ref to a list of usernames to use.  Optional.
32 subdomain => The subdomain of example.com which should be used for
33     email addresses.
34 group => The name of the group these users should belong to.  Creates
35     the group if it does not yet exist.
36 privileged => Whether the users should be able to be granted rights.
37     Default is 1.
38 attributes => a ref to a list of hashrefs containing the arguments for 
39     any unsupported attribute we should add to the user (for example, a 
40     user saved search.)
41
42 =back
43
44 =cut
45
46 sub create_users {
47     my %ARGS = (number => 1,
48                 subdomain => undef,
49                 privileged => 1,
50                 @_);
51     my $lorem = Text::Lorem->new();
52     my @users_returned;
53
54     my @usernames;
55     my $anon;
56     if ($ARGS{'users'}) {
57         @usernames = @{$ARGS{'users'}};
58         $anon = 0;
59     } else {
60         @usernames = split(/\s+/, $lorem->words($ARGS{'number'}));
61         $anon = 1;
62     }
63
64     my $domain = 'example.com';
65     $domain = $ARGS{'subdomain'} . ".$domain" if $ARGS{'subdomain'};
66
67     foreach my $user (@usernames) {
68         my $user_obj = RT::User->new($RT::SystemUser);
69         $user_obj->Load($user);
70         if ($user_obj->Id() && !$anon) {
71             # Use this user; assume we know what we're doing.  Don't
72             # modify it, other than adding it to any group specified.
73             push(@users_returned, $user_obj);
74         } elsif ($user_obj->Id()) {
75             # Oops.  Get a different username and stick it on the back
76             # of the list.
77             append(@users, $lorem->words(1));
78         } else {
79             $user_obj->Create(Name => $user,
80                               Password => $user."pass",
81                               EmailAddress => $user.'@'.$domain,
82                               RealName => "$user ipsum",
83                               Privileged => $ARGS{'privileged'},
84                               );
85             push(@users_returned, $user_obj);
86         }
87     }
88
89     # Now we have our list of users.  Did we have groups to add them
90     # to?
91
92     if ($ARGS{'groups'}) {
93         my @groups = @{$ARGS{'groups'}};
94         foreach my $group (@groups) {
95             my $group_obj = RT::Group->new();
96             $group_obj->LoadUserDefinedGroup($group);
97             unless ($group_obj->Id()) {
98                 # Create it.
99                 $group_obj->CreateUserDefinedGroup(
100                                 Name => $group,
101                                 Description => "lorem defined group $group",
102                                                    );
103             }
104             foreach (@users_returned) {
105                 $group_obj->AddMember($_->Id);
106             }
107         }
108     }
109
110     # Do we have attributes to apply to the users?
111     if ($ARGS{'attributes'}) {
112         foreach my $attrib (@{$ARGS{'attributes'}}) {
113             my %attr_args = %{$attrib};
114             foreach (@users_returned) {
115                 $_->AddAttribute(%attr_args);
116             }
117         }
118     }
119
120     # Return our list of users.
121     return \@users_returned;
122 }
123
124 #### Generate any RT groups.  These ought to be named, by function.
125 #### The group names should be given either as part of user creation,
126 #### or as a name with a number of subgroups which should be members.
127
128
129 #### Generate some queues.  Users/groups who have permissions on
130 #### queues need to be specified on this point.  Permissions can be
131 #### specified by role, e.g. "client" or "staffmember" or "admin" for
132 #### each queue.  If the queue should have anything special like a
133 #### custom field, say so here.
134
135
136 #### Generate some tickets and transactions.