Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / t / suite / 07-pkg_change_location.t
1 #!/usr/bin/perl
2
3 =head2 DESCRIPTION
4
5 Test scheduling a package location change through the UI, then billing
6 on the day of the scheduled change.
7
8 =cut
9
10 use Test::More tests => 6;
11 use FS::Test;
12 use Date::Parse 'str2time';
13 use Date::Format 'time2str';
14 use Test::MockTime qw(set_fixed_time);
15 use FS::cust_pkg;
16 my $FS = FS::Test->new;
17 my $error;
18
19 # set up a customer with an active package
20 my $cust = $FS->new_customer('Future location change');
21 $error = $cust->insert;
22 my $pkg = FS::cust_pkg->new({pkgpart => 2});
23 $error ||= $cust->order_pkg({ cust_pkg => $pkg });
24 my $date = str2time('2016-04-01');
25 set_fixed_time($date);
26 $error ||= $cust->bill_and_collect;
27 BAIL_OUT($error) if $error;
28
29 # get the form
30 my %args = ( pkgnum  => $pkg->pkgnum,
31              pkgpart => $pkg->pkgpart,
32              locationnum => -1);
33 $FS->post('/misc/change_pkg.cgi', %args);
34 my $form = $FS->form('OrderPkgForm');
35
36 # Schedule the package change two days from now.
37 $date += 86400*2;
38 my $date_str = time2str('%x', $date);
39
40 my %params = (
41   start_date              => $date_str,
42   delay                   => 1,
43   address1                => int(rand(1000)) . ' Changed Street',
44   city                    => 'New City',
45   state                   => 'CA',
46   zip                     => '90001',
47   country                 => 'US',
48 );
49
50 diag "requesting location change to $params{address1}";
51
52 foreach (keys %params) {
53   $form->value($_, $params{$_});
54 }
55 $FS->post($form);
56 ok( $FS->error eq '' , 'form posted' );
57 if ( ok( $FS->page =~ m[location.reload], 'location change accepted' )) {
58   #nothing
59 } else {
60   $FS->post($FS->redirect);
61   BAIL_OUT( $FS->error);
62 }
63 # check that the package change is set
64 $pkg = $pkg->replace_old;
65 my $new_pkgnum = $pkg->change_to_pkgnum;
66 ok( $new_pkgnum, 'package change is scheduled' );
67
68 # run it and check that the package change happened
69 diag("billing customer on $date_str");
70 set_fixed_time($date);
71 my $error = $cust->bill_and_collect;
72 BAIL_OUT($error) if $error;
73
74 $pkg = $pkg->replace_old;
75 ok($pkg->get('cancel'), "old package is canceled");
76 my $new_pkg = $FS->qsearchs('cust_pkg', { pkgnum => $new_pkgnum });
77 ok($new_pkg->setup, "new package is active");
78 ok($new_pkg->cust_location->address1 eq $params{'address1'}, "new location is correct")
79   or diag $new_pkg->cust_location->address1;
80
81 1;
82