#!/usr/bin/perl

=pod

=head1 NAME

test.pl - a set of basic tests for Wala.pm

=head1 SYNOPSIS

Given a working installation and configuration file:

    ./test.pl

=head1 DESCRIPTION

This section to-come.

=head1 MISSING TESTS

These items aren't tested at all, at the moment.  A number of them aren't
particularly trivial to test.

    sub get_diff
    sub get_latest_diff_date
    sub write_page
    sub add_to_page
    sub log_page_edit
    sub parse_cookies
    sub set_cookies
    sub setup
    sub parse_parameters
    sub write_diff
    sub merge_diff

=head1 SEE ALSO

validate.pl in the WalaWiki distribution.

=cut

use strict;
use warnings;
use lib 'lib';
#use Test::HTML::W3C 'show_detail';
use Test::Simple tests => 11;

use Wala;


# TESTS

section ('configuration');

    our (%WalaConf, %DISPLAY_CONF);
    do 'p1k3.conf.pl';

    ok (%WalaConf, 'External configuration hash defined.');

    ok (my $w = Wala->new(%WalaConf), 'Got Wala object.');

    # This will turn off stuff that breaks testing.
    $w->TestMode(1);

section ('individual subroutine tests');

    my $username = $w->get_username;
    ok ($username eq 'Anonymous',
        "get_username returns $username.");

    ok (Wala::get_mtime('Wala.pm') =~ m/\d+/,
        'get_mtime returns digits.');

    ok (Wala::mygmtime(0) eq 'Thu, 01-Jan-1970 00:00:00 GMT',
        'mygmtime(0) returns epoch');

    # Hack.  Depends on ./log
    LOGFILE: {
      my $tmp = $w->LogFile;
      $w->LogFile("./log");
      ok ($w->recent_changes(1) =~ m/\d+ \w+ \w+.*$/,
          'recent_changes(1) returns an appropriate logfile line.');
      $w->LogFile($tmp);
    }

    ok ($w->spamcheck("http://spamcheckfunction.com", "TEST", "TEST") == 1,
        'spamcheck trips on a URL');

section ('starting link code testing');
  
    my $scriptname = $w->ScriptName;

    ok (
      $w->convert_wikiwords('TestWord')
        =~ m/<a href="$scriptname\?.*?TestWord"/,
      'Return link for TestWord'
    );

    ok (
      $w->convert_wikiwords('[TestWord|T Word]')
        =~ m/<a href="$scriptname\?.*?TestWord".*?>T Word</,
      'Return labeled link for TestWord'
    );

    ok (
      Wala::get_bracketed_link("[http://p1k3.com/ p1k3]")
      eq qq{<a href="http://p1k3.com/" class="external">p1k3</a>},
      'Return bracketed link'
    );

    # this isn't doing what I'd like it to.
    ok (
      Wala::pagelinks('PageLink [other page link]'),
      'Pagelinks sub finds ' . Wala::pagelinks('PageLink [other page link]') . ' links'
    );


# END OF TESTS


# Utility routines.

sub hash_print {
  my %hash = @_;
  my $string;
  for my $key (keys %hash) {
    $string .= "\n$key: $hash{$key}";
  }

  return $string;
}

sub pause {
  my $time = shift;
  #print "    pausing $time seconds\n";
  sleep $time;
}

sub section {
   my $msg = shift;
   print "\n-- $msg --\n\n";
}
