1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Tests; |
4
|
|
|
|
5
|
|
|
use SMW\DIProperty; |
6
|
|
|
use SMW\DIWikiPage; |
7
|
|
|
use SMW\HashBuilder; |
8
|
|
|
use SMW\SemanticData; |
9
|
|
|
use Title; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers \SMW\HashBuilder |
13
|
|
|
* @group semantic-mediawiki |
14
|
|
|
* |
15
|
|
|
* @license GNU GPL v2+ |
16
|
|
|
* @since 2.1 |
17
|
|
|
* |
18
|
|
|
* @author mwjames |
19
|
|
|
*/ |
20
|
|
|
class HashBuilderTest extends \PHPUnit_Framework_TestCase { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @dataProvider segmentProvider |
24
|
|
|
*/ |
25
|
|
|
public function testTitleRoundTrip( $namespace, $title, $interwiki , $fragment ) { |
26
|
|
|
|
27
|
|
|
$title = Title::makeTitle( $namespace, $title, $fragment, $interwiki ); |
28
|
|
|
|
29
|
|
|
$this->assertEquals( |
30
|
|
|
$title, |
31
|
|
|
HashBuilder::newTitleFromHash( |
32
|
|
|
HashBuilder::getHashIdForTitle( $title ) |
33
|
|
|
) |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @dataProvider segmentProvider |
39
|
|
|
*/ |
40
|
|
|
public function testDiWikiPageRoundTrip( $namespace, $title, $interwiki, $subobjectName ) { |
41
|
|
|
|
42
|
|
|
$dataItem = new DIWikiPage( $title, $namespace, $interwiki, $subobjectName ); |
43
|
|
|
|
44
|
|
|
$this->assertEquals( |
45
|
|
|
$dataItem, |
46
|
|
|
HashBuilder::newDiWikiPageFromHash( |
47
|
|
|
HashBuilder::getHashIdForDiWikiPage( $dataItem ) |
48
|
|
|
) |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testPredefinedProperty() { |
53
|
|
|
|
54
|
|
|
$instance = new HashBuilder(); |
55
|
|
|
|
56
|
|
|
$property = new DIProperty( '_MDAT' ); |
57
|
|
|
$dataItem = $property->getDiWikiPage(); |
58
|
|
|
|
59
|
|
|
$this->assertEquals( |
60
|
|
|
$dataItem, |
61
|
|
|
$instance->newDiWikiPageFromHash( |
62
|
|
|
$instance->getHashIdForDiWikiPage( $dataItem ) |
|
|
|
|
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$this->assertEquals( |
67
|
|
|
$dataItem, |
68
|
|
|
$instance->newDiWikiPageFromHash( |
69
|
|
|
$instance->createHashIdFromSegments( $property->getKey(), SMW_NS_PROPERTY ) |
|
|
|
|
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testContentHashId() { |
75
|
|
|
|
76
|
|
|
$hash = HashBuilder::createHashIdForContent( 'Foo' ); |
77
|
|
|
|
78
|
|
|
$this->assertInternalType( |
79
|
|
|
'string', |
80
|
|
|
$hash |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$this->assertSame( |
84
|
|
|
$hash, |
85
|
|
|
HashBuilder::createHashIdForContent( array( 'Foo' ) ) |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$this->assertContains( |
89
|
|
|
'Bar', |
90
|
|
|
HashBuilder::createHashIdForContent( array( 'Foo' ), 'Bar' ) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testCreateFromSemanticData() { |
95
|
|
|
|
96
|
|
|
$semanticData = new SemanticData( |
97
|
|
|
DIWikiPage::newFromText( __METHOD__ ) |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$this->assertInternalType( |
101
|
|
|
'string', |
102
|
|
|
HashBuilder::createFromSemanticData( $semanticData ) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function segmentProvider() { |
107
|
|
|
|
108
|
|
|
$provider[] = array( NS_FILE, 'ichi', '', '' ); |
|
|
|
|
109
|
|
|
$provider[] = array( NS_HELP, 'ichi', 'ni', '' ); |
110
|
|
|
$provider[] = array( NS_MAIN, 'ichi maru', 'ni', 'san' ); |
111
|
|
|
|
112
|
|
|
return $provider; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: