summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/qunit/smw/query/ext.smw.query.test.js
blob: ce0f74bd140ea077bdcbd5f6aa6e8b8b8a77e451 (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
/**
 * This file is part of the Semantic MediaWiki QUnit test suite
 * @see https://semantic-mediawiki.org/
 *
 * @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.query.js
 * @ignore
 */
( function ( $, mw, smw ) {
	'use strict';

	QUnit.module( 'ext.smw.Query', QUnit.newMwEnvironment() );
	var jsonString = '{\"conditions\":\"[[Modification date::+]]\",\"parameters\":{\"limit\":10,\"offset\":0,\"link\":\"all\",\"headers\":\"show\",\"mainlabel\":\"\",\"intro\":\"\",\"outro\":\"\",\"searchlabel\":\"\\u2026 further results\",\"default\":\"\",\"class\":\"\"},\"printouts\":[\"?Modification date\"]}';

	var pass = 'Passes because ';

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

		var result;

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

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

	} );

	/**
	 * Test toString sanity
	 *
	 * @since: 1.9
	 * @ignore
	 */
	QUnit.test( 'toString sanity test', function ( assert ) {
		assert.expect( 11 );

		var result;

		assert.throws( function() {
			new smw.query( '', '' ,'' ).toString();
		}, pass + 'an error was raised due to missing conditions' );

		assert.throws( function() {
			new smw.query( [], {} ,'' ).toString();
		}, pass + 'an error was raised due to missing conditions' );

		assert.throws( function() {
			new smw.query( [], [] , '[[Modification date::+]]' ).toString();
		}, pass + 'an error was raised due to parameters being a non object' );

		assert.throws( function() {
			new smw.query( '', [] , '[[Modification date::+]]' ).toString();
		}, pass + 'an error was raised due to parameters being a non object' );

		assert.throws( function() {
			new smw.query( '?Modification date', {'limit' : 10, 'offset': 0 } , '[[Modification date::+]]' ).toString();
		}, pass + 'an error was raised due to printouts weren\'t empty at first, contained values but those weren\'t of type array' );

		assert.throws( function() {
			new smw.query( ['?Modification date'], ['limit'], '[[Modification date::+]]' ).toString();
		}, pass + 'an error was raised due to parameters weren\'t empty at first, contained values but those weren\'t of type object' );

		result = new smw.query( '', '' , ['[[Modification date::+]]'] ).toString();
		assert.equal( result, '[[Modification date::+]]', pass + '.toString() returned a string' );

		result = new smw.query( [], {} , ['[[Modification date::+]]'] ).toString();
		assert.equal( result, '[[Modification date::+]]', pass + '.toString() returned a string' );

		result = new smw.query(
			'',
			{'limit' : 10, 'offset': 0 } ,
			'[[Modification date::+]]'
		).toString();
		assert.equal( result, '[[Modification date::+]]|limit=10|offset=0', pass + '(printouts = empty, parameters = object, conditions = array).toString() returned a string,' );

		result = new smw.query(
			['?Modification date'],
			{'limit' : 10, 'offset': 0 } ,
			'[[Modification date::+]]'
		).toString();
		assert.equal( result, '[[Modification date::+]]|?Modification date|limit=10|offset=0', pass + '(printouts = array, parameters = object, conditions = array).toString() returned a string,' );

		result = new smw.query(
			['?Modification date'],
			{'limit' : 10, 'offset': 0 } ,
			{ foo: '[[Modification date::+]]', bar: '[[Modification date::>2013-04-01]]' }
		).toString();
		assert.equal( result, '[[Modification date::+]][[Modification date::>2013-04-01]]|?Modification date|limit=10|offset=0', pass + '(printouts = array, parameters = object, conditions = object).toString() returned a string,' );

	} );

	/**
	 * Test toString
	 *
	 * @since: 1.9
	 * @ignore
	 */
	QUnit.test( 'toString Ajax response test', function ( assert ) {
		assert.expect( 4 );

		var done = assert.async();

		var smwApi = new smw.api();
		var queryObject = smwApi.parse( jsonString );

		var query = new smw.query ( queryObject.printouts, queryObject.parameters, queryObject.conditions );

		assert.ok( $.type( query.toString() ) === 'string', pass + 'the query is a string' );
		assert.ok( $.type( query.getQueryString() ) === 'string', pass + 'the function alias returned a string' );

		// Ajax
		smwApi.fetch( query.toString() )
		.done( function ( results ) {

			assert.ok( true, pass + 'the query returned with a positive server response' );
			assert.ok( results instanceof Object, pass + 'the query returned with a result object' );

			done();
		} );

	} );

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

		var smwApi = new smw.api();
		var queryObject = smwApi.parse( jsonString );

		var query = new smw.query ( queryObject.printouts, queryObject.parameters, queryObject.conditions );
		assert.equal( query.getLimit(), 10, pass + 'the query limit parameter returned 10' );

	} );


	/**
	 * Test getLink
	 *
	 * @since: 1.9
	 * @ignore
	 */
	QUnit.test( 'getLink', function ( assert ) {
		assert.expect( 5 );

		var result, context;

		var smwApi = new smw.api();
		var queryObject = smwApi.parse( jsonString );

		result = new smw.query ( queryObject.printouts, queryObject.parameters, queryObject.conditions ).getLink();
		assert.equal( $.type( result ), 'string', pass + 'the query link returned was a string' );

		// DOM test
		context = $( '<div></div>', '#qunit-fixture' );
		context.append( result );
		assert.equal( context.find( 'a' ).attr( 'class' ), 'query-link', pass + 'DOM object returned class attribute "query-link"' );
		assert.ok( context.find( 'a' ).attr( 'href' ), pass + 'DOM object returned a href attribute' );

		// Caption text test
		context = $( '<div></div>', '#qunit-fixture' );
		queryObject.parameters.searchlabel = 'test';
		result = new smw.query ( queryObject.printouts, queryObject.parameters, queryObject.conditions ).getLink();
		context.append( result );
		assert.equal( context.find( 'a' ).text( ), 'test', pass + 'parameters.searchlabel is used to set the caption text' );

		context = $( '<div></div>', '#qunit-fixture' );
		result = new smw.query ( queryObject.printouts, queryObject.parameters, queryObject.conditions ).getLink( 'test 2' );
		context.append( result );
		assert.equal( context.find( 'a' ).text( ), 'test 2', pass + 'getLink() is used to set the caption text' );

	} );

}( jQuery, mediaWiki, semanticMediaWiki ) );