]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | /** | |
3 | * Utilities' tests | |
4 | */ | |
5 | ||
6 | require_once 'application/Utils.php'; | |
7 | ||
8 | /** | |
9 | * Unitary tests for Shaarli utilities | |
10 | */ | |
11 | class UtilsTest extends PHPUnit_Framework_TestCase | |
12 | { | |
13 | /** | |
14 | * Represent a link by its hash | |
15 | */ | |
16 | public function testSmallHash() | |
17 | { | |
18 | $this->assertEquals('CyAAJw', smallHash('http://test.io')); | |
19 | $this->assertEquals(6, strlen(smallHash('https://github.com'))); | |
20 | } | |
21 | ||
22 | /** | |
23 | * Look for a substring at the beginning of a string | |
24 | */ | |
25 | public function testStartsWithCaseInsensitive() | |
26 | { | |
27 | $this->assertTrue(startsWith('Lorem ipsum', 'lorem', false)); | |
28 | $this->assertTrue(startsWith('Lorem ipsum', 'LoReM i', false)); | |
29 | } | |
30 | ||
31 | /** | |
32 | * Look for a substring at the beginning of a string (case-sensitive) | |
33 | */ | |
34 | public function testStartsWithCaseSensitive() | |
35 | { | |
36 | $this->assertTrue(startsWith('Lorem ipsum', 'Lorem', true)); | |
37 | $this->assertFalse(startsWith('Lorem ipsum', 'lorem', true)); | |
38 | $this->assertFalse(startsWith('Lorem ipsum', 'LoReM i', true)); | |
39 | } | |
40 | ||
41 | /** | |
42 | * Look for a substring at the beginning of a string (Unicode) | |
43 | */ | |
44 | public function testStartsWithSpecialChars() | |
45 | { | |
46 | $this->assertTrue(startsWith('å!ùµ', 'å!', false)); | |
47 | $this->assertTrue(startsWith('µ$åù', 'µ$', true)); | |
48 | } | |
49 | ||
50 | /** | |
51 | * Look for a substring at the end of a string | |
52 | */ | |
53 | public function testEndsWithCaseInsensitive() | |
54 | { | |
55 | $this->assertTrue(endsWith('Lorem ipsum', 'ipsum', false)); | |
56 | $this->assertTrue(endsWith('Lorem ipsum', 'm IpsUM', false)); | |
57 | } | |
58 | ||
59 | /** | |
60 | * Look for a substring at the end of a string (case-sensitive) | |
61 | */ | |
62 | public function testEndsWithCaseSensitive() | |
63 | { | |
64 | $this->assertTrue(endsWith('lorem Ipsum', 'Ipsum', true)); | |
65 | $this->assertFalse(endsWith('lorem Ipsum', 'ipsum', true)); | |
66 | $this->assertFalse(endsWith('lorem Ipsum', 'M IPsuM', true)); | |
67 | } | |
68 | ||
69 | /** | |
70 | * Look for a substring at the end of a string (Unicode) | |
71 | */ | |
72 | public function testEndsWithSpecialChars() | |
73 | { | |
74 | $this->assertTrue(endsWith('å!ùµ', 'ùµ', false)); | |
75 | $this->assertTrue(endsWith('µ$åù', 'åù', true)); | |
76 | } | |
77 | ||
78 | /** | |
79 | * Check valid date strings, according to a DateTime format | |
80 | */ | |
81 | public function testCheckValidDateFormat() | |
82 | { | |
83 | $this->assertTrue(checkDateFormat('Ymd', '20150627')); | |
84 | $this->assertTrue(checkDateFormat('Y-m-d', '2015-06-27')); | |
85 | } | |
86 | ||
87 | /** | |
88 | * Check erroneous date strings, according to a DateTime format | |
89 | */ | |
90 | public function testCheckInvalidDateFormat() | |
91 | { | |
92 | $this->assertFalse(checkDateFormat('Ymd', '2015')); | |
93 | $this->assertFalse(checkDateFormat('Y-m-d', '2015-06')); | |
94 | $this->assertFalse(checkDateFormat('Ymd', 'DeLorean')); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Test generate location with valid data. | |
99 | */ | |
100 | public function testGenerateLocation() { | |
101 | $ref = 'http://localhost/?test'; | |
102 | $this->assertEquals($ref, generateLocation($ref, 'localhost')); | |
103 | $ref = 'http://localhost:8080/?test'; | |
104 | $this->assertEquals($ref, generateLocation($ref, 'localhost:8080')); | |
105 | } | |
106 | ||
107 | /** | |
108 | * Test generate location - anti loop. | |
109 | */ | |
110 | public function testGenerateLocationLoop() { | |
111 | $ref = 'http://localhost/?test'; | |
112 | $this->assertEquals('?', generateLocation($ref, 'localhost', array('test'))); | |
113 | } | |
114 | ||
115 | /** | |
116 | * Test generate location - from other domain. | |
117 | */ | |
118 | public function testGenerateLocationOut() { | |
119 | $ref = 'http://somewebsite.com/?test'; | |
120 | $this->assertEquals('?', generateLocation($ref, 'localhost')); | |
121 | } | |
122 | ||
123 | /** | |
124 | * Check supported PHP versions | |
125 | */ | |
126 | public function testCheckSupportedPHPVersion() | |
127 | { | |
128 | $minVersion = '5.3'; | |
129 | checkPHPVersion($minVersion, '5.4.32'); | |
130 | checkPHPVersion($minVersion, '5.5'); | |
131 | checkPHPVersion($minVersion, '5.6.10'); | |
132 | } | |
133 | ||
134 | /** | |
135 | * Check a unsupported PHP version | |
136 | * @expectedException Exception | |
137 | * @expectedExceptionMessageRegExp /Your PHP version is obsolete/ | |
138 | */ | |
139 | public function testCheckSupportedPHPVersion51() | |
140 | { | |
141 | checkPHPVersion('5.3', '5.1.0'); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Check another unsupported PHP version | |
146 | * @expectedException Exception | |
147 | * @expectedExceptionMessageRegExp /Your PHP version is obsolete/ | |
148 | */ | |
149 | public function testCheckSupportedPHPVersion52() | |
150 | { | |
151 | checkPHPVersion('5.3', '5.2'); | |
152 | } | |
153 | ||
154 | /** | |
155 | * Test is_session_id_valid with a valid ID. | |
156 | */ | |
157 | public function testIsSessionIdValid() | |
158 | { | |
159 | $this->assertTrue(is_session_id_valid('azertyuiop123456789AZERTYUIOP1aA')); | |
160 | } | |
161 | ||
162 | /** | |
163 | * Test is_session_id_valid with invalid IDs. | |
164 | */ | |
165 | public function testIsSessionIdInvalid() | |
166 | { | |
167 | $this->assertFalse(is_session_id_valid('')); | |
168 | $this->assertFalse(is_session_id_valid(array())); | |
169 | $this->assertFalse(is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=')); | |
170 | } | |
171 | } |