~ubuntu-branches/ubuntu/quantal/gallery2/quantal

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/UrlGeneratorTest.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2007-09-10 20:22:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070910202219-0jsuntvqge4ade6b
Tags: 2.2.3-2
Add Slovak translation of Debconf templates.  (Thanks to 
Ivan Masá.  Closes: #441671)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Gallery - a web based photo album viewer and editor
4
 
 * Copyright (C) 2000-2007 Bharat Mediratta
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or (at
9
 
 * your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful, but
12
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 * General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19
 
 */
20
 
 
21
 
/**
22
 
 * URL generator tests.
23
 
 * @package GalleryCore
24
 
 * @subpackage PHPUnit
25
 
 * @author Bharat Mediratta <bharat@menalto.com>
26
 
 * @version $Revision: 15792 $
27
 
 */
28
 
class UrlGeneratorTest extends GalleryTestCase {
29
 
    var $_urlGenerator;
30
 
    var $_savedGet;
31
 
 
32
 
    function UrlGeneratorTest($methodName) {
33
 
        $this->GalleryTestCase($methodName);
34
 
    }
35
 
 
36
 
    function setUp() {
37
 
        parent::setUp();
38
 
        $_SERVER['REQUEST_URI'] = '/gallery2/phpunit_test.php?foo=bar';
39
 
        $_SERVER['HTTP_HOST'] = 'example.com';
40
 
        $_SERVER['SERVER_PORT'] = 80;
41
 
 
42
 
        /* PHP/CGI sets PATH_INFO, but we don't want that for our tests */
43
 
        unset($_SERVER['PATH_INFO']);
44
 
 
45
 
        /* For the cookie.path */
46
 
        $ret = $this->_markPluginParametersForCleanup('module', 'core');
47
 
        if ($ret) {
48
 
            print $ret->getAsHtml();
49
 
            return $ret;
50
 
        }
51
 
 
52
 
        $this->_urlGenerator = new GalleryUrlGenerator();
53
 
        $ret = $this->_urlGenerator->init('phpunit_test.php');
54
 
        if ($ret) {
55
 
            print $ret->getAsHtml();
56
 
            return $ret;
57
 
        }
58
 
 
59
 
        $this->_origEmbed = null;
60
 
        if (GalleryDataCache::containsKey('G2_EMBED')) {
61
 
            $this->_origEmbed = GalleryDataCache::get('G2_EMBED');
62
 
        }
63
 
    }
64
 
 
65
 
    function tearDown() {
66
 
        global $gallery;
67
 
        $session =& $gallery->getSession();
68
 
 
69
 
        if ($this->_origEmbed) {
70
 
            GalleryDataCache::put('G2_EMBED', $this->_origEmbed, true);
71
 
        } else {
72
 
            GalleryDataCache::remove('G2_EMBED');
73
 
            $this->assert(!GalleryDataCache::containsKey('G2_EMBED'), 'Could not remove a GDC var');
74
 
        }
75
 
        $session->remove('core.authToken');
76
 
 
77
 
        parent::tearDown();
78
 
    }
79
 
 
80
 
    /*
81
 
     * Initiates a new url generator
82
 
     */
83
 
    function _initNewUrlGenerator($baseUri=null, $g2Uri=null, $embedSessionString=null) {
84
 
        $this->_urlGenerator = new GalleryUrlGenerator();
85
 
        $baseUri = !isset($baseUri) ? 'phpunit_test.php' : $baseUri;
86
 
        return $this->_urlGenerator->init($baseUri, $g2Uri, $embedSessionString);
87
 
    }
88
 
 
89
 
    /*
90
 
     * Set the cookie path to a new value / or make sure it isn't set. Then init the session
91
 
     * again, since the session and the Url Generator are both involved in this matter.
92
 
     */
93
 
    function _prepareGenerateUrl($cookiePath, $baseUri, $g2Uri, $embedSessionString=null) {
94
 
        global $gallery;
95
 
 
96
 
        /*
97
 
         * Configure the cookie path:
98
 
         *  - if empty (=not set): g2 should append the session id to embedded downloaditem requests
99
 
         *  - else, no session id has to be appended
100
 
         */
101
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'cookie.path', $cookiePath);
102
 
        if ($ret) {
103
 
            return array($ret, null, null);
104
 
        }
105
 
        /* Check if the correct value was stored */
106
 
        list ($ret, $realValue) = GalleryCoreApi::getPluginParameter('module', 'core',
107
 
                                                                     'cookie.path');
108
 
        if ($ret) {
109
 
            return array($ret, null, null);
110
 
        }
111
 
        $this->assertEquals($cookiePath, $realValue, 'Could not reset cookie path');
112
 
 
113
 
        /* Let the url generator know of the new cookie.path value */
114
 
        $this->_urlGenerator = new GalleryUrlGenerator();
115
 
        $ret = $this->_urlGenerator->init($baseUri, $g2Uri, $embedSessionString);
116
 
        if ($ret) {
117
 
            return array($ret, null, null);
118
 
        }
119
 
        $gallery->setUrlGenerator($this->_urlGenerator);
120
 
        /* Let the session know of the new cookie.path value */
121
 
        $session = new GallerySession();
122
 
        $ret = $session->init();
123
 
        if ($ret) {
124
 
            return array($ret, null, null);
125
 
        }
126
 
        $session->put('foo', 1);
127
 
        $ret = $session->start();
128
 
        if ($ret) {
129
 
            return array($ret, null, null);
130
 
        }
131
 
 
132
 
        $gallery->_session = $session;
133
 
 
134
 
        $params['a'] = 123;
135
 
        $params['b'] = 'bBbB';
136
 
        $params['c'] = serialize(array(1, 2, 3));
137
 
        $params['d'] = array('<', 2);
138
 
 
139
 
        $values[] = GalleryUtilities::prefixFormVariable('a') . '=' . urlencode('123');
140
 
        $values[] = GalleryUtilities::prefixFormVariable('b') . '=' . urlencode('bBbB');
141
 
        $values[] = GalleryUtilities::prefixFormVariable('c') . '=' .
142
 
            urlencode(serialize(array(1, 2, 3)));
143
 
        $values[] = GalleryUtilities::prefixFormVariable(urlencode('d[0]')) . '=' . urlencode('<');
144
 
        $values[] = GalleryUtilities::prefixFormVariable(urlencode('d[1]')) . '=' . urlencode('2');
145
 
 
146
 
        return array(null, $params, $values);
147
 
    }
148
 
 
149
 
    function testGenerateUrl() {
150
 
        global $gallery;
151
 
 
152
 
        /* Make sure the cookie path is not set etc. */
153
 
        list ($ret, $params, $values) = $this->_prepareGenerateUrl('', 'phpunit_test.php', null);
154
 
        if ($ret) {
155
 
            $this->failWithStatus($ret);
156
 
        }
157
 
        $session =& $gallery->getSession();
158
 
 
159
 
        $actual = $this->_urlGenerator->generateUrl($params);
160
 
 
161
 
        /* If we're not using cookies, expect there to be an additional value */
162
 
        $session =& $gallery->getSession();
163
 
        if (!$session->isUsingCookies()) {
164
 
            $values[] = GalleryUtilities::prefixFormVariable($session->getKey()) .
165
 
                '=' . urlencode($session->getId());
166
 
        }
167
 
        $expected = 'phpunit_test.php?' . join('&amp;', $values);
168
 
 
169
 
        $this->assertEquals($expected, $actual);
170
 
    }
171
 
 
172
 
    function testGenerateUrlEmbeddedFullBaseUri() {
173
 
        /* Simulate embbeded with full url as embedUri */
174
 
        $_SERVER['REQUEST_URI'] = '/cms/otherpage?foo=bar';
175
 
        GalleryDataCache::put( 'G2_EMBED', 1, true);
176
 
        list ($ret, $params, $values) =
177
 
            $this->_prepareGenerateUrl('',
178
 
                                       'http://example.com/cms/phpunit_test.php?module=gallery2',
179
 
                                       '/gallery2/');
180
 
        if ($ret) {
181
 
            $this->failWithStatus($ret);
182
 
        }
183
 
 
184
 
        $this->assertEquals('/cms/phpunit_test.php?module=gallery2&amp;' .
185
 
            implode('&amp;', $values), $this->_urlGenerator->generateUrl($params),
186
 
            'embedded 1');
187
 
    }
188
 
 
189
 
    function testGenerateUrlEmbeddedPathBaseUri() {
190
 
        /* Simulate embedded, only path + uri */
191
 
        $_SERVER['REQUEST_URI'] = '/cms/phpunit_test.php?module=gallery2&foo=bar';
192
 
        GalleryDataCache::put('G2_EMBED', 1, true);
193
 
 
194
 
        list ($ret, $params, $values) =
195
 
            $this->_prepareGenerateUrl('', '/cms/phpunit_test.php?module=gallery2', '/gallery2/');
196
 
        if ($ret) {
197
 
            $this->failWithStatus($ret);
198
 
        }
199
 
 
200
 
        $this->assertEquals('phpunit_test.php?module=gallery2&amp;' .
201
 
            implode('&amp;', $values), $this->_urlGenerator->generateUrl($params),
202
 
            'embedded 2');
203
 
    }
204
 
 
205
 
    function testGenerateUrlCookielessEmbedded() {
206
 
        global $gallery;
207
 
 
208
 
        /* Simulate cookieless embedded */
209
 
        $_SERVER['REQUEST_URI'] = '/cms/phpunit_test.php?module=gallery2&foo=bar';
210
 
        GalleryDataCache::put( 'G2_EMBED', 1, true);
211
 
        list ($ret, $params, $values) =
212
 
            $this->_prepareGenerateUrl('', '/cms/phpunit_test.php?module=gallery2',
213
 
                                       '/gallery2/', 'CMSSID=123456');
214
 
        if ($ret) {
215
 
            $this->failWithStatus($ret);
216
 
        }
217
 
        $session =& $gallery->getSession();
218
 
 
219
 
        $this->assertEquals('phpunit_test.php?module=gallery2&amp;' .
220
 
                            implode('&amp;', $values) . '&amp;CMSSID=123456',
221
 
                            $this->_urlGenerator->generateUrl($params,
222
 
                                                              array('forceSessionId' => true)),
223
 
                            'cookieless embedded');
224
 
 
225
 
        $this->assertEquals('/gallery2/main.php?g2_view=' .
226
 
                            urlencode('core.DownloadItem') . '&amp;g2_itemId=12345&amp;' .
227
 
                            GalleryUtilities::prefixFormVariable($session->getKey()) . '=' .
228
 
                            urlencode($session->getId()),
229
 
                            $this->_urlGenerator->generateUrl(array('view' => 'core.DownloadItem',
230
 
                                                                    'itemId' => 12345),
231
 
                                                              array('forceSessionId' => true)),
232
 
                            'core.DownloadItem always direct-g2 url, even when embedded');
233
 
 
234
 
        $this->assertEquals('/gallery2/main.php?g2_view=' .
235
 
                            urlencode('test.View') . '&amp;g2_param=12345',
236
 
                            $this->_urlGenerator->generateUrl(
237
 
                                array('view' => 'test.View', 'param' => 12345),
238
 
                                array('forceDirect' => true,
239
 
                                      'forceSessionId' => false)),
240
 
                            'force direct-g2 url, even when embedded');
241
 
    }
242
 
 
243
 
    function testGenerateUrlForView() {
244
 
        $this->assertEquals('phpunit_test.php?g2_view=' . urlencode('test.View') .
245
 
                            '&amp;g2_param=12345',
246
 
                            $this->_urlGenerator->generateUrl(
247
 
                                array('view' => 'test.View', 'param' => 12345)));
248
 
    }
249
 
 
250
 
    function testGenerateUrlForController() {
251
 
        $this->assertEquals($this->_signUrl('phpunit_test.php?g2_controller=' .
252
 
                                            urlencode('test.Controller') . '&amp;g2_param=12345'),
253
 
                            $this->_urlGenerator->generateUrl(
254
 
                                array('controller' => 'test.Controller', 'param' => 12345)));
255
 
    }
256
 
 
257
 
    function testGenerateUrlEmbeddedDownloadItemCookiePathNotSet() {
258
 
        global $gallery;
259
 
 
260
 
        /*
261
 
         * DownloadItem requests in embedded G2 should have the GALLERYSID in the URL if the cookie
262
 
         * path is not defined, independent of cookieless browsing
263
 
         */
264
 
        GalleryDataCache::put('G2_EMBED', 1, true);
265
 
        $_SERVER['REQUEST_URI'] = '/cms/otherpage/?foo=bar';
266
 
        list ($ret) =
267
 
            $this->_prepareGenerateUrl('', '/cms/phpunit_test.php?module=gallery2', '/gallery2/');
268
 
        if ($ret) {
269
 
            $this->failWithStatus($ret);
270
 
        }
271
 
        $session =& $gallery->getSession();
272
 
 
273
 
        /* Do the actual test */
274
 
        $this->assertEquals('/gallery2/main.php?g2_view=' .
275
 
                            urlencode('core.DownloadItem') . '&amp;g2_itemId=12345&amp;' .
276
 
                            GalleryUtilities::prefixFormVariable($session->getKey()) . '=' .
277
 
                            urlencode($session->getId()),
278
 
                            $this->_urlGenerator->generateUrl(array('view' => 'core.DownloadItem',
279
 
                                                                    'itemId' => 12345)),
280
 
                            'embedded G2 DownloadItem URL without fixed cookie path test failed');
281
 
 
282
 
        /*
283
 
         * All embedded G2 requests that do not go to the G2 base directly shouldn't have the
284
 
         * GALLERYSID in the URLs
285
 
         */
286
 
        $this->assertEquals('/cms/phpunit_test.php?module=gallery2&amp;g2_itemId=12345',
287
 
            $this->_urlGenerator->generateUrl(array('view' => 'core.ShowItem', 'itemId' => 12345)),
288
 
            'embedded G2 ShowItem URL without fixed cookie path test failed');
289
 
    }
290
 
 
291
 
    function testGenerateUrlInitWithForceEmptyFilePart() {
292
 
        global $gallery;
293
 
 
294
 
        /* Set g2Uri file to empty string with special override character '|' */
295
 
        GalleryDataCache::put('G2_EMBED', 1, true);
296
 
        $_SERVER['REQUEST_URI'] = '/cms/otherpage/?foo=bar';
297
 
        list ($ret) = $this->_prepareGenerateUrl('', '/cms/phpunit_test.php?module=gallery2',
298
 
                                                         '/gallery2/|');
299
 
        if ($ret) {
300
 
            $this->failWithStatus($ret);
301
 
        }
302
 
        $session =& $gallery->getSession();
303
 
 
304
 
        /* Do the actual test */
305
 
        $this->assertEquals('/gallery2/?g2_view=' .
306
 
                            urlencode('core.DownloadItem') . '&amp;g2_itemId=12345&amp;' .
307
 
                            GalleryUtilities::prefixFormVariable($session->getKey()) . '=' .
308
 
                            urlencode($session->getId()),
309
 
                            $this->_urlGenerator->generateUrl(array('view' => 'core.DownloadItem',
310
 
                                                             'itemId' => 12345)),
311
 
                            'embedded G2 DownloadItem URL without fixed cookie path test failed');
312
 
    }
313
 
 
314
 
    function testGenerateUrlEmbeddedWithCookiePathSet() {
315
 
        global $gallery;
316
 
 
317
 
        /* Now test the same with fixed cookie path */
318
 
        GalleryDataCache::put('G2_EMBED', 1, true);
319
 
        $_SERVER['REQUEST_URI'] = '/cms/otherpage?foo=bar';
320
 
        list ($ret) = $this->_prepareGenerateUrl('/', '/cms/phpunit_test.php?module=gallery2',
321
 
                                                 '/gallery2/main.php');
322
 
        if ($ret) {
323
 
            $this->failWithStatus($ret);
324
 
        }
325
 
 
326
 
        /* Do the actual test */
327
 
        $this->assertEquals('/gallery2/main.php?g2_view=' .
328
 
                            urlencode('core.DownloadItem') . '&amp;g2_itemId=12345',
329
 
                            $this->_urlGenerator->generateUrl(array('view' => 'core.DownloadItem',
330
 
                                                                    'itemId' => 12345)),
331
 
                            'embedded G2 DownloadItem URL with fixed cookie path test failed');
332
 
 
333
 
        /* All other embedded G2 requests should still not have an appended SID */
334
 
        $this->assertEquals('/cms/phpunit_test.php?module=gallery2&amp;g2_itemId=12345',
335
 
                            $this->_urlGenerator->generateUrl(array('view' => 'core.ShowItem',
336
 
                                                                    'itemId' => 12345)),
337
 
                            'embedded G2 ShowItem URL without fixed cookie path test failed');
338
 
    }
339
 
 
340
 
    function testGenerateUrlWithoutSession() {
341
 
        global $gallery;
342
 
        $backup = $gallery->_session;
343
 
        $gallery->_session = null;
344
 
 
345
 
        $urlGenerator = new GalleryUrlGenerator();
346
 
        $ret = $urlGenerator->init('phpunit_test.php');
347
 
        if ($ret) {
348
 
            return $this->failWithStatus($ret);
349
 
        }
350
 
 
351
 
        $this->assertEquals('phpunit_test.php?g2_view=foo',
352
 
                            $urlGenerator->generateUrl(array('view' => 'foo')));
353
 
        $gallery->_session = $backup;
354
 
    }
355
 
 
356
 
    function testGenerateHref() {
357
 
        global $gallery;
358
 
        $session =& $gallery->getSession();
359
 
 
360
 
        /* href URLs default to no sessionid */
361
 
        $this->assertEquals('test/test.css',
362
 
            $this->_urlGenerator->generateUrl(array('href' => 'test/test.css')),
363
 
            'relative');
364
 
 
365
 
        /* Absolute URL */
366
 
        $this->assertEquals('http://sf.net/',
367
 
            $this->_urlGenerator->generateUrl(array('href' => 'http://sf.net/')),
368
 
            'absolute');
369
 
 
370
 
        /* Absolute URL path */
371
 
        $this->assertEquals('/cms/test/test.css',
372
 
            $this->_urlGenerator->generateUrl(array('href' => '/cms/test/test.css')),
373
 
            'absolute path');
374
 
 
375
 
        /* Simulate embedded */
376
 
        $_SERVER['REQUEST_URI'] = '/phpunit_test.php?module=gallery2&foo=bar';
377
 
        $urlGenerator = new GalleryUrlGenerator();
378
 
        $ret = $urlGenerator->init('/phpunit_test.php?module=gallery2', '/gallery2/');
379
 
        if ($ret) {
380
 
            return $this->failWithStatus($ret);
381
 
        }
382
 
 
383
 
        $this->assertEquals('gallery2/test/test.css',
384
 
            $urlGenerator->generateUrl(array('href' => 'test/test.css')),
385
 
            'relative embedded');
386
 
 
387
 
        /* Embedded, trailing slash omitted */
388
 
        $_SERVER['REQUEST_URI'] = '/cms/phpunit_test.php?module=gallery2&foo=bar';
389
 
        $urlGenerator = new GalleryUrlGenerator();
390
 
        $ret = $urlGenerator->init('/cms/phpunit_test.php?module=gallery2', '/gallery2');
391
 
        if ($ret) {
392
 
            return $this->failWithStatus($ret);
393
 
        }
394
 
 
395
 
        $this->assertEquals('/test/test.css',
396
 
            $urlGenerator->generateUrl(array('href' => 'test/test.css')),
397
 
            'relative embedded, no trailing slash');
398
 
    }
399
 
 
400
 
    function testGenerateUrlNoHtmlEntities() {
401
 
        $values[] = GalleryUtilities::prefixFormVariable('a') . '=' . urlencode('123');
402
 
        $values[] = GalleryUtilities::prefixFormVariable('b') . '=' . urlencode('ab&amp;c');
403
 
 
404
 
        $this->assertEquals(
405
 
            'phpunit_test.php?' . implode('&', $values),
406
 
            $this->_urlGenerator->generateUrl(array('a' => 123, 'b' => 'ab&amp;c'),
407
 
                                              array('htmlEntities' => false)), 'url');
408
 
 
409
 
        $this->assertEquals(
410
 
            'test/blah.php?' . implode('&', $values),
411
 
            $this->_urlGenerator->generateUrl(array('href' => 'test/blah.php',
412
 
                                                    'a' => 123, 'b' => 'ab&amp;c'),
413
 
                                              array('htmlEntities' => false)), 'href');
414
 
    }
415
 
 
416
 
    function testGenerateUrlNoUrlEncode() {
417
 
        $values[] = GalleryUtilities::prefixFormVariable('a') . '=' . 'The Quick!Brown#Fox';
418
 
        $values[] = GalleryUtilities::prefixFormVariable('b') . '=' . 'Jumps Over%The/Lazy_Dog';
419
 
 
420
 
        $this->assertEquals(
421
 
            'phpunit_test.php?' . implode('&amp;', $values),
422
 
            $this->_urlGenerator->generateUrl(array('a' => 'The Quick!Brown#Fox',
423
 
                                                    'b' => 'Jumps Over%The/Lazy_Dog'),
424
 
                                              array('urlEncode' => false)), 'url');
425
 
 
426
 
        $this->assertEquals(
427
 
            'test/blah.php?' . implode('&amp;', $values),
428
 
            $this->_urlGenerator->generateUrl(array('href' => 'test/blah.php',
429
 
                                                    'a' => 'The Quick!Brown#Fox',
430
 
                                                    'b' => 'Jumps Over%The/Lazy_Dog'),
431
 
                                              array('urlEncode' => false)), 'href');
432
 
    }
433
 
 
434
 
    function testGenerateReturnUrl() {
435
 
        $_SERVER['REQUEST_URI'] = '/gallery2/phpunit_test.php?' .
436
 
            GalleryUtilities::prefixFormVariable('view') . '=foo&' .
437
 
            GalleryUtilities::prefixFormVariable('a') . '=123';
438
 
        $_GET[GalleryUtilities::prefixFormVariable('view')] = 'foo';
439
 
        $_GET[GalleryUtilities::prefixFormVariable('a')] = '123';
440
 
 
441
 
        $params['a+'] = 456;
442
 
        $params['view'] = 'bar';
443
 
        $params['return'] = 1;
444
 
        $actual = $this->_urlGenerator->generateUrl($params, array('forceSessionId' => false));
445
 
 
446
 
        /*
447
 
         * The return URL needs to be server relative because we don't know at this point in
448
 
         * wich context it'll be used later on.
449
 
         */
450
 
        $params['return'] =
451
 
            GalleryUrlGenerator::appendParamsToUrl('/gallery2/phpunit_test.php',
452
 
                                                   array('view' => 'foo', 'a' => '123'));
453
 
        /*
454
 
         * Note: encoded 'return' url should separate request variables with '%26'
455
 
         * and not '%26amp%3D' because sending this back to G2 would yield &amp;amp;
456
 
         */
457
 
        $params['return'] = str_replace('&amp;', '&', $params['return']);
458
 
        foreach ($params as $key => $value) {
459
 
            $values[] = GalleryUtilities::prefixFormVariable(urlencode($key))
460
 
                      . '=' . urlencode($value);
461
 
        }
462
 
        $expected = 'phpunit_test.php?' . implode('&amp;', $values);
463
 
        $this->assertEquals($expected, $actual);
464
 
    }
465
 
 
466
 
    function testGenerateUrlWithEmbeddedSessionId() {
467
 
        global $gallery;
468
 
 
469
 
        $params['a'] = 123;
470
 
        $params['b'] = 'bBbB';
471
 
        $params['c'] = serialize(array(1, 2, 3));
472
 
        $actual = $this->_urlGenerator->generateUrl($params, array('forceSessionId' => true));
473
 
 
474
 
        foreach ($params as $key => $value) {
475
 
            $values[] = GalleryUtilities::prefixFormVariable($key) . '=' . urlencode($value);
476
 
        }
477
 
 
478
 
        /* No matter what, expect the session id */
479
 
        $session =& $gallery->getSession();
480
 
        $values[] = GalleryUtilities::prefixFormVariable($session->getKey()) .
481
 
            '=' . urlencode($session->getId());
482
 
        $expected = 'phpunit_test.php?' . join('&amp;', $values);
483
 
        $this->assertEquals($expected, $actual);
484
 
    }
485
 
 
486
 
    function testGenerateUrlWithoutEmbeddedSessionId() {
487
 
        $params['a'] = 123;
488
 
        $params['b'] = 'bBbB';
489
 
        $params['c'] = serialize(array(1, 2, 3));
490
 
        $actual = $this->_urlGenerator->generateUrl($params, array('forceSessionId' => false));
491
 
 
492
 
        foreach ($params as $key => $value) {
493
 
            $values[] = GalleryUtilities::prefixFormVariable($key) . '=' . urlencode($value);
494
 
        }
495
 
 
496
 
        /* No matter what, expect no session id */
497
 
        $expected = 'phpunit_test.php?' . join('&amp;', $values);
498
 
        $this->assertEquals($expected, $actual);
499
 
    }
500
 
 
501
 
    function testGenerateUrlInMultisite() {
502
 
        global $gallery;
503
 
        /* Simulate multisite (url for codebase) */
504
 
        $gallery->setConfig('galleryBaseUrl', 'test://codebase/somepath/');
505
 
 
506
 
        /* Views should go to the multisite */
507
 
        $this->assertEquals('phpunit_test.php?g2_view=' . urlencode('test.View') .
508
 
                            '&amp;g2_param=12345',
509
 
                            $this->_urlGenerator->generateUrl(
510
 
                                array('view' => 'test.View', 'param' => 12345)));
511
 
 
512
 
        $this->assertEquals('phpunit_test.php?g2_view=' . urlencode('core.DownloadItem') .
513
 
                            '&amp;g2_itemId=99',
514
 
                            $this->_urlGenerator->generateUrl(
515
 
                                array('view' => 'core.DownloadItem', 'itemId' => 99)));
516
 
 
517
 
        /* Controllers too */
518
 
        $this->assertEquals($this->_signUrl('phpunit_test.php?g2_controller=' .
519
 
                                            urlencode('test.Controller') . '&amp;g2_param=12345'),
520
 
                            $this->_urlGenerator->generateUrl(
521
 
                                array('controller' => 'test.Controller', 'param' => 12345)));
522
 
 
523
 
        /* Hrefs should go to the multisite codebase, unless forceDirect given */
524
 
        $this->assertEquals('test://codebase/somepath/theme/test_3745/images/foo.jpg',
525
 
                            $this->_urlGenerator->generateUrl(
526
 
                                array('href' => 'theme/test_3745/images/foo.jpg')));
527
 
        $this->assertEquals('site/image.jpg',
528
 
                            $this->_urlGenerator->generateUrl(
529
 
                                array('href' => 'site/image.jpg'), array('forceDirect' => true)));
530
 
 
531
 
        /* URL with baseUrl shouldn't be changed */
532
 
        $this->assertEquals('fix_base_url/foo.php?g2_foo=bar',
533
 
                            $this->_urlGenerator->generateUrl(
534
 
                                array('foo' => 'bar'), array('baseUrl' => 'fix_base_url/foo.php')));
535
 
    }
536
 
 
537
 
    function testGenerateUrlEmbeddedMultisite() {
538
 
        global $gallery;
539
 
        /* Simulate multisite (url for codebase) */
540
 
        $gallery->setConfig('galleryBaseUrl', 'test://codebase/somepath/');
541
 
        /* Simulate embedded */
542
 
        GalleryDataCache::put('G2_EMBED', 1, true);
543
 
        $_SERVER['REQUEST_URI'] = '/cms/otherpage?foo=bar';
544
 
        list ($ret) = $this->_prepareGenerateUrl('/', '/cms/phpunit_test.php?module=gallery2',
545
 
                                                 '/gallery2/main.php');
546
 
        if ($ret) {
547
 
            $this->failWithStatus($ret);
548
 
        }
549
 
 
550
 
        /* Views should go to the embedded multisite */
551
 
        $this->assertEquals('/cms/phpunit_test.php?module=gallery2&amp;g2_view=' .
552
 
                            urlencode('test.View') . '&amp;g2_param=12345',
553
 
                            $this->_urlGenerator->generateUrl(
554
 
                                array('view' => 'test.View', 'param' => 12345)));
555
 
 
556
 
        /* Download view should go to direct multisite */
557
 
        $this->assertEquals('/gallery2/main.php?g2_view=' . urlencode('core.DownloadItem') .
558
 
                            '&amp;g2_itemId=99',
559
 
                            $this->_urlGenerator->generateUrl(
560
 
                                array('view' => 'core.DownloadItem', 'itemId' => 99)));
561
 
 
562
 
        /* Controllers should go to the embedded multisite */
563
 
        $this->assertEquals($this->_signUrl('/cms/phpunit_test.php?module=gallery2&amp;' .
564
 
                                            'g2_controller=' . urlencode('test.Controller') .
565
 
                                            '&amp;g2_param=12345'),
566
 
                            $this->_urlGenerator->generateUrl(
567
 
                                array('controller' => 'test.Controller', 'param' => 12345)));
568
 
 
569
 
        /* Hrefs should go to the multisite codebase */
570
 
        $this->assertEquals('test://codebase/somepath/theme/test_3745/images/foo.jpg',
571
 
                            $this->_urlGenerator->generateUrl(
572
 
                                array('href' => 'theme/test_3745/images/foo.jpg')));
573
 
 
574
 
        /* URL with baseUrl shouldn't be changed */
575
 
        $this->assertEquals('fix_base_url/foo.php?g2_foo=bar',
576
 
                            $this->_urlGenerator->generateUrl(
577
 
                                array('foo' => 'bar'), array('baseUrl' => 'fix_base_url/foo.php')));
578
 
    }
579
 
 
580
 
    function testGetCurrentRequestUri() {
581
 
        $expected = '/gallery2/phpunit_test.php?foo=bar';
582
 
        $this->assertEquals($expected, $this->_urlGenerator->getCurrentRequestUri());
583
 
 
584
 
        /* IIS.. REQUEST_URI is just empty; but has SCRIPT_NAME.. */
585
 
        $_SERVER['REQUEST_URI'] = '';
586
 
        $_SERVER['SCRIPT_NAME'] = '/gallery2/phpunit_test.php';
587
 
        $_SERVER['QUERY_STRING'] = 'foo=bar';
588
 
        $this->assertEquals($expected, $this->_urlGenerator->getCurrentRequestUri(), 'iis');
589
 
 
590
 
        /* Sometimes IIS has PATH_INFO value the same as SCRIPT_NAME.. */
591
 
        $_SERVER['PATH_INFO'] = $_SERVER['SCRIPT_NAME'];
592
 
        $this->assertEquals($expected, $this->_urlGenerator->getCurrentRequestUri(),
593
 
                            'iis path_info');
594
 
    }
595
 
    
596
 
    /*
597
 
     * Apache is not very clear about whether REQUEST_URI is specified to be an URI without the
598
 
     * schema and host part or not. There's at least 1 Apache 2 setup where REQUEST_URI was a
599
 
     * complete URL.
600
 
     * Since the requestUri in Gallery is expected to be an URI w/o the host part, we must
601
 
     * normalize whatever Apache returns.
602
 
     */
603
 
    function testGetCurrentRequestUriForAbsoluteUrl() {
604
 
        $expected = '/gallery2/phpunit_test.php?foo=bar';
605
 
        $_SERVER['REQUEST_URI'] = 'http://example.com' . $expected;
606
 
        $this->assertEquals($expected, $this->_urlGenerator->getCurrentRequestUri());
607
 
 
608
 
        /* And a case with a fragment part. */
609
 
        $expected = '/gallery2/phpunit_test.php?foo=bar#anchor';
610
 
        $_SERVER['REQUEST_URI'] = 'http://example.com' . $expected;
611
 
        $this->assertEquals($expected, $this->_urlGenerator->getCurrentRequestUri());
612
 
    }
613
 
 
614
 
    function testGetCurrentUrl() {
615
 
        $expected = 'http://example.com/gallery2/phpunit_test.php?foo=bar';
616
 
        $this->assertEquals($expected, $this->_urlGenerator->getCurrentUrl());
617
 
 
618
 
        /* IIS.. no REQUEST_URI; has SCRIPT_NAME.. */
619
 
        unset($_SERVER['REQUEST_URI']);
620
 
        $_SERVER['SCRIPT_NAME'] = '/gallery2/phpunit_test.php';
621
 
        $_SERVER['QUERY_STRING'] = 'foo=bar';
622
 
        unset($this->_urlGenerator->_currentUrl);
623
 
        $this->assertEquals($expected, $this->_urlGenerator->getCurrentUrl(), 'iis');
624
 
 
625
 
        /* Sometimes IIS has PATH_INFO value the same as SCRIPT_NAME.. */
626
 
        $_SERVER['PATH_INFO'] = $_SERVER['SCRIPT_NAME'];
627
 
        unset($this->_urlGenerator->_currentUrl);
628
 
        $this->assertEquals($expected, $this->_urlGenerator->getCurrentUrl(), 'iis path_info');
629
 
    }
630
 
 
631
 
    function testGetCurrentUrlDir() {
632
 
        $this->assertEquals('http://example.com/gallery2/',
633
 
                            $this->_urlGenerator->getCurrentUrlDir(), 'basic');
634
 
 
635
 
        /* Simulate embedded.. */
636
 
        $_SERVER['REQUEST_URI'] = '/phpunit_test.php?module=gallery2&foo=bar';
637
 
        $urlGenerator = new GalleryUrlGenerator();
638
 
        $ret = $urlGenerator->init('/phpunit_test.php?module=gallery2','/gallery2/');
639
 
        if ($ret) {
640
 
            return $this->failWithStatus($ret);
641
 
        }
642
 
        $this->assertEquals('http://example.com/', $urlGenerator->getCurrentUrlDir(), 'embedded');
643
 
        $this->assertEquals('http://example.com/gallery2/', $urlGenerator->getCurrentUrlDir(true),
644
 
                            'embedded forceDirect');
645
 
 
646
 
        $_SERVER['REQUEST_URI'] = '/cms/phpunit_test.php?module=gallery2&foo=bar';
647
 
        $urlGenerator = new GalleryUrlGenerator();
648
 
        $ret = $urlGenerator->init('/cms/phpunit_test.php?module=gallery2', '/gallery2/');
649
 
        if ($ret) {
650
 
            return $this->failWithStatus($ret);
651
 
        }
652
 
        $this->assertEquals('http://example.com/cms/', $urlGenerator->getCurrentUrlDir(),
653
 
                            'embedded, different path');
654
 
        $this->assertEquals('http://example.com/gallery2/', $urlGenerator->getCurrentUrlDir(true),
655
 
                            'embedded, different path, forceDirect');
656
 
    }
657
 
 
658
 
    function testGetCurrentUrlDirBaseFileNotFound() {
659
 
        $_SERVER['REQUEST_URI'] = '/somedir/?filter=foo';
660
 
        $urlGenerator = new GalleryUrlGenerator();
661
 
        $ret = $urlGenerator->init('index.php');
662
 
        if ($ret) {
663
 
            return $this->failWithStatus($ret);
664
 
        }
665
 
        $this->assertEquals('http://example.com/somedir/', $urlGenerator->getCurrentUrlDir());
666
 
    }
667
 
 
668
 
    function testGetHostName() {
669
 
        $_SERVER['HTTP_HOST'] = 'http_host';
670
 
        if ($ret = $this->_initNewUrlGenerator()) {
671
 
            return $this->failWithStatus($ret);
672
 
        }
673
 
        $this->assertEquals('http_host', $this->_urlGenerator->getHostName());
674
 
 
675
 
        $_SERVER['HTTP_X_FORWARDED_HOST'] = 'http_x_forwarded_host';
676
 
        if ($ret = $this->_initNewUrlGenerator()) {
677
 
            return $this->failWithStatus($ret);
678
 
        }
679
 
        $this->assertEquals('http_x_forwarded_host', $this->_urlGenerator->getHostName());
680
 
    }
681
 
 
682
 
    function testGetHostNameOverride() {
683
 
        $_SERVER['HTTP_HOST'] = 'http_host';
684
 
        $_SERVER['HTTP_X_FORWARDED_HOST'] = 'http_host';
685
 
 
686
 
        if ($ret = $this->_initNewUrlGenerator('override/')) {
687
 
            return $this->failWithStatus($ret);
688
 
        }
689
 
        $this->assertEquals('override', $this->_urlGenerator->getHostName());
690
 
        $this->assertEquals('override', $this->_urlGenerator->getHostName(false));
691
 
        $this->assertEquals('override', $this->_urlGenerator->getHostName(true));
692
 
    }
693
 
 
694
 
    function testGetHostNameEmbeddedOverride() {
695
 
        $_SERVER['HTTP_HOST'] = 'http_host';
696
 
        $_SERVER['HTTP_X_FORWARDED_HOST'] = 'http_host';
697
 
 
698
 
        if ($ret = $this->_initNewUrlGenerator('embedded_override/', 'http://direct_override/')) {
699
 
            return $this->failWithStatus($ret);
700
 
        }
701
 
        $this->assertEquals('embedded_override', $this->_urlGenerator->getHostName());
702
 
        $this->assertEquals('embedded_override', $this->_urlGenerator->getHostName(false));
703
 
        $this->assertEquals('direct_override', $this->_urlGenerator->getHostName(true));
704
 
    }
705
 
 
706
 
    /**
707
 
     * Basic tests
708
 
     */
709
 
    function testMakeUrl1() {
710
 
        $this->assertEquals('http://example.com/test/path.php',
711
 
                            $this->_urlGenerator->makeUrl('/test/path.php'), 'with slash');
712
 
 
713
 
        $this->assertEquals('http://example.com/test/test.jpg',
714
 
                            $this->_urlGenerator->makeUrl('test/test.jpg'), 'no slash');
715
 
 
716
 
        $this->assertEquals('http://example.com/',
717
 
                            $this->_urlGenerator->makeUrl(''), 'empty');
718
 
    }
719
 
 
720
 
    /**
721
 
     * Test non-standard ports go in both HTTP_HOST and SERVER_PORT (also tests ::init)
722
 
     */
723
 
    function testMakeUrl2() {
724
 
        $_SERVER['HTTP_HOST'] = 'example.com:8080';
725
 
        $_SERVER['SERVER_PORT'] = 8080;
726
 
        if ($ret = $this->_initNewUrlGenerator()) {
727
 
            return $this->failWithStatus($ret);
728
 
        }
729
 
        $this->assertEquals('http://example.com:8080/test.php?fun=yes',
730
 
                            $this->_urlGenerator->makeUrl('/test.php?fun=yes'));
731
 
    }
732
 
 
733
 
    /**
734
 
     * Test browsing to https://example.com
735
 
     */
736
 
    function testMakeUrl3() {
737
 
        $_SERVER['HTTP_HOST'] = 'example.com';
738
 
        $_SERVER['SERVER_PORT'] = 443;
739
 
        $_SERVER['HTTPS'] = 'on';
740
 
        if ($ret = $this->_initNewUrlGenerator()) {
741
 
            return $this->failWithStatus($ret);
742
 
        }
743
 
        $this->assertEquals('https://example.com/test/go.php?a=one&amp;b=two',
744
 
                            $this->_urlGenerator->makeUrl('test/go.php?a=one&amp;b=two'));
745
 
    }
746
 
 
747
 
    /**
748
 
     * Test non-standard port only in SERVER_PORT.  This is the case where the
749
 
     * browser went to http://example.com and got internally redirected to
750
 
     * something like http://internal.server:82/.  We want to serve up a new
751
 
     * url that the browser can handle.
752
 
     */
753
 
    function testMakeUrl4() {
754
 
        $_SERVER['HTTP_HOST'] = 'example.com';
755
 
        $_SERVER['SERVER_PORT'] = 82;
756
 
        if ($ret = $this->_initNewUrlGenerator()) {
757
 
            return $this->failWithStatus($ret);
758
 
        }
759
 
        $this->assertEquals('http://example.com/test.php?fun=yes',
760
 
                            $this->_urlGenerator->makeUrl('/test.php?fun=yes'));
761
 
    }
762
 
 
763
 
    /* Test host and protocol override */
764
 
    function testMakeUrl5() {
765
 
        $this->_urlGenerator->_currentBaseHost[0] = 'http://photos.example.com';
766
 
        $this->assertEquals('http://photos.example.com/test.php?fun=yes',
767
 
                            $this->_urlGenerator->makeUrl('/test.php?fun=yes'), 'override');
768
 
        $this->_urlGenerator->_currentBaseHost[0] = 'foo';
769
 
        $this->_urlGenerator->_currentBaseHost[1] = 'http://photos.example.com';
770
 
        $this->assertEquals('http://photos.example.com/test.php?fun=yes',
771
 
                            $this->_urlGenerator->makeUrl('/test.php?fun=yes', true),
772
 
                            'override with forceG2Base');
773
 
        $this->_urlGenerator->_currentBaseHost[0] = 'https://photos.example.com';
774
 
        $this->assertEquals('https://photos.example.com/test.php?fun=yes',
775
 
                            $this->_urlGenerator->makeUrl('/test.php?fun=yes'),
776
 
                            'host and protocol override');
777
 
        $this->assertEquals('http://photos.example.com/test.php?fun=yes',
778
 
                            $this->_urlGenerator->makeUrl('/test.php?fun=yes', true),
779
 
                            'host and protocol override with forceG2Base');
780
 
    }
781
 
 
782
 
    function testMakeAbsoluteUrlForRelativeUrl() {
783
 
        $this->_urlGenerator->_currentUrlBaseDir[1] = 'http://photos.example.com/app/';
784
 
        $this->assertEquals('http://example.com/gallery2/test/test.jpg',
785
 
                            $this->_urlGenerator->makeAbsoluteUrl('test/test.jpg'), 'relative URL');
786
 
        $this->assertEquals('http://photos.example.com/app/test/test.jpg',
787
 
                            $this->_urlGenerator->makeAbsoluteUrl('test/test.jpg', true),
788
 
                            'force direct');
789
 
    }
790
 
 
791
 
    function testMakeAbsoluteUrlForServerRelativeUrl() {
792
 
        $this->_urlGenerator->_currentBaseHost[1] = 'http://photos.example.com';
793
 
        $this->assertEquals('http://example.com/test/test.jpg',
794
 
                            $this->_urlGenerator->makeAbsoluteUrl('/test/test.jpg'),
795
 
                            'relative URL');
796
 
        $this->assertEquals('http://photos.example.com/test/test.jpg',
797
 
                            $this->_urlGenerator->makeAbsoluteUrl('/test/test.jpg', true),
798
 
                            'force direct');
799
 
        $this->assertEquals('http://example.com/test/test.jpg',
800
 
                            $this->_urlGenerator->makeAbsoluteUrl('  /test/test.jpg'),
801
 
                            'relative URL with leading whitespace characters');
802
 
    }
803
 
 
804
 
    function testMakeAbsoluteUrlForAbsoluteUrl() {
805
 
        $this->assertEquals('http://example.com/test.jpg',
806
 
                $this->_urlGenerator->makeAbsoluteUrl('http://example.com/test.jpg'),
807
 
                'absolute URL');
808
 
        $this->assertEquals('http://other.example.com/test.jpg',
809
 
                $this->_urlGenerator->makeAbsoluteUrl('http://other.example.com/test.jpg'),
810
 
                'absolute URL of non-Gallery host');
811
 
        $this->assertEquals('webdav16+10://other.protocol.com/test.jpg',
812
 
                $this->_urlGenerator->makeAbsoluteUrl('webdav16+10://other.protocol.com/test.jpg'),
813
 
                'absolute URL with a different schema');
814
 
        $this->assertEquals('http://example.com/test.jpg',
815
 
                $this->_urlGenerator->makeAbsoluteUrl('http://example.com/test.jpg', true),
816
 
                'absolute URL, force direct');
817
 
    }
818
 
 
819
 
    /**
820
 
     * Test non-standard ports go in both HTTP_HOST and SERVER_PORT
821
 
     */
822
 
    function testInit() {
823
 
        $_SERVER['HTTP_HOST'] = 'example.com:8080';
824
 
        $_SERVER['SERVER_PORT'] = 8080;
825
 
        if ($ret = $this->_initNewUrlGenerator()) {
826
 
            return $this->failWithStatus($ret);
827
 
        }
828
 
        $this->assertEquals('http', $this->_urlGenerator->_protocol[0], 'protocol');
829
 
        $this->assertEquals('http', $this->_urlGenerator->_protocol[1], 'forceDirect protocol');
830
 
        $this->assertEquals('example.com:8080', $this->_urlGenerator->_host[0], 'host');
831
 
        $this->assertEquals('example.com:8080', $this->_urlGenerator->_host[1], 'forceDirect host');
832
 
        $this->assertEquals('http://example.com:8080', $this->_urlGenerator->_currentBaseHost[0],
833
 
                            'currentBaseHost');
834
 
        $this->assertEquals('http://example.com:8080', $this->_urlGenerator->_currentBaseHost[1],
835
 
                            'forceDirect currentBaseHost');
836
 
    }
837
 
 
838
 
    function testInitDefault() {
839
 
        unset($_SERVER['HTTPS']);
840
 
        $_SERVER['HTTP_X_FORWARDED_HOST'] = 'http_x_forwarded_host';
841
 
        $uG = new GalleryUrlGenerator();
842
 
        if ($ret = $uG->init()) {
843
 
            return $this->failWithStatus($ret);
844
 
        }
845
 
 
846
 
        foreach (array(0, 1) as $i) {
847
 
            $this->assertEquals(GALLERY_MAIN_PHP, $uG->_file[$i], "file[$i]");
848
 
            $this->assert(empty($uG->_path[$i]), "path[$i]");
849
 
            $this->assertEquals('http', $uG->_protocol[$i], "protocol[$i]");
850
 
            $this->assertEquals('http_x_forwarded_host', $uG->_host[$i], "host[$i]");
851
 
            $this->assertEquals('http://http_x_forwarded_host', $uG->_currentBaseHost[$i],
852
 
                                "currentBaseHost[$i]");
853
 
        }
854
 
        $this->assertEquals(null, $uG->_embedSessionString, 'embedSessionString');
855
 
    }
856
 
 
857
 
    function testInitOverride() {
858
 
        unset($_SERVER['HTTPS']);
859
 
        $_SERVER['HTTP_X_FORWARDED_HOST'] = 'http_x_forwarded_host';
860
 
        $uG = new GalleryUrlGenerator();
861
 
        if ($ret = $uG->init('https://override:8080/new_path/index.php?foo=bar')) {
862
 
            return $this->failWithStatus($ret);
863
 
        }
864
 
 
865
 
        foreach (array(0, 1) as $i) {
866
 
            $this->assertEquals('index.php?foo=bar', $uG->_file[$i], "file[$i]");
867
 
            $this->assertEquals('/new_path/', $uG->_path[$i], "path[$i]");
868
 
            $this->assertEquals('https', $uG->_protocol[$i], "protocol[$i]");
869
 
            $this->assertEquals('override:8080', $uG->_host[$i], "host[$i]");
870
 
            $this->assertEquals('https://override:8080', $uG->_currentBaseHost[$i],
871
 
                                "currentBaseHost[$i]");
872
 
        }
873
 
    }
874
 
 
875
 
    function testInitEmbedded() {
876
 
        $_SERVER['HTTPS'] = 'on';
877
 
        $_SERVER['HTTP_X_FORWARDED_HOST'] = 'http_x_forwarded_host';
878
 
        $uG = new GalleryUrlGenerator();
879
 
        if ($ret = $uG->init('/cms/index.php?foo=bar', 'http://direct/gallery2/', 'session=123')) {
880
 
            return $this->failWithStatus($ret);
881
 
        }
882
 
 
883
 
        $this->assertEquals('index.php?foo=bar', $uG->_file[0], 'file[0]');
884
 
        $this->assertEquals(GALLERY_MAIN_PHP, $uG->_file[1], 'file[1]');
885
 
        $this->assertEquals('/cms/', $uG->_path[0], 'path[0]');
886
 
        $this->assertEquals('/gallery2/', $uG->_path[1], 'path[1]');
887
 
        $this->assertEquals('https', $uG->_protocol[0], 'protocol[0]');
888
 
        $this->assertEquals('http', $uG->_protocol[1], 'protocol[1]');
889
 
        $this->assertEquals('http_x_forwarded_host', $uG->_host[0], 'host[0]');
890
 
        $this->assertEquals('direct', $uG->_host[1], 'host[1]');
891
 
        $this->assertEquals('https://http_x_forwarded_host', $uG->_currentBaseHost[0],
892
 
                                'currentBaseHost[0]');
893
 
        $this->assertEquals('http://direct', $uG->_currentBaseHost[1],
894
 
                                'currentBaseHost[1]');
895
 
    }
896
 
 
897
 
    function testBaseFileWithQMark() {
898
 
        global $gallery;
899
 
        $urlGenerator = new GalleryUrlGenerator();
900
 
        $ret = $urlGenerator->init('phpunit_test.php?test=on');
901
 
        if ($ret) {
902
 
            return $this->failWithStatus($ret);
903
 
        }
904
 
        $_SERVER['REQUEST_URI'] = '/gallery2/phpunit_test.php?test=on&foo=bar';
905
 
 
906
 
        $params['a'] = 123;
907
 
        $params['b'] = 'bBbB';
908
 
        $params['c'] = serialize(array(1, 2, 3));
909
 
        $actual = $urlGenerator->generateUrl($params);
910
 
 
911
 
        foreach ($params as $key => $value) {
912
 
            $values[] = GalleryUtilities::prefixFormVariable($key) . '=' . urlencode($value);
913
 
        }
914
 
 
915
 
        /* If we're not using cookies, expect there to be an additional value */
916
 
        $session =& $gallery->getSession();
917
 
        if (!$session->isUsingCookies()) {
918
 
            $values[] = GalleryUtilities::prefixFormVariable($session->getKey()) .
919
 
                '=' . urlencode($session->getId());
920
 
        }
921
 
        $expected = 'phpunit_test.php?test=on&amp;' .
922
 
            implode('&amp;', $values);
923
 
 
924
 
        $this->assertEquals($expected, $actual);
925
 
    }
926
 
 
927
 
    function testGetNavigationReturnUrl() {
928
 
        $_GET['g2_foo'] = 'bar';
929
 
        $_GET['g2_return'] = 'http://blabla';
930
 
        $_GET['g2_returnName'] = 'name';
931
 
        $_GET['g2_navId'] = 'navId';
932
 
        $_GET['g2_fromNavId'] = 'fromNavId';
933
 
        /*
934
 
         * We're forwarding the HTTP authorization header via mod_rewrite to the URL params and
935
 
         * need to remove it manually for return URLs again.  But we keep it in other places (e.g.
936
 
         * cacheable URLs) since it might influence Gallery's behavior.
937
 
         */
938
 
        $_GET['g2_authorization'] = 'authorization';
939
 
        $expected = '/gallery2/phpunit_test.php?g2_foo=bar';
940
 
 
941
 
        /* Normal behaviour (php+apache) */
942
 
        $this->assertEquals($expected, $this->_urlGenerator->getNavigationReturnUrl());
943
 
 
944
 
        /* IIS: no REQUEST_URI or PATH_INFO, just SCRIPT_NAME */
945
 
        unset($_SERVER['REQUEST_URI']);
946
 
        $_SERVER['SCRIPT_NAME'] = '/gallery2/phpunit_test.php';
947
 
        unset($this->_urlGenerator->_currentUrl);
948
 
        $this->assertEquals($expected, $this->_urlGenerator->getNavigationReturnUrl(), 'iis');
949
 
 
950
 
        /* Sometimes IIS has PATH_INFO value the same as SCRIPT_NAME.. */
951
 
        $_SERVER['PATH_INFO'] = $_SERVER['SCRIPT_NAME'];
952
 
        unset($this->_urlGenerator->_currentUrl);
953
 
        $this->assertEquals($expected, $this->_urlGenerator->getNavigationReturnUrl(),
954
 
                            'iis path_info');
955
 
    }
956
 
 
957
 
    function testGetNavigationReturnUrlNoCookies() {
958
 
        global $gallery;
959
 
 
960
 
        /* Session without cookies */
961
 
        $session =& $gallery->getSession();
962
 
        $savedIsUsingCookies = $session->_isUsingCookies;
963
 
        $session->_isUsingCookies = false;
964
 
 
965
 
        $sessionParam =
966
 
            GalleryUtilities::prefixFormVariable($session->getKey()) . '=' . $session->getId();
967
 
 
968
 
        /* No other params in URL */
969
 
        $_SERVER['REQUEST_URI'] = '/gallery2/phpunit_test.php';
970
 
        $_GET = array();
971
 
        $expected = '/gallery2/phpunit_test.php?' . $sessionParam;
972
 
        $this->assertEquals($expected, $this->_urlGenerator->getNavigationReturnUrl(),
973
 
                            'no other params');
974
 
 
975
 
        /* With other params in URL */
976
 
        $_SERVER['REQUEST_URI'] = '/gallery2/phpunit_test.php?foo=bar';
977
 
        $_GET['g2_foo'] = 'bar';
978
 
        unset($this->_urlGenerator->_currentUrl);
979
 
        $expected = '/gallery2/phpunit_test.php?g2_foo=bar&amp;' . $sessionParam;
980
 
        $this->assertEquals($expected, $this->_urlGenerator->getNavigationReturnUrl(),
981
 
                            'with other params');
982
 
 
983
 
        /* With session already in URL */
984
 
        $_GET[GalleryUtilities::prefixFormVariable($session->getKey())] = $session->getId();
985
 
        unset($this->_urlGenerator->_currentUrl);
986
 
        $expected = '/gallery2/phpunit_test.php?g2_foo=bar&amp;' . $sessionParam;
987
 
        $this->assertEquals($expected, $this->_urlGenerator->getNavigationReturnUrl(),
988
 
                            'with session already in URL');
989
 
 
990
 
        $session->_isUsingCookies = $savedIsUsingCookies;
991
 
    }
992
 
 
993
 
    function testGetNavigationReturnUrlArrayParam() {
994
 
        $_GET['g2_foo'] = array('test' => 'bar');
995
 
        $_GET['g2_form'] = array('one' => array('two' => array('three' => 'four')));
996
 
        $expected = '/gallery2/phpunit_test.php?g2_foo' . urlencode('[test]') . '=bar&amp;'
997
 
                  . urlencode('g2_form[one][two][three]') . '=four';
998
 
        $this->assertEquals($expected, $this->_urlGenerator->getNavigationReturnUrl());
999
 
    }
1000
 
 
1001
 
    /*
1002
 
     * With the g2_return parameter users can add any "back to " link or direct redirect to any
1003
 
     * URL. We try to detect those phishing attacks by comparing the returnUrl with our current
1004
 
     * $this->getCurrentUrlDir(). If returnUrl doesn't start with getCurrentUrlDir(), it's not a
1005
 
     * redirect into the same G2 installation.
1006
 
     * initNavigation is called after GalleryInitFirstPass and init storage, thus we can use
1007
 
     * getCurrentUrlDir(), else it might have been a problem (RewriteUrlGenerator)
1008
 
     */
1009
 
    function testDetectPhishingAttack() {
1010
 
        /* Not the same domain name or not the same path are not allowed */
1011
 
        $badUrls = array(
1012
 
            'http://EVIL.com/gallery2/main.php?g2_view=core.UserAdmin&g2_subView=core.UserLogin',
1013
 
            'https://EVIL.com/gallery2/main.php',
1014
 
            $this->_urlGenerator->makeUrl('/EVIL/main.php?g2_subView=core.UserLogin'),
1015
 
            '../EVIL/main.php?g2_view=core.UserAdmin',
1016
 
            '/EVIL/../gallery2/main.php?g2_view=core.UserAdmin',
1017
 
            '/gallery2/../EVIL/main.php?g2_view=core.UserAdmin',
1018
 
            "..\\\\EVIL\\\\main.php",
1019
 
            "/gallery/foo\\\\../main.php",
1020
 
            ' /EVIL/gallery2/main.php',
1021
 
            ' /../gallery2/EVIL/main.php',
1022
 
            ' ../gallery2/EVIL/main.php');
1023
 
        foreach ($badUrls as $url) {
1024
 
            $_GET['g2_return'] = $url;
1025
 
            $ret = $this->_urlGenerator->initNavigation();
1026
 
            $this->assert($ret->getErrorCode() & ERROR_PERMISSION_DENIED,
1027
 
                          "Phishing attack '$url' not detected for url");
1028
 
        }
1029
 
        /*
1030
 
         * Check host and path for absolute urls, absolute path for server relative URLs
1031
 
         * and allow relative URLs that don't try to break out of the good path
1032
 
         */
1033
 
        $goodUrls = array(
1034
 
            $this->_urlGenerator->generateUrl(
1035
 
                array('view' => 'core.UserAdmin', 'subView' => 'core.UserLogin')),
1036
 
            '/gallery2/main.php?g2_view=core.UserAdmin',
1037
 
            'v/GoodAlbumName/',
1038
 
            'www.DONTCARE.com/gallery2/main.php',
1039
 
            '\\gallery2\\main.php');
1040
 
        foreach ($goodUrls as $url) {
1041
 
            $_GET['g2_return'] = $url;
1042
 
            $ret = $this->_urlGenerator->initNavigation();
1043
 
            if ($ret) {
1044
 
                $this->assert(false, "Normal returnUrl '$url' not accepted");
1045
 
                $this->failWithStatus($ret);
1046
 
            }
1047
 
        }
1048
 
    }
1049
 
 
1050
 
    function testDetectHeaderInjectionAttack() {
1051
 
        $badUrls = array(
1052
 
            'http://EVIL.com/gallery2/main.php?test%0d%0afoo',
1053
 
            'main.php?foo%250Abar',
1054
 
            'main.php?g2_subView=core.UserLogin&amp;%0A',
1055
 
            'main.php?foo' . chr(13). 'bar');
1056
 
        foreach ($badUrls as $url) {
1057
 
            $_GET['g2_return'] = $url;
1058
 
            $ret = $this->_urlGenerator->initNavigation();
1059
 
            $this->assert($ret->getErrorCode() & ERROR_PERMISSION_DENIED,
1060
 
                          "Header injection '$url' not detected for url");
1061
 
        }
1062
 
    }
1063
 
 
1064
 
    /*
1065
 
     * Make sure getNavigationLinks() returns unchanged URLs
1066
 
     * (part of our phishing attack protection)
1067
 
     */
1068
 
    function testGetNavigationLinks() {
1069
 
        list ($ret, $core) = GalleryCoreApi::loadPlugin('module', 'core');
1070
 
        if ($ret) {
1071
 
            return $this->failWithStatus($ret);
1072
 
        }
1073
 
 
1074
 
        /*
1075
 
         * In initNavigation we accept returnUrl = 'www.EVIL.com' because we have to interpret it
1076
 
         * as relative URL, e.g. for http://example.com/www.EVIL.com/ (album called www.EVIL.com)
1077
 
         * Here we must ensure that we don't add http:// in front the returned URL, since that
1078
 
         * would change the relative to an absolute URL.
1079
 
         */
1080
 
        $_GET['g2_return'] = 'www.DONTCARE.com';
1081
 
        $_GET['g2_returnName'] = 'I want a pony';
1082
 
        $ret = $this->_urlGenerator->initNavigation();
1083
 
        if ($ret) {
1084
 
            $this->failWithStatus($ret);
1085
 
        }
1086
 
        list ($ret, $links) = $this->_urlGenerator->getNavigationLinks();
1087
 
        if ($ret) {
1088
 
            $this->failWithStatus($ret);
1089
 
        }
1090
 
        $params = array('fromNavId' => $this->_urlGenerator->_navId);
1091
 
        $expectedUrl = GalleryUrlGenerator::appendParamsToUrl('www.DONTCARE.com', $params);
1092
 
        $this->assertEquals(array(array('url' => $expectedUrl, 'name' => $core->translate(
1093
 
                                array('text' => 'Back to %s', 'arg1' => 'I want a pony')))),
1094
 
                            $links, 'Navigation link was altered');
1095
 
    }
1096
 
 
1097
 
    /* Regression test to check that we handle embedUri = /foo/bar/ correctly (no file part) */
1098
 
    function testInitNavigationHandlesEmbedUriWithoutFilePart() {
1099
 
        $urlGenerator = new GalleryUrlGenerator();
1100
 
        $ret = $urlGenerator->init('/foo/bar/');
1101
 
        if ($ret) {
1102
 
            $this->failWithStatus($ret);
1103
 
        }
1104
 
 
1105
 
        GalleryUtilities::putRequestVariable('return', 'foo');
1106
 
        /* Assert that there's no PHP warning during the execution of this function */
1107
 
        $ret = $urlGenerator->initNavigation();
1108
 
        if ($ret) {
1109
 
            return $ret;
1110
 
        }
1111
 
 
1112
 
        $this->assertEquals(null, $ret);
1113
 
    }
1114
 
 
1115
 
    function testRelativeUrlOverrides() {
1116
 
        global $gallery;
1117
 
        $session =& $gallery->getSession();
1118
 
 
1119
 
        $gallery->setPlatform(new UrlGeneratorTestPlatform());
1120
 
        $qs = $session->isUsingCookies() ? '' : '?' .
1121
 
            GalleryUtilities::prefixFormVariable($session->getKey()) . '=' .
1122
 
            urlencode($session->getId());
1123
 
 
1124
 
        /* Try with a file that has no local override */
1125
 
        $actual = $this->_urlGenerator->generateUrl(array('href' => 'test/test1.css'));
1126
 
        $this->assertEquals('test/test1.css' . $qs, $actual);
1127
 
 
1128
 
        /* Try with a file that has a local override */
1129
 
        $actual = $this->_urlGenerator->generateUrl(array('href' => 'test/test2.css'));
1130
 
        $this->assertEquals('test/local/test2.css' . $qs, $actual);
1131
 
 
1132
 
        /* We don't allow overrides with top level files (nor do we generate an error) */
1133
 
        $actual = $this->_urlGenerator->generateUrl(array('href' => 'favicon.ico'));
1134
 
        $this->assertEquals('favicon.ico' . $qs, $actual);
1135
 
    }
1136
 
 
1137
 
    function testCurrentUrlToken() {
1138
 
        $_SERVER['REQUEST_URI'] = '/gallery2/phpunit_test.php?a=b';
1139
 
 
1140
 
        $url = $this->_urlGenerator->generateUrl(array('view' => 'foo', 'test' => '%CURRENT_URL%'));
1141
 
        $this->assertEquals('phpunit_test.php?g2_view=foo&amp;' .
1142
 
            'g2_test=http%3A%2F%2Fexample.com%2Fgallery2%2Fphpunit_test.php%3Fa%3Db', $url);
1143
 
    }
1144
 
 
1145
 
    function testGetCookiePath() {
1146
 
        /* First check if the configured value is taken if available */
1147
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'cookie.path', '/bad/');
1148
 
        if ($ret) {
1149
 
            return $this->failWithStatus($ret);
1150
 
        }
1151
 
        /* Check if the correct value was stored */
1152
 
        list ($ret, $realValue) = GalleryCoreApi::getPluginParameter('module', 'core',
1153
 
                                                                     'cookie.path');
1154
 
        if ($ret) {
1155
 
            return $this->failWithStatus($ret);
1156
 
        }
1157
 
        $this->assertEquals('/bad/', $realValue, 'could not set plugin parameter cookie.path');
1158
 
        /* Now do the actual check */
1159
 
        $_SERVER['REQUEST_URI'] = '/cms/phpunit_test.php?a=b';
1160
 
        $urlGenerator = new GalleryUrlGenerator();
1161
 
        $ret = $urlGenerator->init('/cms/phpunit_test.php', '/gallery2/');
1162
 
        if ($ret) {
1163
 
            return $this->failWithStatus($ret);
1164
 
        }
1165
 
        list ($ret, $path) = $urlGenerator->getCookiePath();
1166
 
        if ($ret) {
1167
 
            return $this->failWithStatus($ret);
1168
 
        }
1169
 
        $this->assertEquals('/bad/', $path, 'test 0');
1170
 
        list ($ret, $path) = $urlGenerator->getCookiePath(true);
1171
 
        if ($ret) {
1172
 
            return $this->failWithStatus($ret);
1173
 
        }
1174
 
        $this->assertEquals('/bad/', $path, 'test 0.1');
1175
 
 
1176
 
        /*
1177
 
         * Now reset the cookie path configuration parameter such that the computed value is
1178
 
         * chosen
1179
 
         */
1180
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'cookie.path', '');
1181
 
        if ($ret) {
1182
 
            return $this->failWithStatus($ret);
1183
 
        }
1184
 
        /* Check if the correct value was stored */
1185
 
        list ($ret, $realValue) = GalleryCoreApi::getPluginParameter('module', 'core',
1186
 
                                                                     'cookie.path');
1187
 
        if ($ret) {
1188
 
            return $this->failWithStatus($ret);
1189
 
        }
1190
 
        $this->assertEquals('', $realValue, 'could not reset plugin parameter cookie.path');
1191
 
 
1192
 
        /* Check the computed cookie path */
1193
 
        $tests = array();
1194
 
        $tests[] = array('baseUri' => '/gallery2/phpunit_test.php?foo=bar', 'er' => '/gallery2/',
1195
 
                         'g2Uri' => '/gallery2/phpunit_test.php', 'force' => false);
1196
 
        $tests[] = array('baseUri' => '/phpunit_test.php?foo=bar','g2Uri' => '/gallery2/foo.php',
1197
 
                         'er' => '/', 'force' => false);
1198
 
        /* g2Uri is only relevant if forceG2Path is true */
1199
 
        $tests[] = array('g2Uri' => '/gallery2/index.php?module=gallery2&foo=bar', 'er' => '/cms/',
1200
 
                         'baseUri' => '/cms/index.php?module=gallery2', 'force' => false);
1201
 
        $tests[] = array('baseUri' => '/index.php?module=gallery2&foo=bar',  'er' => '/',
1202
 
                         'g2Uri' => '/gallery2/index.php?module=gallery2', 'force' => false);
1203
 
        $tests[] = array('baseUri' => '/cms/index.php?module=gallery2&foo=bar', 'force' => false,
1204
 
                         'g2Uri' => '/modules/gallery2/index.php?module=gallery2', 'er' => '/cms/');
1205
 
        $tests[] = array('baseUri' => '/apps/cms/index.php?module=gallery2&foo=bar',
1206
 
                         'g2Uri' => '/apps/galery2/index.php?module=gallery2', 'force' => false,
1207
 
                         'er' => '/apps/cms/');
1208
 
        $tests[] = array('baseUri' => '/cms/index.php?module=gallery2&foo=bar',
1209
 
                         'g2Uri' => '/gallery2/index.php?module=gallery2', 'force' => true,
1210
 
                         'er' => '/gallery2/');
1211
 
        $tests[] = array('baseUri' => '/cms/index.php?module=gallery2&foo=bar', 'force' => true,
1212
 
                         'g2Uri' => '/cms/modules/gallery2/index.php?module=gallery2',
1213
 
                         'er' => '/cms/modules/gallery2/');
1214
 
        $tests[] = array('baseUri' => 'phpunit_test.php', 'g2Uri' => null, 'force' => true,
1215
 
                         'er' => '/foobar/');
1216
 
        $tests[] = array('baseUri' => 'phpunit_test.php', 'g2Uri' => null, 'force' => false,
1217
 
                         'er' => '/foobar/');
1218
 
 
1219
 
        $i = 1;
1220
 
        foreach ($tests as $testCase) {
1221
 
            $_SERVER['REQUEST_URI'] = !isset($testCase['g2Uri']) ? '/foobar/' : '';
1222
 
            $_SERVER['REQUEST_URI'] .= $testCase['baseUri'];
1223
 
            $urlGenerator = new GalleryUrlGenerator();
1224
 
            $ret = $urlGenerator->init($testCase['baseUri'], $testCase['g2Uri']);
1225
 
            if ($ret) {
1226
 
                return $this->failWithStatus($ret);
1227
 
            }
1228
 
            list ($ret, $path) = $urlGenerator->getCookiePath($testCase['force']);
1229
 
            if ($ret) {
1230
 
                return $this->failWithStatus($ret);
1231
 
            }
1232
 
            $this->assertEquals($testCase['er'], $path, "test $i");
1233
 
            $i++;
1234
 
        }
1235
 
    }
1236
 
 
1237
 
    function testEmbedForceSessionId() {
1238
 
        $key = 'G2_EMBED';
1239
 
        $savedEmbed = null;
1240
 
        if (GalleryDataCache::containsKey($key)) {
1241
 
            $savedEmbed = GalleryDataCache::get($key);
1242
 
        }
1243
 
 
1244
 
        /* Permute all involved variables, exhaustive test */
1245
 
        foreach (array(true, false) as $downloadItemRequest) {
1246
 
            $params = array('view' => 'core.ShowItem');
1247
 
            if ($downloadItemRequest) {
1248
 
                $params = array('view' => 'core.DownloadItem');
1249
 
            }
1250
 
            foreach (array(true, false) as $isEmbedded) {
1251
 
                GalleryDataCache::put($key, $isEmbedded, true);
1252
 
                $this->assertEquals($isEmbedded, GalleryDataCache::get($key),
1253
 
                                    'Could not set isEmbedded');
1254
 
                foreach (array(true, false) as $cookiePathConfigured) {
1255
 
                    $this->_urlGenerator->_isCookiePathConfigured = $cookiePathConfigured;
1256
 
                    /* Execute the test */
1257
 
                    $result = $this->_urlGenerator->embedForceSessionId($params);
1258
 
                    /* Some debug information */
1259
 
                    $debugInfo = 'isEmbedded: ' . intval($isEmbedded) . ', cookiePathConfigured: ' .
1260
 
                        intval($cookiePathConfigured) . ', downloadItemRequest: ' .
1261
 
                        intval($downloadItemRequest);
1262
 
                    /* Check the result */
1263
 
                    if ($isEmbedded && !$cookiePathConfigured && $downloadItemRequest) {
1264
 
                        $this->assertEquals(true, $result, 'wrong result for test case: ' .
1265
 
                                            $debugInfo);
1266
 
                    } else {
1267
 
                        $this->assertEquals(false, $result, 'wrong result for test case: ' .
1268
 
                                            $debugInfo);
1269
 
                    }
1270
 
                }
1271
 
            }
1272
 
        }
1273
 
 
1274
 
        /* Restore */
1275
 
        if ($savedEmbed) {
1276
 
            GalleryDataCache::put($key, $savedEmbed, true);
1277
 
        } else {
1278
 
            GalleryDataCache::remove($key);
1279
 
            $this->assert(!GalleryDataCache::containsKey($key), 'Could not remove a GDC var');
1280
 
        }
1281
 
    }
1282
 
 
1283
 
    function testGenerateFullUrlOn() {
1284
 
        $this->assertEquals('http://example.com/gallery2/phpunit_test.php?g2_a=b',
1285
 
            $this->_urlGenerator->generateUrl(array('a' => 'b'), array('forceFullUrl' => true)));
1286
 
    }
1287
 
 
1288
 
    function testGenerateFullUrlOnForRelative() {
1289
 
        $this->assertEquals('http://example.com/gallery2/favicon.ico',
1290
 
                            $this->_urlGenerator->generateUrl(array('href' => 'favicon.ico'),
1291
 
                                                              array('forceFullUrl' => true)));
1292
 
    }
1293
 
 
1294
 
    function testGenerateFullUrlOff() {
1295
 
        $this->assertEquals('phpunit_test.php?g2_a=b',
1296
 
            $this->_urlGenerator->generateUrl(array('a' => 'b'), array('forceFullUrl' => false)));
1297
 
    }
1298
 
 
1299
 
    function testGenerateServerRelativeUrlOn() {
1300
 
        $this->assertEquals('/gallery2/phpunit_test.php?g2_a=b',
1301
 
                            $this->_urlGenerator->generateUrl(
1302
 
                                array('a' => 'b'), array('forceServerRelativeUrl' => true)));
1303
 
    }
1304
 
 
1305
 
    function testGenerateServerRelativeUrlOnForRelative() {
1306
 
        $this->assertEquals('/gallery2/favicon.ico',
1307
 
                            $this->_urlGenerator->generateUrl(
1308
 
                                array('href' => 'favicon.ico'),
1309
 
                                array('forceServerRelativeUrl' => true)));
1310
 
    }
1311
 
 
1312
 
    function testGenerateServerRelativeUrlOff() {
1313
 
        $this->assertEquals('phpunit_test.php?g2_a=b',
1314
 
                            $this->_urlGenerator->generateUrl(
1315
 
                                array('a' => 'b'), array('forceServerRelativeUrl' => false)));
1316
 
    }
1317
 
 
1318
 
    /* ForceFullUrl has precedence over ForceServerRelativeUrl */
1319
 
    function testGenerateServerRelativeUrlOnFullUrlOn() {
1320
 
        $this->assertEquals('http://example.com/gallery2/phpunit_test.php?g2_a=b',
1321
 
                            $this->_urlGenerator->generateUrl(
1322
 
                                array('a' => 'b'), array('forceServerRelativeUrl' => true,
1323
 
                                                         'forceFullUrl' => true)));
1324
 
    }
1325
 
 
1326
 
    function testMakeRelativeUrl() {
1327
 
        $this->assertEquals('phpunit_test.php?g2_a=b', $this->_urlGenerator->makeRelativeUrl(
1328
 
                                'http://example.com/gallery2/phpunit_test.php?g2_a=b'));
1329
 
    }
1330
 
 
1331
 
    function testMakeRelativeUrlForceServerRelative() {
1332
 
        $this->assertEquals('/gallery2/phpunit_test.php?g2_a=b',
1333
 
                            $this->_urlGenerator->makeRelativeUrl(
1334
 
                                'http://example.com/gallery2/phpunit_test.php?g2_a=b', true));
1335
 
    }
1336
 
 
1337
 
    function testMakeRelativeUrlDontForceServerRelative() {
1338
 
        $this->assertEquals('phpunit_test.php?g2_a=b',
1339
 
                            $this->_urlGenerator->makeRelativeUrl(
1340
 
                                'http://example.com/gallery2/phpunit_test.php?g2_a=b', false));
1341
 
    }
1342
 
 
1343
 
    function testMakeServerRelativeUrl() {
1344
 
        $this->_urlGenerator->_file[0] = '';
1345
 
        $this->assertEquals('/gallery2/v/AlbumName', $this->_urlGenerator->makeRelativeUrl(
1346
 
                                'http://example.com/gallery2/v/AlbumName'));
1347
 
    }
1348
 
 
1349
 
    /* Multisite / embedded G2 case, some URLs go to another (sub)domain */
1350
 
    function testMakeRelativeUrlDifferentSubdomains() {
1351
 
        $this->_urlGenerator->_host[0] = 'www.example.com';
1352
 
        $this->assertEquals('http://photos.example.com/gallery2/phpunit_test.php',
1353
 
                            $this->_urlGenerator->makeRelativeUrl(
1354
 
                                'http://photos.example.com/gallery2/phpunit_test.php'));
1355
 
    }
1356
 
 
1357
 
    function testMakeRelativeUrlBaseFileNotInRequestUri() {
1358
 
        $_SERVER['REQUEST_URI'] = '/gallery2/v/AlbumName';
1359
 
        $this->_urlGenerator->_file[0] = 'main.php';
1360
 
        $this->assertEquals('/gallery2/v/AnotherAlbum', $this->_urlGenerator->makeRelativeUrl(
1361
 
                                'http://example.com/gallery2/v/AnotherAlbum'));
1362
 
    }
1363
 
 
1364
 
    /* Don't be fooled by baseFile in the return URL */
1365
 
    function testMakeRelativeUrlBaseFileInReturnUrl() {
1366
 
        $_SERVER['REQUEST_URI'] = '/gallery2/v/AlbumName?g2_return=main.php';
1367
 
        $this->_urlGenerator->_file[0] = 'main.php';
1368
 
        $this->assertEquals('/gallery2/v/AnotherAlbum?g2_return=main.php',
1369
 
                            $this->_urlGenerator->makeRelativeUrl(
1370
 
                                'http://example.com/gallery2/v/AnotherAlbum?g2_return=main.php'));
1371
 
 
1372
 
    }
1373
 
 
1374
 
    function testMakeRelativeUrlBaseFileNotInReturnUrl() {
1375
 
        $_SERVER['REQUEST_URI'] = '/gallery2/phpunit_test.php';
1376
 
        $this->_urlGenerator->_file[0] = 'phpunit_test.php';
1377
 
        $this->assertEquals('phpunit_test.php?foo=bar',
1378
 
                            $this->_urlGenerator->makeRelativeUrl(
1379
 
                                'http://example.com/gallery2/phpunit_test.php?foo=bar'));
1380
 
 
1381
 
    }
1382
 
 
1383
 
    function testMakeRelativeUrlOtherProtocol() {
1384
 
        $this->_urlGenerator->_currentBaseHost[0] = 'https://example.com';
1385
 
        $this->assertEquals('http://example.com/gallery2/phpunit_test.php',
1386
 
                            $this->_urlGenerator->makeRelativeUrl(
1387
 
                                'http://example.com/gallery2/phpunit_test.php'));
1388
 
    }
1389
 
 
1390
 
    /* A multisite / embedded G2 scenario */
1391
 
    function testMakeRelativeUrlDifferentServerPath() {
1392
 
        $this->_urlGenerator->_path[0] = '/portal/';
1393
 
        $this->assertEquals('/gallery2/phpunit_test.php',
1394
 
                            $this->_urlGenerator->makeRelativeUrl('/gallery2/phpunit_test.php'));
1395
 
    }
1396
 
 
1397
 
    /*
1398
 
     * _ parseUri should accept various formats and - similar to parse_url() -
1399
 
     * split the URI into its subparts
1400
 
     */
1401
 
    function testParseUri() {
1402
 
        $tests = array();
1403
 
        $tests[] = array('uri' => 'main.php', 'protocol' => null,
1404
 
                         'host' => null, 'path' => null, 'file' => 'main.php');
1405
 
        $tests[] = array('uri' => '/main.php', 'protocol' => null,
1406
 
                         'host' => null, 'path' => '/', 'file' => 'main.php');
1407
 
        $tests[] = array('uri' => 'index.php?module=gallery2', 'protocol' => null,
1408
 
                         'host' => null, 'path' => null, 'file' => 'index.php?module=gallery2');
1409
 
        $tests[] = array('uri' => 'main.php/', 'protocol' => null,
1410
 
                         'host' => 'main.php', 'path' => '/', 'file' => '');
1411
 
        $tests[] = array('uri' => '/index.php/', 'protocol' => null,
1412
 
                         'host' => null, 'path' => '/index.php/', 'file' => '');
1413
 
        $tests[] = array('uri' => '/gallery2/foo', 'protocol' => null,
1414
 
                         'host' => null, 'path' => '/gallery2/', 'file' => 'foo');
1415
 
        $tests[] = array('uri' => '/', 'protocol' => null,
1416
 
                         'host' => null, 'path' => '/', 'file' => '');
1417
 
        $tests[] = array('uri' => 'www.example.com/', 'protocol' => null,
1418
 
                         'host' => 'www.example.com', 'path' => '/', 'file' => '');
1419
 
        $tests[] = array('uri' => 'www.example.com', 'protocol' => null,
1420
 
                         'host' => null, 'path' => null, 'file' => 'www.example.com');
1421
 
        $tests[] = array('uri' => 'https://www.example.com', 'protocol' => 'https',
1422
 
                         'host' => 'www.example.com', 'path' => null, 'file' => null);
1423
 
        $tests[] = array('uri' => 'www.example.com', 'protocol' => null,
1424
 
                         'host' => null, 'path' => null, 'file' => 'www.example.com');
1425
 
        $tests[] = array('uri' => 'www.example.com/mod/gallery2/', 'protocol' => null,
1426
 
                         'host' => 'www.example.com', 'path' => '/mod/gallery2/', 'file' => '');
1427
 
        $tests[] = array('uri' => 'http://localhost/index.php?mod=gallery2', 'protocol' => 'http',
1428
 
                         'host' => 'localhost', 'path' => '/', 'file' => 'index.php?mod=gallery2');
1429
 
        $tests[] = array('uri' => 'https://127.0.0.1/test/index.php', 'protocol' => 'https',
1430
 
                         'host' => '127.0.0.1', 'path' => '/test/', 'file' => 'index.php');
1431
 
        $tests[] = array('uri' => '127.0.0.1/test/index.php/', 'protocol' => null,
1432
 
                         'host' => '127.0.0.1', 'path' => '/test/index.php/', 'file' => '');
1433
 
 
1434
 
        foreach ($tests as $test) {
1435
 
            $expected = array($test['protocol'], $test['host'], $test['path'], $test['file']);
1436
 
            $this->assertEquals($expected, $this->_urlGenerator->_parseUri($test['uri']),
1437
 
                                "URI = '" . $test['uri'] . "'");
1438
 
        }
1439
 
    }
1440
 
 
1441
 
    function testInitSetsIsCookiePathConfiguredIfEmbedded() {
1442
 
        foreach (array(true, false) as $embedded) {
1443
 
            GalleryDataCache::put( 'G2_EMBED', $embedded, true);
1444
 
            foreach (array(true, false) as $isSet) {
1445
 
                $value = $isSet ? '/' : '';
1446
 
                $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'cookie.path', $value);
1447
 
                if ($ret) {
1448
 
                    return $this->failWithStatus($ret);
1449
 
                }
1450
 
                /* Check if the correct value was stored */
1451
 
                list ($ret, $realValue) = GalleryCoreApi::getPluginParameter('module', 'core',
1452
 
                                                                             'cookie.path');
1453
 
                if ($ret) {
1454
 
                    return $this->failWithStatus($ret);
1455
 
                }
1456
 
                $this->assertEquals($value, $realValue,
1457
 
                                    'could not set plugin parameter cookie.path');
1458
 
 
1459
 
                /* Execute the test */
1460
 
                $urlGenerator = new GalleryUrlGenerator();
1461
 
                $ret = $urlGenerator->init();
1462
 
                if ($ret) {
1463
 
                    return $this->failWithStatus($ret);
1464
 
                }
1465
 
                /* Check what we want to verfiy */
1466
 
                $msg = $embedded ? 'embedded, ' : 'standalone, ';
1467
 
                $msg .= $isSet ? 'cookie path set' : 'cookie path not set';
1468
 
                if ($embedded) {
1469
 
                    $this->assertEquals($isSet, $urlGenerator->_isCookiePathConfigured, $msg);
1470
 
                } else {
1471
 
                    $this->assert(!isset($urlGenerator->_isCookiePathConfigured), $msg);
1472
 
                }
1473
 
            }
1474
 
        }
1475
 
    }
1476
 
 
1477
 
    function testGetCacheableUrl1() {
1478
 
        $_SERVER['REQUEST_URI'] = '/gallery2/phpunit_test.php?foo=bar&g2_navId=blah&bar=baz';
1479
 
        $this->assertEquals('http://example.com/gallery2/phpunit_test.php?foo=bar&bar=baz',
1480
 
                            $this->_urlGenerator->getCacheableUrl());
1481
 
    }
1482
 
 
1483
 
    function testGetCacheableUrl2() {
1484
 
        $_SERVER['REQUEST_URI'] =
1485
 
            '/gallery2/phpunit_test.php?foo=bar&g2_navId=blah&bar=baz&g2_return=blaa';
1486
 
        $this->assertEquals('http://example.com/gallery2/phpunit_test.php?foo=bar&bar=baz',
1487
 
                            $this->_urlGenerator->getCacheableUrl());
1488
 
    }
1489
 
 
1490
 
    function testGetCacheableUrl3() {
1491
 
        $_SERVER['REQUEST_URI'] = '/gallery2/phpunit_test.php?g2_navId=blah';
1492
 
        $this->assertEquals('http://example.com/gallery2/phpunit_test.php',
1493
 
                            $this->_urlGenerator->getCacheableUrl());
1494
 
    }
1495
 
 
1496
 
    function testGetCacheableUrl4() {
1497
 
        $_SERVER['REQUEST_URI'] = '/gallery2/phpunit_test.php';
1498
 
        $this->assertEquals('http://example.com/gallery2/phpunit_test.php',
1499
 
                            $this->_urlGenerator->getCacheableUrl());
1500
 
    }
1501
 
 
1502
 
    function testGenerateUrlWithNavIdAndSessionString() {
1503
 
        global $gallery;
1504
 
        list ($ret) = $this->_prepareGenerateUrl('', 'phpunit_test.php', null);
1505
 
        if ($ret) {
1506
 
            $this->failWithStatus($ret);
1507
 
        }
1508
 
        $session =& $gallery->getSession();
1509
 
        $this->assertEquals(true, $session->isPersistent(), 'persistent session');
1510
 
        $this->assert(!empty($session->_sessionId), 'session id');
1511
 
 
1512
 
        $actual = $this->_urlGenerator->generateUrl(array('view' => 'foo.Bar', 'navId' => '1234'),
1513
 
                                                    array('forceSessionId' => true));
1514
 
        $expected = 'phpunit_test.php?g2_view=foo.Bar&amp;g2_navId=1234&amp;g2_' .
1515
 
            $session->getKey() . '=' . $session->getId();
1516
 
 
1517
 
        $this->assertEquals($expected, $actual, 'url');
1518
 
    }
1519
 
 
1520
 
    function testGenerateUrlWithoutPersistentSession() {
1521
 
        global $gallery;
1522
 
        GalleryUtilities::removeRequestVariable(SESSION_ID_PARAMETER);
1523
 
        unset($_COOKIE[SESSION_ID_PARAMETER]);
1524
 
        unset($_SERVER['HTTP_COOKIE']);
1525
 
 
1526
 
        $session = new GallerySession();
1527
 
        $ret = $session->init();
1528
 
        if ($ret) {
1529
 
            return $this->failWithStatus($ret);
1530
 
        }
1531
 
        $backup = $gallery->_session;
1532
 
        $gallery->_session =& $session;
1533
 
        $this->assertEquals(false, $session->isPersistent(), 'not persistent');
1534
 
        $this->assert(empty($session->_sessionId), 'session id');
1535
 
        $this->assertEquals(SESSION_TEMP_ID, $session->getId(), 'temp id');
1536
 
 
1537
 
        $actual = $this->_urlGenerator->generateUrl(array('view' => 'foo.Bar', 'navId' => '1234'),
1538
 
                                                    array('forceSessionId' => true));
1539
 
        /* No navId since there's no persistent session to store the nav history */
1540
 
        $expected = 'phpunit_test.php?g2_view=foo.Bar&amp;g2_' . $session->getKey() .
1541
 
            '=' . $session->getId();
1542
 
 
1543
 
        $this->assertEquals($expected, $actual, 'url');
1544
 
 
1545
 
        /* And now force to not use the temp id */
1546
 
        $gallery->_session->doNotUseTempId();
1547
 
        $actual = $this->_urlGenerator->generateUrl(array('view' => 'foo.Bar', 'navId' => '1234'),
1548
 
                                                    array('forceSessionId' => true));
1549
 
        $expected = 'phpunit_test.php?g2_view=foo.Bar';
1550
 
        $this->assertEquals($expected, $actual, 'url without temp session id');
1551
 
 
1552
 
        $gallery->_session = $backup;
1553
 
    }
1554
 
 
1555
 
    function testOmitDefaults() {
1556
 
        global $gallery;
1557
 
        $view = GALLERY_DEFAULT_VIEW;
1558
 
        list ($ret, $rootId) = GalleryCoreApi::getDefaultAlbumId();
1559
 
        if ($ret) {
1560
 
            return $this->failWithStatus($ret);
1561
 
        }
1562
 
 
1563
 
        /* Omit view parameter for default view */
1564
 
        $url = $this->_urlGenerator->generateUrl(
1565
 
                array('view' => $view, 'itemId' => 123),
1566
 
                array('forceSessionId' => false));
1567
 
        $this->assertEquals('phpunit_test.php?g2_itemId=123', $url);
1568
 
 
1569
 
        /* Omit itemId parameter for default album with default view */
1570
 
        $url = $this->_urlGenerator->generateUrl(
1571
 
                array('view' => $view, 'itemId' => $rootId),
1572
 
                array('forceSessionId' => false));
1573
 
        $this->assertEquals('phpunit_test.php', $url);
1574
 
 
1575
 
        $gallery->setConfig('defaultAlbumId', 123);
1576
 
        unset($this->_urlGenerator->_rootItemId);
1577
 
 
1578
 
        $url = $this->_urlGenerator->generateUrl(
1579
 
                array('view' => $view, 'itemId' => 123),
1580
 
                array('forceSessionId' => false));
1581
 
        $this->assertEquals('phpunit_test.php', $url, 'alternate default album');
1582
 
    }
1583
 
 
1584
 
    function testGenerateUrlWithProtocol() {
1585
 
        $this->assertEquals('webdav://example.com/gallery2/phpunit_test.php?g2_a=b',
1586
 
            $this->_urlGenerator->generateUrl(array('a' => 'b'), array('protocol' => 'webdav')));
1587
 
    }
1588
 
 
1589
 
    function testGenerateUrlAddsAuthTokenForControllerUrls() {
1590
 
        global $gallery;
1591
 
        $session =& $gallery->getSession();
1592
 
 
1593
 
        $session->put('core.authToken', 'AUTH-TOKEN');
1594
 
        $url = $this->_urlGenerator->generateUrl(
1595
 
                array('controller' => 'core.UserLogin', 'foo' => 123));
1596
 
        $expectedUrl =
1597
 
            $this->_signUrl('phpunit_test.php?g2_controller=core.UserLogin&amp;g2_foo=123');
1598
 
        $this->assertEquals($expectedUrl, $url);
1599
 
    }
1600
 
 
1601
 
    function testGenerateUrlUseAuthTokenTrueHasAuthToken() {
1602
 
        global $gallery;
1603
 
        $session =& $gallery->getSession();
1604
 
 
1605
 
        $session->put('core.authToken', 'AUTH-TOKEN');
1606
 
        $url = $this->_urlGenerator->generateUrl(
1607
 
                array('view' => 'core.UserLogin', 'foo' => 123), array('useAuthToken' => 1));
1608
 
        $expectedUrl = $this->_signUrl('phpunit_test.php?g2_view=core.UserLogin&amp;g2_foo=123');
1609
 
        $this->assertEquals($expectedUrl, $url);
1610
 
    }
1611
 
 
1612
 
    function testGenerateUrlUseAuthTokenTrueControllerUrlHasAuthToken() {
1613
 
        global $gallery;
1614
 
        $session =& $gallery->getSession();
1615
 
 
1616
 
        $session->put('core.authToken', 'AUTH-TOKEN');
1617
 
        $url = $this->_urlGenerator->generateUrl(
1618
 
            array('controller' => 'core.UserLogin', 'foo' => 123),
1619
 
            array('useAuthToken' => true));
1620
 
        $expectedUrl =
1621
 
            $this->_signUrl('phpunit_test.php?g2_controller=core.UserLogin&amp;g2_foo=123');
1622
 
        $this->assertEquals($expectedUrl, $url);
1623
 
    }
1624
 
 
1625
 
    function testGenerateUrlUseAuthTokenFalseHasNoAuthToken() {
1626
 
        global $gallery;
1627
 
        $session =& $gallery->getSession();
1628
 
 
1629
 
        $session->put('core.authToken', 'AUTH-TOKEN');
1630
 
        $url = $this->_urlGenerator->generateUrl(
1631
 
            array('controller' => 'core.UserLogin', 'foo' => 123),
1632
 
            array('useAuthToken' => false));
1633
 
        $expectedUrl = 'phpunit_test.php?g2_controller=core.UserLogin&amp;g2_foo=123';
1634
 
        $this->assertEquals($expectedUrl, $url);
1635
 
    }
1636
 
 
1637
 
    function testAppendParamsToUrl() {
1638
 
        $this->assertEquals(
1639
 
            'test.php?g2_a=foo1%25bar&amp;g2_b=foo2%25bar',
1640
 
            GalleryUrlGenerator::appendParamsToUrl(
1641
 
                'test.php',
1642
 
                array('a' => 'foo1%bar',
1643
 
                      'b' => 'foo2%bar')));
1644
 
    }
1645
 
 
1646
 
    function testAppendNestedParamsToUrl() {
1647
 
        $this->assertEquals(
1648
 
            'test.php?g2_a%5Bb%5D=foo1%25bar&amp;g2_a%5Bc%5D=foo2%25bar',
1649
 
            GalleryUrlGenerator::appendParamsToUrl(
1650
 
                'test.php',
1651
 
                array('a' => array('b' => 'foo1%bar',
1652
 
                                   'c' => 'foo2%bar'),
1653
 
                      'd' => array())));
1654
 
    }
1655
 
 
1656
 
    function testAppendParamsToUrlWithQueryString() {
1657
 
        $this->assertEquals(
1658
 
            'test.php?foo=bar&amp;g2_a=foo1%25bar&amp;g2_b=foo2%25bar',
1659
 
            GalleryUrlGenerator::appendParamsToUrl(
1660
 
                'test.php?foo=bar',
1661
 
                array('a' => 'foo1%bar',
1662
 
                      'b' => 'foo2%bar')));
1663
 
    }
1664
 
 
1665
 
    function testAppendParamsToUrlNoPrefix() {
1666
 
        $this->assertEquals(
1667
 
            'test.php?a=foo1%25bar&amp;b=foo2%25bar',
1668
 
            GalleryUrlGenerator::appendParamsToUrl(
1669
 
                'test.php',
1670
 
                array('a' => 'foo1%bar',
1671
 
                      'b' => 'foo2%bar'),
1672
 
                false));
1673
 
    }
1674
 
 
1675
 
    function testAppendParamsToUrlNoHtmlEntities() {
1676
 
        $this->assertEquals(
1677
 
            'test.php?g2_a=foo1%25bar&g2_b=foo2%25bar',
1678
 
            GalleryUrlGenerator::appendParamsToUrl(
1679
 
                'test.php',
1680
 
                array('a' => 'foo1%bar',
1681
 
                      'b' => 'foo2%bar'),
1682
 
                true, false));
1683
 
    }
1684
 
 
1685
 
    function testAppendParamsToUrlUrlEncodesParamKeyCorrectly() {
1686
 
        $this->assertEquals(
1687
 
            'test.php?g2_a%25=foo1%25bar&amp;' . urlencode('g2_b%[c%d][e%f]') . '=foo2%25bar',
1688
 
            GalleryUrlGenerator::appendParamsToUrl(
1689
 
                'test.php',
1690
 
                array('a%' => 'foo1%bar',
1691
 
                      'b%' => array('c%d' => array('e%f' => 'foo2%bar')))));
1692
 
    }
1693
 
 
1694
 
    function testAppendParamsToUrlNoUrlEncode() {
1695
 
        $this->assertEquals(
1696
 
            'test.php?g2_a%=foo1%bar&amp;g2_b%[c%d][e%f]=foo2%bar',
1697
 
            GalleryUrlGenerator::appendParamsToUrl(
1698
 
                'test.php',
1699
 
                array('a%' => 'foo1%bar',
1700
 
                      'b%' => array('c%d' => array('e%f' => 'foo2%bar'))),
1701
 
                true, true, false));
1702
 
    }
1703
 
 
1704
 
    function testAppendParamsToUrlPreservesParamOrder() {
1705
 
        $this->assertEquals(
1706
 
            'test.php?a=1&b[sub1]=2&b[sub2]=3&c=4',
1707
 
            GalleryUrlGenerator::appendParamsToUrl(
1708
 
                'test.php',
1709
 
                array('a' => '1',
1710
 
                      'b' => array('sub1' => '2',
1711
 
                                   'sub2' => '3'),
1712
 
                      'c' => '4'),
1713
 
                false, false, false)); /* Makes it easier to read */
1714
 
    }
1715
 
 
1716
 
    function testAppendParamsToUrlEmptyParams() {
1717
 
        $this->assertEquals(
1718
 
            'test.php',
1719
 
            GalleryUrlGenerator::appendParamsToUrl('test.php', array()));
1720
 
    }
1721
 
 
1722
 
    /*
1723
 
     * Verify that we don't do any special handling for paramater keys with brackets. There seems to
1724
 
     * be no escape sequence for them anyway. 
1725
 
     */ 
1726
 
    function testAppendParamsToUrlDoesNotHandleBracketsInKeyString() {
1727
 
        $this->assertEquals(
1728
 
            'test.php?foo[bar=1&a[b]=2',
1729
 
            GalleryUrlGenerator::appendParamsToUrl(
1730
 
                'test.php',
1731
 
                array('foo[bar' => '1', 'a[b]' => '2'),
1732
 
                false, false, false)); /* Makes it easier to read */
1733
 
    }
1734
 
}
1735
 
 
1736
 
class UrlGeneratorTestPlatform extends GalleryPlatform {
1737
 
 
1738
 
    function file_exists($filename) {
1739
 
        $basedir = realpath(dirname(__FILE__) . '/../../classes');
1740
 
        switch($filename) {
1741
 
        case "$basedir/../../../test/local/test1.css":
1742
 
            return false;
1743
 
 
1744
 
        case "$basedir/../../../test/local/test2.css":
1745
 
            return true;
1746
 
 
1747
 
        default:
1748
 
            print "Unexpected file_exists($filename) call";
1749
 
            return false;
1750
 
        }
1751
 
    }
1752
 
}
1753
 
?>