summaryrefslogtreecommitdiff
blob: 7094c300eece8b0eda99051f1285b08b52408da8 (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
<?php

namespace SMW\Tests;

use SMW\Tests\Util\ParserFactory;

use SMW\ConceptParserFunction;
use SMW\MessageFormatter;
use SMW\ParserData;

use Title;
use ParserOutput;

/**
 * @covers \SMW\ConceptParserFunction
 *
 * @ingroup Test
 *
 * @group SMW
 * @group SMWExtension
 * @group medium
 *
 * @license GNU GPL v2+
 * @since   1.9
 *
 * @author mwjames
 */
class ConceptParserFunctionTest extends \PHPUnit_Framework_TestCase {

	public function testCanConstruct() {

		$this->assertInstanceOf(
			'\SMW\ConceptParserFunction',
			$this->newInstance()
		);
	}

	/**
	 * @dataProvider namespaceDataProvider
	 */
	public function testErrorOnNamespace( $namespace ) {

		$title = Title::newFromText( __METHOD__, $namespace );

		$instance = $this->newInstance( $title, new ParserOutput() );

		$this->assertEquals(
			$this->getMessageText( $title, 'smw_no_concept_namespace' ),
			$instance->parse( array() )
		);
	}

	/**
	 * @dataProvider queryParameterProvider
	 */
	public function testErrorOnDoubleParse( array $params ) {

		$title = Title::newFromText( __METHOD__, SMW_NS_CONCEPT );

		$instance = $this->newInstance( $title, new ParserOutput() );
 		$instance->parse( $params );

		$instance->parse( $params );

		$this->assertEquals(
			$this->getMessageText( $title, 'smw_multiple_concepts' ),
			$instance->parse( $params )
		);
	}

	/**
	 * @dataProvider queryParameterProvider
	 */
	public function testParse( array $params, array $expected ) {

		$parserOutput =  new ParserOutput();
		$title = Title::newFromText( __METHOD__, SMW_NS_CONCEPT );

		$instance = $this->newInstance( $title, $parserOutput );
		$instance->parse( $params );

		$parserData = new ParserData( $title, $parserOutput );

		$this->assertCount(
			$expected['propertyCount'],
			$parserData->getSemanticData()->getProperties()
		);

		// Confirm concept property
		foreach ( $parserData->getSemanticData()->getProperties() as $key => $diproperty ){
			$this->assertInstanceOf( 'SMWDIProperty', $diproperty );
			$this->assertEquals( '_CONC' , $diproperty->getKey() );

			// Confirm concept property values
			foreach ( $parserData->getSemanticData()->getPropertyValues( $diproperty ) as $dataItem ){
				$this->assertEquals( $expected['conceptQuery'], $dataItem->getConceptQuery() );
				$this->assertEquals( $expected['conceptDocu'], $dataItem->getDocumentation() );
				$this->assertEquals( $expected['conceptSize'], $dataItem->getSize() );
				$this->assertEquals( $expected['conceptDepth'], $dataItem->getDepth() );
			}
		}
	}

	public function testStaticRender() {

		$parser = ParserFactory::newFromTitle( Title::newFromText( __METHOD__ ) );

		$this->assertInternalType(
			'string',
			ConceptParserFunction::render( $parser )
		);
	}

	/**
	 * @return array
	 */
	public function queryParameterProvider() {

		$provider = array();

		// #0
		// {{#concept: [[Modification date::+]]
		// }}
		$provider[] = array(
			array(
				'[[Modification date::+]]'
			),
			array(
				'result' => true,
				'propertyCount' => 1,
				'conceptQuery'  => '[[Modification date::+]]',
				'conceptDocu'   => '',
				'conceptSize'   => 1,
				'conceptDepth'  => 1,
			)
		);

		// #1
		// {{#concept: [[Modification date::+]]
		// |Foooooooo
		// }}
		$provider[] = array(
			array(
				'[[Modification date::+]]',
				'Foooooooo'
			),
			array(
				'result' => true,
				'propertyCount' => 1,
				'conceptQuery'  => '[[Modification date::+]]',
				'conceptDocu'   => 'Foooooooo',
				'conceptSize'   => 1,
				'conceptDepth'  => 1,
			)
		);

		// #2 (includes Parser object)
		$provider[] = array(
			array(
				ParserFactory::newFromTitle( Title::newFromText( __METHOD__ ) ),
				'[[Modification date::+]]',
				'Foooooooo'
			),
			array(
				'result' => true,
				'propertyCount' => 1,
				'conceptQuery'  => '[[Modification date::+]]',
				'conceptDocu'   => 'Foooooooo',
				'conceptSize'   => 1,
				'conceptDepth'  => 1,
			)
		);

		return $provider;

	}

	/**
	 * NameSpaceDataProvider
	 *
	 * @return array
	 */
	public function namespaceDataProvider() {
		return array(
			array( NS_MAIN ),
			array( NS_HELP )
		);
	}

	private function newInstance( Title $title = null, ParserOutput $parserOutput = null ) {

		if ( $title === null ) {
			$title = Title::newFromText( __METHOD__, SMW_NS_CONCEPT );
		}

		if ( $parserOutput === null ) {
			$parserOutput = new ParserOutput();
		}

		return new ConceptParserFunction(
			new ParserData( $title, $parserOutput ),
			new MessageFormatter( $title->getPageLanguage() )
		);
	}

	private function getMessageText( Title $title, $error ) {
		$message = new MessageFormatter( $title->getPageLanguage() );
		return $message->addFromKey( $error )->getHtml();
	}

}