| 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 => 26; |
|---|
| 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 | # Try without search parameters |
|---|
| 55 | my %ttvars = eval { |
|---|
| 56 | $guide->show_missing_metadata( return_tt_vars=> 1 ); |
|---|
| 57 | }; |
|---|
| 58 | my @nodes; |
|---|
| 59 | is( $@, "", "->show_missing_metadata doesn't die" ); |
|---|
| 60 | |
|---|
| 61 | is( scalar @{$ttvars{'nodes'}}, 0, "No nodes when no search params" ); |
|---|
| 62 | is( $ttvars{'done_search'}, 0, "Didn't search" ); |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | # Now try searching for those without lat |
|---|
| 66 | %ttvars = eval { |
|---|
| 67 | $guide->show_missing_metadata( |
|---|
| 68 | metadata_type => 'lat', |
|---|
| 69 | return_tt_vars => 1 |
|---|
| 70 | ); |
|---|
| 71 | }; |
|---|
| 72 | |
|---|
| 73 | @nodes = sort {$a->{'name'} cmp $b->{'name'}} @{$ttvars{'nodes'}}; |
|---|
| 74 | is( scalar @nodes, 2, "Two without / with empty lat" ); |
|---|
| 75 | is( $ttvars{'done_search'}, 1, "Did search" ); |
|---|
| 76 | is( $nodes[0]->{'name'}, "Test Page", "Right nodes" ); |
|---|
| 77 | is( $nodes[1]->{'name'}, "Test Page 3", "Right nodes" ); |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | # Now try searching for those without lat=22.22 |
|---|
| 81 | %ttvars = eval { |
|---|
| 82 | $guide->show_missing_metadata( |
|---|
| 83 | metadata_type => 'lat', |
|---|
| 84 | metadata_value => '22.22', |
|---|
| 85 | return_tt_vars => 1 |
|---|
| 86 | ); |
|---|
| 87 | }; |
|---|
| 88 | |
|---|
| 89 | @nodes = sort {$a->{'name'} cmp $b->{'name'}} @{$ttvars{'nodes'}}; |
|---|
| 90 | is( scalar @nodes, 4, "Four without that lat" ); |
|---|
| 91 | is( $ttvars{'done_search'}, 1, "Did search" ); |
|---|
| 92 | is( $nodes[0]->{'name'}, "Category Foo", "Right nodes" ); |
|---|
| 93 | is( $nodes[1]->{'name'}, "Locale Bar", "Right nodes" ); |
|---|
| 94 | is( $nodes[2]->{'name'}, "Test Page", "Right nodes" ); |
|---|
| 95 | is( $nodes[3]->{'name'}, "Test Page 3", "Right nodes" ); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | # Try again, but exclude locale and category |
|---|
| 99 | %ttvars = eval { |
|---|
| 100 | $guide->show_missing_metadata( |
|---|
| 101 | metadata_type => 'lat', |
|---|
| 102 | metadata_value => '22.22', |
|---|
| 103 | exclude_locales => 1, |
|---|
| 104 | exclude_categories => 2, |
|---|
| 105 | return_tt_vars => 1 |
|---|
| 106 | ); |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | @nodes = sort {$a->{'name'} cmp $b->{'name'}} @{$ttvars{'nodes'}}; |
|---|
| 110 | is( scalar @nodes, 2, "Two without that lat" ); |
|---|
| 111 | is( $ttvars{'done_search'}, 1, "Did search" ); |
|---|
| 112 | is( $nodes[0]->{'name'}, "Test Page", "Right nodes" ); |
|---|
| 113 | is( $nodes[1]->{'name'}, "Test Page 3", "Right nodes" ); |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | # Test the normal, HTML version |
|---|
| 117 | my $output = eval { |
|---|
| 118 | $guide->show_missing_metadata( return_output=>1 ); |
|---|
| 119 | }; |
|---|
| 120 | is( $@, "", "->how_missing_metadata doesn't die" ); |
|---|
| 121 | |
|---|
| 122 | like( $output, qr|Missing Metadata|, "Right page" ); |
|---|
| 123 | like( $output, qr|Metadata Type|, "Has prompts" ); |
|---|
| 124 | unlike( $output, qr|<h3>Pages</h3>|, "Didn't search" ); |
|---|
| 125 | |
|---|
| 126 | $output = eval { |
|---|
| 127 | $guide->show_missing_metadata( return_output=>1, metadata_type=>'lat' ); |
|---|
| 128 | }; |
|---|
| 129 | is( $@, "", "->how_missing_metadata doesn't die" ); |
|---|
| 130 | like( $output, qr|<h3>Pages</h3>|, "searched" ); |
|---|
| 131 | like( $output, qr|Test Page|, "had node" ); |
|---|