#!/usr/bin/perl # Copyright 2025 Koha Development team # # This file is part of Koha # # Koha is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # Koha is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Koha; if not, see use Modern::Perl; use Test::More tests => 7; use Test::NoWarnings; use Test::Exception; use Test::Warn; BEGIN { use_ok('C4::Scrubber'); } subtest 'new() constructor tests' => sub { plan tests => 8; my $scrubber; lives_ok { $scrubber = C4::Scrubber->new() } 'Constructor with no parameters succeeds'; isa_ok( $scrubber, 'HTML::Scrubber', 'Constructor returns HTML::Scrubber object' ); lives_ok { $scrubber = C4::Scrubber->new('default') } 'Constructor with default type succeeds'; isa_ok( $scrubber, 'HTML::Scrubber', 'Constructor with default type returns HTML::Scrubber object' ); lives_ok { $scrubber = C4::Scrubber->new('comment') } 'Constructor with comment type succeeds'; isa_ok( $scrubber, 'HTML::Scrubber', 'Constructor with comment type returns HTML::Scrubber object' ); lives_ok { $scrubber = C4::Scrubber->new('note') } 'Constructor with note type succeeds'; isa_ok( $scrubber, 'HTML::Scrubber', 'Constructor with note type returns HTML::Scrubber object' ); }; subtest 'constructor error handling' => sub { plan tests => 5; my $scrubber; lives_ok { $scrubber = C4::Scrubber->new(undef) } 'Constructor with undef type succeeds (treated as default)'; isa_ok( $scrubber, 'HTML::Scrubber', 'Constructor with undef type returns HTML::Scrubber object' ); throws_ok { C4::Scrubber->new(''); } qr/New called with unrecognized type/, 'Constructor throws exception for empty string type'; throws_ok { C4::Scrubber->new('invalid_type'); } qr/New called with unrecognized type/, 'Constructor throws exception for invalid type'; throws_ok { C4::Scrubber->new('Client'); } qr/New called with unrecognized type/, 'Constructor throws exception for Client type'; }; subtest 'default scrubber functionality' => sub { plan tests => 5; my $scrubber = C4::Scrubber->new('default'); my $malicious_html = q| Click me

Paragraph

Content
Plain text content |; my $result = $scrubber->scrub($malicious_html); unlike( $result, qr/'; my $result = $scrubber->scrub($test_html); like( $result, qr/Bold<\/b>/, 'Bold tags are preserved' ); like( $result, qr/Italic<\/i>/, 'Italic tags are preserved' ); like( $result, qr/Emphasis<\/em>/, 'Em tags are preserved' ); like( $result, qr/Big<\/big>/, 'Big tags are preserved' ); like( $result, qr/Small<\/small>/, 'Small tags are preserved' ); like( $result, qr/Strong<\/strong>/, 'Strong tags are preserved' ); like( $result, qr/
/, 'Break tags are preserved' ); unlike( $result, qr/

/, 'Paragraph tags are removed' ); unlike( $result, qr//, 'Span tags are removed' ); unlike( $result, qr/'; my $safe_result = $scrubber->scrub($malicious_note); like( $safe_result, qr/

Safe content<\/p>/, 'Safe content is preserved' ); unlike( $safe_result, qr/