]> dev.renevier.net Git - syp.git/blob - openlayers/tests/Protocol/WFS.html
fix broken script in wizard.php
[syp.git] / openlayers / tests / Protocol / WFS.html
1 <html>
2 <head>
3   <script src="../../lib/OpenLayers.js"></script>
4   <script type="text/javascript">
5
6     function test_initialize(t) {
7         t.plan(2);
8
9         var protocol = new OpenLayers.Protocol.WFS({
10             url: "http://some.url.org",
11             featureNS: "http://namespace.org",
12             featureType: "type"
13         });
14         t.ok(protocol instanceof OpenLayers.Protocol.WFS.v1_0_0,
15              "initialize returns instance of default versioned protocol")
16
17         var protocol = new OpenLayers.Protocol.WFS({
18             url: "http://some.url.org",
19             featureNS: "http://namespace.org",
20             featureType: "type",
21             version: "1.1.0"
22         });
23         t.ok(protocol instanceof OpenLayers.Protocol.WFS.v1_1_0,
24              "initialize returns instance of custom versioned protocol")
25     }
26     
27     function test_read(t) {
28         t.plan(6);
29
30         var protocol = new OpenLayers.Protocol.WFS({
31             url: "http://some.url.org",
32             featureNS: "http://namespace.org",
33             featureType: "type",
34             parseFeatures: function(request) {
35                 t.eq(request.responseText, "foo", "parseFeatures called properly");
36                 return "foo";
37             }
38         });
39
40         var _POST = OpenLayers.Request.POST;
41         
42         var expected, status;
43         OpenLayers.Request.POST = function(obj) {
44             t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "GetFeature request is correct");
45             obj.status = status;
46             obj.responseText = "foo";
47             obj.options = {};
48             t.delay_call(0.1, function() {obj.callback.call(this)});
49             return obj;
50         };
51         
52         expected = readXML("GetFeature_1");
53         status = 200;
54         var response = protocol.read({callback: function(response) {
55             t.eq(response.features, "foo", "user callback properly called with features");
56             t.eq(response.code, OpenLayers.Protocol.Response.SUCCESS, "success reported properly");
57         }});
58         
59         options = {
60             maxFeatures: 10,
61             featureType: 'type2',
62             srsName: 'EPSG:900913',
63             featureNS: 'htttp://alternative.namespace.org',
64             callback: function(response) {
65                 t.eq(response.code, OpenLayers.Protocol.Response.FAILURE, "failure reported properly to user callback");
66             }
67         };
68         expected = readXML("GetFeature_2");
69         status = 400;
70         var response = protocol.read(options);
71
72         OpenLayers.Request.POST = _POST;
73     }
74         
75     function test_commit(t){
76         t.plan(4);
77
78         var url = "http://some.url.org";
79         var protocol = new OpenLayers.Protocol.WFS({
80             url: url,
81             featureNS: "http://namespace.org",
82             featureType: "type"
83         });
84         protocol.format.read = function(data) {
85             t.eq(data, "foo", "callback called with correct argument");
86             return {
87                 insertIds: new Array(3),
88                 success: true
89             }
90         };
91
92         var _POST = OpenLayers.Request.POST;
93         
94         var expected;        
95         OpenLayers.Request.POST = function(obj) {
96             t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "Transaction XML with Insert, Update and Delete created correctly");
97             obj.responseText = "foo";
98             obj.options = {};
99             t.delay_call(0.1, function() {obj.callback.call(this)});
100             return obj;
101         };
102         
103         var featureDelete = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(42, 7), {has : "cheeseburger"});
104         featureDelete.fid = "fid.37";
105         featureDelete.state = OpenLayers.State.DELETE;
106         featureDelete.layer = {
107             projection: {
108                 getCode : function(){
109                     return "EPSG:4326";
110                 }
111             }
112         }
113         var featureInsert = featureDelete.clone();
114         featureInsert.state = OpenLayers.State.INSERT;
115         var featureModify = featureDelete.clone();
116         featureModify.fid = "fid.37";
117         featureModify.state = OpenLayers.State.UPDATE;
118
119         options = {
120             featureNS: "http://some.namespace.org",
121             featureType: "type",
122             callback: function(response) {
123                 t.eq(response.insertIds.length, 3, "correct response passed to user callback");
124                 t.eq(response.code, OpenLayers.Protocol.Response.SUCCESS, "success properly reported to user callback");
125             }
126         }
127         
128         expected = readXML("commit");
129         var response = protocol.commit([featureInsert, featureModify, featureDelete], options);
130
131         OpenLayers.Request.POST = _POST;
132
133     }
134
135     function test_filterDelete(t) {
136         t.plan(2)
137
138         var url = "http://some.url.org";
139         var protocol = new OpenLayers.Protocol.WFS({
140             url: url,
141             featureNS: "http://namespace.org",
142             featureType: "type"
143         });
144         
145         var filter = new OpenLayers.Filter.Spatial({
146             type: OpenLayers.Filter.Spatial.BBOX,
147             value: new OpenLayers.Bounds(-5, -5, 5, 5)
148         });
149
150         var _POST = OpenLayers.Request.POST;
151
152         var expected = readXML("filter_delete");
153         OpenLayers.Request.POST = function(obj) {
154             t.xml_eq(new OpenLayers.Format.XML().read(obj.data).documentElement, expected, "request data correct");
155             t.delay_call(0.1, function() {obj.callback.call(this)});
156             return obj;
157         };
158         
159         var response = protocol.filterDelete(filter, {
160             callback: function() {
161                 t.ok("user callback function called");
162             }
163         });
164     
165         OpenLayers.Request.POST = _POST;
166     }
167     
168     function test_abort(t) {
169         t.plan(1);
170         var protocol = new OpenLayers.Protocol.WFS({
171             url: "http://example.com",
172             featureNS: "http://example.com#namespace",
173             featureType: "type"
174         });
175         
176         var response = {
177             priv: {
178                 abort: function() {
179                     aborted = true;
180                 }
181             }
182         };
183         
184         // call abort with mocked response
185         var aborted = false;
186         protocol.abort(response);
187         t.eq(aborted, true, "abort called on response.priv");
188
189     }
190     
191     function test_fromWMSLayer(t) {
192         t.plan(8);
193         var map = new OpenLayers.Map("map", {
194             projection: "EPSG:1234"
195         });
196         var layer = new OpenLayers.Layer.WMS("foo", "htttp://foo/ows",
197             {layers: "topp:states"}
198         );
199         map.addLayer(layer);
200         var protocol = OpenLayers.Protocol.WFS.fromWMSLayer(layer);
201         t.eq(protocol.url, "htttp://foo/ows", "url taken from wms layer");
202         t.eq(protocol.featurePrefix, "topp", "feature prefix correctly extracted");
203         t.eq(protocol.featureType, "states", "typeName correctly extracted");
204         t.eq(protocol.srsName, "EPSG:1234", "srsName set correctly");
205         t.eq(protocol.version, "1.1.0", "version set correctly");
206         
207         layer.params["LAYERS"] = ["topp:street_centerline", "topp:states"];
208         layer.projection = new OpenLayers.Projection("EPSG:900913");
209         protocol = OpenLayers.Protocol.WFS.fromWMSLayer(layer);
210         t.eq(protocol.featurePrefix, "topp", "featurePrefix from layer param array");
211         t.eq(protocol.featureType, "street_centerline", "first layer from layer param array as featureType"); 
212         t.eq(protocol.srsName, "EPSG:900913", "projection from layer preferred"); 
213     }
214
215     function readXML(id) {
216         var xml = document.getElementById(id).firstChild.nodeValue;
217         return new OpenLayers.Format.XML().read(xml).documentElement;        
218     }
219
220   </script>
221 </head>
222 <body>
223 <div id="map" style="width:512px; height:256px"> </div>
224 <div id="GetFeature_1"><!--
225 <wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
226     <wfs:Query typeName="feature:type" xmlns:feature="http://namespace.org"/>
227 </wfs:GetFeature>
228 --></div>
229 <div id="GetFeature_2"><!--
230 <wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" maxFeatures="10" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
231     <wfs:Query typeName="feature:type2" xmlns:feature="htttp://alternative.namespace.org"/>
232 </wfs:GetFeature>
233 --></div>
234 <div id="commit"><!--
235 <wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-transaction.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
236     <wfs:Insert>
237         <feature:type xmlns:feature="http://namespace.org">
238             <feature:the_geom>
239                 <gml:Point xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">
240                     <gml:coordinates decimal="." cs="," ts=" ">42,7</gml:coordinates>
241                 </gml:Point>
242             </feature:the_geom>
243             <feature:has>cheeseburger</feature:has>
244         </feature:type>
245     </wfs:Insert>
246     <wfs:Update typeName="feature:type" xmlns:feature="http://namespace.org">
247         <wfs:Property>
248             <wfs:Name>the_geom</wfs:Name>
249             <wfs:Value>
250                 <gml:Point xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">
251                     <gml:coordinates decimal="." cs="," ts=" ">42,7</gml:coordinates>
252                 </gml:Point>
253             </wfs:Value>
254         </wfs:Property>
255         <wfs:Property>
256             <wfs:Name>has</wfs:Name>
257             <wfs:Value>cheeseburger</wfs:Value>
258         </wfs:Property>
259         <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
260             <ogc:FeatureId fid="fid.37"/>
261         </ogc:Filter>
262     </wfs:Update>
263     <wfs:Delete typeName="feature:type" xmlns:feature="http://namespace.org">
264         <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
265             <ogc:FeatureId fid="fid.37"/>
266         </ogc:Filter>
267     </wfs:Delete>
268 </wfs:Transaction>
269 --></div>
270 <div id="filter_delete"><!--
271 <wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.0.0">
272     <wfs:Delete typeName="feature:type" xmlns:feature="http://namespace.org">
273         <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
274             <ogc:BBOX>
275                 <ogc:PropertyName/>
276                 <gml:Box xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">
277                     <gml:coordinates decimal="." cs="," ts=" ">-5,-5 5,5</gml:coordinates>
278                 </gml:Box>
279             </ogc:BBOX>
280         </ogc:Filter>
281     </wfs:Delete>
282 </wfs:Transaction>
283 --></div>
284 </body>
285 </html>