source: trunk/t/21_rdf.t @ 1016

Last change on this file since 1016 was 1016, checked in by kake, 15 years ago

Move node RDF generation from inline strings in OpenGuides::RDF to its own template; also make sure all weird characters are encoded.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 KB
Line 
1use Wiki::Toolkit::Setup::SQLite;
2use OpenGuides;
3use OpenGuides::Config;
4use OpenGuides::RDF;
5use OpenGuides::Utils;
6use OpenGuides::Test;
7use URI::Escape;
8use Test::More;
9
10eval { require DBD::SQLite; };
11my $have_sqlite = $@ ? 0 : 1;
12
13if ( $@ ) {
14    my ($error) = $@ =~ /^(.*?)\n/;
15    plan skip_all => "DBD::SQLite could not be used - no database to test with. ($error)";
16}
17
18plan tests => 27;
19
20Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } );
21my $config = OpenGuides::Test->make_basic_config;
22$config->script_url( "http://wiki.example.com/" );
23$config->script_name( "mywiki.cgi" );
24$config->site_name( "Wiki::Toolkit Test Site" );
25$config->default_city( "London" );
26$config->default_country( "United Kingdom" );
27
28eval { require Wiki::Toolkit::Search::Plucene; };
29if ( $@ ) { $config->use_plucene ( 0 ) };
30
31
32my $wiki = OpenGuides::Utils->make_wiki_object( config => $config );
33
34# Clear out the database from any previous runs.
35foreach my $del_node ( $wiki->list_all_nodes ) {
36    $wiki->delete_node( $del_node ) or die "Can't delete $del_node";
37}
38
39my $rdf_writer = eval {
40    OpenGuides::RDF->new( wiki => $wiki, config => $config );
41};
42is( $@, "", "'new' doesn't croak if wiki and config objects supplied" );
43isa_ok( $rdf_writer, "OpenGuides::RDF" );
44
45# Test the data for a node that exists.
46$wiki->write_node( "Calthorpe Arms",
47    "CAMRA-approved pub near King's Cross",
48    undef,
49    {
50        comment            => "Stub page, please update!",
51        username           => "Kake",
52        postcode           => "WC1X 8JR",
53        locale             => [ "Bloomsbury", "St Pancras" ],
54        phone              => "test phone number",
55        website            => "test website",
56        opening_hours_text => "test hours",
57        latitude           => "51.524193",
58        longitude          => "-0.114436",
59        summary            => "a nice pub",
60    }
61);
62
63my $rdfxml = $rdf_writer->emit_rdfxml( node => "Calthorpe Arms" );
64
65like( $rdfxml, qr|<\?xml version="1.0"\?>|, "RDF is encoding-neutral" );
66
67like( $rdfxml, qr|<dc:title>Bloomsbury</dc:title>|,
68    "finds the first locale" );
69like( $rdfxml, qr|<dc:title>St Pancras</dc:title>|,
70    "finds the second locale" );
71
72like( $rdfxml, qr|<phone>test phone number</phone>|,
73    "picks up phone number" );
74
75like( $rdfxml, qr|<chefmoz:Hours>test hours</chefmoz:Hours>|,
76    "picks up opening hours text" );
77
78like( $rdfxml, qr|<foaf:homepage rdf:resource="test website" />|, "picks up website" );
79
80like( $rdfxml,
81    qr|<dc:title>Wiki::Toolkit Test Site: Calthorpe Arms</dc:title>|,
82    "sets the title correctly" );
83
84like( $rdfxml, qr|<foaf:Person rdf:ID="Kake">|,
85    "last username to edit used as contributor" );
86
87like( $rdfxml, qr|<wiki:version>1</wiki:version>|, "version picked up" );
88
89like( $rdfxml, qr|<rdf:Description rdf:about="">|, "sets the 'about' correctly" );
90
91like( $rdfxml, qr|<dc:source rdf:resource="http://wiki.example.com/mywiki.cgi\?Calthorpe_Arms" />|,
92    "set the dc:source with the version-independent uri" );
93
94like( $rdfxml, qr|<country>United Kingdom</country>|, "country" ).
95like( $rdfxml, qr|<city>London</city>|, "city" ).
96like( $rdfxml, qr|<postalCode>WC1X 8JR</postalCode>|, "postcode" );
97like( $rdfxml, qr|<geo:lat>51.524193</geo:lat>|, "latitude" );
98like( $rdfxml, qr|<geo:long>-0.114436</geo:long>|, "longitude" );
99like( $rdfxml, qr|<dc:description>a nice pub</dc:description>|, "summary (description)" );
100
101like( $rdfxml, qr|<dc:date>|, "date element included" );
102unlike( $rdfxml, qr|<dc:date>1970|, "hasn't defaulted to the epoch" );
103
104# Check that default city and country can be set to blank.
105$config = OpenGuides::Test->make_basic_config;
106$config->default_city( "" );
107$config->default_country( "" );
108my $guide = OpenGuides->new( config => $config );
109OpenGuides::Test->write_data(
110                                guide => $guide,
111                                node  => "Star Tavern",
112                                latitude => 51.498,
113                                longitude => -0.154,
114                            );
115$rdf_writer = OpenGuides::RDF->new( wiki => $guide->wiki, config => $config );
116$rdfxml = $rdf_writer->emit_rdfxml( node => "Star Tavern" );
117unlike( $rdfxml, qr|<city>|, "no city in RDF when no default city" );
118unlike( $rdfxml, qr|<country>|, "...same for country" );
119
120# Now test that there's a nice failsafe where a node doesn't exist.
121$rdfxml = eval { $rdf_writer->emit_rdfxml( node => "I Do Not Exist" ); };
122is( $@, "", "->emit_rdfxml doesn't die when called on a nonexistent node" );
123
124like( $rdfxml, qr|<wiki:version>0</wiki:version>|, "...and wiki:version is 0" );
125
126# Test the data for a node that redirects.
127$wiki->write_node( "Calthorpe Arms Pub",
128    "#REDIRECT [[Calthorpe Arms]]",
129    undef,
130    {
131        comment  => "Created as redirect to Calthorpe Arms page.",
132        username => "Earle",
133    }
134);
135
136my $redirect_rdf = $rdf_writer->emit_rdfxml( node => "Calthorpe Arms Pub" );
137
138like( $redirect_rdf, qr|<owl:sameAs rdf:resource="/\?id=Calthorpe_Arms;format=rdf#obj" />|,
139    "redirecting node gets owl:sameAs to target" );
140
141$wiki->write_node( "Nonesuch Stores",
142    "A metaphysical wonderland",
143    undef,
144    {
145        comment            => "Yup.",
146        username           => "Nobody",
147        opening_hours_text => "Open All Hours",
148    }
149);
150
151$rdfxml = $rdf_writer->emit_rdfxml( node => "Nonesuch Stores" );
152
153like( $rdfxml, qr|<geo:SpatialThing rdf:ID="obj" dc:title="Nonesuch Stores">|,
154    "having opening hours marks node as geospatial" );
155
Note: See TracBrowser for help on using the repository browser.