root/trunk/t/44_node_image.t

Revision 996, 9.4 kB (checked in by kake, 22 months ago)

Fix test failure on machines with XML::LibXML installed.

Line 
1use strict;
2use OpenGuides;
3use OpenGuides::Test;
4use Test::More;
5use Wiki::Toolkit::Setup::SQLite;
6
7eval { require DBD::SQLite; };
8if ( $@ ) {
9    my ($error) = $@ =~ /^(.*?)\n/;
10    plan skip_all => "DBD::SQLite could not be used - no database to test with ($error)";
11}
12
13eval { require Test::HTML::Content; };
14if ( $@ ) {
15    plan skip_all => "Test::HTML::Content not installed.";
16}
17
18plan tests => 30;
19
20my ( $config, $guide, $wiki );
21
22# Clear out the database from any previous runs.
23unlink "t/node.db";
24unlink <t/indexes/*>;
25Wiki::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
34my $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
42Test::HTML::Content::no_tag( $output, "input", { name => "node_image" },
43    "node_image field not in edit form if config says it shouldn't be" );
44Test::HTML::Content::no_tag( $output, "input",
45                             { name => "node_image_licence" },
46                             "...ditto node_image_licence" );
47Test::HTML::Content::no_tag( $output, "input",
48                             { name => "node_image_copyright" },
49                             "...ditto node_image_copyright" );
50Test::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
67Test::HTML::Content::tag_ok( $output, "input", { name => "node_image" },
68    "node_image field appears in edit form if config says it should" );
69Test::HTML::Content::tag_ok( $output, "input",
70                             { name => "node_image_licence" },
71                             "...ditto node_image_licence" );
72Test::HTML::Content::tag_ok( $output, "input",
73                             { name => "node_image_copyright" },
74                             "...ditto node_image_copyright" );
75Test::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.
80OpenGuides::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
89my %node_data = $wiki->retrieve_node( "Red Lion" );
90is( $node_data{metadata}{node_image}[0], "http://example.com/foo.jpg",
91    "node_image saved to database on node write" );
92is( $node_data{metadata}{node_image_licence}[0], "http://example.com/bar/",
93    "...node_image_licence too" );
94is( $node_data{metadata}{node_image_copyright}[0], "Kake L Pugh",
95    "...node_image_copyright too" );
96is( $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
108Test::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                           );
113Test::HTML::Content::tag_ok( $output, "input",
114                             { name  => "node_image_licence",
115                               value => "http://example.com/bar/" },
116                             "...ditto node_image_licence" );
117Test::HTML::Content::tag_ok( $output, "input",
118                             { name  => "node_image_copyright",
119                               value => "Kake L Pugh" },
120                             "...ditto node_image_copyright" );
121Test::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
135Test::HTML::Content::tag_ok( $output, "img",
136                             { src => "http://example.com/foo.jpg" },
137                             "node_image displayed on page" );
138Test::HTML::Content::tag_ok( $output, "a",
139                             { href => "http://example.com/bar/" },
140                             "...ditto node_image_licence" );
141Test::HTML::Content::text_ok( $output, qr/Kake L Pugh/,
142                              "...ditto node_image_copyright" );
143Test::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
164Test::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" );
169Test::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" );
173Test::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" );
177Test::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" );
181Test::HTML::Content::text_ok( $output, "http://eg.com/foo.jpg",
182                              "...new node_image value appears too" );
183Test::HTML::Content::text_ok( $output, "http://eg.com/bar/",
184                              "...as does new node_image_licence value" );
185Test::HTML::Content::text_ok( $output, "NotKakeNo",
186                              "...as does new node_image_copyright value" );
187Test::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.
192OpenGuides::Test->write_data(
193                              guide => $guide,
194                              node  => "Angel and Greyhound",
195                              node_image => " ",
196                            );
197%node_data = $wiki->retrieve_node( "Angel and Greyhound" );
198ok( !$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;
206Test::HTML::Content::no_tag( $output, "img" => { id => "node_image" },
207                             "...or displayed on page" );
Note: See TracBrowser for help on using the browser.