<commit_before>#!/usr/bin/perl -w

use lib '.';
use test_setup;

use Test::More 'no_plan';
use Test::Exception;
use MIME::Lite;
use Test::Output qw(stdout_from);
use Error;
use strict;
use CGI;

# Miscellaneous tests of the saliweb::frontend class

my $exitvalue;

BEGIN {
    # Make sure we can catch the result from exit()
    *CORE::GLOBAL::exit = sub { $exitvalue = $_[0] };
    use_ok('saliweb::frontend');
    require Dummy;
}

# Test help_link method
{
    my $cgi = new CGI;
    my $cls = {CGI=>$cgi};
    my $link = saliweb::frontend::help_link($cls, 'mytarget');

    is($link, '<a class="helplink" onclick="launchHelp(\'help.cgi?style=' .
              'helplink&amp;type=help#mytarget\'); return false;" ' .
              'href="help.cgi?style=helplink&amp;type=help#mytarget">' .
              '<img class="helplink" src="/saliweb/img/help.jpg" ' .
              'alt="help" /></a>' . "\n", "check help_link");
}

# Test _admin_email method
{
    my $default = '<EMAIL>';
    my $self = {};
    bless($self, 'saliweb::frontend');
    is($self->_admin_email, $default, 'admin_email (default 1)');
    $self->{config} = {};
    is($self->_admin_email, $default, '            (default 2)');
    $self->{config}->{general} = {};
    is($self->_admin_email, $default, '            (default 3)');
    $self->{config}->{general}->{admin_email} = "myemail";
    is($self->_admin_email, "myemail", '            (config)');
}

# Test set_page_title method
{
    my $self = {server_name=>'myserver'};
    bless($self, 'saliweb::frontend');
    $self->set_page_title("test page");
    is($self->{page_title}, "myserver test page", 'set_page_title');
}

# Test start_html method
{
    my $self = {CGI=>new CGI, page_title=>'test page title'};
    bless($self, 'saliweb::frontend');
    like($self->start_html(),
         '/<!DOCTYPE html.*<head>.*<title>test page title<\/title>' .
         '.*<link rel="stylesheet" type="text\/css" ' .
         'href="\/saliweb\/css\/server.css" \/>' .
         '.*<script src="\/saliweb\/js\/salilab\.js".*<\/head>' .
         '.*<body onload=/s', "start_html (default style)");
    like($self->start_html('mystyle.css'),
         '/<!DOCTYPE html.*<head>.*<title>test page title<\/title>' .
         '.*<link rel="stylesheet".*href="mystyle.css"' .
         '.*<script src="\/saliweb\/js\/salilab\.js".*<\/head>' .
         '.*<body onload=/s', "           (mystyle)");
}

# Test simple accessors
{
    my $user_info = {email=>'myemail'};
    my $self = {CGI=>'mycgi', htmlroot=>'myhtmlroot', cgiroot=>'mycgiroot',
                user_info=>$user_info};
    bless($self, 'saliweb::frontend');
    is($self->cgi, 'mycgi', 'saliweb::frontend accessors: cgi');
    is($self->htmlroot, 'myhtmlroot', '                             htmlroot');
    is($self->cgiroot, 'mycgiroot', '                             cgiroot');
    is($self->email, 'myemail', '                             email');
    undef $self->{user_info};
    is($self->email, undef, '                             undef email');
}

# Test URL methods
{
    my $self = {};
    bless($self, 'saliweb::frontend');
    is($self->index_url, '.', 'saliweb::frontend URL (index)');
    is($self->submit_url, 'submit.cgi', '                      (submit)');
    is($self->queue_url, 'queue.cgi', '                      (queue)');
    is($self->help_url, 'help.cgi?type=help', '                      (help)');
    is($self->news_url, 'help.cgi?type=news', '                      (news)');
    is($self->contact_url, 'help.cgi?type=contact',
       '                      (contact)');
    is($self->results_url, 'results.cgi', '                      (results)');
}


# Test setup_cgi method
{
    my $self = {};
    bless($self, 'saliweb::frontend');
    my $q = $self->_setup_cgi();
    isa_ok($q, 'CGI', 'CGI object');

    # Force failure of creation of CGI object
    $CGI::POST_MAX = 1;
    $ENV{'CONTENT_LENGTH'} = 1024;
    my $out = stdout_from { $self->_setup_cgi() };

    # Reset to defaults
    $CGI::POST_MAX = -1;
    undef $ENV{'CONTENT_LENGTH'};

    is($exitvalue, 0, 'setup_cgi with bad input should call exit()');
    $exitvalue = undef;
    like($out, '/Status: 413 Request entity too large.*' .
               '<!DOCTYPE html.*<html.*<head>.*<title>CGI error.*' .
               '<body>.*<h2>.*CGI error.*413 Request entity too large.*' .
               '<\/html>/s',
         '                         should return a 413 HTML page');
}

# Test end_html method
{
    my $self = {CGI=>new CGI};
    bless($self, 'saliweb::frontend');
    like($self->end_html(), qr/<\/body>.*<\/html>/s, "end_html");
}
<commit_msg>Make tests work with older CGI.pm.<commit_after>#!/usr/bin/perl -w

use lib '.';
use test_setup;

use Test::More 'no_plan';
use Test::Exception;
use MIME::Lite;
use Test::Output qw(stdout_from);
use Error;
use File::Temp;
use strict;
use CGI;

# Miscellaneous tests of the saliweb::frontend class

# Redirect STDIN to a temporary file so older versions of CGI.pm
# have somewhere to read data from
my $stdin_fh = File::Temp->new();
print $stdin_fh "1234567890";
$stdin_fh->close();
open(STDIN, "<", $stdin_fh->filename) or die "can't open tempfile: $!";

my $exitvalue;

BEGIN {
    # Make sure we can catch the result from exit()
    *CORE::GLOBAL::exit = sub { $exitvalue = $_[0] };
    use_ok('saliweb::frontend');
    require Dummy;
}

# Test help_link method
{
    my $cgi = new CGI;
    my $cls = {CGI=>$cgi};
    my $link = saliweb::frontend::help_link($cls, 'mytarget');

    is($link, '<a class="helplink" onclick="launchHelp(\'help.cgi?style=' .
              'helplink&amp;type=help#mytarget\'); return false;" ' .
              'href="help.cgi?style=helplink&amp;type=help#mytarget">' .
              '<img class="helplink" src="/saliweb/img/help.jpg" ' .
              'alt="help" /></a>' . "\n", "check help_link");
}

# Test _admin_email method
{
    my $default = '<EMAIL>';
    my $self = {};
    bless($self, 'saliweb::frontend');
    is($self->_admin_email, $default, 'admin_email (default 1)');
    $self->{config} = {};
    is($self->_admin_email, $default, '            (default 2)');
    $self->{config}->{general} = {};
    is($self->_admin_email, $default, '            (default 3)');
    $self->{config}->{general}->{admin_email} = "myemail";
    is($self->_admin_email, "myemail", '            (config)');
}

# Test set_page_title method
{
    my $self = {server_name=>'myserver'};
    bless($self, 'saliweb::frontend');
    $self->set_page_title("test page");
    is($self->{page_title}, "myserver test page", 'set_page_title');
}

# Test start_html method
{
    my $self = {CGI=>new CGI, page_title=>'test page title'};
    bless($self, 'saliweb::frontend');
    like($self->start_html(),
         '/<!DOCTYPE html.*<head>.*<title>test page title<\/title>' .
         '.*<link rel="stylesheet" type="text\/css" ' .
         'href="\/saliweb\/css\/server.css" \/>' .
         '.*<script src="\/saliweb\/js\/salilab\.js".*<\/head>' .
         '.*<body onload=/s', "start_html (default style)");
    like($self->start_html('mystyle.css'),
         '/<!DOCTYPE html.*<head>.*<title>test page title<\/title>' .
         '.*<link rel="stylesheet".*href="mystyle.css"' .
         '.*<script src="\/saliweb\/js\/salilab\.js".*<\/head>' .
         '.*<body onload=/s', "           (mystyle)");
}

# Test simple accessors
{
    my $user_info = {email=>'myemail'};
    my $self = {CGI=>'mycgi', htmlroot=>'myhtmlroot', cgiroot=>'mycgiroot',
                user_info=>$user_info};
    bless($self, 'saliweb::frontend');
    is($self->cgi, 'mycgi', 'saliweb::frontend accessors: cgi');
    is($self->htmlroot, 'myhtmlroot', '                             htmlroot');
    is($self->cgiroot, 'mycgiroot', '                             cgiroot');
    is($self->email, 'myemail', '                             email');
    undef $self->{user_info};
    is($self->email, undef, '                             undef email');
}

# Test URL methods
{
    my $self = {};
    bless($self, 'saliweb::frontend');
    is($self->index_url, '.', 'saliweb::frontend URL (index)');
    is($self->submit_url, 'submit.cgi', '                      (submit)');
    is($self->queue_url, 'queue.cgi', '                      (queue)');
    is($self->help_url, 'help.cgi?type=help', '                      (help)');
    is($self->news_url, 'help.cgi?type=news', '                      (news)');
    is($self->contact_url, 'help.cgi?type=contact',
       '                      (contact)');
    is($self->results_url, 'results.cgi', '                      (results)');
}


# Test setup_cgi method
{
    my $self = {};
    bless($self, 'saliweb::frontend');
    my $q = $self->_setup_cgi();
    isa_ok($q, 'CGI', 'CGI object');

    # Force failure of creation of CGI object
    $CGI::POST_MAX = 1;
    $ENV{'CONTENT_LENGTH'} = 6;
    my $out = stdout_from { $self->_setup_cgi() };

    # Reset to defaults
    $CGI::POST_MAX = -1;
    delete $ENV{'CONTENT_LENGTH'};

    is($exitvalue, 0, 'setup_cgi with bad input should call exit()');
    $exitvalue = undef;
    like($out, '/Status: 413 Request entity too large.*' .
               '<!DOCTYPE html.*<html.*<head>.*<title>CGI error.*' .
               '<body>.*<h2>.*CGI error.*413 Request entity too large.*' .
               '<\/html>/s',
         '                         should return a 413 HTML page');
}

# Test end_html method
{
    my $self = {CGI=>new CGI};
    bless($self, 'saliweb::frontend');
    like($self->end_html(), qr/<\/body>.*<\/html>/s, "end_html");
}

{'fraction_non_alphanumeric': 0.20335429769392033, 'fraction_numerical': 0.005136268343815514, 'mean_word_length': 2.5219638242894056, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 0, '>': 126, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4480402', 'n_tokens_mistral': 3419, 'n_tokens_neox': 3289, 'n_words': 652}