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

« back to all changes in this revision

Viewing changes to modules/imageblock/test/phpunit/ImageBlockDisabledTest.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
 
GalleryCoreApi::requireOnce('modules/imageblock/Callbacks.inc');
22
 
 
23
 
/**
24
 
 * ImageBlockOption tests
25
 
 * @package ImageBlock
26
 
 * @subpackage PHPUnit
27
 
 * @author Jay Rossiter <cryptographite@users.sf.net>
28
 
 * @version $Revision: 15513 $
29
 
 */
30
 
class ImageBlockDisabledTest extends GalleryTestCase {
31
 
 
32
 
    function ImageBlockDisabledTest($methodName) {
33
 
        $this->GalleryTestCase($methodName);
34
 
    }
35
 
 
36
 
    /**
37
 
     * Create an album for testing
38
 
     */
39
 
    function setUp() {
40
 
        global $gallery;
41
 
        parent::setUp();
42
 
        /* Register a dummy toolkit and operation */
43
 
        $ret = GalleryCoreApi::registerFactoryImplementation(
44
 
            'GalleryToolkit', 'ImageBlockToolkit', 'ImageBlockToolkit',
45
 
            'modules/imageblock/test/phpunit/ImageBlockDisabledTest.class', 'imageblock', null);
46
 
        if ($ret) {
47
 
            print $ret->getAsHtml();
48
 
            return $this->failWithStatus($ret);
49
 
        }
50
 
        $this->_markToolkitForCleanup('ImageBlockToolkit');
51
 
 
52
 
        $ret = GalleryCoreApi::registerToolkitOperation(
53
 
            'ImageBlockToolkit', array('image/gif'), 'testoperation',
54
 
            array(array('type' => 'int', 'description' => 'size')), 'desc');
55
 
        if ($ret) {
56
 
            print $ret->getAsHtml();
57
 
            return $this->failWithStatus($ret);
58
 
        }
59
 
 
60
 
        list ($ret, $this->_module) = GalleryCoreApi::loadPlugin('module', 'imageblock');
61
 
        if ($ret || !isset($this->_module)) {
62
 
            print $ret->getAsHtml();
63
 
            return $this->failWithStatus($ret);
64
 
        }
65
 
 
66
 
        list ($ret, $this->_topAlbum, $this->_topItems) =
67
 
            $this->_createSubAlbumWithItems($this->_getRootId());
68
 
        if ($ret) {
69
 
            print $ret->getAsHtml();
70
 
            return $this->failWithStatus($ret);
71
 
        }
72
 
 
73
 
        $this->_markForCleanup($this->_topAlbum);
74
 
 
75
 
        list ($ret, $this->_subAlbum, $this->_subItems) =
76
 
            $this->_createSubAlbumWithItems($this->_topAlbum->getId());
77
 
        if ($ret) {
78
 
            print $ret->getAsHtml();
79
 
            return $this->failWithStatus($ret);
80
 
        }
81
 
 
82
 
        /* Set the ImageBlockDisabled flag */
83
 
        $ret = ImageBlockHelper::setDisabledFlag($this->_topAlbum, true, true);
84
 
        if ($ret) {
85
 
            print $ret->getAsHtml();
86
 
            return $this->failWithStatus($ret);
87
 
        }
88
 
 
89
 
        list ($ret, $this->_itemId) = $this->_module->getParameter('itemId');
90
 
        if ($ret) {
91
 
            print $ret->getAsHtml();
92
 
            return $this->failWithStatus($ret);
93
 
        }
94
 
 
95
 
        $ret = $this->_module->setParameter('itemId', $this->_topAlbum->getId());
96
 
        if ($ret) {
97
 
            print $ret->getAsHtml();
98
 
            return $this->failWithStatus($ret);
99
 
        }
100
 
    }
101
 
 
102
 
    /**
103
 
     * Delete the album that was used for testing
104
 
     */
105
 
    function tearDown() {
106
 
        $ret = GalleryCoreApi::removePluginParameter(
107
 
            $this->_module->getPluginType(), $this->_module->getId(), 'itemId');
108
 
        if ($ret) {
109
 
            print $ret->getAsHtml();
110
 
            return $this->failWithStatus($ret);
111
 
        }
112
 
 
113
 
        /* Unset the ImageBlockDisabled flag */
114
 
        $ret = ImageBlockHelper::setDisabledFlag($this->_topAlbum, true, false);
115
 
        if ($ret) {
116
 
            print $ret->getAsHtml();
117
 
            return $this->failWithStatus($ret);
118
 
        }
119
 
 
120
 
        parent::tearDown();
121
 
    }
122
 
 
123
 
    function _createSubAlbumWithItems($parentId) {
124
 
        /* Create a new album and image, set thumbnail and highlight */
125
 
        list ($ret, $album) = $this->_createRandomAlbum($parentId);
126
 
        if ($ret) {
127
 
            return array($ret, null, null);
128
 
        }
129
 
 
130
 
        list ($ret, $lockId) = GalleryCoreApi::acquireReadLock($album->getId());
131
 
        if ($ret) {
132
 
            return array($ret, null, null);
133
 
        }
134
 
 
135
 
        $imagePath = GalleryCoreApi::getPluginBaseDir('module', 'core')
136
 
            . 'modules/core/test/data/test1.gif';
137
 
        list ($ret, $photo, $thumbId) = $this->_addPhoto($album->getId(), $imagePath);
138
 
        if ($ret) {
139
 
            return array($ret, null, null);
140
 
        }
141
 
        $ret = GalleryCoreApi::releaseLocks($lockId);
142
 
        if ($ret) {
143
 
            return array($ret, null, null);
144
 
        }
145
 
        list ($ret, $tmpId) =
146
 
            $this->_addDerivative($album, $photo, DERIVATIVE_TYPE_IMAGE_THUMBNAIL, 150);
147
 
        if ($ret) {
148
 
            return array($ret, null, null);
149
 
        }
150
 
 
151
 
        $items = array();
152
 
        $items[] = $photo->getId();
153
 
        return array(null, $album, $items);
154
 
    }
155
 
 
156
 
    function _addPhoto($parentId, $imagePath) {
157
 
        list ($ret, $photo) =
158
 
            GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryPhotoItem');
159
 
        if ($ret) {
160
 
            return array($ret, null, null);
161
 
        }
162
 
        list ($ret, $mimeType) = GalleryCoreApi::getMimeType($imagePath);
163
 
        if ($ret) {
164
 
            return array($ret, null, null);
165
 
        }
166
 
        $ret = $photo->create($parentId, $imagePath, $mimeType,
167
 
                              'testitem_' . time() . substr($imagePath, -4));
168
 
        if ($ret) {
169
 
            return array($ret, null, null);
170
 
        }
171
 
        $photo->setTitle('Test Item');
172
 
        $ret = $photo->save();
173
 
        if ($ret) {
174
 
            return array($ret, null, null);
175
 
        }
176
 
        /* add thumbnail */
177
 
        list ($ret, $thumbId) = $this->_addDerivative($photo, $photo,
178
 
            DERIVATIVE_TYPE_IMAGE_THUMBNAIL, 150);
179
 
        if ($ret) {
180
 
            return array($ret, null, null);
181
 
        }
182
 
        return array(null, $photo, $thumbId);
183
 
    }
184
 
 
185
 
    function _addDerivative(&$item, &$src, $derivativeType, $size) {
186
 
        list ($ret, $derivative) =
187
 
            GalleryCoreApi::newFactoryInstanceByHint('GalleryDerivative', $item->getEntityType());
188
 
        if ($ret) {
189
 
            return array($ret, null);
190
 
        }
191
 
        if (!isset($derivative)) {
192
 
            return array(GalleryCoreApi::error(ERROR_MISSING_OBJECT), null);
193
 
        }
194
 
        $ret = $derivative->create($item->getId(), $derivativeType);
195
 
        if ($ret) {
196
 
            return array($ret, null);
197
 
        }
198
 
        $derivative->setDerivativeSourceId($src->getId());
199
 
        $derivative->setMimeType($src->getMimeType());
200
 
        $derivative->setDerivativeOperations('testoperation|0');
201
 
        if ($size) {
202
 
            /* Set scaled size: */
203
 
            $w = $src->getWidth();
204
 
            $h = $src->getHeight();
205
 
            if ($w <= $size && $h <= $size && $derivativeType == DERIVATIVE_TYPE_IMAGE_THUMBNAIL) {
206
 
                $derivative->setWidth($w);
207
 
                $derivative->setHeight($h);
208
 
            } else if ($w >= $h) {
209
 
                $derivative->setWidth($size);
210
 
                $derivative->setHeight((int)($size*$h/$w));
211
 
            } else {
212
 
                $derivative->setHeight($size);
213
 
                $derivative->setWidth((int)($size*$w/$h));
214
 
            }
215
 
        } else {
216
 
            /* Set rotated size (swap wd/ht): */
217
 
            $derivative->setWidth($src->getHeight());
218
 
            $derivative->setHeight($src->getWidth());
219
 
        }
220
 
        $ret = $derivative->save();
221
 
        if ($ret) {
222
 
            return array($ret, null);
223
 
        }
224
 
        return array(null, $derivative->getId());
225
 
    }
226
 
 
227
 
    function assertBlock($block) {
228
 
        $this->assert(isset($block['id']), 'Missing id');
229
 
        $this->assert(isset($block['viewCount']), 'Missing viewCount');
230
 
        $this->assert(isset($block['title']), 'Missing title');
231
 
        $this->assert(isset($block['item']), 'Missing item');
232
 
        $this->assert(isset($block['thumb']), 'Missing thumb');
233
 
    }
234
 
 
235
 
    function testImageBlockDisabledNoItemsReturned() {
236
 
        $smarty = new GallerySmarty();
237
 
        $callbacks = new ImageBlockCallbacks();
238
 
        $ret = $callbacks->callback(array('useDefaults' => true), $smarty, 'LoadImageBlock');
239
 
        if ($ret) {
240
 
            return $this->failWithStatus($ret);
241
 
        }
242
 
        $data = $smarty->_tpl_vars;
243
 
        $this->assert(!isset($data['ImageBlockData']['blocks']), 'Block should not exist');
244
 
    }
245
 
 
246
 
    function testImageBlockDisabledOneItemReturned() {
247
 
        /* Unset the ImageBlockDisabled flag on the subAlbum */
248
 
        $ret = ImageBlockHelper::setDisabledFlag($this->_subAlbum, true, false);
249
 
        if ($ret) {
250
 
            return $this->failWithStatus($ret);
251
 
        }
252
 
 
253
 
        $smarty = new GallerySmarty();
254
 
        $callbacks = new ImageBlockCallbacks();
255
 
        $ret = $callbacks->callback(array('useDefaults' => true), $smarty, 'LoadImageBlock');
256
 
        if ($ret) {
257
 
            return $this->failWithStatus($ret);
258
 
        }
259
 
        $data = $smarty->_tpl_vars;
260
 
 
261
 
        $this->assert(1 == count($data['ImageBlockData']['blocks']), 'One block');
262
 
        $this->assertBlock($data['ImageBlockData']['blocks'][0]);
263
 
    }
264
 
}
265
 
?>