1<?php 2 3namespace Sabre\DAV; 4 5use Sabre\HTTP; 6 7require_once 'Sabre/HTTP/ResponseMock.php'; 8require_once 'Sabre/DAV/AbstractServer.php'; 9 10class GetIfConditionsTest extends AbstractServer { 11 12 function testNoConditions() { 13 14 $request = new HTTP\Request(); 15 16 $conditions = $this->server->getIfConditions($request); 17 $this->assertEquals(array(),$conditions); 18 19 } 20 21 function testLockToken() { 22 23 $request = new HTTP\Request('GET', '/path/', ['If' => '(<opaquelocktoken:token1>)']); 24 $conditions = $this->server->getIfConditions($request); 25 26 $compare = array( 27 28 array( 29 'uri' => 'path', 30 'tokens' => array( 31 array( 32 'negate' => false, 33 'token' => 'opaquelocktoken:token1', 34 'etag' => '', 35 ), 36 ), 37 38 ), 39 40 ); 41 42 $this->assertEquals($compare,$conditions); 43 44 } 45 46 function testNotLockToken() { 47 48 $serverVars = array( 49 'HTTP_IF' => '(Not <opaquelocktoken:token1>)', 50 'REQUEST_URI' => '/bla' 51 ); 52 53 $request = HTTP\Sapi::createFromServerArray($serverVars); 54 $conditions = $this->server->getIfConditions($request); 55 56 $compare = array( 57 58 array( 59 'uri' => 'bla', 60 'tokens' => array( 61 array( 62 'negate' => true, 63 'token' => 'opaquelocktoken:token1', 64 'etag' => '', 65 ), 66 ), 67 68 ), 69 70 ); 71 $this->assertEquals($compare,$conditions); 72 73 } 74 75 function testLockTokenUrl() { 76 77 $serverVars = array( 78 'HTTP_IF' => '<http://www.example.com/> (<opaquelocktoken:token1>)', 79 ); 80 81 $request = HTTP\Sapi::createFromServerArray($serverVars); 82 $conditions = $this->server->getIfConditions($request); 83 84 $compare = array( 85 86 array( 87 'uri' => '', 88 'tokens' => array( 89 array( 90 'negate' => false, 91 'token' => 'opaquelocktoken:token1', 92 'etag' => '', 93 ), 94 ), 95 96 ), 97 98 ); 99 $this->assertEquals($compare,$conditions); 100 101 } 102 103 function test2LockTokens() { 104 105 $serverVars = array( 106 'HTTP_IF' => '(<opaquelocktoken:token1>) (Not <opaquelocktoken:token2>)', 107 'REQUEST_URI' => '/bla', 108 ); 109 110 $request = HTTP\Sapi::createFromServerArray($serverVars); 111 $conditions = $this->server->getIfConditions($request); 112 113 $compare = array( 114 115 array( 116 'uri' => 'bla', 117 'tokens' => array( 118 array( 119 'negate' => false, 120 'token' => 'opaquelocktoken:token1', 121 'etag' => '', 122 ), 123 array( 124 'negate' => true, 125 'token' => 'opaquelocktoken:token2', 126 'etag' => '', 127 ), 128 ), 129 130 ), 131 132 ); 133 $this->assertEquals($compare,$conditions); 134 135 } 136 137 function test2UriLockTokens() { 138 139 $serverVars = array( 140 'HTTP_IF' => '<http://www.example.org/node1> (<opaquelocktoken:token1>) <http://www.example.org/node2> (Not <opaquelocktoken:token2>)', 141 ); 142 143 $request = HTTP\Sapi::createFromServerArray($serverVars); 144 $conditions = $this->server->getIfConditions($request); 145 146 $compare = array( 147 148 array( 149 'uri' => 'node1', 150 'tokens' => array( 151 array( 152 'negate' => false, 153 'token' => 'opaquelocktoken:token1', 154 'etag' => '', 155 ), 156 ), 157 ), 158 array( 159 'uri' => 'node2', 160 'tokens' => array( 161 array( 162 'negate' => true, 163 'token' => 'opaquelocktoken:token2', 164 'etag' => '', 165 ), 166 ), 167 168 ), 169 170 ); 171 $this->assertEquals($compare,$conditions); 172 173 } 174 175 function test2UriMultiLockTokens() { 176 177 $serverVars = array( 178 'HTTP_IF' => '<http://www.example.org/node1> (<opaquelocktoken:token1>) (<opaquelocktoken:token2>) <http://www.example.org/node2> (Not <opaquelocktoken:token3>)', 179 ); 180 181 $request = HTTP\Sapi::createFromServerArray($serverVars); 182 $conditions = $this->server->getIfConditions($request); 183 184 $compare = array( 185 186 array( 187 'uri' => 'node1', 188 'tokens' => array( 189 array( 190 'negate' => false, 191 'token' => 'opaquelocktoken:token1', 192 'etag' => '', 193 ), 194 array( 195 'negate' => false, 196 'token' => 'opaquelocktoken:token2', 197 'etag' => '', 198 ), 199 ), 200 ), 201 array( 202 'uri' => 'node2', 203 'tokens' => array( 204 array( 205 'negate' => true, 206 'token' => 'opaquelocktoken:token3', 207 'etag' => '', 208 ), 209 ), 210 211 ), 212 213 ); 214 $this->assertEquals($compare,$conditions); 215 216 } 217 218 function testEtag() { 219 220 $serverVars = array( 221 'HTTP_IF' => '(["etag1"])', 222 'REQUEST_URI' => '/foo', 223 ); 224 225 $request = HTTP\Sapi::createFromServerArray($serverVars); 226 $conditions = $this->server->getIfConditions($request); 227 228 $compare = array( 229 230 array( 231 'uri' => 'foo', 232 'tokens' => array( 233 array( 234 'negate' => false, 235 'token' => '', 236 'etag' => '"etag1"', 237 ), 238 ), 239 ), 240 241 ); 242 $this->assertEquals($compare,$conditions); 243 244 } 245 246 function test2Etags() { 247 248 $serverVars = array( 249 'HTTP_IF' => '<http://www.example.org/> (["etag1"]) (["etag2"])', 250 ); 251 252 $request = HTTP\Sapi::createFromServerArray($serverVars); 253 $conditions = $this->server->getIfConditions($request); 254 255 $compare = array( 256 257 array( 258 'uri' => '', 259 'tokens' => array( 260 array( 261 'negate' => false, 262 'token' => '', 263 'etag' => '"etag1"', 264 ), 265 array( 266 'negate' => false, 267 'token' => '', 268 'etag' => '"etag2"', 269 ), 270 ), 271 ), 272 273 ); 274 $this->assertEquals($compare,$conditions); 275 276 } 277 278 function testComplexIf() { 279 280 $serverVars = array( 281 'HTTP_IF' => '<http://www.example.org/node1> (<opaquelocktoken:token1> ["etag1"]) ' . 282 '(Not <opaquelocktoken:token2>) (["etag2"]) <http://www.example.org/node2> ' . 283 '(<opaquelocktoken:token3>) (Not <opaquelocktoken:token4>) (["etag3"])', 284 ); 285 286 $request = HTTP\Sapi::createFromServerArray($serverVars); 287 $conditions = $this->server->getIfConditions($request); 288 289 $compare = array( 290 291 array( 292 'uri' => 'node1', 293 'tokens' => array( 294 array( 295 'negate' => false, 296 'token' => 'opaquelocktoken:token1', 297 'etag' => '"etag1"', 298 ), 299 array( 300 'negate' => true, 301 'token' => 'opaquelocktoken:token2', 302 'etag' => '', 303 ), 304 array( 305 'negate' => false, 306 'token' => '', 307 'etag' => '"etag2"', 308 ), 309 ), 310 ), 311 array( 312 'uri' => 'node2', 313 'tokens' => array( 314 array( 315 'negate' => false, 316 'token' => 'opaquelocktoken:token3', 317 'etag' => '', 318 ), 319 array( 320 'negate' => true, 321 'token' => 'opaquelocktoken:token4', 322 'etag' => '', 323 ), 324 array( 325 'negate' => false, 326 'token' => '', 327 'etag' => '"etag3"', 328 ), 329 ), 330 ), 331 332 ); 333 $this->assertEquals($compare,$conditions); 334 335 } 336 337} 338