| 1 | use strict; |
|---|
| 2 | use Wiki::Toolkit::Setup::SQLite; |
|---|
| 3 | use OpenGuides; |
|---|
| 4 | use OpenGuides::Test; |
|---|
| 5 | use Test::More; |
|---|
| 6 | |
|---|
| 7 | eval { require DBD::SQLite; }; |
|---|
| 8 | |
|---|
| 9 | if ( $@ ) { |
|---|
| 10 | my ($error) = $@ =~ /^(.*?)\n/; |
|---|
| 11 | plan skip_all => "DBD::SQLite could not be used - no database to test with ($error)"; |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | plan tests => 14; |
|---|
| 15 | |
|---|
| 16 | Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 17 | my $config = OpenGuides::Test->make_basic_config; |
|---|
| 18 | $config->script_name( "wiki.cgi" ); |
|---|
| 19 | $config->script_url( "http://example.com/" ); |
|---|
| 20 | my $guide = OpenGuides->new( config => $config ); |
|---|
| 21 | isa_ok( $guide, "OpenGuides" ); |
|---|
| 22 | my $wiki = $guide->wiki; |
|---|
| 23 | isa_ok( $wiki, "Wiki::Toolkit" ); |
|---|
| 24 | |
|---|
| 25 | # Clear out the database from any previous runs. |
|---|
| 26 | foreach my $del_node ( $wiki->list_all_nodes ) { |
|---|
| 27 | print "# Deleting node $del_node\n"; |
|---|
| 28 | $wiki->delete_node( $del_node ) or die "Can't delete $del_node"; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | # Add 3 different pages, one of which with two versions |
|---|
| 33 | $wiki->write_node( "Test Page", "foo", undef, |
|---|
| 34 | { category => "Alpha", lat=>"" } ) |
|---|
| 35 | or die "Couldn't write node"; |
|---|
| 36 | $wiki->write_node( "Test Page 2", "foo2", undef, |
|---|
| 37 | { category => "Alpha", lat=>"22.22" } ) |
|---|
| 38 | or die "Couldn't write node"; |
|---|
| 39 | $wiki->write_node( "Test Page 3", "foo33", undef, |
|---|
| 40 | { category => "Alpha" } ) |
|---|
| 41 | or die "Couldn't write node"; |
|---|
| 42 | $wiki->write_node( "Category Foo", "foo", undef, |
|---|
| 43 | { category => "Categories", lat=>"-8.77" } ) |
|---|
| 44 | or die "Couldn't write category"; |
|---|
| 45 | $wiki->write_node( "Locale Bar", "foo", undef, |
|---|
| 46 | { category => "Locales", lat=>"8.22" } ) |
|---|
| 47 | or die "Couldn't write locale"; |
|---|
| 48 | my %data = $wiki->retrieve_node( "Locale Bar" ); |
|---|
| 49 | $wiki->write_node( "Locale Bar", "foo version 2", $data{checksum}, |
|---|
| 50 | { category => "Locales", lat=>"8.88" } ) |
|---|
| 51 | or die "Couldn't write locale for the 2nd time"; |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | # First up, try with no password |
|---|
| 55 | my $output = $guide->set_node_moderation( |
|---|
| 56 | id => "Test Page 3", |
|---|
| 57 | moderation_flag => 0, |
|---|
| 58 | return_output => 1 |
|---|
| 59 | ); |
|---|
| 60 | like($output, qr|Change moderation status|, "Confirm page"); |
|---|
| 61 | like($output, qr|Confirm Moderation|, "Confirm page"); |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | # Now, try with the wrong password |
|---|
| 65 | $output = $guide->set_node_moderation( |
|---|
| 66 | id => "Test Page 3", |
|---|
| 67 | moderation_flag => 0, |
|---|
| 68 | password => "I_AM_WRONG", |
|---|
| 69 | return_output => 1 |
|---|
| 70 | ); |
|---|
| 71 | like($output, qr|Incorrect Password|, "Wrong password"); |
|---|
| 72 | like($output, qr|Incorrect password for page moderation|, "Wrong password"); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | # Check that "Test Page 3" doesn't have moderation set |
|---|
| 76 | my %node = $wiki->retrieve_node("Test Page 3"); |
|---|
| 77 | is($node{'node_requires_moderation'}, 0, "Doesn't have moderation on by default"); |
|---|
| 78 | |
|---|
| 79 | # Set the moderation flag on it to off |
|---|
| 80 | $guide->set_node_moderation( |
|---|
| 81 | id => "Test Page 3", |
|---|
| 82 | moderation_flag => 0, |
|---|
| 83 | password => $guide->config->admin_pass |
|---|
| 84 | ); |
|---|
| 85 | %node = $wiki->retrieve_node("Test Page 3"); |
|---|
| 86 | is($node{'node_requires_moderation'}, 0, "Doesn't have moderation set when called with 0"); |
|---|
| 87 | |
|---|
| 88 | # Set it to on |
|---|
| 89 | $guide->set_node_moderation( |
|---|
| 90 | id => "Test Page 3", |
|---|
| 91 | moderation_flag => 1, |
|---|
| 92 | password => $guide->config->admin_pass |
|---|
| 93 | ); |
|---|
| 94 | %node = $wiki->retrieve_node("Test Page 3"); |
|---|
| 95 | is($node{'node_requires_moderation'}, 1, "Turned on properly"); |
|---|
| 96 | |
|---|
| 97 | # Set it back to off |
|---|
| 98 | $guide->set_node_moderation( |
|---|
| 99 | id => "Test Page 3", |
|---|
| 100 | moderation_flag => 0, |
|---|
| 101 | password => $guide->config->admin_pass |
|---|
| 102 | ); |
|---|
| 103 | %node = $wiki->retrieve_node("Test Page 3"); |
|---|
| 104 | is($node{'node_requires_moderation'}, 0, "Turned off properly"); |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | # Test we were sent to the right place |
|---|
| 108 | $output = $guide->set_node_moderation( |
|---|
| 109 | id => "Test Page 3", |
|---|
| 110 | moderation_flag => 0, |
|---|
| 111 | password => $guide->config->admin_pass, |
|---|
| 112 | return_output => 1 |
|---|
| 113 | ); |
|---|
| 114 | like($output, qr|Location: http://example.com/wiki.cgi\?action=admin;moderation=changed|, "Right location"); |
|---|
| 115 | like($output, qr|Status: 302|, "Right status"); |
|---|
| 116 | |
|---|
| 117 | # And again, but this time with a made up node |
|---|
| 118 | $output = $guide->set_node_moderation( |
|---|
| 119 | id => "THIS PAGE DOES NOT EXIST", |
|---|
| 120 | moderation_flag => 0, |
|---|
| 121 | password => $guide->config->admin_pass, |
|---|
| 122 | return_output => 1 |
|---|
| 123 | ); |
|---|
| 124 | like($output, qr|Location: http://example.com/wiki.cgi\?action=admin;moderation=unknown_node|, "Right location"); |
|---|
| 125 | like($output, qr|Status: 302|, "Right status"); |
|---|