1<?php
2
3/**
4 * Tests types.
5 *
6 * @group plugin_strata
7 * @group plugins
8 */
9class types_test extends DokuWikiTest {
10
11	function setup() {
12		$this->_types = new helper_plugin_strata_util();
13	}
14
15	function testString() {
16		$type = $this->_types->loadType('text');
17		// Empty hint
18		$s = $type->normalize('bob', '');
19		$this->assertEquals($s, 'bob');
20		// Empty hint
21		$s = $type->normalize('Bob', '');
22		$this->assertEquals($s, 'Bob');
23		// Numerical hint
24		$s = $type->normalize('Bob', 10);
25		$this->assertEquals($s, 'Bob');
26		// String hint
27		$s = $type->normalize('Bob', 'master');
28		$this->assertEquals($s, 'Bob');
29		// Whitespace
30		$s = $type->normalize('  Bob   ', '');
31		$this->assertEquals($s, '  Bob   ');
32		// Special characters
33		$s = $type->normalize('Bob & Alice', '');
34		$this->assertEquals($s, 'Bob & Alice');
35		// Unicode
36		$s = $type->normalize('Één ís één.', '');
37		$this->assertEquals($s, 'Één ís één.');
38	}
39
40	function testPage() {
41		$type = $this->_types->loadType('page');
42		// Empty hint
43		$s = $type->normalize('bob', '');
44		$this->assertEquals($s, 'bob');
45		// Empty hint
46		$s = $type->normalize('Bob', '');
47		$this->assertEquals($s, 'bob');
48		// Numerical hint
49		$s = $type->normalize('Bob', 10);
50		$this->assertEquals($s, '10:bob');
51		// String hint
52		$s = $type->normalize('Bob', 'master');
53		$this->assertEquals($s, 'master:bob');
54		// Whitespace
55		$s = $type->normalize('  Bob   ', '');
56		$this->assertEquals($s, 'bob');
57		// Special characters
58		$s = $type->normalize('Bob & Alice', '');
59		$this->assertEquals($s, 'bob_alice');
60		// Unicode
61		$s = $type->normalize('Één ís één.', '');
62		$this->assertEquals($s, 'een_is_een');
63		// Relative pathes
64		$s = $type->normalize('..:.:Bob', 'master:user');
65		$this->assertEquals($s, 'master:bob');
66		$s = $type->normalize('.:..:Bob', 'master:user');
67		$this->assertEquals($s, 'master:bob');
68        // Fragments in url (link to namespace start)
69		$s = $type->normalize(':#Bob', 'master:user');
70		$this->assertEquals($s, 'start#bob');
71		$s = $type->normalize('.:#Bob', 'master:user');
72		$this->assertEquals($s, 'master:user:start#bob');
73		$s = $type->normalize('..:#Bob', 'master:user');
74		$this->assertEquals($s, 'master:start#bob');
75	}
76
77	function testPageWithID() {
78		// Set ID
79		global $ID;
80		$this->assertEquals($ID, null); // Test whether the test suite is initialised as expected.
81		$ID = 'an_id:sub:current_page';
82
83		$type = $this->_types->loadType('page');
84		// Empty hint
85		$s = $type->normalize('bob', '');
86		$this->assertEquals($s, 'an_id:sub:bob');
87		// Empty hint
88		$s = $type->normalize('Bob', '');
89		$this->assertEquals($s, 'an_id:sub:bob');
90		// Numerical hint
91		$s = $type->normalize('Bob', 10);
92		$this->assertEquals($s, '10:bob');
93		// String hint
94		$s = $type->normalize('Bob', 'master');
95		$this->assertEquals($s, 'master:bob');
96		// Whitespace
97		$s = $type->normalize('  Bob   ', '');
98		$this->assertEquals($s, 'an_id:sub:bob');
99		// Special characters
100		$s = $type->normalize('Bob & Alice', '');
101		$this->assertEquals($s, 'an_id:sub:bob_alice');
102		// Unicode
103		$s = $type->normalize('Één ís één.', '');
104		$this->assertEquals($s, 'an_id:sub:een_is_een');
105		// Relative pathes w.r.t. given namespace
106		$s = $type->normalize('..:.:Bob', 'master:user');
107		$this->assertEquals($s, 'master:bob');
108		$s = $type->normalize('.:..:Bob', 'master:user');
109		$this->assertEquals($s, 'master:bob');
110        // Fragments in url w.r.t. given namespace
111		$s = $type->normalize('.:#Bob', 'master:user');
112		$this->assertEquals($s, 'master:user:start#bob');
113		$s = $type->normalize('..:#Bob', 'master:user');
114		$this->assertEquals($s, 'master:start#bob');
115		$s = $type->normalize('#Bob', 'master:user');
116		$this->assertEquals($s, 'an_id:sub:current_page#bob');
117		// Relative pathes w.r.t. ID
118		$s = $type->normalize('..:.:Bob', '');
119		$this->assertEquals($s, 'an_id:bob');
120		$s = $type->normalize('.:..:Bob', '');
121		$this->assertEquals($s, 'an_id:bob');
122        // Fragments in url w.r.t. ID
123		$s = $type->normalize('.:#Bob', '');
124		$this->assertEquals($s, 'an_id:sub:start#bob');
125		$s = $type->normalize('..:#Bob', '');
126		$this->assertEquals($s, 'an_id:start#bob');
127		$s = $type->normalize('#Bob', '');
128		$this->assertEquals($s, 'an_id:sub:current_page#bob');
129		$s = $type->normalize('Other Page#Bob', '');
130		$this->assertEquals($s, 'an_id:sub:other_page#bob');
131
132		// Restore global to avoid interference with other tests
133		$ID = null;
134	}
135
136	function testRef() {
137		$type = $this->_types->loadType('ref');
138		// Empty hint
139		$s = $type->normalize('bob', '');
140		$this->assertEquals($s, 'bob');
141		// Empty hint
142		$s = $type->normalize('Bob', '');
143		$this->assertEquals($s, 'bob');
144		// Numerical hint
145		$s = $type->normalize('Bob', 10);
146		$this->assertEquals($s, '10:bob');
147		// String hint
148		$s = $type->normalize('Bob', 'master');
149		$this->assertEquals($s, 'master:bob');
150		// Whitespace
151		$s = $type->normalize('  Bob   ', '');
152		$this->assertEquals($s, 'bob');
153		// Special characters
154		$s = $type->normalize('Bob & Alice', '');
155		$this->assertEquals($s, 'bob_alice');
156		// Unicode
157		$s = $type->normalize('Één ís één.', '');
158		$this->assertEquals($s, 'een_is_een');
159		// Relative pathes
160		$s = $type->normalize('..:.:Bob', 'master:user');
161		$this->assertEquals($s, 'master:bob');
162		$s = $type->normalize('.:..:Bob', 'master:user');
163		$this->assertEquals($s, 'master:bob');
164        // Fragments in url (link to namespace)
165		$s = $type->normalize('.:#Bob', 'master:user');
166		$this->assertEquals($s, 'master:user:start#bob');
167		$s = $type->normalize('..:#Bob', 'master:user');
168		$this->assertEquals($s, 'master:start#bob');
169		$s = $type->normalize(':#Bob', 'master:user');
170		$this->assertEquals($s, 'start#bob');
171	}
172
173	function testImage() {
174		$type = $this->_types->loadType('image');
175		// Empty hint
176		$s = $type->normalize('bob.png', '');
177		$this->assertEquals($s, 'bob.png');
178		// Empty hint
179		$s = $type->normalize('Bob.png', '');
180		$this->assertEquals($s, 'bob.png');
181		// Numerical hint
182		$s = $type->normalize('Bob.png', 10);
183		$this->assertEquals($s, 'bob.png');
184		// Whitespace
185		$s = $type->normalize('  Bob.png   ', '');
186		$this->assertEquals($s, 'bob.png');
187		// Special characters
188		$s = $type->normalize('Bob & Alice.png', '');
189		$this->assertEquals($s, 'bob_alice.png');
190		// Unicode
191		$s = $type->normalize('Één ís één.png', '');
192		$this->assertEquals($s, 'een_is_een.png');
193	}
194}
195
196