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

« back to all changes in this revision

Viewing changes to modules/linkitem/test/phpunit/LinkItemTest.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
 
 * Additional LinkItem tests
23
 
 * @package LinkItem
24
 
 * @subpackage PHPUnit
25
 
 * @author Alan Harder <alan.harder@sun.com>
26
 
 * @version $Revision: 15513 $
27
 
 */
28
 
class LinkItemTest extends GalleryTestCase {
29
 
 
30
 
    function setUp() {
31
 
        parent::setUp();
32
 
 
33
 
        list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
34
 
        if ($ret) {
35
 
            print $ret->getAsHtml();
36
 
            return $this->failWithStatus($ret);
37
 
        }
38
 
        $this->_markForCleanup($this->_album);
39
 
 
40
 
        list ($ret, $this->_targetAlbum) = $this->_createRandomAlbum($this->_getRootId());
41
 
        if ($ret) {
42
 
            print $ret->getAsHtml();
43
 
            return $this->failWithStatus($ret);
44
 
        }
45
 
 
46
 
        list ($ret, $this->_lockId) = GalleryCoreApi::acquireReadLock(
47
 
            array($this->_album->getId(), $this->_targetAlbum->getId()));
48
 
        if ($ret) {
49
 
            print $ret->getAsHtml();
50
 
            return $this->failWithStatus($ret);
51
 
        }
52
 
 
53
 
        list ($ret, $this->_link) =
54
 
            $this->_createLinkItem($this->_album->getId(), 'http://test.com/test.html');
55
 
        if ($ret) {
56
 
            print $ret->getAsHtml();
57
 
            return $this->failWithStatus($ret);
58
 
        }
59
 
    }
60
 
 
61
 
    function _createLinkItem($parentId, $target) {
62
 
        list ($ret, $link) =
63
 
            GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryLinkItem');
64
 
        if ($ret) {
65
 
            return array($ret, null);
66
 
        }
67
 
        if (empty($link)) {
68
 
            return array(GalleryCoreApi::error(ERROR_MISSING_OBJECT), null);
69
 
        }
70
 
        $ret = $link->create($parentId, $target);
71
 
        if ($ret) {
72
 
            return array($ret, null);
73
 
        }
74
 
        $ret = $link->save();
75
 
        if ($ret) {
76
 
            return array($ret, null);
77
 
        }
78
 
        return array(null, $link);
79
 
    }
80
 
 
81
 
    function tearDown() {
82
 
        if (isset($this->_lockId)) {
83
 
            $ret = GalleryCoreApi::releaseLocks($this->_lockId);
84
 
            if ($ret) {
85
 
                $this->failWithStatus($ret);
86
 
            }
87
 
        }
88
 
 
89
 
        parent::tearDown();
90
 
    }
91
 
 
92
 
    function testMoveLink() {
93
 
        /* We don't really have anything on the filesystem; make sure move() works ok */
94
 
        $this->_markForCleanup($this->_targetAlbum);
95
 
 
96
 
        $ret = $this->_link->move($this->_targetAlbum->getId());
97
 
        if ($ret) {
98
 
            return $this->failWithStatus($ret);
99
 
        }
100
 
        list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($this->_link->getId());
101
 
        if ($ret) {
102
 
            return $this->failWithStatus($ret);
103
 
        }
104
 
 
105
 
        $this->assertEquals($this->_targetAlbum->getId(), $this->_link->getParentId(), 'parent');
106
 
        $this->assertEquals(array($this->_getRootId(), $this->_targetAlbum->getId()),
107
 
                            $parentSequence, 'parent sequence');
108
 
    }
109
 
 
110
 
    function testDeleteAlbum() {
111
 
        /* Verify delete of an album also deletes links that point to it */
112
 
        list ($ret, $link) =
113
 
            $this->_createLinkItem($this->_album->getId(), $this->_targetAlbum->getId());
114
 
        if ($ret) {
115
 
            $this->failWithStatus($ret);
116
 
        }
117
 
        $ret = GalleryCoreApi::releaseLocks($this->_lockId);
118
 
        if ($ret) {
119
 
            $this->failWithStatus($ret);
120
 
        }
121
 
        unset($this->_lockId);
122
 
 
123
 
        $ret = GalleryCoreApi::deleteEntityById($this->_targetAlbum->getId());
124
 
        if ($ret) {
125
 
            return $this->failWithStatus($ret);
126
 
        }
127
 
 
128
 
        list ($ret, $gone) = $link->refresh();
129
 
        $this->assert(!isset($gone), 'link should be gone');
130
 
        if ($ret) {
131
 
            $this->assertEquals(GALLERY_ERROR | ERROR_MISSING_OBJECT,
132
 
                                $ret->getErrorCode(), 'expected error code');
133
 
        } else {
134
 
            $this->assert(false, 'expected error');
135
 
        }
136
 
    }
137
 
 
138
 
    function testDeleteSubalbum() {
139
 
        /* Verify delete of an album also deletes links that point to its subalbums */
140
 
        list ($ret, $subalbum) = $this->_createRandomAlbum($this->_targetAlbum->getId());
141
 
        if ($ret) {
142
 
            return $this->failWithStatus($ret);
143
 
        }
144
 
        list ($ret, $link) =
145
 
            $this->_createLinkItem($this->_album->getId(), $subalbum->getId());
146
 
        if ($ret) {
147
 
            $this->failWithStatus($ret);
148
 
        }
149
 
        $ret = GalleryCoreApi::releaseLocks($this->_lockId);
150
 
        if ($ret) {
151
 
            $this->failWithStatus($ret);
152
 
        }
153
 
        unset($this->_lockId);
154
 
 
155
 
        $ret = GalleryCoreApi::deleteEntityById($this->_targetAlbum->getId());
156
 
        if ($ret) {
157
 
            return $this->failWithStatus($ret);
158
 
        }
159
 
 
160
 
        list ($ret, $gone) = $link->refresh();
161
 
        $this->assert(!isset($gone), 'link should be gone');
162
 
        if ($ret) {
163
 
            $this->assertEquals(GALLERY_ERROR | ERROR_MISSING_OBJECT,
164
 
                                $ret->getErrorCode(), 'expected error code');
165
 
        } else {
166
 
            $this->assert(false, 'expected error');
167
 
        }
168
 
    }
169
 
 
170
 
    function testShowLinkItemNoPersistentSession() {
171
 
        global $gallery;
172
 
        $backup = $gallery->_session;
173
 
        /* Make sure the redirect has no sessionId in it */
174
 
        GalleryUtilities::removeRequestVariable(SESSION_ID_PARAMETER);
175
 
        unset($_COOKIE[SESSION_ID_PARAMETER]);
176
 
        unset($_SERVER['HTTP_COOKIE']);
177
 
        $session = new GallerySession();
178
 
        $ret = $session->init();
179
 
        if ($ret) {
180
 
            return $this->failWithStatus($ret);
181
 
        }
182
 
        $this->assertEquals(false, $session->isUsingCookies(), 'isUsingCookies');
183
 
        $this->assertEquals(SESSION_TEMP_ID, $session->getId(), 'temp session id');
184
 
 
185
 
        $gallery->_phpVm = $phpVm = new LinkItemTestMockPhpVm();
186
 
        $_REQUEST['phpVmHeaders'] = array();
187
 
        $_REQUEST['phpVmExitCalls'] = 0;
188
 
 
189
 
        $this->_link->setLink($this->_targetAlbum->getId());
190
 
        $this->_markForCleanup($this->_targetAlbum);
191
 
        $this->_lockId = array($this->_lockId);
192
 
        list ($ret, $this->_lockId[]) =
193
 
            GalleryCoreApi::acquireWriteLock(array($this->_link->getId()));
194
 
        if ($ret) {
195
 
            return $this->failWithStatus($ret);
196
 
        }
197
 
        $ret = $this->_link->save();
198
 
        if ($ret) {
199
 
            return $this->failWithStatus($ret);
200
 
        }
201
 
        $gallery->_session = $session;
202
 
        /* Make sure it passes the onLoad conditions */
203
 
        GalleryUtilities::putRequestVariable('view', 'core.ShowItem');
204
 
        GalleryUtilities::putRequestVariable('itemId', $this->_link->getId());
205
 
        /* Make sure onLoad is called */
206
 
        GalleryDataCache::setMemoryCachingEnabled(false);
207
 
        /* Execute the test */
208
 
        list ($ret, $link) = GalleryCoreApi::loadEntitiesById($this->_link->getId());
209
 
        if ($ret) {
210
 
            $gallery->_session = $backup;
211
 
            return $this->failWithStatus($ret);
212
 
        }
213
 
        GalleryDataCache::setMemoryCachingEnabled(true);
214
 
        /* Verify results */
215
 
        $this->assertEquals(1, $_REQUEST['phpVmExitCalls'], 'nr of exit calls');
216
 
        $urlGenerator =& $gallery->getUrlGenerator();
217
 
        $baseUrl = $urlGenerator->getCurrentUrlDir();
218
 
        if (!empty($_REQUEST['phpVmHeaders']['Location'])) {
219
 
            $this->assertEquals($urlGenerator->getCurrentUrlDir() . 'main.php?g2_itemId=' .
220
 
                                  $this->_targetAlbum->getId(),
221
 
                                trim($_REQUEST['phpVmHeaders']['Location']),
222
 
                                'redirect URL');
223
 
        } else {
224
 
            $this->assert(false, 'no Location header');
225
 
        }
226
 
 
227
 
        $gallery->_session = $backup;
228
 
    }
229
 
 
230
 
    function testToolkit() {
231
 
        global $gallery;
232
 
        $this->_markForCleanup($this->_targetAlbum);
233
 
 
234
 
        list ($ret, $toolkit, $mimeType) =
235
 
            GalleryCoreApi::getToolkitByOperation('gallery/linkitem', 'convert-to-image/jpeg');
236
 
        if ($ret) {
237
 
            return $this->failWithStatus($ret);
238
 
        }
239
 
        $this->assertEquals('image/jpeg', $mimeType);
240
 
        if (!GalleryUtilities::isExactlyA($toolkit, 'LinkItemToolkit')) {
241
 
            return $this->assert(false, 'LinkItemToolkit');
242
 
        }
243
 
 
244
 
        $ret = $this->_markPluginParametersForCleanup('module', 'linkitem');
245
 
        if ($ret) {
246
 
            return $this->failWithStatus($ret);
247
 
        }
248
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'linkitem', 'urlSnapshot', '0');
249
 
        if ($ret) {
250
 
            return $this->failWithStatus($ret);
251
 
        }
252
 
 
253
 
        $savePlatform =& $gallery->getPlatform();
254
 
        $platform = new UnitTestPlatform();
255
 
        $platform->setReply('copy', array('fuzzy' => '{/images/link.jpg$}', 'outputFile'), true);
256
 
        $gallery->setPlatform($platform);
257
 
 
258
 
        list ($ret, $mimeType, $context) = $toolkit->performOperation('gallery/linkitem',
259
 
                'convert-to-image/jpeg', 'theURL', 'outputFile', array(), array());
260
 
        if ($ret) {
261
 
            return $this->failWithStatus($ret);
262
 
        }
263
 
 
264
 
        $this->assertEquals('image/jpeg', $mimeType);
265
 
        $this->assertEquals(array('width' => 270, 'height' => 224), $context);
266
 
        $this->assert($platform->isComplete(), $platform->getRemaining());
267
 
 
268
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'linkitem', 'urlSnapshot', '1');
269
 
        if ($ret) {
270
 
            return $this->failWithStatus($ret);
271
 
        }
272
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'linkitem',
273
 
                                                  'snapshotExec', 'getSnapshot');
274
 
        if ($ret) {
275
 
            return $this->failWithStatus($ret);
276
 
        }
277
 
 
278
 
        $platform = new UnitTestPlatform();
279
 
        $platform->setReply('tempnam', array('fuzzy' => '{.}', 'lnk_'), 'tmpfile');
280
 
        $platform->setReply('getDirectorySeparator', array(), '/');
281
 
        $platform->setReply('file_exists', array('fuzzy' => '{params.txt$}'), true);
282
 
        $platform->setReply('file', array('fuzzy' => '{params.txt$}', false),
283
 
                            array('%URL% %WIDTH% %HEIGHT% --out %OUTFILE%' . "\n"));
284
 
        $platform->setReply('exec',
285
 
                array(array(array('getSnapshot', 'theURL', 1024, 768, '--out', 'tmpfile.jpg'))),
286
 
                array(true, '', ''));
287
 
        $platform->setReply('unlink', array('tmpfile'), true);
288
 
        $platform->setReply('copy', array('tmpfile.jpg', 'outputFile'), true);
289
 
        $platform->setReply('unlink', array('tmpfile.jpg'), true);
290
 
        $gallery->setPlatform($platform);
291
 
 
292
 
        list ($ret, $mimeType, $context) = $toolkit->performOperation('gallery/linkitem',
293
 
                'convert-to-image/jpeg', 'theURL', 'outputFile', array(), array());
294
 
        if ($ret) {
295
 
            return $this->failWithStatus($ret);
296
 
        }
297
 
 
298
 
        $this->assertEquals('image/jpeg', $mimeType);
299
 
        $this->assertEquals(array('width' => 1024, 'height' => 768), $context);
300
 
        $this->assert($platform->isComplete(), $platform->getRemaining());
301
 
        $gallery->setPlatform($savePlatform);
302
 
    }
303
 
}
304
 
 
305
 
class LinkItemTestMockPhpVm extends GalleryPhpVm {
306
 
    function header($string, $replace=null) {
307
 
        if ($pos = strpos($string, ':')) {
308
 
            $_REQUEST['phpVmHeaders'][substr($string, 0, $pos)] = substr($string, $pos+1);
309
 
        }
310
 
    }
311
 
 
312
 
    function exit_($status=null) {
313
 
        $_REQUEST['phpVmExitCalls']++;
314
 
    }
315
 
}
316
 
?>