3
* Gallery - a web based photo album viewer and editor
4
* Copyright (C) 2000-2007 Bharat Mediratta
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.
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.
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.
22
* Additional LinkItem tests
25
* @author Alan Harder <alan.harder@sun.com>
26
* @version $Revision: 15513 $
28
class LinkItemTest extends GalleryTestCase {
33
list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
35
print $ret->getAsHtml();
36
return $this->failWithStatus($ret);
38
$this->_markForCleanup($this->_album);
40
list ($ret, $this->_targetAlbum) = $this->_createRandomAlbum($this->_getRootId());
42
print $ret->getAsHtml();
43
return $this->failWithStatus($ret);
46
list ($ret, $this->_lockId) = GalleryCoreApi::acquireReadLock(
47
array($this->_album->getId(), $this->_targetAlbum->getId()));
49
print $ret->getAsHtml();
50
return $this->failWithStatus($ret);
53
list ($ret, $this->_link) =
54
$this->_createLinkItem($this->_album->getId(), 'http://test.com/test.html');
56
print $ret->getAsHtml();
57
return $this->failWithStatus($ret);
61
function _createLinkItem($parentId, $target) {
63
GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryLinkItem');
65
return array($ret, null);
68
return array(GalleryCoreApi::error(ERROR_MISSING_OBJECT), null);
70
$ret = $link->create($parentId, $target);
72
return array($ret, null);
76
return array($ret, null);
78
return array(null, $link);
82
if (isset($this->_lockId)) {
83
$ret = GalleryCoreApi::releaseLocks($this->_lockId);
85
$this->failWithStatus($ret);
92
function testMoveLink() {
93
/* We don't really have anything on the filesystem; make sure move() works ok */
94
$this->_markForCleanup($this->_targetAlbum);
96
$ret = $this->_link->move($this->_targetAlbum->getId());
98
return $this->failWithStatus($ret);
100
list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($this->_link->getId());
102
return $this->failWithStatus($ret);
105
$this->assertEquals($this->_targetAlbum->getId(), $this->_link->getParentId(), 'parent');
106
$this->assertEquals(array($this->_getRootId(), $this->_targetAlbum->getId()),
107
$parentSequence, 'parent sequence');
110
function testDeleteAlbum() {
111
/* Verify delete of an album also deletes links that point to it */
113
$this->_createLinkItem($this->_album->getId(), $this->_targetAlbum->getId());
115
$this->failWithStatus($ret);
117
$ret = GalleryCoreApi::releaseLocks($this->_lockId);
119
$this->failWithStatus($ret);
121
unset($this->_lockId);
123
$ret = GalleryCoreApi::deleteEntityById($this->_targetAlbum->getId());
125
return $this->failWithStatus($ret);
128
list ($ret, $gone) = $link->refresh();
129
$this->assert(!isset($gone), 'link should be gone');
131
$this->assertEquals(GALLERY_ERROR | ERROR_MISSING_OBJECT,
132
$ret->getErrorCode(), 'expected error code');
134
$this->assert(false, 'expected error');
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());
142
return $this->failWithStatus($ret);
145
$this->_createLinkItem($this->_album->getId(), $subalbum->getId());
147
$this->failWithStatus($ret);
149
$ret = GalleryCoreApi::releaseLocks($this->_lockId);
151
$this->failWithStatus($ret);
153
unset($this->_lockId);
155
$ret = GalleryCoreApi::deleteEntityById($this->_targetAlbum->getId());
157
return $this->failWithStatus($ret);
160
list ($ret, $gone) = $link->refresh();
161
$this->assert(!isset($gone), 'link should be gone');
163
$this->assertEquals(GALLERY_ERROR | ERROR_MISSING_OBJECT,
164
$ret->getErrorCode(), 'expected error code');
166
$this->assert(false, 'expected error');
170
function testShowLinkItemNoPersistentSession() {
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();
180
return $this->failWithStatus($ret);
182
$this->assertEquals(false, $session->isUsingCookies(), 'isUsingCookies');
183
$this->assertEquals(SESSION_TEMP_ID, $session->getId(), 'temp session id');
185
$gallery->_phpVm = $phpVm = new LinkItemTestMockPhpVm();
186
$_REQUEST['phpVmHeaders'] = array();
187
$_REQUEST['phpVmExitCalls'] = 0;
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()));
195
return $this->failWithStatus($ret);
197
$ret = $this->_link->save();
199
return $this->failWithStatus($ret);
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());
210
$gallery->_session = $backup;
211
return $this->failWithStatus($ret);
213
GalleryDataCache::setMemoryCachingEnabled(true);
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']),
224
$this->assert(false, 'no Location header');
227
$gallery->_session = $backup;
230
function testToolkit() {
232
$this->_markForCleanup($this->_targetAlbum);
234
list ($ret, $toolkit, $mimeType) =
235
GalleryCoreApi::getToolkitByOperation('gallery/linkitem', 'convert-to-image/jpeg');
237
return $this->failWithStatus($ret);
239
$this->assertEquals('image/jpeg', $mimeType);
240
if (!GalleryUtilities::isExactlyA($toolkit, 'LinkItemToolkit')) {
241
return $this->assert(false, 'LinkItemToolkit');
244
$ret = $this->_markPluginParametersForCleanup('module', 'linkitem');
246
return $this->failWithStatus($ret);
248
$ret = GalleryCoreApi::setPluginParameter('module', 'linkitem', 'urlSnapshot', '0');
250
return $this->failWithStatus($ret);
253
$savePlatform =& $gallery->getPlatform();
254
$platform = new UnitTestPlatform();
255
$platform->setReply('copy', array('fuzzy' => '{/images/link.jpg$}', 'outputFile'), true);
256
$gallery->setPlatform($platform);
258
list ($ret, $mimeType, $context) = $toolkit->performOperation('gallery/linkitem',
259
'convert-to-image/jpeg', 'theURL', 'outputFile', array(), array());
261
return $this->failWithStatus($ret);
264
$this->assertEquals('image/jpeg', $mimeType);
265
$this->assertEquals(array('width' => 270, 'height' => 224), $context);
266
$this->assert($platform->isComplete(), $platform->getRemaining());
268
$ret = GalleryCoreApi::setPluginParameter('module', 'linkitem', 'urlSnapshot', '1');
270
return $this->failWithStatus($ret);
272
$ret = GalleryCoreApi::setPluginParameter('module', 'linkitem',
273
'snapshotExec', 'getSnapshot');
275
return $this->failWithStatus($ret);
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);
292
list ($ret, $mimeType, $context) = $toolkit->performOperation('gallery/linkitem',
293
'convert-to-image/jpeg', 'theURL', 'outputFile', array(), array());
295
return $this->failWithStatus($ret);
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);
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);
312
function exit_($status=null) {
313
$_REQUEST['phpVmExitCalls']++;