more robust testing platform, #37340
[freeside.git] / FS / t / suite / WRITING
1 WRITING TESTS
2
3 Load the test database (kept in FS-Test/share/test.sql for now). This has
4 a large set of customers in a known initial state.  You can login through
5 the web interface as "admin"/"admin" to examine the state of things and plan
6 your test.
7
8 The test scripts now have access to BOTH sides of the web interface, so you
9 can create an object through the UI and then examine its internal
10 properties, etc.
11
12   use Test::More tests => 1;
13   use FS::Test;
14   my $FS = FS::Test->new;
15
16 $FS has qsearch and qsearchs methods for finding objects directly. You can
17 do anything with those objects that Freeside backend code could normally do.
18 For example, this will bill a customer:
19
20   my $cust = $FS->qsearchs('cust_main', { custnum => 52 });
21   my $error = $cust->bill;
22
23 TESTING UI INTERACTION
24
25 To fetch a page from the UI, use the post() method:
26
27   $FS->post('/view/cust_main.cgi?52');
28   ok( $FS->error eq '', 'fetched customer view' ) or diag($FS->error);
29   ok( $FS->page =~ /Customer, New/, 'customer is named "Customer, New"' );
30
31 To simulate a user filling in and submitting a form, first fetch the form,
32 and select it by name:
33
34   $FS->post('/edit/svc_acct.cgi?98');
35   my $form = $FS->form('OneTrueForm');
36
37 then fill it in and submit it:
38
39   $form->value('clear_password', '1234abcd');
40   $FS->post($form);
41
42 and examine the result:
43
44   my $svc_acct = $FS->qsearch('svc_acct', { svcnum => 98 });
45   ok( $svc_acct->_password eq '1234abcd', 'password was changed' );
46
47 TESTING UI FLOW (EDIT/PROCESS/VIEW SEQUENCE)
48
49 Forms for editing records will post to a processing page. $FS->post($form)
50 handles this. The processing page will usually redirect back to the view
51 page on success, and back to the edit form with an error on failure.
52 Determine which kind of redirect it is. If it's a redirect to the edit form,
53 you need to follow it to report the error.
54
55   if ( $FS->redirect =~ m[^/view/svc_acct.cgi] ) {
56
57     pass('redirected to view page');
58
59   } elsif ( $FS->redirect =~ m[^/edit/svc_acct.cgi] ) {
60
61     fail('redirected back to edit form');
62     $FS->post($FS->redirect);
63     diag($FS->error);
64
65   } else {
66
67     fail('unsure what happened');
68     diag($FS->page);
69
70   }
71
72 RUNNING TESTS AT A SPECIFIC DATE
73
74 Important for testing package billing. Test::MockTime provides the
75 set_fixed_time() function, which will freeze the time returned by the time()
76 function at a specific value. I recommend giving it a unix timestamp rather
77 than a date string to avoid any confusion about time zones.
78
79 Note that FS::Cron::bill and some other parts of the system look at the $^T
80 variable (the time that the current program started running). You can
81 override that by just assigning to the variable.
82
83 Customers in the test database are billed up through Mar 1 2016. This will
84 bill a customer for the next month after that:
85
86   use Test::MockTime qw(set_fixed_time);
87   use Date::Parse qw(str2time);
88
89   my $cust = $FS->qsearchs('cust_main', { custnum => 52 });
90   set_fixed_time( str2time('2016-04-01') );
91   $cust->bill;
92
93