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

« back to all changes in this revision

Viewing changes to modules/imageblock/test/phpunit/ImageBlockHelperTest.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 Image Block helper
23
 
 * @package ImageBlock
24
 
 * @subpackage PHPUnit
25
 
 * @author Jay Rossiter <cryptographite@users.sf.net>
26
 
 * @version $Revision: 15875 $
27
 
 */
28
 
class ImageBlockHelperTest extends GalleryTestCase {
29
 
 
30
 
    function ImageBlockHelperTest($methodName) {
31
 
        $this->GalleryTestCase($methodName);
32
 
    }
33
 
 
34
 
    function _createTopAlbum() {
35
 
        /* Create a new album and image, set thumbnail and highlight */
36
 
        list ($ret, $album) = $this->_createRandomAlbum($this->_getRootId());
37
 
        if ($ret) {
38
 
            return array($ret, null);
39
 
        }
40
 
        $this->_markForCleanup($album);
41
 
        return array(null, $album);
42
 
    }
43
 
 
44
 
    function _createHierarchy() {
45
 
        list ($ret, $this->_topAlbum, $this->_topItems) =
46
 
            $this->_createSubAlbumWithItems($this->_getRootId());
47
 
        if ($ret) {
48
 
            return $this->failWithStatus($ret);
49
 
        }
50
 
        $this->_markForCleanup($this->_topAlbum);
51
 
 
52
 
        list ($ret, $this->_subAlbum, $this->_subItems) =
53
 
            $this->_createSubAlbumWithItems($this->_topAlbum->getId());
54
 
        if ($ret) {
55
 
            return $this->failWithStatus($ret);
56
 
        }
57
 
    }
58
 
 
59
 
    function _createSubAlbumWithItems($parentId) {
60
 
        /* Create a new album and image, set thumbnail and highlight */
61
 
        list ($ret, $album) = $this->_createRandomAlbum($parentId);
62
 
        if ($ret) {
63
 
            return array($ret, null, null);
64
 
        }
65
 
 
66
 
        $items = array();
67
 
        for ($i = 0; $i < 5; $i++) {
68
 
            list ($ret, $items[$i]) = $this->_createRandomDataItem($album->getId());
69
 
            if ($ret) {
70
 
                return array($ret, null, null);
71
 
            }
72
 
        }
73
 
 
74
 
        return array(null, $album, $items);
75
 
    }
76
 
 
77
 
    function testImageBlockHelperGetDisabledFlag() {
78
 
        $ret = $this->_createHierarchy();
79
 
        if ($ret) {
80
 
            return $this->failWithStatus($ret);
81
 
        }
82
 
 
83
 
        $ret = GalleryCoreApi::addMapEntry(
84
 
            'ImageBlockDisabledMap', array('itemId' => $this->_topAlbum->getId()));
85
 
        if ($ret) {
86
 
            return $this->failWithStatus($ret);
87
 
        }
88
 
        list ($ret, $disabledFlag) = ImageBlockHelper::getDisabledFlag($this->_topAlbum->getId());
89
 
        $this->assert($disabledFlag, 'getDisabledFlag returned false');
90
 
 
91
 
        $ret = GalleryCoreApi::removeMapEntry(
92
 
            'ImageBlockDisabledMap', array('itemId' => $this->_topAlbum->getId()));
93
 
        if ($ret) {
94
 
            return $this->failWithStatus($ret);
95
 
        }
96
 
 
97
 
        list ($ret, $disabledFlag) = ImageBlockHelper::getDisabledFlag($this->_topAlbum->getId());
98
 
        $this->assert(!$disabledFlag, 'getDisabledFlag returned true');
99
 
    }
100
 
 
101
 
    function testImageBlockHelperSetDisabledFlag() {
102
 
        global $gallery;
103
 
 
104
 
        $ret = $this->_createHierarchy();
105
 
        if ($ret) {
106
 
            return $this->failWithStatus($ret);
107
 
        }
108
 
 
109
 
        $ret = ImageBlockHelper::setDisabledFlag($this->_topAlbum, false, true);
110
 
        if ($ret) {
111
 
            return $this->failWithStatus($ret);
112
 
        }
113
 
 
114
 
        $itemIds = array();
115
 
        foreach ($this->_topItems as $item) {
116
 
            $itemIds[] = (int)$item->getId();
117
 
        }
118
 
        $itemIds[] = (int)$this->_topAlbum->getId();
119
 
        sort($itemIds);
120
 
 
121
 
        $query = 'SELECT [ImageBlockDisabledMap::itemId] ' .
122
 
                 'FROM [ImageBlockDisabledMap] ' .
123
 
                 'WHERE [ImageBlockDisabledMap::itemId] IN (' .
124
 
                 GalleryUtilities::makeMarkers(sizeof($itemIds)) .
125
 
                 ')';
126
 
 
127
 
        list ($ret, $searchResults) = $gallery->search($query, $itemIds);
128
 
        if ($ret) {
129
 
            return $this->failWithStatus($ret);
130
 
        }
131
 
 
132
 
        $dbItemIds = array();
133
 
        while ($rec = $searchResults->nextResult()) {
134
 
            $dbItemIds[] = (int)$rec[0];
135
 
        }
136
 
        sort($itemIds);
137
 
        sort($dbItemIds);
138
 
        $this->assertEquals($itemIds, $dbItemIds, 'Items not added to the DisableMap');
139
 
 
140
 
        $ret = ImageBlockHelper::setDisabledFlag($this->_topAlbum, false, false);
141
 
        if ($ret) {
142
 
            return $this->failWithStatus($ret);
143
 
        }
144
 
 
145
 
        list ($ret, $searchResults) = $gallery->search($query, $itemIds);
146
 
        if ($ret) {
147
 
            return $this->failWithStatus($ret);
148
 
        }
149
 
 
150
 
        $dbItemIds = array();
151
 
        while ($rec = $searchResults->nextResult()) {
152
 
            $dbItemIds[] = (int)$rec[0];
153
 
        }
154
 
        $this->assertEquals(array(), $dbItemIds, 'Items not removed from the DisabledMap');
155
 
    }
156
 
 
157
 
    function testImageBlockHelperSetDisabledFlagRecursive() {
158
 
        global $gallery;
159
 
 
160
 
        $ret = $this->_createHierarchy();
161
 
        if ($ret) {
162
 
            return $this->failWithStatus($ret);
163
 
        }
164
 
 
165
 
        $mockAdapter = new MockTemplateAdapter();
166
 
        $gallery->_templateAdapter =& $mockAdapter;
167
 
 
168
 
        $ret = ImageBlockHelper::setDisabledFlag($this->_topAlbum, true, true);
169
 
        if ($ret) {
170
 
            return $this->failWithStatus($ret);
171
 
        }
172
 
 
173
 
        $itemIds = array();
174
 
        foreach (array_merge($this->_topItems, $this->_subItems) as $item) {
175
 
            $itemIds[] = (int)$item->getId();
176
 
        }
177
 
        $itemIds[] = (int)$this->_topAlbum->getId();
178
 
        $itemIds[] = (int)$this->_subAlbum->getId();
179
 
        sort($itemIds);
180
 
 
181
 
        $query = 'SELECT [ImageBlockDisabledMap::itemId] ' .
182
 
                 'FROM [ImageBlockDisabledMap] ' .
183
 
                 'WHERE [ImageBlockDisabledMap::itemId] IN (' .
184
 
                 GalleryUtilities::makeMarkers(sizeof($itemIds)) .
185
 
                 ')';
186
 
 
187
 
        list ($ret, $searchResults) = $gallery->search($query, $itemIds);
188
 
        if ($ret) {
189
 
            return $this->failWithStatus($ret);
190
 
        }
191
 
 
192
 
        $dbItemIds = array();
193
 
        while ($rec = $searchResults->nextResult()) {
194
 
            $dbItemIds[] = (int)$rec[0];
195
 
        }
196
 
        sort($dbItemIds);
197
 
        $this->assertEquals($itemIds, $dbItemIds, 'Items not added to the DisableMap');
198
 
 
199
 
        $ret = ImageBlockHelper::setDisabledFlag($this->_topAlbum, true, false);
200
 
        if ($ret) {
201
 
            return $this->failWithStatus($ret);
202
 
        }
203
 
 
204
 
        $query = 'SELECT [ImageBlockDisabledMap::itemId] ' .
205
 
                 'FROM [ImageBlockDisabledMap] ' .
206
 
                 'WHERE [ImageBlockDisabledMap::itemId] IN (' .
207
 
                 GalleryUtilities::makeMarkers(sizeof($itemIds)) .
208
 
                 ')';
209
 
 
210
 
        list ($ret, $searchResults) = $gallery->search($query, $itemIds);
211
 
        if ($ret) {
212
 
            return $this->failWithStatus($ret);
213
 
        }
214
 
 
215
 
        $dbItemIds = array();
216
 
        while ($rec = $searchResults->nextResult()) {
217
 
            $dbItemIds[] = (int)$rec[0];
218
 
        }
219
 
        $this->assertEquals(array(), $dbItemIds, 'Items not removed from the DisabledMap');
220
 
    }
221
 
 
222
 
    function testImageBlockHelperSetDisabledFlagFirstSubAlbumThenRecursive() {
223
 
        global $gallery;
224
 
 
225
 
        $ret = $this->_createHierarchy();
226
 
        if ($ret) {
227
 
            return $this->failWithStatus($ret);
228
 
        }
229
 
 
230
 
        /* Prepare the test by first disabling the subalbum */
231
 
        $ret = ImageBlockHelper::setDisabledFlag($this->_subAlbum, false, true);
232
 
        if ($ret) {
233
 
            return $this->failWithStatus($ret);
234
 
        }
235
 
 
236
 
        $itemIds = array();
237
 
        foreach ($this->_subItems as $item) {
238
 
            $itemIds[] = (int)$item->getId();
239
 
        }
240
 
        $itemIds[] = (int)$this->_subAlbum->getId();
241
 
 
242
 
        /* Verify the precondition of the test */
243
 
        list ($ret, $searchResults) = GalleryCoreApi::getMapEntry('ImageBlockDisabledMap',
244
 
                                                                  array('itemId'),
245
 
                                                                  array('itemId' => $itemIds));
246
 
        if ($ret) {
247
 
            return $this->failWithStatus($ret);
248
 
        }
249
 
 
250
 
        $dbItemIds = array();
251
 
        while ($rec = $searchResults->nextResult()) {
252
 
            $dbItemIds[] = (int)$rec[0];
253
 
        }
254
 
 
255
 
        sort($itemIds);
256
 
        sort($dbItemIds);
257
 
        $this->assertEquals($itemIds, $dbItemIds,
258
 
                            'Precondition: Items not added to the DisableMap');
259
 
 
260
 
        /* Execute the test: Ensure that we succeed and don't attempt to insert duplicate rows. */
261
 
        $ret = ImageBlockHelper::setDisabledFlag($this->_topAlbum, true, true);
262
 
        if ($ret) {
263
 
            return $this->failWithStatus($ret);
264
 
        }
265
 
 
266
 
        foreach ($this->_topItems as $item) {
267
 
            $itemIds[] = (int)$item->getId();
268
 
        }
269
 
        $itemIds[] = (int)$this->_topAlbum->getId();
270
 
 
271
 
        /* Verify the results */
272
 
        list ($ret, $searchResults) = GalleryCoreApi::getMapEntry('ImageBlockDisabledMap',
273
 
                                                                  array('itemId'),
274
 
                                                                  array('itemId' => $itemIds));
275
 
        if ($ret) {
276
 
            return $this->failWithStatus($ret);
277
 
        }
278
 
 
279
 
        $dbItemIds = array();
280
 
        while ($rec = $searchResults->nextResult()) {
281
 
            $dbItemIds[] = (int)$rec[0];
282
 
        }
283
 
 
284
 
        sort($dbItemIds);
285
 
        sort($itemIds);
286
 
        $this->assertEquals($itemIds, $dbItemIds, 'Not all items have been flagged as disabled');
287
 
    }
288
 
 
289
 
    /**
290
 
     * Verify the fix for bug 1536395
291
 
     * http://sourceforge.net/tracker/index.php?func=detail&aid=1536395&group_id=7130&atid=107130
292
 
     */
293
 
    function testMaxSizeWithoutViewSourcePermission() {
294
 
        global $gallery;
295
 
 
296
 
        list ($ret, $top) = $this->_createTopAlbum();
297
 
        if ($ret) {
298
 
            return $this->failWithStatus($ret);
299
 
        }
300
 
 
301
 
        list ($ret, $album) = $this->_createRandomAlbum($top->getId());
302
 
        if ($ret) {
303
 
            return $this->failWithStatus($ret);
304
 
        }
305
 
 
306
 
        list ($ret, $photo) = $this->_createRandomDataItem($album->getId());
307
 
        if ($ret) {
308
 
            return $this->failWithStatus($ret);
309
 
        }
310
 
 
311
 
        list ($ret, $thumbnail) = $this->_createDerivative(
312
 
            $album, $photo->getId(), DERIVATIVE_TYPE_IMAGE_THUMBNAIL);
313
 
        if ($ret) {
314
 
            return $this->failWithStatus($ret);
315
 
        }
316
 
 
317
 
        $ret = GalleryCoreApi::removeItemPermissions($photo->getId());
318
 
        if ($ret) {
319
 
            return $this->failWithStatus($ret);
320
 
        }
321
 
 
322
 
        $ret = GalleryCoreApi::addUserPermission(
323
 
            $photo->getId(), $gallery->getActiveUserId(), 'core.view');
324
 
        if ($ret) {
325
 
            return $this->failWithStatus($ret);
326
 
        }
327
 
 
328
 
        list ($ret, $results) = ImageBlockHelper::_getBlockData(
329
 
            'Album', 'recent', 1, $top->getId(), false, null, 200 /* maxSize */);
330
 
        if ($ret) {
331
 
            return $this->failWithStatus($ret);
332
 
        }
333
 
 
334
 
        $this->assertEquals($thumbnail->getId(), $results[0]['thumb']['id']);
335
 
    }
336
 
}
337
 
?>