summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/qunit/smw/data/ext.smw.data.test.js
blob: 634dc34ef1fa135eb9ce6ca52a112775d355ac88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
 * This file is part of the Semantic MediaWiki QUnit test suite
 * @see https://semantic-mediawiki.org/wiki/QUnit
 *
 * @section LICENSE
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 *
 * @since 1.9
 *
 * @file
 * @ignore
 *
 * @ingroup SMW
 * @ingroup Test
 *
 * @licence GNU GPL v2+
 * @author mwjames
 */

/**
 * Tests methods provided by ext.smw.data.js
 * @ignore
 */
( function ( $, mw, smw ) {
	'use strict';

	QUnit.module( 'ext.smw.Data', QUnit.newMwEnvironment() );

	var pass = 'Passes because ';
	var testString = '{\"query\":{\"result\":{\"printrequests\":[{\"label\":\"\",\"typeid\":\"_wpg\",\"mode\":2},{\"label\":\"Has test date\",\"typeid\":\"_dat\",\"mode\":1},{\"label\":\"Has test page\",\"typeid\":\"_wpg\",\"mode\":1},{\"label\":\"Has test string\",\"typeid\":\"_str\",\"mode\":1},{\"label\":\"Has test text\",\"typeid\":\"_txt\",\"mode\":1},{\"label\":\"Has test url\",\"typeid\":\"_uri\",\"mode\":1}],\"results\":{\"DataitemFactory\\/1\":{\"printouts\":{\"Has test date\":[\"947548800\"],\"Has test page\":[{\"fulltext\":\"File:FooBarfoo.png\",\"fullurl\":\"http:\\/\\/localhost\\/mw\\/index.php\\/File:FooBarfoo.png\",\"namespace\":6},{\"fulltext\":\"Foo page\",\"fullurl\":\"http:\\/\\/localhost\\/mw\\/index.php\\/Foo_page\",\"namespace\":0}],\"Has test string\":[\"Foobar string\"],\"Has test text\":[\"foo foo string\"],\"Has test url\":[\"http:\\/\\/localhost\\/mw\\/foobarfoo\"]},\"fulltext\":\"DataitemFactory\\/1\",\"fullurl\":\"http:\\/\\/localhost\\/mw\\/index.php\\/DataitemFactory\\/1\",\"namespace\":0},\"DataitemFactory\\/2\":{\"printouts\":{\"Has test date\":[\"1010707200\"],\"Has test page\":[{\"fulltext\":\"File:Foo.png\",\"fullurl\":\"http:\\/\\/localhost\\/mw\\/index.php\\/File:Foo.png\",\"namespace\":6},{\"fulltext\":\"Bar page\",\"fullurl\":\"http:\\/\\/localhost\\/mw\\/index.php\\/Bar_page\",\"namespace\":0}],\"Has test string\":[\"foo string\"],\"Has test text\":[\"fooBar string\"],\"Has test url\":[\"http:\\/\\/localhost\\/mw\\/foo\"]},\"fulltext\":\"DataitemFactory\\/2\",\"fullurl\":\"http:\\/\\/localhost\\/mw\\/index.php\\/DataitemFactory\\/2\",\"namespace\":0},\"DataitemFactory\\/3\":{\"printouts\":{\"Has test date\":[\"1010725200\"],\"Has test page\":[{\"fulltext\":\"Foo page\",\"fullurl\":\"http:\\/\\/localhost\\/mw\\/index.php\\/Foo_page\",\"namespace\":0},{\"fulltext\":\"File:FooBar.png\",\"fullurl\":\"http:\\/\\/localhost\\/mw\\/index.php\\/File:FooBar.png\",\"namespace\":6}],\"Has test string\":[\"bar string\"],\"Has test text\":[\"fooBar foo string\"],\"Has test url\":[\"http:\\/\\/localhost\\/index.php?title=foo\"]},\"fulltext\":\"DataitemFactory\\/3\",\"fullurl\":\"http:\\/\\/localhost\\/mw\\/index.php\\/DataitemFactory\\/3\",\"namespace\":0}},\"meta\":{\"hash\":\"c26caabe31af28817c3f87f26cfd0a3d\",\"count\":3,\"offset\":0}},\"ask\":{\"conditions\":\"[[Has test string::+]]\",\"parameters\":{\"limit\":50,\"offset\":0,\"format\":\"datatables\",\"link\":\"all\",\"headers\":\"show\",\"mainlabel\":\"\",\"intro\":\"\",\"outro\":\"\",\"searchlabel\":\"\\u2026 further results\",\"default\":\"\",\"class\":\"\",\"theme\":\"bootstrap\"},\"printouts\":[\"?Has test date\",\"?Has test page\",\"?Has test string\",\"?Has test text\",\"?Has test url\"]}},\"version\":\"0.1\"}';
	var subjectLessResult = '{\"query\":{\"result\":{\"printrequests\":[{\"label\":\"Modification date\",\"typeid\":\"_dat\",\"mode\":1}],\"results\":{\"Concepttest3\":{\"printouts\":{\"Modification date\":[\"1358906761\"]}},\"Concepttest4\":{\"printouts\":{\"Modification date\":[\"1358905485\"]}},\"Category:Concepts\":{\"printouts\":{\"Modification date\":[\"1358896550\"]}}},\"meta\":{\"hash\":\"8c727bd6aa52f47caa39bd0c1b93ee59\",\"count\":3,\"offset\":0}},\"ask\":{\"conditions\":\"[[Modification date::+]]\",\"parameters\":{\"limit\":3,\"offset\":0,\"format\":\"datatables\",\"link\":\"all\",\"headers\":\"show\",\"mainlabel\":\"-\",\"intro\":\"\",\"outro\":\"\",\"searchlabel\":\"\\u2026 further results\",\"default\":\"\",\"class\":\"\",\"theme\":\"bootstrap\"},\"printouts\":[\"?Modification date\"]}},\"version\":\"0.1\"}';
	var unknownType = '{\"query\":{\"result\":{\"printrequests\":[{\"label\":\"\",\"typeid\":\"_wpg\",\"mode\":2},{\"label\":\"Has description\",\"typeid\":\"_foo\",\"mode\":1}],\"results\":{\"File:IMG0027040123.jpg\":{\"printouts\":{\"Has description\":[\"Mauris pellentesque aliquet leo Nam nibh metus facilisi et sem laoreet. Netus ipsum montes et a neque in pulvinar nibh\",\"Facilisi et sem laoreet. Netus ipsum montes et a neque in pulvinar nibh\"]},\"fulltext\":\"File:IMG0027040123.jpg\",\"fullurl\":\"http:\\/\\/localhost\\/mw\\/index.php\\/File:IMG0027040123.jpg\",\"namespace\":6}},\"meta\":{\"hash\":\"a116c98703f48d231ec4c8eaee4038f8\",\"count\":1,\"offset\":0}},\"ask\":{\"conditions\":\"[[Has description::+]]\",\"parameters\":{\"limit\":1,\"offset\":0,\"link\":\"all\",\"headers\":\"show\",\"mainlabel\":\"\",\"intro\":\"\",\"outro\":\"\",\"searchlabel\":\"\\u2026 further results\",\"default\":\"\"},\"printouts\":[\"?Has description\"]}},\"version\":\"0.1\"}';
	var numberType = '{\"query\":{\"result\":{\"printrequests\":[{\"label\":\"\",\"typeid\":\"_wpg\",\"mode\":2},{\"label\":\"Has number\",\"typeid\":\"_num\",\"mode\":1}],\"results\":{\"Image\\/1\":{\"printouts\":{\"Has number\":[1220,1320,99]},\"fulltext\":\"Image\\/1\",\"fullurl\":\"http:\\/\\/localhost\\/mw\\/index.php\\/Image\\/1\",\"namespace\":0,\"exists\":true}},\"meta\":{\"hash\":\"c2aa1dc43849ad148c9649e818eeb29b\",\"count\":1,\"offset\":0}},\"ask\":{\"conditions\":\"[[has number::+]]\",\"parameters\":{\"limit\":50,\"offset\":0,\"format\":\"datatables\",\"link\":\"all\",\"headers\":\"show\",\"mainlabel\":\"\",\"intro\":\"\",\"outro\":\"\",\"searchlabel\":\"\\u2026 further results\",\"default\":\"\",\"class\":\"\",\"theme\":\"bootstrap\"},\"printouts\":[\"?Has number\"]}},\"version\":\"0.1\"}';
	var quantityType = '{\"query\":{\"result\":{\"printrequests\":[{\"label\":\"\",\"typeid\":\"_wpg\",\"mode\":2},{\"label\":\"Area\",\"typeid\":\"_qty\",\"mode\":1}],\"results\":{\"Berlin\":{\"printouts\":{\"Area\":[{\"value\":891.85,\"unit\":\"km\\u00b2\"}]},\"fulltext\":\"Berlin\",\"fullurl\":\"http:\\/\\/localhost\\/mw\\/index.php\\/Berlin\",\"namespace\":0,\"exists\":true}},\"meta\":{\"hash\":\"f0f072f414c3e814c847aff1ba87dfb3\",\"count\":1,\"offset\":0}},\"ask\":{\"conditions\":\"[[Area::+]]\",\"parameters\":{\"limit\":50,\"offset\":0,\"format\":\"datatables\",\"link\":\"all\",\"headers\":\"show\",\"mainlabel\":\"\",\"intro\":\"\",\"outro\":\"\",\"searchlabel\":\"\\u2026 further results\",\"default\":\"\",\"class\":\"\",\"theme\":\"bootstrap\"},\"printouts\":[\"?Area\"]}},\"version\":\"0.2.5\"}';

	/**
	 * Test accessibility
	 *
	 * @since: 1.9
	 */
	QUnit.test( 'instance', function ( assert ) {
		assert.expect( 1 );

		var result = new smw.Data();
		assert.ok( result instanceof Object, pass + 'the smw.dataItem instance was accessible' );

	} );

	/**
	 * Test accessibility
	 *
	 * @since: 1.9
	 */
	QUnit.test( 'comparison $.parseJSON() vs. smw.Api.parse()', function ( assert ) {
		assert.expect( 2 );

		var result;
		var startDate;
		var smwApi = new smw.Api();

		startDate = new Date();
		result = $.parseJSON( testString );
		assert.ok( result, 'Using parseJSON took: ' + ( new Date().getTime() - startDate.getTime() ) + ' ms' );

		startDate = new Date();
		result = smwApi.parse( testString );
		assert.ok( result, 'using smw.Api.parse took: ' + ( new Date().getTime() - startDate.getTime() ) + ' ms' );

	} );

	/**
	 * Test smw.dataItem.property factory
	 *
	 * @since: 1.9
	 */
	QUnit.test( 'smw.dataItem.property factory test', function ( assert ) {
		assert.expect( 5 );

		// Testing indirect via the smw.Api otherwise the whole JSON parsing
		// needs to be copied
		var smwApi = new smw.Api();
		var result = smwApi.parse( testString );

		$.map ( result.query.result.results['DataitemFactory/1'].printouts, function( value, key ) {
			if ( value instanceof smw.dataItem.property ){
				assert.equal( value.getLabel(), key , pass + 'the parser returned ' + key );
			}
		} );

	} );

	/**
	 * Test subject less
	 *
	 * @since: 1.9
	 */
	QUnit.test( 'smw.dataItem.property subject less factory test', function ( assert ) {
		assert.expect( 3 );

		// Testing indirect via the smw.Api otherwise the whole JSON parsing
		// needs to be copied
		var smwApi = new smw.Api();
		var result = smwApi.parse( subjectLessResult );

		$.map ( result.query.result.results, function( printouts, head ) {
			$.map ( printouts, function( values ) {
				$.map ( values, function( value, key ) {
					if ( value instanceof smw.dataItem.property ){
						assert.equal( value.getLabel(), key , pass + 'the parser returned ' + key + ' for ' + head );
					}
				} );
			} );
		} );

	} );

	/**
	 * Test smw.dataItem.wikiPage factory
	 *
	 * @since: 1.9
	 */
	QUnit.test( 'smw.dataItem.wikiPage subject factory test', function ( assert ) {
		assert.expect( 3 );

		// Testing indirect via the smw.Api otherwise the whole JSON parsing
		// needs to be copied
		var smwApi = new smw.Api();
		var result = smwApi.parse( testString );

		assert.ok( result.query.result.results['DataitemFactory/1'] instanceof smw.dataItem.wikiPage, pass + 'the parser returned a smw.dataItem.wikiPage object' );
		assert.equal( result.query.result.results['DataitemFactory/1'].getHtml(), 'DataitemFactory/1', pass + 'the object method .getHtml() was accessible' );
		assert.equal( result.query.result.results['DataitemFactory/1'].getUri(), 'http://localhost/mw/index.php/DataitemFactory/1', pass + 'the object method .getUri() was accessible' );

	} );

	/**
	 * Test smw.dataItem.wikiPage factory
	 *
	 * @since: 1.9
	 */
	QUnit.test( 'smw.dataItem.wikiPage multiValue factory test', function ( assert ) {
		assert.expect( 4 );

		// Testing indirect via the smw.Api otherwise the whole JSON parsing
		// needs to be copied
		var smwApi = new smw.Api();
		var result = smwApi.parse( testString );

		var expectedMultiValue = ['File:FooBarfoo.png', 'Foo page'];

		$.map ( result.query.result.results['DataitemFactory/1'].printouts, function( values ) {
			if ( values instanceof smw.dataItem.property ){
				$.map ( values, function( value, key ) {
					if ( value instanceof smw.dataItem.wikiPage ){
						assert.ok( value instanceof smw.dataItem.wikiPage, pass + 'the parser returned a smw.dataItem.wikiPage object' );
						assert.equal( value.getHtml(), expectedMultiValue[key] , pass + 'the parser returned ' + expectedMultiValue[key] );
					}
				} );
			}
		} );

	} );

	/**
	 * Test smw.dataItem.time factory
	 *
	 * @since: 1.9
	 */
	QUnit.test( 'smw.dataItem.time factory', function ( assert ) {
		assert.expect( 4 );

		// Use as helper to fetch language dep. month name
		var monthNames = [];
		$.map ( mw.config.get( 'wgMonthNames' ), function( value ) {
			if( value !== '' ){
				monthNames.push( value );
			}
		} );

		// Testing indirect via the smw.Api otherwise the whole JSON parsing
		// needs to be copied
		var smwApi = new smw.Api();
		var result = smwApi.parse( testString );

		$.map ( result.query.result.results['DataitemFactory/1'].printouts, function( values ) {
			if ( values instanceof smw.dataItem.property ){
				$.map ( values, function( value ) {
					if ( value instanceof smw.dataItem.time ){
						assert.ok( value instanceof smw.dataItem.time, pass + 'the parser returned a smw.dataItem.time object' );
						assert.equal( value.getMediaWikiDate(), '11 ' + monthNames[0] + ' 2000' , pass + 'the parser returned "11 '+ monthNames[0] +' 2000"' );
					}
				} );
			}
		} );

		$.map ( result.query.result.results['DataitemFactory/3'].printouts, function( values ) {
			if ( values instanceof smw.dataItem.property ){
				$.map ( values, function( value ) {
					if ( value instanceof smw.dataItem.time ){
						assert.ok( value instanceof smw.dataItem.time, pass + 'the parser returned a smw.dataItem.time object' );
						assert.equal( value.getMediaWikiDate(), '11 '+ monthNames[0] +' 2002 05:00:00' , pass + 'the parser returned "11 '+ monthNames[0] +' 2002 05:00:00"' );
					}
				} );
			}
		} );

	} );

	/**
	 * Test smw.dataItem.uri factory
	 *
	 * @since: 1.9
	 */
	QUnit.test( 'smw.dataItem.uri factory', function ( assert ) {
		assert.expect( 4 );

		// Testing indirect via the smw.Api otherwise the whole JSON parsing
		// needs to be copied
		var smwApi = new smw.Api();
		var result = smwApi.parse( testString );

		$.map ( result.query.result.results['DataitemFactory/2'].printouts, function( values ) {
			if ( values instanceof smw.dataItem.property ){
				$.map ( values, function( value ) {
					if ( value instanceof smw.dataItem.uri ){
					assert.ok( value instanceof smw.dataItem.uri, pass + 'the parser returned a smw.dataItem.uri object' );
					assert.equal( value.getUri(), 'http://localhost/mw/foo' , pass + 'getUri() returned "http://localhost/mw/foo"' );
					assert.equal( value.getHtml( false ), 'http://localhost/mw/foo' , pass + 'getHtml( false ) returned "http://localhost/mw/foo"' );
					assert.equal( value.getHtml( true ), '<a href=\"http://localhost/mw/foo\">http://localhost/mw/foo</a>' , pass + 'getHtml( true ) returned a href element' );
						}
				} );
			}
		} );
	} );

	/**
	 * Test number factory
	 *
	 * @since: 1.9
	 */
	QUnit.test( 'smw.dataItem.number factory', function ( assert ) {
		assert.expect( 3 );

		// Testing indirect via the smw.Api otherwise the whole JSON parsing
		// needs to be copied
		var smwApi = new smw.Api();
		var result = smwApi.parse( numberType );
		var expectedNumber = [1220,1320,99];
		var i=0;

		Object.values( result.query.result.results ).forEach( function ( result ) {
			Object.values( result.printouts ).forEach( function ( property ) {
				if ( property instanceof smw.dataItem.property ){
					$.map ( property, function( value ) {
						if ( value instanceof smw.dataItem.number ){
							assert.equal( value.getNumber(), expectedNumber[i] , pass + 'getNumber() returned ' + expectedNumber[i] );
							i++;
						}
					} );
				}
			} );
		} );
	} );

	/**
	 * Test quantity factory
	 *
	 * @since: 1.9
	 */
	QUnit.test( 'smw.dataValue.quantity factory', function ( assert ) {
		assert.expect( 2 );

		// Testing indirect via the smw.Api otherwise the whole JSON parsing
		// needs to be copied
		var smwApi = new smw.Api();
		var result = smwApi.parse( quantityType );
		var expected = { value: 891.85, unit: 'kmĀ²' };

		Object.values( result.query.result.results ).forEach( function ( result ) {
			Object.values( result.printouts ).forEach( function ( property ) {
				if ( property instanceof smw.dataItem.property ){
					$.map ( property, function( value ) {
						if ( value instanceof smw.dataValue.quantity ){
							assert.equal( value.getValue(), expected.value , pass + 'getValue() returned ' + expected.value );
							assert.equal( value.getUnit(), expected.unit , pass + 'getUnit() returned ' + expected.unit );
						}
					} );
				}
			} );
		} );
	} );


	/**
	 * Test unlisted factory
	 *
	 * @since: 1.9
	 */
	QUnit.test( 'smw.dataItem.unknown factory', function ( assert ) {
		assert.expect( 4 );

		// Testing indirect via the smw.Api otherwise the whole JSON parsing
		// needs to be copied
		var smwApi = new smw.Api();
		var result = smwApi.parse( unknownType );

		Object.values( result.query.result.results ).forEach( function ( result ) {
			Object.values( result.printouts ).forEach( function ( property ) {
				if ( property instanceof smw.dataItem.property ){
					$.map ( property, function( value ) {
						if ( value instanceof smw.dataItem.unknown ){
							assert.ok( value instanceof smw.dataItem.unknown, pass + 'the parser returned a smw.dataItem.unknown object' );
							assert.equal( value.getDIType(), '_foo' , pass + 'getDIType() returned an unknown type _foo' );
						}
					} );
				}
			} );
		} );
	} );

}( jQuery, mediaWiki, semanticMediaWiki ) );