| 1 | use strict; |
|---|
| 2 | use OpenGuides; |
|---|
| 3 | use OpenGuides::Test; |
|---|
| 4 | use Test::More; |
|---|
| 5 | use Wiki::Toolkit::Setup::SQLite; |
|---|
| 6 | |
|---|
| 7 | eval { require DBD::SQLite; }; |
|---|
| 8 | if ( $@ ) { |
|---|
| 9 | my ($error) = $@ =~ /^(.*?)\n/; |
|---|
| 10 | plan skip_all => "DBD::SQLite could not be used - no database to test with ($error)"; |
|---|
| 11 | } |
|---|
| 12 | |
|---|
| 13 | eval { require Test::HTML::Content; }; |
|---|
| 14 | if ( $@ ) { |
|---|
| 15 | plan skip_all => "Test::HTML::Content not installed."; |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | plan tests => 30; |
|---|
| 19 | |
|---|
| 20 | my ( $config, $guide, $wiki ); |
|---|
| 21 | |
|---|
| 22 | # Clear out the database from any previous runs. |
|---|
| 23 | unlink "t/node.db"; |
|---|
| 24 | unlink <t/indexes/*>; |
|---|
| 25 | Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 26 | |
|---|
| 27 | # Make sure node image fields don't show up in edit form if config says |
|---|
| 28 | # they shouldn't. |
|---|
| 29 | $config = OpenGuides::Test->make_basic_config; |
|---|
| 30 | $config->enable_node_image( 0 ); |
|---|
| 31 | $guide = OpenGuides->new( config => $config ); |
|---|
| 32 | $wiki = $guide->wiki; |
|---|
| 33 | |
|---|
| 34 | my $output = $guide->display_edit_form( |
|---|
| 35 | id => "Red Lion", |
|---|
| 36 | return_output => 1, |
|---|
| 37 | ); |
|---|
| 38 | |
|---|
| 39 | # Strip Content-Type header to stop Test::HTML::Content getting confused. |
|---|
| 40 | $output =~ s/^Content-Type.*[\r\n]+//m; |
|---|
| 41 | |
|---|
| 42 | Test::HTML::Content::no_tag( $output, "input", { name => "node_image" }, |
|---|
| 43 | "node_image field not in edit form if config says it shouldn't be" ); |
|---|
| 44 | Test::HTML::Content::no_tag( $output, "input", |
|---|
| 45 | { name => "node_image_licence" }, |
|---|
| 46 | "...ditto node_image_licence" ); |
|---|
| 47 | Test::HTML::Content::no_tag( $output, "input", |
|---|
| 48 | { name => "node_image_copyright" }, |
|---|
| 49 | "...ditto node_image_copyright" ); |
|---|
| 50 | Test::HTML::Content::no_tag( $output, "input", |
|---|
| 51 | { name => "node_image_url" }, |
|---|
| 52 | "...ditto node_image_url" ); |
|---|
| 53 | |
|---|
| 54 | # And make sure they do if it says they should. |
|---|
| 55 | $config->enable_node_image( 1 ); |
|---|
| 56 | $guide = OpenGuides->new( config => $config ); |
|---|
| 57 | $wiki = $guide->wiki; |
|---|
| 58 | |
|---|
| 59 | $output = $guide->display_edit_form( |
|---|
| 60 | id => "Red Lion", |
|---|
| 61 | return_output => 1, |
|---|
| 62 | ); |
|---|
| 63 | |
|---|
| 64 | # Strip Content-Type header to stop Test::HTML::Content getting confused. |
|---|
| 65 | $output =~ s/^Content-Type.*[\r\n]+//m; |
|---|
| 66 | |
|---|
| 67 | Test::HTML::Content::tag_ok( $output, "input", { name => "node_image" }, |
|---|
| 68 | "node_image field appears in edit form if config says it should" ); |
|---|
| 69 | Test::HTML::Content::tag_ok( $output, "input", |
|---|
| 70 | { name => "node_image_licence" }, |
|---|
| 71 | "...ditto node_image_licence" ); |
|---|
| 72 | Test::HTML::Content::tag_ok( $output, "input", |
|---|
| 73 | { name => "node_image_copyright" }, |
|---|
| 74 | "...ditto node_image_copyright" ); |
|---|
| 75 | Test::HTML::Content::tag_ok( $output, "input", |
|---|
| 76 | { name => "node_image_url" }, |
|---|
| 77 | "...ditto node_image_url" ); |
|---|
| 78 | |
|---|
| 79 | # Write all four fields to database, and make sure they're there. |
|---|
| 80 | OpenGuides::Test->write_data( |
|---|
| 81 | guide => $guide, |
|---|
| 82 | node => "Red Lion", |
|---|
| 83 | node_image => "http://example.com/foo.jpg", |
|---|
| 84 | node_image_licence => "http://example.com/bar/", |
|---|
| 85 | node_image_copyright => "Kake L Pugh", |
|---|
| 86 | node_image_url => "http://example.com/~kake/", |
|---|
| 87 | ); |
|---|
| 88 | |
|---|
| 89 | my %node_data = $wiki->retrieve_node( "Red Lion" ); |
|---|
| 90 | is( $node_data{metadata}{node_image}[0], "http://example.com/foo.jpg", |
|---|
| 91 | "node_image saved to database on node write" ); |
|---|
| 92 | is( $node_data{metadata}{node_image_licence}[0], "http://example.com/bar/", |
|---|
| 93 | "...node_image_licence too" ); |
|---|
| 94 | is( $node_data{metadata}{node_image_copyright}[0], "Kake L Pugh", |
|---|
| 95 | "...node_image_copyright too" ); |
|---|
| 96 | is( $node_data{metadata}{node_image_url}[0], "http://example.com/~kake/", |
|---|
| 97 | "...node_image_url too" ); |
|---|
| 98 | |
|---|
| 99 | # Make sure their content shows up in the edit form. |
|---|
| 100 | $output = $guide->display_edit_form( |
|---|
| 101 | id => "Red Lion", |
|---|
| 102 | return_output => 1, |
|---|
| 103 | ); |
|---|
| 104 | |
|---|
| 105 | # Strip Content-Type header to stop Test::HTML::Content getting confused. |
|---|
| 106 | $output =~ s/^Content-Type.*[\r\n]+//m; |
|---|
| 107 | |
|---|
| 108 | Test::HTML::Content::tag_ok( $output, "input", |
|---|
| 109 | { name => "node_image", |
|---|
| 110 | value => "http://example.com/foo.jpg" }, |
|---|
| 111 | "node_image field has correct value in edit form", |
|---|
| 112 | ); |
|---|
| 113 | Test::HTML::Content::tag_ok( $output, "input", |
|---|
| 114 | { name => "node_image_licence", |
|---|
| 115 | value => "http://example.com/bar/" }, |
|---|
| 116 | "...ditto node_image_licence" ); |
|---|
| 117 | Test::HTML::Content::tag_ok( $output, "input", |
|---|
| 118 | { name => "node_image_copyright", |
|---|
| 119 | value => "Kake L Pugh" }, |
|---|
| 120 | "...ditto node_image_copyright" ); |
|---|
| 121 | Test::HTML::Content::tag_ok( $output, "input", |
|---|
| 122 | { name => "node_image_url", |
|---|
| 123 | value => "http://example.com/~kake/" }, |
|---|
| 124 | "...ditto node_image_url" ); |
|---|
| 125 | |
|---|
| 126 | # Make sure they're displayed when a page is viewed. |
|---|
| 127 | $output = $guide->display_node( |
|---|
| 128 | id => "Red Lion", |
|---|
| 129 | return_output => 1, |
|---|
| 130 | ); |
|---|
| 131 | |
|---|
| 132 | # Strip Content-Type header to stop Test::HTML::Content getting confused. |
|---|
| 133 | $output =~ s/^Content-Type.*[\r\n]+//m; |
|---|
| 134 | |
|---|
| 135 | Test::HTML::Content::tag_ok( $output, "img", |
|---|
| 136 | { src => "http://example.com/foo.jpg" }, |
|---|
| 137 | "node_image displayed on page" ); |
|---|
| 138 | Test::HTML::Content::tag_ok( $output, "a", |
|---|
| 139 | { href => "http://example.com/bar/" }, |
|---|
| 140 | "...ditto node_image_licence" ); |
|---|
| 141 | Test::HTML::Content::text_ok( $output, qr/Kake L Pugh/, |
|---|
| 142 | "...ditto node_image_copyright" ); |
|---|
| 143 | Test::HTML::Content::tag_ok( $output, "a", |
|---|
| 144 | { href => "http://example.com/~kake/" }, |
|---|
| 145 | "...ditto node_image_url" ); |
|---|
| 146 | |
|---|
| 147 | # Now try to commit some edits without giving the checksum. |
|---|
| 148 | $output = OpenGuides::Test->write_data( |
|---|
| 149 | guide => $guide, |
|---|
| 150 | node => "Red Lion", |
|---|
| 151 | node_image => "http://eg.com/foo.jpg", |
|---|
| 152 | node_image_licence |
|---|
| 153 | => "http://eg.com/bar/", |
|---|
| 154 | node_image_copyright => "NotKakeNo", |
|---|
| 155 | node_image_url |
|---|
| 156 | => "http://eg.com/~kake/", |
|---|
| 157 | omit_checksum => 1, |
|---|
| 158 | return_output => 1, |
|---|
| 159 | ); |
|---|
| 160 | |
|---|
| 161 | # Strip Content-Type header to stop Test::HTML::Content getting confused. |
|---|
| 162 | $output =~ s/^Content-Type.*[\r\n]+//m; |
|---|
| 163 | |
|---|
| 164 | Test::HTML::Content::tag_ok( $output, "input", |
|---|
| 165 | { name => "node_image", |
|---|
| 166 | value => "http://example.com/foo.jpg" }, |
|---|
| 167 | "Edit conflict form has input box with old " |
|---|
| 168 | . "node_image value in" ); |
|---|
| 169 | Test::HTML::Content::tag_ok( $output, "input", |
|---|
| 170 | { name => "node_image_licence", |
|---|
| 171 | value => "http://example.com/bar/" }, |
|---|
| 172 | "...and one with old node_image_licence value" ); |
|---|
| 173 | Test::HTML::Content::tag_ok( $output, "input", |
|---|
| 174 | { name => "node_image_copyright", |
|---|
| 175 | value => "Kake L Pugh" }, |
|---|
| 176 | "...and one with old node_image_copyright value" ); |
|---|
| 177 | Test::HTML::Content::tag_ok( $output, "input", |
|---|
| 178 | { name => "node_image_url", |
|---|
| 179 | value => "http://example.com/~kake/" }, |
|---|
| 180 | "...and one with old node_image_url value" ); |
|---|
| 181 | Test::HTML::Content::text_ok( $output, "http://eg.com/foo.jpg", |
|---|
| 182 | "...new node_image value appears too" ); |
|---|
| 183 | Test::HTML::Content::text_ok( $output, "http://eg.com/bar/", |
|---|
| 184 | "...as does new node_image_licence value" ); |
|---|
| 185 | Test::HTML::Content::text_ok( $output, "NotKakeNo", |
|---|
| 186 | "...as does new node_image_copyright value" ); |
|---|
| 187 | Test::HTML::Content::text_ok( $output, "http://eg.com/~kake/", |
|---|
| 188 | "...as does new node_image_url value" ); |
|---|
| 189 | |
|---|
| 190 | # Write node with node_image field consisting only of whitespace, make |
|---|
| 191 | # sure it gets stripped. |
|---|
| 192 | OpenGuides::Test->write_data( |
|---|
| 193 | guide => $guide, |
|---|
| 194 | node => "Angel and Greyhound", |
|---|
| 195 | node_image => " ", |
|---|
| 196 | ); |
|---|
| 197 | %node_data = $wiki->retrieve_node( "Angel and Greyhound" ); |
|---|
| 198 | ok( !$node_data{metadata}{node_image}, |
|---|
| 199 | "node_image of whitespace only isn't saved to database" ); |
|---|
| 200 | |
|---|
| 201 | $output = $guide->display_node( |
|---|
| 202 | id => "Angel and Greyhound", |
|---|
| 203 | return_output => 1, |
|---|
| 204 | ); |
|---|
| 205 | $output =~ s/^Content-Type.*[\r\n]+//m; |
|---|
| 206 | Test::HTML::Content::no_tag( $output, "img" => { id => "node_image" }, |
|---|
| 207 | "...or displayed on page" ); |
|---|