<commit_before>
use strict;
use warnings;
use Test::More qw | no_plan |;
use lib 't/lib';
#use SGN::Test;
use SGN::Test::WWW::Mechanize;
use CXGN::DB::Connection;
use Carp qw | verbose |;

my $mech = SGN::Test::WWW::Mechanize->new;
$mech->get_ok("http://localhost:3000/contact/form", "Check the form page");

#Tests the general form
&check_basic_parts_are_ok($mech);
my @categories = ("name", "email", "subject");
foreach my $category (@categories)
{
   #$mech->has_tag("input", qr/value="".*?name="$category"/sx, 
   #                  "The form has $category as an input.");
   $mech->content_contains("Your $category", "Check if category was added");
   $mech->has_tag("input", "", "The form has $category as an input.");
}
$mech->has_tag("textarea", qr/name="body"/,
			"The form has textarea to put message.");

#Tests the form if someone has an account, but no email
#my $dbh = CXGN::DB::Connection->new();
#if( my $u_id = CXGN::People::Person->get_person_by_username( $dbh, "testusername" ) ) {
#    CXGN::People::Person->new( $dbh, $u_id )->hard_delete;
#}
#my $p = CXGN::People::Person->new($dbh);
#$p->set_first_name("testfirstname");
#$p->set_last_name("testlastname");
#$p->store();
#$dbh->commit();

#Tests for a user
my %user;
$user{first_name} = "Test";
$user{last_name} = "Tester";
$mech->while_logged_in(\%user, 
sub {
    &check_basic_parts_are_ok($mech);
    $mech->has_tag("input", 
		    qr/value="TestTester".*?name="name"/sx, 
                     "If user logs in, name will go on the form."); 
});
$user{email} = '<EMAIL>';
$mech->while_logged_in(\%user, 
sub {
   &check_basic_parts_are_ok($mech);
   $mech->has_tag("input", 
		    qr/value='<EMAIL>'.*?name="name"/sx, 
                     "If user logs in, email will go on the form."); 
});
$user{first_name} = "";
$user{last_name} = "";
$mech->while_logged_in(\%user,
sub {
   &check_basic_parts_are_ok($mech);
   $mech->has_tag("input", 
		    qr/value="".*?name=""/sx, 
                     "If user logs in, name will go on the form."); 
});

my $form_name = "contactForm";
my %entry_for_field = ('name' => '<NAME>', 
			 'email' => '<EMAIL>', 
			   'subject' => 'Testing',
			      'body' => 'Hello World');
my @fields = keys %entry_for_field;
print "Sending complete form from form page\n";
&send_complete_form_and_check($mech, $form_name, %entry_for_field);
$mech->back();
print "Sending empty form from form page\n";
for (my $i = 0; $i < 2; $i++)
{  
   print "Sending empty form from submit page\n" if $i == 1;
   &send_blank_form_and_check($mech, $form_name, @fields);
}
print "Sending full form from submit page\n";
&send_complete_form_and_check($mech, $form_name, %entry_for_field);
$mech->back();
$mech->back();
foreach my $field (@fields)
{
   my $filledEntry = {$field => $entry_for_field{$field}};
   my $testDesc = "Form with $field filled in sent. ";
   &test_currently_filled_and_oppositely_filled_forms($mech, $form_name, 
			$field, $filledEntry, $testDesc, %entry_for_field);
   $mech->back();
   $mech->back();
   if ($field ne 'name')
   {
      $$filledEntry{'name'} = $entry_for_field{$field};
      &test_currently_filled_and_oppositely_filled_forms($mech, $form_name, 
			$field, $filledEntry, $testDesc, %entry_for_field);
      delete $$filledEntry{'name'};
      $mech->back();
      $mech->back();
   }
   delete $$filledEntry{$field};
}

sub check_basic_parts_are_ok
{
   my $mech = shift;
   $mech->get_ok("/contact/form", "Check the form page");
   $mech->html_lint_ok("Check form page html");
   $mech->page_links_ok('Check all links');
   $mech->text_contains("Contact", "Title is there");
}

sub send_complete_form_and_check
{
   my ($mech, $form_name, %entry_for_field) = @_;
   $mech->submit_form_ok({'name'=>$form_name, 'fields'=>\%entry_for_field}, 
			     "Check submitting; filled out form was sent.");<commit_msg>New Contact Page-Updated test again<commit_after>
use strict;
use warnings;
use Test::More qw | no_plan |;
use lib 't/lib';
#use SGN::Test;
use SGN::Test::WWW::Mechanize;
use CXGN::DB::Connection;
use Carp qw | verbose |;

my $mech = SGN::Test::WWW::Mechanize->new;

#Tests the general form
&check_basic_parts_are_ok($mech);
my @categories = ("name", "email", "subject");
foreach my $category (@categories)
{
   #$mech->has_tag("input", qr/value="".*?name="$category"/sx, 
   #                  "The form has $category as an input.");
   $mech->content_contains("Your $category", "Check if category was added");
   $mech->has_tag("input", "", "The form has $category as an input.");
}
$mech->has_tag("textarea", qr/name="body"/,
			"The form has textarea to put message.");

#Tests the form if someone has an account, but no email
#my $dbh = CXGN::DB::Connection->new();
#if( my $u_id = CXGN::People::Person->get_person_by_username( $dbh, "testusername" ) ) {
#    CXGN::People::Person->new( $dbh, $u_id )->hard_delete;
#}
#my $p = CXGN::People::Person->new($dbh);
#$p->set_first_name("testfirstname");
#$p->set_last_name("testlastname");
#$p->store();
#$dbh->commit();

#Tests for a user
my %user;
$user{first_name} = "Test";
$user{last_name} = "Tester";
$mech->while_logged_in(\%user, 
sub {
    &check_basic_parts_are_ok($mech);
    $mech->has_tag("input", 
		    qr/value="TestTester".*?name="name"/sx, 
                     "Check if user logs in, name will go on form."); 
});
$user{email} = '<EMAIL>';
$mech->while_logged_in(\%user, 
sub {
   &check_basic_parts_are_ok($mech);
   $mech->has_tag("input", 
		    qr/value='<EMAIL>'.*?name="name"/sx, 
                     "Check if user logs in, email will go on the form."); 
});
$user{first_name} = "";
$user{last_name} = "";
$mech->while_logged_in(\%user,
sub {
   &check_basic_parts_are_ok($mech);
   $mech->has_tag("input", 
		    qr/value="".*?name=""/sx, 
                     "Check if user logs in and does not have name, name won't go on."); 
});

my $form_name = "contactForm";
my %entry_for_field = ('name' => '<NAME>', 
			 'email' => '<EMAIL>', 
			   'subject' => 'Testing',
			      'body' => 'Hello World');
my @fields = keys %entry_for_field;
print "Sending complete form from form page\n";
&send_complete_form_and_check($mech, $form_name, %entry_for_field);
$mech->back();
print "Sending empty form from form page\n";
for (my $i = 0; $i < 2; $i++)
{  
   print "Sending empty form from submit page\n" if $i == 1;
   &send_blank_form_and_check($mech, $form_name, @fields);
}
print "Sending full form from submit page\n";
&send_complete_form_and_check($mech, $form_name, %entry_for_field);
$mech->back();
$mech->back();
foreach my $field (@fields)
{
   my $filledEntry = {$field => $entry_for_field{$field}};
   my $testDesc = "Form with $field filled in sent ";
   &test_currently_filled_and_oppositely_filled_forms($mech, $form_name, 
			$field, $filledEntry, $testDesc, %entry_for_field);
   $mech->back();
   $mech->back();
   if ($field ne 'name')
   {
      $$filledEntry{'name'} = $entry_for_field{$field};
      my $testDesc = "Form with name and $field filled in sent ";
      &test_currently_filled_and_oppositely_filled_forms($mech, $form_name, 
			$field, $filledEntry, $testDesc, %entry_for_field);
      delete $$filledEntry{'name'};
      $mech->back();
      $mech->back();
   }
   delete $$filledEntry{$field};
}

sub check_basic_parts_are_ok
{
   my $mech = shift;
   $mech->get_ok("/contact/form", "Check the form page");
   $mech->html_lint_ok("Check form page html");
   #$mech->page_links_ok('Check all links');
   $mech->text_contains("Contact", "Title is there");
}

sub send_complete_form_and_check
{
   my ($mech, $form_name, %entry_for_field) = @_;
   $mech->submit_form_ok({'name'=>$form_name, 'fields'=>\%entry_for_field}, 
			     "Check submitting; filled out form was sent.");
{'fraction_non_alphanumeric': 0.18875502008032127, 'fraction_numerical': 0.0012955045990413266, 'mean_word_length': 4.302197802197802, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '480135', 'n_tokens_mistral': 2956, 'n_tokens_neox': 2843, 'n_words': 639}