Bug 5327 shifting database dependent modules and scripts to t/db_dependent
[koha.git] / t / db_dependent / lib / KohaTest / Members / GetMemberDetails.pm
1 package KohaTest::Members::GetMemberDetails;
2 use base qw( KohaTest::Members );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8
9 use C4::Members;
10
11 sub testing_class { 'C4::Members' }
12
13 =head3 STARTUP METHODS
14
15 These are run once, before the main test methods in this module.
16
17 =head2 startup_create_detailed_borrower
18
19 Creates a new borrower to be used by the testing methods.  Also
20 populates the class hash with values to be compared from the database
21 retrieval.
22
23 =cut
24
25 sub startup_create_detailed_borrower : Test( startup => 2 ) {
26     my $self = shift;
27     my ( $description, $type, $amount, $user );
28
29     my $memberinfo = {
30         surname      => 'surname' . $self->random_string(),
31         firstname    => 'firstname' . $self->random_string(),
32         address      => 'address' . $self->random_string(),
33         city         => 'city' . $self->random_string(),
34         cardnumber   => 'card' . $self->random_string(),
35         branchcode   => 'CPL',
36         categorycode => 'B',
37         dateexpiry   => '2020-01-01',
38         password     => 'testpassword',
39         userid       => 'testuser',
40         flags        => '0',
41         dateofbirth  => $self->random_date(),
42     };
43
44     my $borrowernumber = AddMember( %$memberinfo );
45     ok( $borrowernumber, "created member: $borrowernumber" );
46     $self->{detail_borrowernumber} = $borrowernumber;
47     $self->{detail_cardnumber}     = $memberinfo->{cardnumber};
48
49     #values for adding a record to accounts
50     $description = 'Test account';
51     $type        = 'M';
52     $amount      = 5.00;
53     $user        = '';
54
55     my $acct_added =
56       C4::Accounts::manualinvoice( $borrowernumber, undef, $description, $type, $amount,
57         $user );
58
59     ok( $acct_added == 0, 'added account for borrower' );
60
61     $self->{amountoutstanding} = $amount;
62
63     return;
64 }
65
66 =head2 TESTING METHODS
67
68 =head3 borrower_detail_get
69
70 Tests the functionality of the GetMemberDetails method in C4::Members.
71 Validates the join on categories table works as well as the extra fields
72 the method gets from outside of either the borrowers and categories table like
73 amountoutstanding and user flags.
74
75 =cut
76
77 sub borrower_detail_get : Test( 8 ) {
78     my $self = shift;
79
80     ok( $self->{detail_borrowernumber},
81         'we have a valid detailed borrower to test with' );
82
83     my $details = C4::Members::GetMemberDetails( $self->{detail_borrowernumber} );
84     ok( $details, 'we successfully called GetMemberDetails' );
85     ok( exists $details->{categorycode},
86         'member details has a "categorycode" attribute' );
87     ok( $details->{categorycode}, '...and it is set to something' );
88
89     ok( exists $details->{category_type}, "categories in the join returned values" );
90
91     ok( $details->{category_type}, '...and category_type is valid' );
92
93     ok( $details->{amountoutstanding}, 'an amountoutstanding exists' );
94     is( $details->{amountoutstanding},
95         $self->{amountoutstanding},
96         '...and matches inserted account record'
97     );
98
99 }
100
101 =head3 cardnumber_detail_get
102
103 This method tests the capability of GetMemberDetails to search on cardnumber.  There doesn't seem to be any
104 current calls to GetMemberDetail using cardnumber though, so this test may not be necessary.
105
106 =cut
107
108 sub cardnumber_detail_get : Test( 8 ) {
109     my $self = shift;
110
111     ok( $self->{detail_cardnumber},
112         "we have a valid detailed borrower to test with $self->{detail_cardnumber}" );
113
114     my $details = C4::Members::GetMemberDetails( undef, $self->{detail_cardnumber} );
115     ok( $details, 'we successfully called GetMemberDetails' );
116     ok( exists $details->{categorycode},
117         "member details has a 'categorycode' attribute $details->{categorycode}" );
118     ok( $details->{categorycode}, '...and it is set to something' );
119
120     ok( exists $details->{category_type}, "categories in the join returned values" );
121
122     ok( $details->{category_type}, '...and category_type is valid' );
123
124 #FIXME These 2 methods will fail as borrowernumber is not set in GetMemberDetails when cardnumber is used instead.
125 #ok( $details->{amountoutstanding}, 'an amountoutstanding exists' );
126 #is( $details->{amountoutstanding}, $self->{amountoutstanding}, '...and matches inserted account record' );
127 }
128
129 =head2 SHUTDOWN METHDOS
130
131 These get run once, after the main test methods in this module.
132
133 =head3 shutdown_remove_new_borrower
134
135 Removes references in the Class to the new borrower created
136 in the startup methods.
137
138 =cut
139
140 sub shutdown_remove_new_borrower : Test( shutdown => 0 ) {
141     my $self = shift;
142
143     delete $self->{detail_borrowernumber};
144     delete $self->{detail_cardnumber};
145     delete $self->{amountoutstanding};
146
147     return;
148 }
149
150 1;