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

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/DownloadItemTest.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 DownloadItem functionality
23
 
 * @package GalleryCore
24
 
 * @subpackage PHPUnit
25
 
 * @author Alan Harder <alan.harder@sun.com>
26
 
 * @version $Revision: 15513 $
27
 
 */
28
 
class DownloadItemTest extends GalleryTestCase {
29
 
 
30
 
    function DownloadItemTest($methodName) {
31
 
        $this->GalleryTestCase($methodName);
32
 
    }
33
 
 
34
 
    function setUp() {
35
 
        global $gallery;
36
 
        parent::setUp();
37
 
 
38
 
        list ($ret, $this->_item) = $this->_createRandomDataItem($this->_getRootId());
39
 
        if ($ret) {
40
 
            print $ret->getAsHtml();
41
 
            return $this->failWithStatus($ret);
42
 
        }
43
 
        $ret = GalleryCoreApi::removeItemPermissions($this->_item->getId());
44
 
        if ($ret) {
45
 
            print $ret->getAsHtml();
46
 
            return $this->failWithStatus($ret);
47
 
        }
48
 
        $this->_markForCleanup($this->_item);
49
 
 
50
 
        list ($ret, $this->_view) = GalleryView::loadView('core.DownloadItem');
51
 
        if ($ret) {
52
 
            print $ret->getAsHtml();
53
 
            return $this->failWithStatus($ret);
54
 
        }
55
 
        $this->_userId = $gallery->getActiveUserId();
56
 
        $gallery->_phpVm = new DownloadItemTestPhpVm();
57
 
    }
58
 
 
59
 
    function _download() {
60
 
        ob_start();
61
 
        $ret = $this->_view->renderImmediate(array(), array());
62
 
        $output = ob_get_contents();
63
 
        ob_end_clean();
64
 
        return array($ret, $output);
65
 
    }
66
 
 
67
 
    function testNoItemId() {
68
 
        list ($ret, $output) = $this->_download();
69
 
        $this->assert($ret && $ret->getErrorCode() & ERROR_BAD_PARAMETER, 'expected error');
70
 
    }
71
 
 
72
 
    function testMaliciousFilename() {
73
 
        GalleryUtilities::putRequestVariable('itemId', $this->_item->getId());
74
 
        GalleryUtilities::putRequestVariable('fileName', 'voodoo');
75
 
        list ($ret, $output) = $this->_download();
76
 
        $this->assert($ret && $ret->getErrorCode(), 'expected error');
77
 
    }
78
 
 
79
 
    function testItemPermission() {
80
 
        /* Item needs core.viewSource */
81
 
        $ret = GalleryCoreApi::addUserPermission($this->_item->getId(), $this->_userId,
82
 
                                                 'core.view');
83
 
        if ($ret) {
84
 
            return $this->failWithStatus($ret);
85
 
        }
86
 
        $ret = GalleryCoreApi::addUserPermission($this->_item->getId(), $this->_userId,
87
 
                                                 'core.viewResizes');
88
 
        if ($ret) {
89
 
            return $this->failWithStatus($ret);
90
 
        }
91
 
 
92
 
        GalleryUtilities::putRequestVariable('itemId', $this->_item->getId());
93
 
        list ($ret, $output) = $this->_download();
94
 
        $this->assert($ret && $ret->getErrorCode() & ERROR_PERMISSION_DENIED, 'expected error');
95
 
 
96
 
        $ret = GalleryCoreApi::addUserPermission($this->_item->getId(), $this->_userId,
97
 
                                                 'core.viewSource');
98
 
        if ($ret) {
99
 
            return $this->failWithStatus($ret);
100
 
        }
101
 
 
102
 
        list ($ret, $output) = $this->_download();
103
 
        $this->assert(!$ret, 'expected success');
104
 
    }
105
 
 
106
 
    function testPreferredPermission() {
107
 
        /* Preferred needs core.viewSource */
108
 
        list ($ret, $derivative) = $this->_createDerivative($this->_item, $this->_item->getId(),
109
 
                                                            DERIVATIVE_TYPE_IMAGE_PREFERRED);
110
 
        if ($ret) {
111
 
            return $this->failWithStatus($ret);
112
 
        }
113
 
        $ret = GalleryCoreApi::addUserPermission($this->_item->getId(), $this->_userId,
114
 
                                                 'core.view');
115
 
        if ($ret) {
116
 
            return $this->failWithStatus($ret);
117
 
        }
118
 
        $ret = GalleryCoreApi::addUserPermission($this->_item->getId(), $this->_userId,
119
 
                                                 'core.viewResizes');
120
 
        if ($ret) {
121
 
            return $this->failWithStatus($ret);
122
 
        }
123
 
 
124
 
        GalleryUtilities::putRequestVariable('itemId', $derivative->getId());
125
 
        list ($ret, $output) = $this->_download();
126
 
        $this->assert($ret && $ret->getErrorCode() & ERROR_PERMISSION_DENIED, 'expected error');
127
 
 
128
 
        $ret = GalleryCoreApi::addUserPermission($this->_item->getId(), $this->_userId,
129
 
                                                 'core.viewSource');
130
 
        if ($ret) {
131
 
            return $this->failWithStatus($ret);
132
 
        }
133
 
 
134
 
        list ($ret, $output) = $this->_download();
135
 
        $this->assert(!$ret, 'expected success');
136
 
    }
137
 
 
138
 
    function testResizePermission() {
139
 
        /* Resize needs core.viewResizes */
140
 
        list ($ret, $derivative) = $this->_createDerivative($this->_item, $this->_item->getId(),
141
 
                                                            DERIVATIVE_TYPE_IMAGE_RESIZE);
142
 
        if ($ret) {
143
 
            return $this->failWithStatus($ret);
144
 
        }
145
 
        $ret = GalleryCoreApi::addUserPermission($this->_item->getId(), $this->_userId,
146
 
                                                 'core.view');
147
 
        if ($ret) {
148
 
            return $this->failWithStatus($ret);
149
 
        }
150
 
        $ret = GalleryCoreApi::addUserPermission($this->_item->getId(), $this->_userId,
151
 
                                                 'core.viewSource');
152
 
        if ($ret) {
153
 
            return $this->failWithStatus($ret);
154
 
        }
155
 
 
156
 
        GalleryUtilities::putRequestVariable('itemId', $derivative->getId());
157
 
        list ($ret, $output) = $this->_download();
158
 
        $this->assert($ret && $ret->getErrorCode() & ERROR_PERMISSION_DENIED, 'expected error');
159
 
 
160
 
        $ret = GalleryCoreApi::addUserPermission($this->_item->getId(), $this->_userId,
161
 
                                                 'core.viewResizes');
162
 
        if ($ret) {
163
 
            return $this->failWithStatus($ret);
164
 
        }
165
 
 
166
 
        list ($ret, $output) = $this->_download();
167
 
        $this->assert(!$ret, 'expected success');
168
 
    }
169
 
 
170
 
    function testThumbnailPermission() {
171
 
        /* Thumbnail needs core.view */
172
 
        list ($ret, $derivative) = $this->_createDerivative($this->_item, $this->_item->getId(),
173
 
                                                            DERIVATIVE_TYPE_IMAGE_THUMBNAIL);
174
 
        if ($ret) {
175
 
            return $this->failWithStatus($ret);
176
 
        }
177
 
        $ret = GalleryCoreApi::addUserPermission($this->_item->getId(), $this->_userId,
178
 
                                                 'core.viewResizes');
179
 
        if ($ret) {
180
 
            return $this->failWithStatus($ret);
181
 
        }
182
 
        $ret = GalleryCoreApi::addUserPermission($this->_item->getId(), $this->_userId,
183
 
                                                 'core.viewSource');
184
 
        if ($ret) {
185
 
            return $this->failWithStatus($ret);
186
 
        }
187
 
 
188
 
        GalleryUtilities::putRequestVariable('itemId', $derivative->getId());
189
 
        list ($ret, $output) = $this->_download();
190
 
        $this->assert($ret && $ret->getErrorCode() & ERROR_PERMISSION_DENIED, 'expected error');
191
 
 
192
 
        $ret = GalleryCoreApi::addUserPermission($this->_item->getId(), $this->_userId,
193
 
                                                 'core.view');
194
 
        if ($ret) {
195
 
            return $this->failWithStatus($ret);
196
 
        }
197
 
 
198
 
        list ($ret, $output) = $this->_download();
199
 
        $this->assert(!$ret, 'expected success');
200
 
    }
201
 
 
202
 
    function testPrintService() {
203
 
        /* Flag in session allows print service to bypass viewResizes/viewSource for set of ids */
204
 
        global $gallery;
205
 
        $session =& $gallery->getSession();
206
 
        $session->put('core.isPrintService', array(2, $this->_item->getId()));
207
 
 
208
 
        /* Still need core.view */
209
 
        GalleryUtilities::putRequestVariable('itemId', $this->_item->getId());
210
 
        list ($ret, $output) = $this->_download();
211
 
        $this->assert($ret && $ret->getErrorCode() & ERROR_PERMISSION_DENIED, 'expected error 1');
212
 
 
213
 
        $ret = GalleryCoreApi::addUserPermission($this->_item->getId(), $this->_userId,
214
 
                                                 'core.view');
215
 
        if ($ret) {
216
 
            return $this->failWithStatus($ret);
217
 
        }
218
 
 
219
 
        $session->put('core.isPrintService', array(2, 5));
220
 
        list ($ret, $output) = $this->_download();
221
 
        $this->assert($ret && $ret->getErrorCode() & ERROR_PERMISSION_DENIED, 'expected error 2');
222
 
 
223
 
        $session->put('core.isPrintService', array(2, $this->_item->getId(), 5));
224
 
        list ($ret, $output) = $this->_download();
225
 
        $this->assert(!$ret, 'expected success');
226
 
    }
227
 
}
228
 
 
229
 
/**
230
 
 * Test PhpVm to ignore header() calls
231
 
 */
232
 
class DownloadItemTestPhpVm extends GalleryPhpVm {
233
 
    function header($string, $replace=null) {
234
 
    }
235
 
}
236
 
?>