1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Tests\DataValues\ValueFormatters; |
4
|
|
|
|
5
|
|
|
use SMW\DataValues\ValueFormatters\PropertyValueFormatter; |
6
|
|
|
use SMWPropertyValue as PropertyValue; |
7
|
|
|
use SMW\DataItemFactory; |
8
|
|
|
use SMW\Tests\TestEnvironment; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers \SMW\DataValues\ValueFormatters\PropertyValueFormatter |
12
|
|
|
* @group semantic-mediawiki |
13
|
|
|
* |
14
|
|
|
* @license GNU GPL v2+ |
15
|
|
|
* @since 2.5 |
16
|
|
|
* |
17
|
|
|
* @author mwjames |
18
|
|
|
*/ |
19
|
|
|
class PropertyValueFormatterTest extends \PHPUnit_Framework_TestCase { |
20
|
|
|
|
21
|
|
|
private $dataItemFactory; |
22
|
|
|
private $propertyLabelFinder; |
23
|
|
|
|
24
|
|
|
protected function setUp() { |
25
|
|
|
parent::setUp(); |
26
|
|
|
|
27
|
|
|
$this->testEnvironment = new TestEnvironment(); |
28
|
|
|
$this->dataItemFactory = new DataItemFactory(); |
29
|
|
|
|
30
|
|
|
$this->propertyLabelFinder = $this->getMockBuilder( '\SMW\PropertyLabelFinder' ) |
31
|
|
|
->disableOriginalConstructor() |
32
|
|
|
->getMock(); |
33
|
|
|
|
34
|
|
|
$this->testEnvironment->registerObject( 'PropertyLabelFinder', $this->propertyLabelFinder ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testCanConstruct() { |
38
|
|
|
|
39
|
|
|
$this->assertInstanceOf( |
40
|
|
|
'\SMW\DataValues\ValueFormatters\PropertyValueFormatter', |
41
|
|
|
new PropertyValueFormatter() |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testIsFormatterForValidation() { |
46
|
|
|
|
47
|
|
|
$propertyValue = $this->getMockBuilder( '\SMWPropertyValue' ) |
48
|
|
|
->disableOriginalConstructor() |
49
|
|
|
->getMock(); |
50
|
|
|
|
51
|
|
|
$instance = new PropertyValueFormatter(); |
52
|
|
|
|
53
|
|
|
$this->assertTrue( |
54
|
|
|
$instance->isFormatterFor( $propertyValue ) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testWithCaptionOutput() { |
59
|
|
|
|
60
|
|
|
$propertyValue = new PropertyValue(); |
61
|
|
|
$propertyValue->setDataItem( $this->dataItemFactory->newDIProperty( 'Foo' ) ); |
62
|
|
|
$propertyValue->setCaption( 'ABC[<>]' ); |
63
|
|
|
|
64
|
|
|
$instance = new PropertyValueFormatter( $propertyValue ); |
65
|
|
|
|
66
|
|
|
$this->assertEquals( |
67
|
|
|
'ABC[<>]', |
68
|
|
|
$instance->format( PropertyValueFormatter::WIKI_SHORT ) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
$this->assertEquals( |
72
|
|
|
'ABC[<>]', |
73
|
|
|
$instance->format( PropertyValueFormatter::HTML_SHORT ) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @dataProvider propertyValueProvider |
79
|
|
|
*/ |
80
|
|
|
public function testFormat( $property, $type, $linker, $expected ) { |
81
|
|
|
|
82
|
|
|
$propertyValue = new PropertyValue(); |
83
|
|
|
$propertyValue->setDataItem( $property ); |
84
|
|
|
|
85
|
|
|
$propertyValue->setOption( PropertyValue::OPT_CONTENT_LANGUAGE, 'en' ); |
|
|
|
|
86
|
|
|
$propertyValue->setOption( PropertyValue::OPT_USER_LANGUAGE, 'en' ); |
|
|
|
|
87
|
|
|
|
88
|
|
|
$instance = new PropertyValueFormatter( $propertyValue ); |
89
|
|
|
|
90
|
|
|
$this->assertEquals( |
91
|
|
|
$expected, |
92
|
|
|
$instance->format( $type, $linker ) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @dataProvider preferredLabelValueProvider |
98
|
|
|
*/ |
99
|
|
|
public function testFormatWithPreferredLabel( $property, $preferredLabel, $type, $linker, $expected ) { |
100
|
|
|
|
101
|
|
|
// Ensures the mocked instance is injected and registered with the |
102
|
|
|
// PropertyRegistry instance |
103
|
|
|
\SMW\PropertyRegistry::clear(); |
104
|
|
|
|
105
|
|
|
$this->propertyLabelFinder->expects( $this->any() ) |
106
|
|
|
->method( 'findPreferredPropertyLabelByLanguageCode' ) |
107
|
|
|
->will( $this->returnValue( $preferredLabel ) ); |
108
|
|
|
|
109
|
|
|
$this->propertyLabelFinder->expects( $this->any() ) |
110
|
|
|
->method( 'searchPropertyIdByLabel' ) |
111
|
|
|
->will( $this->returnValue( false ) ); |
112
|
|
|
|
113
|
|
|
$propertyValue = new PropertyValue(); |
114
|
|
|
|
115
|
|
|
$propertyValue->setOption( 'smwgDVFeatures', SMW_DV_PROV_LHNT ); |
|
|
|
|
116
|
|
|
$propertyValue->setOption( PropertyValue::OPT_CONTENT_LANGUAGE, 'en' ); |
|
|
|
|
117
|
|
|
$propertyValue->setOption( PropertyValue::OPT_USER_LANGUAGE, 'en' ); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$propertyValue->setUserValue( $property ); |
120
|
|
|
|
121
|
|
|
$instance = new PropertyValueFormatter( $propertyValue ); |
122
|
|
|
|
123
|
|
|
$this->assertEquals( |
124
|
|
|
$expected, |
125
|
|
|
$instance->format( $type, $linker ) |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
\SMW\PropertyRegistry::clear(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testTryToFormatOnMissingDataValueThrowsException() { |
132
|
|
|
|
133
|
|
|
$instance = new PropertyValueFormatter(); |
134
|
|
|
|
135
|
|
|
$this->setExpectedException( 'RuntimeException' ); |
136
|
|
|
$instance->format( PropertyValueFormatter::VALUE ); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function propertyValueProvider() { |
140
|
|
|
|
141
|
|
|
$dataItemFactory = new DataItemFactory(); |
142
|
|
|
|
143
|
|
|
$provider[] = array( |
|
|
|
|
144
|
|
|
$dataItemFactory->newDIProperty( 'Foo' ), |
145
|
|
|
PropertyValueFormatter::VALUE, |
146
|
|
|
null, |
147
|
|
|
'Foo' |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
$provider[] = array( |
151
|
|
|
$dataItemFactory->newDIProperty( 'Foo' ), |
152
|
|
|
PropertyValueFormatter::WIKI_SHORT, |
153
|
|
|
null, |
154
|
|
|
'Foo' |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
$provider[] = array( |
158
|
|
|
$dataItemFactory->newDIProperty( 'Foo' ), |
159
|
|
|
PropertyValueFormatter::HTML_SHORT, |
160
|
|
|
null, |
161
|
|
|
'Foo' |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
$provider[] = array( |
165
|
|
|
$dataItemFactory->newDIProperty( 'Foo' ), |
166
|
|
|
PropertyValueFormatter::WIKI_LONG, |
167
|
|
|
null, |
168
|
|
|
'Property:Foo' |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
$provider[] = array( |
172
|
|
|
$dataItemFactory->newDIProperty( 'Foo' ), |
173
|
|
|
PropertyValueFormatter::HTML_LONG, |
174
|
|
|
null, |
175
|
|
|
'Property:Foo' |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
return $provider; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function preferredLabelValueProvider() { |
182
|
|
|
|
183
|
|
|
$dataItemFactory = new DataItemFactory(); |
|
|
|
|
184
|
|
|
$linker = 'some'; |
185
|
|
|
|
186
|
|
|
$provider[] = array( |
|
|
|
|
187
|
|
|
'Foo', |
188
|
|
|
'Bar', |
189
|
|
|
PropertyValueFormatter::VALUE, |
190
|
|
|
null, |
191
|
|
|
'Bar' |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
$provider[] = array( |
195
|
|
|
'Foo', |
196
|
|
|
'Bar', |
197
|
|
|
PropertyValueFormatter::WIKI_SHORT, |
198
|
|
|
null, |
199
|
|
|
'Bar <span title="Foo"><sup>ᵖ</sup></span>' |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
$provider[] = array( |
203
|
|
|
'Foo', |
204
|
|
|
'Bar', |
205
|
|
|
PropertyValueFormatter::HTML_SHORT, |
206
|
|
|
null, |
207
|
|
|
'Bar <span title="Foo"><sup>ᵖ</sup></span>' |
208
|
|
|
); |
209
|
|
|
|
210
|
|
|
$provider[] = array( |
211
|
|
|
'Foo', |
212
|
|
|
'Bar', |
213
|
|
|
PropertyValueFormatter::WIKI_LONG, |
214
|
|
|
$linker, |
215
|
|
|
'[[:Property:Foo|Bar]] <span title="Foo"><sup>ᵖ</sup></span>' |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
$provider[] = array( |
219
|
|
|
'Foo', |
220
|
|
|
'Bar', |
221
|
|
|
PropertyValueFormatter::HTML_LONG, |
222
|
|
|
null, |
223
|
|
|
'Property:Foo <span title="Foo"><sup>ᵖ</sup></span>' |
224
|
|
|
); |
225
|
|
|
|
226
|
|
|
return $provider; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: