| 1 | use strict; |
|---|
| 2 | use Wiki::Toolkit::TestLib; |
|---|
| 3 | use Test::More; |
|---|
| 4 | |
|---|
| 5 | if ( scalar @Wiki::Toolkit::TestLib::wiki_info == 0 ) { |
|---|
| 6 | plan skip_all => "no backends configured"; |
|---|
| 7 | } else { |
|---|
| 8 | plan tests => ( 28 * scalar @Wiki::Toolkit::TestLib::wiki_info ); |
|---|
| 9 | } |
|---|
| 10 | |
|---|
| 11 | my $iterator = Wiki::Toolkit::TestLib->new_wiki_maker; |
|---|
| 12 | |
|---|
| 13 | while ( my $wiki = $iterator->new_wiki ) { |
|---|
| 14 | # Put some test data in. |
|---|
| 15 | ##### Test recent_changes. We'll write these nodes, in this order, |
|---|
| 16 | ##### with this metadata, sleeping for at least a second in between |
|---|
| 17 | ##### writes, and then run the tests. |
|---|
| 18 | ##### |
|---|
| 19 | ##### Node1 (Kake, minor tidying) |
|---|
| 20 | ##### Everyone's Favourite Hobby (nou) |
|---|
| 21 | ##### Another Node (nou) |
|---|
| 22 | |
|---|
| 23 | my $start_time = time; |
|---|
| 24 | do_sleep(); |
|---|
| 25 | my $node = "Node1"; |
|---|
| 26 | my %node_data = $wiki->retrieve_node( $node ); |
|---|
| 27 | $wiki->write_node( $node, "data", @node_data{checksum} , |
|---|
| 28 | { |
|---|
| 29 | username => "Kake", |
|---|
| 30 | edit_type => "Minor tidying", |
|---|
| 31 | comment => "Test", |
|---|
| 32 | } |
|---|
| 33 | ); |
|---|
| 34 | do_sleep(); |
|---|
| 35 | $node = "Everyone's Favourite Hobby"; |
|---|
| 36 | %node_data = $wiki->retrieve_node( $node ); |
|---|
| 37 | $wiki->write_node( $node, "hobby data", @node_data{checksum}, |
|---|
| 38 | { |
|---|
| 39 | comment => "Test", |
|---|
| 40 | edit_type => "Normal edit", |
|---|
| 41 | } |
|---|
| 42 | ); |
|---|
| 43 | do_sleep(); |
|---|
| 44 | $node = "Another Node"; |
|---|
| 45 | %node_data = $wiki->retrieve_node( $node ); |
|---|
| 46 | $wiki->write_node( $node, "another node 1", @node_data{checksum}, |
|---|
| 47 | { |
|---|
| 48 | username => "nou", |
|---|
| 49 | comment => "Test", |
|---|
| 50 | edit_type => "Normal edit", |
|---|
| 51 | } |
|---|
| 52 | ); |
|---|
| 53 | |
|---|
| 54 | ##### |
|---|
| 55 | ##### Ready to run the tests now. |
|---|
| 56 | ##### |
|---|
| 57 | |
|---|
| 58 | # Test by "in last n days". |
|---|
| 59 | my @nodes = $wiki->list_recent_changes( days => 1 ); |
|---|
| 60 | my @nodenames = map { $_->{name} } @nodes; |
|---|
| 61 | my %unique = map { $_ => 1 } @nodenames; |
|---|
| 62 | is_deeply( [sort keys %unique], |
|---|
| 63 | ["Another Node", "Everyone's Favourite Hobby", "Node1"], |
|---|
| 64 | "recent_changes for last 1 day gets the right results" ); |
|---|
| 65 | |
|---|
| 66 | is_deeply( \@nodenames, |
|---|
| 67 | ["Another Node", "Everyone's Favourite Hobby", "Node1"], |
|---|
| 68 | "...in the right order" ); # returns in reverse chron. order |
|---|
| 69 | |
|---|
| 70 | foreach my $node ( @nodes ) { |
|---|
| 71 | is( ref $node->{metadata}{comment}, "ARRAY", |
|---|
| 72 | "...metadata is returned as a hash of array refs" ); |
|---|
| 73 | my @comments = @{$node->{metadata}{comment}}; |
|---|
| 74 | is( $comments[0], "Test", "...correct metadata is returned" ); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | # Test by "last n nodes changed". |
|---|
| 78 | @nodes = $wiki->list_recent_changes( last_n_changes => 2 ); |
|---|
| 79 | @nodenames = map { $_->{name} } @nodes; |
|---|
| 80 | print "# Found nodes: " . join(" ", @nodenames) . "\n"; |
|---|
| 81 | is_deeply( \@nodenames, |
|---|
| 82 | ["Another Node", "Everyone's Favourite Hobby"], |
|---|
| 83 | "recent_changes 'last_n_changes' works" ); |
|---|
| 84 | eval { $wiki->list_recent_changes( last_n_changes => "foo" ); }; |
|---|
| 85 | ok( $@, "...and croaks on bad input" ); |
|---|
| 86 | |
|---|
| 87 | # Test by "since time T". |
|---|
| 88 | @nodes = $wiki->list_recent_changes( since => $start_time ); |
|---|
| 89 | @nodenames = sort map { $_->{name} } @nodes; |
|---|
| 90 | is_deeply( \@nodenames, |
|---|
| 91 | ["Another Node", "Everyone's Favourite Hobby", "Node1"], |
|---|
| 92 | "recent_changes 'since' returns the right results" ); |
|---|
| 93 | ok( $nodes[0]{last_modified}, |
|---|
| 94 | "...and a plausible (not undef or empty) last_modified timestamp"); |
|---|
| 95 | |
|---|
| 96 | # Test metadata_is. (We only actually expect a single result.) |
|---|
| 97 | @nodes = $wiki->list_recent_changes( |
|---|
| 98 | last_n_changes => 2, |
|---|
| 99 | metadata_is => { username => "Kake" } |
|---|
| 100 | ); |
|---|
| 101 | |
|---|
| 102 | @nodenames = map { $_->{name} } @nodes; |
|---|
| 103 | print "# Found nodes: " . join(" ", @nodenames) . "\n"; |
|---|
| 104 | is( scalar @nodes, 1, "metadata_is does constrain the search" ); |
|---|
| 105 | is( $nodes[0]{name}, "Node1", "...correctly" ); |
|---|
| 106 | |
|---|
| 107 | # Test metadata_isnt. |
|---|
| 108 | @nodes = $wiki->list_recent_changes( |
|---|
| 109 | last_n_changes => 1, |
|---|
| 110 | metadata_isnt => { username => "Kake" } |
|---|
| 111 | ); |
|---|
| 112 | is( scalar @nodes, 1, "metadata_isnt, too" ); |
|---|
| 113 | is( $nodes[0]{name}, "Another Node", "...correctly" ); |
|---|
| 114 | print "# " . join(" ", map { $_->{name} } @nodes) . "\n"; |
|---|
| 115 | |
|---|
| 116 | @nodes = $wiki->list_recent_changes( |
|---|
| 117 | last_n_changes => 1, |
|---|
| 118 | metadata_isnt => { edit_type => "Minor tidying" } |
|---|
| 119 | ); |
|---|
| 120 | @nodenames = map { $_->{name} } @nodes; |
|---|
| 121 | print "# Found nodes: " . join(" ", @nodenames) . "\n"; |
|---|
| 122 | is( scalar @nodes, 1, |
|---|
| 123 | "metadata_isnt includes nodes where this metadata type isn't set" ); |
|---|
| 124 | is( $nodes[0]{name}, "Another Node", "...correctly" ); |
|---|
| 125 | |
|---|
| 126 | eval { @nodes = $wiki->list_recent_changes( |
|---|
| 127 | last_n_changes => 1, |
|---|
| 128 | metadata_isnt => { arthropod => "millipede" } |
|---|
| 129 | ); |
|---|
| 130 | }; |
|---|
| 131 | is( $@, "", |
|---|
| 132 | "list_recent_changes doesn't die when metadata_isnt doesn't omit anything" ); |
|---|
| 133 | |
|---|
| 134 | ##### |
|---|
| 135 | ##### Write to Another Node again for testing metadata_was and the |
|---|
| 136 | ##### effect of the presence and absence of include_all_changes |
|---|
| 137 | ##### |
|---|
| 138 | |
|---|
| 139 | do_sleep(); |
|---|
| 140 | $node = "Another Node"; |
|---|
| 141 | %node_data = $wiki->retrieve_node( $node ); |
|---|
| 142 | $wiki->write_node( $node, "another node 2", @node_data{checksum}, |
|---|
| 143 | { |
|---|
| 144 | username => "Kake", |
|---|
| 145 | comment => "Kake writes the node that nou wrote", |
|---|
| 146 | edit_type => "Minor tidying", |
|---|
| 147 | } |
|---|
| 148 | ); |
|---|
| 149 | |
|---|
| 150 | @nodes = $wiki->list_recent_changes( days => 1 ); |
|---|
| 151 | @nodenames = map { $_->{name} } @nodes; |
|---|
| 152 | print "# Found nodes: " . join(" ", @nodenames) . "\n"; |
|---|
| 153 | is( scalar @nodes, 3, |
|---|
| 154 | "By default each node returned only once however many times changed" ); |
|---|
| 155 | |
|---|
| 156 | @nodes = $wiki->list_recent_changes( days => 1, include_all_changes => 1 ); |
|---|
| 157 | is( scalar @nodes, 4, |
|---|
| 158 | "...returned more than once when 'include_all_changes' set" ); |
|---|
| 159 | |
|---|
| 160 | @nodes = $wiki->list_recent_changes( |
|---|
| 161 | last_n_changes => 5, |
|---|
| 162 | metadata_was => { username => "nou" } |
|---|
| 163 | ); |
|---|
| 164 | @nodenames = map { $_->{name} } @nodes; |
|---|
| 165 | print "# Found nodes: " . join(" ", @nodenames) . "\n"; |
|---|
| 166 | is( scalar @nodes, 1, |
|---|
| 167 | "metadata_was returns nodes whose current version doesn't match" ); |
|---|
| 168 | is( $nodes[0]{name}, "Another Node", "...correctly" ); |
|---|
| 169 | is( $nodes[0]{version}, 1, "...and the correct version" ); |
|---|
| 170 | |
|---|
| 171 | ##### Testing metadata_wasnt - Everyone's Favourite Hobby and |
|---|
| 172 | ##### Another Node were both written as *not* minor edits, but |
|---|
| 173 | ##### then a minor edit was made on Another Node. We expect |
|---|
| 174 | ##### metadata_wasnt to still return Another Node though. |
|---|
| 175 | |
|---|
| 176 | @nodes = $wiki->list_recent_changes( |
|---|
| 177 | last_n_changes => 5, |
|---|
| 178 | metadata_wasnt => { |
|---|
| 179 | edit_type => "Minor tidying", |
|---|
| 180 | }, |
|---|
| 181 | ); |
|---|
| 182 | @nodenames = map { $_->{name} } @nodes; |
|---|
| 183 | print "# Found nodes: " . join(" ", @nodenames) . "\n"; |
|---|
| 184 | is( scalar @nodes, 2, |
|---|
| 185 | "metadata_wasnt returns nodes whose current version matches" ); |
|---|
| 186 | |
|---|
| 187 | # lets add yet another normal edit to check proper display of multple normal edits |
|---|
| 188 | do_sleep(); |
|---|
| 189 | $node = "Another Node"; |
|---|
| 190 | %node_data = $wiki->retrieve_node( $node ); |
|---|
| 191 | $wiki->write_node( $node, "another node 3", @node_data{checksum}, |
|---|
| 192 | { |
|---|
| 193 | username => "bob", |
|---|
| 194 | comment => "bob writes the node that nou and Kake aready wrote", |
|---|
| 195 | edit_type => "Normal edit", |
|---|
| 196 | } |
|---|
| 197 | ); |
|---|
| 198 | |
|---|
| 199 | @nodenames = map { $_->{name} } @nodes; |
|---|
| 200 | print "# Found nodes: " . join(" ", @nodenames) . "\n"; |
|---|
| 201 | @nodes = $wiki->list_recent_changes( days => 1 ); |
|---|
| 202 | is( scalar @nodes, 3, |
|---|
| 203 | "By default each node returned only once however many times changed" ); |
|---|
| 204 | @nodes = $wiki->list_recent_changes( days => 1, include_all_changes => 1 ); |
|---|
| 205 | is( scalar @nodes, 5, |
|---|
| 206 | "...returned more than once when 'include_all_changes' set" ); |
|---|
| 207 | @nodes = $wiki->list_recent_changes( |
|---|
| 208 | days => 1, |
|---|
| 209 | metadata_was => { |
|---|
| 210 | edit_type => "Normal edit", |
|---|
| 211 | }, |
|---|
| 212 | ); |
|---|
| 213 | @nodenames = map { $_->{name} } @nodes; |
|---|
| 214 | print "# Found nodes: " . join(" ", @nodenames) . "\n"; |
|---|
| 215 | |
|---|
| 216 | TODO: { |
|---|
| 217 | local $TODO = 'http://www.wiki-toolkit.org/ticket/41'; |
|---|
| 218 | is( scalar @nodes, 2, |
|---|
| 219 | "metadata_was returns nodes whose current version matches" ); |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | sub do_sleep { |
|---|
| 224 | my $slept = sleep(2); |
|---|
| 225 | warn "Slept for less than a second, test results may be unreliable" |
|---|
| 226 | unless $slept >= 1; |
|---|
| 227 | } |
|---|