|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::More tests => 4; |
| 21 |
|
| 22 |
use C4::Context; |
| 23 |
use C4::Installer qw( trigger_exists ); |
| 24 |
|
| 25 |
my $dbh = C4::Context->dbh; |
| 26 |
|
| 27 |
# Start transaction for cleanup |
| 28 |
$dbh->begin_work; |
| 29 |
|
| 30 |
subtest 'trigger_exists function availability' => sub { |
| 31 |
plan tests => 1; |
| 32 |
|
| 33 |
can_ok( 'C4::Installer', 'trigger_exists' ); |
| 34 |
}; |
| 35 |
|
| 36 |
subtest 'trigger_exists with non-existent trigger' => sub { |
| 37 |
plan tests => 1; |
| 38 |
|
| 39 |
my $exists = trigger_exists('non_existent_trigger_name_12345'); |
| 40 |
is( $exists, 0, 'Non-existent trigger returns 0' ); |
| 41 |
}; |
| 42 |
|
| 43 |
subtest 'trigger_exists with existing trigger' => sub { |
| 44 |
plan tests => 3; |
| 45 |
|
| 46 |
# Create a test trigger |
| 47 |
my $test_trigger_name = 'test_trigger_' . time(); |
| 48 |
|
| 49 |
# First verify it doesn't exist |
| 50 |
my $exists_before = trigger_exists($test_trigger_name); |
| 51 |
is( $exists_before, 0, 'Test trigger does not exist initially' ); |
| 52 |
|
| 53 |
# Create the trigger (using a simple trigger that does nothing) |
| 54 |
$dbh->do( |
| 55 |
qq{ |
| 56 |
CREATE TRIGGER $test_trigger_name |
| 57 |
BEFORE INSERT ON borrowers |
| 58 |
FOR EACH ROW |
| 59 |
BEGIN |
| 60 |
-- This is a test trigger that does nothing |
| 61 |
SET NEW.borrowernumber = NEW.borrowernumber; |
| 62 |
END |
| 63 |
} |
| 64 |
); |
| 65 |
|
| 66 |
# Now verify it exists |
| 67 |
my $exists_after = trigger_exists($test_trigger_name); |
| 68 |
is( $exists_after, 1, 'Test trigger exists after creation' ); |
| 69 |
|
| 70 |
# Clean up - drop the trigger |
| 71 |
$dbh->do("DROP TRIGGER IF EXISTS $test_trigger_name"); |
| 72 |
|
| 73 |
# Verify it's gone |
| 74 |
my $exists_after_drop = trigger_exists($test_trigger_name); |
| 75 |
is( $exists_after_drop, 0, 'Test trigger does not exist after dropping' ); |
| 76 |
}; |
| 77 |
|
| 78 |
subtest 'trigger_exists parameter validation' => sub { |
| 79 |
plan tests => 2; |
| 80 |
|
| 81 |
# Test with undefined parameter |
| 82 |
my $exists_undef = trigger_exists(undef); |
| 83 |
is( $exists_undef, 0, 'trigger_exists with undef parameter returns 0' ); |
| 84 |
|
| 85 |
# Test with empty string |
| 86 |
my $exists_empty = trigger_exists(''); |
| 87 |
is( $exists_empty, 0, 'trigger_exists with empty string returns 0' ); |
| 88 |
}; |
| 89 |
|
| 90 |
# Rollback transaction to clean up any test data |
| 91 |
$dbh->rollback; |