bug 2284: ModMember can erase the dateofbirth field
[koha.git] / t / lib / KohaTest.pm
index 8ff9fd0..720017a 100644 (file)
@@ -19,12 +19,28 @@ use C4::Search;
 use C4::Installer;
 use C4::Languages;
 use File::Temp qw/ tempdir /;
+use CGI;
 
 # Since this is an abstract base class, this prevents these tests from
 # being run directly unless we're testing a subclass. It just makes
 # things faster.
 __PACKAGE__->SKIP_CLASS( 1 );
 
+INIT {
+    if ($ENV{SINGLE_TEST}) {
+        # if we're running the tests in one
+        # or more test files specified via
+        #
+        #   make single-test TEST_FILES=lib/KohaTest/Foo.pm
+        #
+        # use this INIT trick taken from the POD for
+        # Test::Class::Load.
+        start_zebrasrv();
+        Test::Class->runtests;
+        stop_zebrasrv();
+    }
+}
+
 use Attribute::Handlers;
 
 =head2 Expensive test method attribute
@@ -269,6 +285,32 @@ sub startup_22_add_bookfund : Test(startup => 2) {
     return;
 }
 
+=head2 startup_24_add_branch
+
+=cut
+
+sub startup_24_add_branch : Test(startup => 1) {
+    my $self = shift;
+
+    my $branch_info = {
+        add            => 1,
+        branchcode     => $self->random_string(3),
+        branchname     => $self->random_string(),
+        branchaddress1 => $self->random_string(),
+        branchaddress2 => $self->random_string(),
+        branchaddress3 => $self->random_string(),
+        branchphone    => $self->random_phone(),
+        branchfax      => $self->random_phone(),
+        brancemail     => $self->random_email(),
+        branchip       => $self->random_ip(),
+        branchprinter  => $self->random_string(),
+      };
+    C4::Branch::ModBranch($branch_info);
+    $self->{'branchcode'} = $branch_info->{'branchcode'};
+    ok( $self->{'branchcode'}, "created branch: $self->{'branchcode'}" );
+
+}
+
 =head2 startup_24_add_member
 
 Add a patron/member for the tests to use
@@ -287,6 +329,7 @@ sub startup_24_add_member : Test(startup => 1) {
                        categorycode => 'PT',  # PT  => PaTron
                        dateexpiry   => '2010-01-01',
                        password     => 'testpassword',
+                       dateofbirth  => $self->random_date(),
                   };
 
     my $borrowernumber = AddMember( %$memberinfo );
@@ -361,7 +404,7 @@ like arbitrary.
 sub random_string {
     my $self = shift;
 
-    my $wordsize = 6;  # how many letters in your string?
+    my $wordsize = shift || 6;  # how many letters in your string?
 
     # leave out these characters: "oOlL10". They're too confusing.
     my @alphabet = ( 'a'..'k','m','n','p'..'z', 'A'..'K','M','N','P'..'Z', 2..9 );
@@ -374,6 +417,64 @@ sub random_string {
     
 }
 
+=head3 random_phone
+
+generates a random phone number. Currently, it's not actually random. It's an unusable US phone number
+
+=cut
+
+sub random_phone {
+    my $self = shift;
+
+    return '212-555-5555';
+    
+}
+
+=head3 random_email
+
+generates a random email address. They're all in the unusable
+'example.com' domain that is designed for this purpose.
+
+=cut
+
+sub random_email {
+    my $self = shift;
+
+    return $self->random_string() . '@example.com';
+    
+}
+
+=head3 random_ip
+
+returns an IP address suitable for testing purposes.
+
+=cut
+
+sub random_ip {
+    my $self = shift;
+
+    return '127.0.0.2';
+    
+}
+
+=head3 random_date
+
+returns a somewhat random date in the iso (yyyy-mm-dd) format.
+
+=cut
+
+sub random_date {
+    my $self = shift;
+
+    my $year  = 1800 + int( rand(300) );    # 1800 - 2199
+    my $month = 1 + int( rand(12) );        # 1 - 12
+    my $day   = 1 + int( rand(28) );        # 1 - 28
+                                            # stop at the 28th to keep us from generating February 31st and such.
+
+    return sprintf( '%04d-%02d-%02d', $year, $month, $day );
+
+}
+
 =head3 add_biblios
 
   $self->add_biblios( count     => 10,