| 1 | use strict; |
|---|
| 2 | use Wiki::Toolkit::Setup::SQLite; |
|---|
| 3 | use OpenGuides; |
|---|
| 4 | use OpenGuides::Test; |
|---|
| 5 | use Test::More tests => 26; |
|---|
| 6 | |
|---|
| 7 | eval { require DBD::SQLite; }; |
|---|
| 8 | my $have_sqlite = $@ ? 0 : 1; |
|---|
| 9 | |
|---|
| 10 | SKIP: { |
|---|
| 11 | skip "DBD::SQLite not installed - no database to test with", 26 |
|---|
| 12 | unless $have_sqlite; |
|---|
| 13 | |
|---|
| 14 | Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 15 | my $config = OpenGuides::Test->make_basic_config; |
|---|
| 16 | $config->script_name( "wiki.cgi" ); |
|---|
| 17 | $config->script_url( "http://example.com/" ); |
|---|
| 18 | my $guide = OpenGuides->new( config => $config ); |
|---|
| 19 | isa_ok( $guide, "OpenGuides" ); |
|---|
| 20 | my $wiki = $guide->wiki; |
|---|
| 21 | isa_ok( $wiki, "Wiki::Toolkit" ); |
|---|
| 22 | |
|---|
| 23 | # Clear out the database from any previous runs. |
|---|
| 24 | foreach my $del_node ( $wiki->list_all_nodes ) { |
|---|
| 25 | print "# Deleting node $del_node\n"; |
|---|
| 26 | $wiki->delete_node( $del_node ) or die "Can't delete $del_node"; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | # Add 3 different pages, one of which with two versions |
|---|
| 31 | $wiki->write_node( "Test Page", "foo", undef, |
|---|
| 32 | { category => "Alpha", lat=>"" } ) |
|---|
| 33 | or die "Couldn't write node"; |
|---|
| 34 | $wiki->write_node( "Test Page 2", "foo2", undef, |
|---|
| 35 | { category => "Alpha", lat=>"22.22" } ) |
|---|
| 36 | or die "Couldn't write node"; |
|---|
| 37 | $wiki->write_node( "Test Page 3", "foo33", undef, |
|---|
| 38 | { category => "Alpha" } ) |
|---|
| 39 | or die "Couldn't write node"; |
|---|
| 40 | $wiki->write_node( "Category Foo", "foo", undef, |
|---|
| 41 | { category => "Categories", lat=>"-8.77" } ) |
|---|
| 42 | or die "Couldn't write category"; |
|---|
| 43 | $wiki->write_node( "Locale Bar", "foo", undef, |
|---|
| 44 | { category => "Locales", lat=>"8.22" } ) |
|---|
| 45 | or die "Couldn't write locale"; |
|---|
| 46 | my %data = $wiki->retrieve_node( "Locale Bar" ); |
|---|
| 47 | $wiki->write_node( "Locale Bar", "foo version 2", $data{checksum}, |
|---|
| 48 | { category => "Locales", lat=>"8.88" } ) |
|---|
| 49 | or die "Couldn't write locale for the 2nd time"; |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | # Try without search parameters |
|---|
| 53 | my %ttvars = eval { |
|---|
| 54 | $guide->show_missing_metadata( return_tt_vars=> 1 ); |
|---|
| 55 | }; |
|---|
| 56 | my @nodes; |
|---|
| 57 | is( $@, "", "->show_missing_metadata doesn't die" ); |
|---|
| 58 | |
|---|
| 59 | is( scalar @{$ttvars{'nodes'}}, 0, "No nodes when no search params" ); |
|---|
| 60 | is( $ttvars{'done_search'}, 0, "Didn't search" ); |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | # Now try searching for those without lat |
|---|
| 64 | %ttvars = eval { |
|---|
| 65 | $guide->show_missing_metadata( |
|---|
| 66 | metadata_type => 'lat', |
|---|
| 67 | return_tt_vars => 1 |
|---|
| 68 | ); |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | @nodes = sort {$a->{'name'} cmp $b->{'name'}} @{$ttvars{'nodes'}}; |
|---|
| 72 | is( scalar @nodes, 2, "Two without / with empty lat" ); |
|---|
| 73 | is( $ttvars{'done_search'}, 1, "Did search" ); |
|---|
| 74 | is( $nodes[0]->{'name'}, "Test Page", "Right nodes" ); |
|---|
| 75 | is( $nodes[1]->{'name'}, "Test Page 3", "Right nodes" ); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | # Now try searching for those without lat=22.22 |
|---|
| 79 | %ttvars = eval { |
|---|
| 80 | $guide->show_missing_metadata( |
|---|
| 81 | metadata_type => 'lat', |
|---|
| 82 | metadata_value => '22.22', |
|---|
| 83 | return_tt_vars => 1 |
|---|
| 84 | ); |
|---|
| 85 | }; |
|---|
| 86 | |
|---|
| 87 | @nodes = sort {$a->{'name'} cmp $b->{'name'}} @{$ttvars{'nodes'}}; |
|---|
| 88 | is( scalar @nodes, 4, "Four without that lat" ); |
|---|
| 89 | is( $ttvars{'done_search'}, 1, "Did search" ); |
|---|
| 90 | is( $nodes[0]->{'name'}, "Category Foo", "Right nodes" ); |
|---|
| 91 | is( $nodes[1]->{'name'}, "Locale Bar", "Right nodes" ); |
|---|
| 92 | is( $nodes[2]->{'name'}, "Test Page", "Right nodes" ); |
|---|
| 93 | is( $nodes[3]->{'name'}, "Test Page 3", "Right nodes" ); |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | # Try again, but exclude locale and category |
|---|
| 97 | %ttvars = eval { |
|---|
| 98 | $guide->show_missing_metadata( |
|---|
| 99 | metadata_type => 'lat', |
|---|
| 100 | metadata_value => '22.22', |
|---|
| 101 | exclude_locales => 1, |
|---|
| 102 | exclude_categories => 2, |
|---|
| 103 | return_tt_vars => 1 |
|---|
| 104 | ); |
|---|
| 105 | }; |
|---|
| 106 | |
|---|
| 107 | @nodes = sort {$a->{'name'} cmp $b->{'name'}} @{$ttvars{'nodes'}}; |
|---|
| 108 | is( scalar @nodes, 2, "Two without that lat" ); |
|---|
| 109 | is( $ttvars{'done_search'}, 1, "Did search" ); |
|---|
| 110 | is( $nodes[0]->{'name'}, "Test Page", "Right nodes" ); |
|---|
| 111 | is( $nodes[1]->{'name'}, "Test Page 3", "Right nodes" ); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | # Test the normal, HTML version |
|---|
| 115 | my $output = eval { |
|---|
| 116 | $guide->show_missing_metadata( return_output=>1 ); |
|---|
| 117 | }; |
|---|
| 118 | is( $@, "", "->how_missing_metadata doesn't die" ); |
|---|
| 119 | |
|---|
| 120 | like( $output, qr|Missing Metadata|, "Right page" ); |
|---|
| 121 | like( $output, qr|Metadata Type|, "Has prompts" ); |
|---|
| 122 | unlike( $output, qr|<h3>Pages</h3>|, "Didn't search" ); |
|---|
| 123 | |
|---|
| 124 | $output = eval { |
|---|
| 125 | $guide->show_missing_metadata( return_output=>1, metadata_type=>'lat' ); |
|---|
| 126 | }; |
|---|
| 127 | is( $@, "", "->how_missing_metadata doesn't die" ); |
|---|
| 128 | like( $output, qr|<h3>Pages</h3>|, "searched" ); |
|---|
| 129 | like( $output, qr|Test Page|, "had node" ); |
|---|
| 130 | } |
|---|