~ubuntu-branches/ubuntu/maverick/gallery2/maverick

« back to all changes in this revision

Viewing changes to modules/zipcart/test/phpunit/ZipCartDownloadTest.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
 
 * Test ZipCart Download functionality
23
 
 *
24
 
 * @package ZipCart
25
 
 * @subpackage PHPUnit
26
 
 * @author Alan Harder <alan.harder@sun.com>
27
 
 * @version $Revision: 15513 $
28
 
 */
29
 
class ZipCartDownloadTest extends GalleryTestCase {
30
 
 
31
 
    function ZipCartDownloadTest($methodName) {
32
 
        $this->GalleryTestCase($methodName);
33
 
    }
34
 
 
35
 
    function setUp() {
36
 
        global $gallery;
37
 
        parent::setUp();
38
 
 
39
 
        list ($ret, $this->_view) = GalleryView::loadView('zipcart.Download');
40
 
        if ($ret) {
41
 
            print $ret->getAsHtml();
42
 
            return $this->failWithStatus($ret);
43
 
        }
44
 
 
45
 
        $gallery->_phpVm = new ZipCartDownloadPhpVm($this);
46
 
        $gallery->setConfig('data.gallery.tmp', '/data/tmp/');
47
 
    }
48
 
 
49
 
    function testDownloadIE() {
50
 
        global $gallery;
51
 
        $platform = new UnitTestPlatform();
52
 
        $platform->setReply('is_readable', array('/data/tmp/test.zip'), true);
53
 
        $platform->setReply('filesize', array('/data/tmp/test.zip'), 123);
54
 
        $platform->setReply('fopen', array('/data/tmp/test.zip', 'rb', 0), 'testfd');
55
 
        $platform->setReply('fread', array('testfd', 65535), 'zipdata');
56
 
        $platform->setReply('fread', array('testfd', 65535), '');
57
 
        $platform->setReply('fclose', array('testfd'), true);
58
 
        $platform->setReply('unlink', array('/data/tmp/test.zip'), true);
59
 
        $platform->setReply('unlink', array('/data/tmp/test'), true);
60
 
        $gallery->setPlatform($platform);
61
 
 
62
 
        GalleryUtilities::putRequestVariable('file', 'test');
63
 
        $_SERVER['HTTP_USER_AGENT'] = 'This is MSIE 6 browser';
64
 
 
65
 
        ob_start();
66
 
        $ret = $this->_view->renderImmediate(array(), array());
67
 
        if ($ret) {
68
 
            return $this->failWithStatus($ret);
69
 
        }
70
 
        $output = ob_get_contents();
71
 
        ob_end_clean();
72
 
 
73
 
        $this->assertEquals(
74
 
            array('Content-Type: application/zip',
75
 
                  'Content-Disposition: inline; filename="G2cart.zip"',
76
 
                  'Content-Length: 123'), $this->_header, 'header');
77
 
        $this->assertEquals('zipdata', $output, 'output');
78
 
    }
79
 
 
80
 
    function testDownloadMultipart() {
81
 
        global $gallery;
82
 
        $platform = new UnitTestPlatform();
83
 
        $platform->setReply('is_readable', array('/data/tmp/test.zip'), true);
84
 
        $platform->setReply('filesize', array('/data/tmp/test.zip'), 123);
85
 
        $platform->setReply('fopen', array('/data/tmp/test.zip', 'rb', 0), 'testfd');
86
 
        $platform->setReply('fread', array('testfd', 65535), 'zipdata');
87
 
        $platform->setReply('fread', array('testfd', 65535), '');
88
 
        $platform->setReply('fclose', array('testfd'), true);
89
 
        $platform->setReply('unlink', array('/data/tmp/test.zip'), true);
90
 
        $platform->setReply('unlink', array('/data/tmp/test'), true);
91
 
        $gallery->setPlatform($platform);
92
 
 
93
 
        GalleryUtilities::putRequestVariable('file', 'test');
94
 
        $_SERVER['HTTP_USER_AGENT'] = 'This is some Gecko browser';
95
 
 
96
 
        ob_start();
97
 
        $ret = $this->_view->renderImmediate(array(), array());
98
 
        if ($ret) {
99
 
            return $this->failWithStatus($ret);
100
 
        }
101
 
        $output = ob_get_contents();
102
 
        ob_end_clean();
103
 
 
104
 
        $this->assertEquals(
105
 
            array('Content-Type: multipart/mixed; boundary=G2ZipCart'), $this->_header, 'header');
106
 
        $this->assertEquals("--G2ZipCart\nContent-Type: application/zip\nContent-Disposition: " .
107
 
            "inline; filename=\"G2cart.zip\"\nContent-Length: 123\n\nzipdata\n--G2ZipCart\n" .
108
 
            "Content-Type: text/html\nPragma: No-cache\n\n<html><body onload=\"location" .
109
 
            ".reload()\"></body></html>\n\n--G2ZipCart--\n", $output, 'output');
110
 
    }
111
 
 
112
 
    function testCartRedirect() {
113
 
        global $gallery;
114
 
        $platform = new UnitTestPlatform();
115
 
        $platform->setReply('is_readable', array('/data/tmp/test.zip'), false);
116
 
        $gallery->setPlatform($platform);
117
 
 
118
 
        GalleryUtilities::putRequestVariable('file', 'test');
119
 
 
120
 
        ob_start();
121
 
        $ret = $this->_view->renderImmediate(array(), array());
122
 
        if ($ret) {
123
 
            return $this->failWithStatus($ret);
124
 
        }
125
 
        $output = ob_get_contents();
126
 
        ob_end_clean();
127
 
 
128
 
        $this->assertEquals(1, count($this->_header), 'header lines');
129
 
        $this->assertEquals('Location:', substr($this->_header[0], 0, 9), 'header');
130
 
        $this->assertEquals('', $output, 'output');
131
 
    }
132
 
 
133
 
    function testBadFile() {
134
 
        global $gallery;
135
 
        $platform = new UnitTestPlatform();
136
 
        $platform->setReply('is_readable', array('/data/tmp/passwd.zip'), false);
137
 
        $gallery->setPlatform($platform);
138
 
 
139
 
        /* Verify a malicious file param can't get out of the tmp dir */
140
 
        GalleryUtilities::putRequestVariable('file', '../../etc/passwd');
141
 
 
142
 
        ob_start();
143
 
        $ret = $this->_view->renderImmediate(array(), array());
144
 
        if ($ret) {
145
 
            return $this->failWithStatus($ret);
146
 
        }
147
 
        $output = ob_get_contents();
148
 
        ob_end_clean();
149
 
 
150
 
        $this->assertEquals(1, count($this->_header), 'header lines');
151
 
        $this->assertEquals('Location:', substr($this->_header[0], 0, 9), 'header');
152
 
        $this->assertEquals('', $output, 'output');
153
 
    }
154
 
}
155
 
 
156
 
/**
157
 
 * ZipCartDownloadPhpVm for header() calls
158
 
 * @package ZipCart
159
 
 * @subpackage PHPUnit
160
 
 */
161
 
class ZipCartDownloadPhpVm extends GalleryPhpVm {
162
 
    function ZipCartDownloadPhpVm(&$test) {
163
 
        $this->_test =& $test;
164
 
    }
165
 
 
166
 
    function header($string) {
167
 
        $this->_test->_header[] = $string;
168
 
    }
169
 
 
170
 
    function ini_set($varname, $newvalue) {
171
 
    }
172
 
}
173
 
?>