| 1 | use strict; |
|---|
| 2 | use Wiki::Toolkit::Setup::SQLite; |
|---|
| 3 | use OpenGuides::Config; |
|---|
| 4 | use OpenGuides; |
|---|
| 5 | use OpenGuides::Feed; |
|---|
| 6 | use OpenGuides::Utils; |
|---|
| 7 | use OpenGuides::Test; |
|---|
| 8 | use Test::More; |
|---|
| 9 | |
|---|
| 10 | eval { require DBD::SQLite; }; |
|---|
| 11 | if ( $@ ) { |
|---|
| 12 | my ($error) = $@ =~ /^(.*?)\n/; |
|---|
| 13 | plan skip_all => "DBD::SQLite could not be used - no database to test with. ($error)"; |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | eval { require Wiki::Toolkit::Search::Plucene; }; |
|---|
| 17 | if ( $@ ) { |
|---|
| 18 | plan skip_all => "Plucene not installed"; |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | plan tests => 7; |
|---|
| 23 | |
|---|
| 24 | # Clear out the database from any previous runs. |
|---|
| 25 | unlink "t/node.db"; |
|---|
| 26 | unlink <t/indexes/*>; |
|---|
| 27 | |
|---|
| 28 | Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 29 | my $config = OpenGuides::Config->new( |
|---|
| 30 | vars => { |
|---|
| 31 | dbtype => "sqlite", |
|---|
| 32 | dbname => "t/node.db", |
|---|
| 33 | indexing_directory => "t/indexes", |
|---|
| 34 | script_name => "wiki.cgi", |
|---|
| 35 | script_url => "http://example.com/", |
|---|
| 36 | site_name => "Test Site", |
|---|
| 37 | template_path => "./templates", |
|---|
| 38 | home_name => "Home", |
|---|
| 39 | use_plucene => 1 |
|---|
| 40 | } |
|---|
| 41 | ); |
|---|
| 42 | |
|---|
| 43 | # Basic sanity check first. |
|---|
| 44 | my $wiki = OpenGuides::Utils->make_wiki_object( config => $config ); |
|---|
| 45 | |
|---|
| 46 | my $feed = OpenGuides::Feed->new( wiki => $wiki, |
|---|
| 47 | config => $config ); |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | # Write the first version |
|---|
| 51 | my $guide = OpenGuides->new( config => $config ); |
|---|
| 52 | |
|---|
| 53 | # Set up CGI parameters ready for a node write. |
|---|
| 54 | my $q = OpenGuides::Test->make_cgi_object( |
|---|
| 55 | content => "foo", |
|---|
| 56 | username => "bob", |
|---|
| 57 | comment => "foo", |
|---|
| 58 | node_image => "image", |
|---|
| 59 | edit_type => "Minor tidying" |
|---|
| 60 | ); |
|---|
| 61 | |
|---|
| 62 | my $output = $guide->commit_node( |
|---|
| 63 | return_output => 1, |
|---|
| 64 | id => "Wombats", |
|---|
| 65 | cgi_obj => $q, |
|---|
| 66 | ); |
|---|
| 67 | |
|---|
| 68 | # Check we have it |
|---|
| 69 | ok( $wiki->node_exists( "Wombats" ), "Wombats written" ); |
|---|
| 70 | |
|---|
| 71 | my %node = $wiki->retrieve_node("Wombats"); |
|---|
| 72 | is( $node{version}, 1, "First version" ); |
|---|
| 73 | is( $node{metadata}->{edit_type}[0], "Minor tidying", "Right edit type" ); |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | # Now write a second version of it |
|---|
| 77 | $q->param( -name => "edit_type", -value => "Normal edit" ); |
|---|
| 78 | $q->param( -name => "checksum", -value => $node{checksum} ); |
|---|
| 79 | $output = $guide->commit_node( |
|---|
| 80 | return_output => 1, |
|---|
| 81 | id => "Wombats", |
|---|
| 82 | cgi_obj => $q, |
|---|
| 83 | ); |
|---|
| 84 | |
|---|
| 85 | # Check it's as expected |
|---|
| 86 | %node = $wiki->retrieve_node("Wombats"); |
|---|
| 87 | is( $node{version}, 2, "First version" ); |
|---|
| 88 | is( $node{metadata}->{edit_type}[0], "Normal edit", "Right edit type" ); |
|---|
| 89 | |
|---|
| 90 | # Now try to commit some invalid data, and make sure we get an edit form back |
|---|
| 91 | $q = OpenGuides::Test->make_cgi_object( |
|---|
| 92 | content => "foo", |
|---|
| 93 | os_x => "fooooo", |
|---|
| 94 | username => "bob", |
|---|
| 95 | comment => "foo", |
|---|
| 96 | node_image => "image", |
|---|
| 97 | edit_type => "Minor tidying" |
|---|
| 98 | ); |
|---|
| 99 | |
|---|
| 100 | $output = $guide->commit_node( |
|---|
| 101 | return_output => 1, |
|---|
| 102 | id => "Wombats again", |
|---|
| 103 | cgi_obj => $q, |
|---|
| 104 | ); |
|---|
| 105 | |
|---|
| 106 | like( $output, qr/Your input was invalid/, |
|---|
| 107 | "Edit form displayed and invalid input message shown if invalid input" ); |
|---|
| 108 | |
|---|
| 109 | like( $output, qr/os_x must be integer/, |
|---|
| 110 | "Edit form displayed and os_x integer message displayed" ); |
|---|