3
* $RCSfile: GalleryRemoteControllerTest.class,v $
5
* Gallery - a web based photo album viewer and editor
6
* Copyright (C) 2000-2005 Bharat Mediratta
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or (at
11
* your option) any later version.
13
* This program is distributed in the hope that it will be useful, but
14
* WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
23
* @version $Revision: 1.21 $ $Date: 2005/08/26 05:15:13 $
24
* @package GalleryRemote
26
* @author Bharat Mediratta <bharat@menalto.com>
30
* GalleryRemote controller tests
32
* @package GalleryRemote
35
class GalleryRemoteControllerTest extends GalleryControllerTestCase {
37
function GalleryRemoteControllerTest($methodName) {
38
$this->GalleryControllerTestCase($methodName, 'remote.GalleryRemote');
46
/* Create a new user */
47
list ($ret, $this->_user) = $this->_createRandomUser();
48
if ($ret->isError()) {
49
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
52
$this->_markForCleanup($this->_user);
54
list ($ret, $this->_rootAlbum) = $this->_createRandomAlbum($this->_getRootId());
55
if ($ret->isError()) {
56
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
59
$this->_markForCleanup($this->_rootAlbum);
61
$this->_saveUser = $gallery->getActiveUser();
62
$this->_code = GalleryRemoteConstants::getStatusCodes();
63
$this->_version = GalleryRemoteConstants::getVersionCodes();
66
function testLogin() {
69
GalleryUtilities::putRequestVariable('form[cmd]', 'login');
70
GalleryUtilities::putRequestVariable('form[uname]', $this->_user->getUserName());
71
GalleryUtilities::putRequestVariable('form[password]', 'password');
73
$results = $this->handleRequest();
74
$this->assertEquals(array(), $results['error']);
75
$this->assertEquals('remote.GalleryRemote', $results['delegate']['view']);
77
$response = $results['status']['controllerResponse'];
78
$this->assertEquals($this->_code['SUCCESS'], (int)$response->getProperty('status'));
79
$this->assertEquals(sprintf('%d.%d', $this->_version['MAJ'], $this->_version['MIN']),
80
$response->getProperty('server_version'));
81
$this->assert($response->hasProperty('status_text'));
83
$this->assertEquals($this->_user->getId(), $gallery->getActiveUserId());
86
function testLoginInvalidUser() {
89
GalleryUtilities::putRequestVariable('form[cmd]', 'login');
90
GalleryUtilities::putRequestVariable('form[uname]', 'some-bogus-user');
91
GalleryUtilities::putRequestVariable('form[password]', 'password');
93
$results = $this->handleRequest();
94
$this->assertEquals(array(), $results['error']);
95
$this->assertEquals('remote.GalleryRemote', $results['delegate']['view']);
97
$response = $results['status']['controllerResponse'];
98
$this->assertEquals($this->_code['PASSWORD_WRONG'], (int)$response->getProperty('status'));
99
$this->assert($response->hasProperty('status_text'));
101
$this->assertEquals($this->_saveUser->getId(), $gallery->getActiveUserId());
104
function testLoginValidUserInvalidPassword() {
107
GalleryUtilities::putRequestVariable('form[cmd]', 'login');
108
GalleryUtilities::putRequestVariable('form[uname]', $this->_user->getUserName());
109
GalleryUtilities::putRequestVariable('form[password]', 'wrong password');
111
$results = $this->handleRequest();
112
$this->assertEquals(array(), $results['error']);
113
$this->assertEquals('remote.GalleryRemote', $results['delegate']['view']);
115
$response = $results['status']['controllerResponse'];
116
$this->assertEquals($this->_code['PASSWORD_WRONG'], (int)$response->getProperty('status'));
117
$this->assert($response->hasProperty('status_text'));
119
$this->assertEquals($this->_saveUser->getId(), $gallery->getActiveUserId());
122
function testLoginGuestUser() {
125
list ($ret, $anonymousUserId) =
126
GalleryCoreApi::getPluginParameter('module', 'core', 'id.anonymousUser');
127
if ($ret->isError()) {
128
$this->failWithStatus($ret->wrap(__FILE__, __LINE__));
132
* Logging in as anonymous works when no existing session exists.
133
* Simulate this by dropping the activeUser.
135
$gallery->_activeUser = null;
137
GalleryUtilities::putRequestVariable('form[cmd]', 'login');
139
$results = $this->handleRequest();
140
$this->assertEquals(array(), $results['error']);
141
$this->assertEquals('remote.GalleryRemote', $results['delegate']['view']);
143
$response = $results['status']['controllerResponse'];
144
$this->assertEquals($this->_code['SUCCESS'], (int)$response->getProperty('status'));
145
$this->assertEquals(sprintf('%d.%d', $this->_version['MAJ'], $this->_version['MIN']),
146
$response->getProperty('server_version'));
147
$this->assert($response->hasProperty('status_text'));
149
$this->assertEquals((int)$anonymousUserId, $gallery->getActiveUserId());
152
function testLoginCurrentUser() {
155
GalleryUtilities::putRequestVariable('form[cmd]', 'login');
157
$results = $this->handleRequest();
158
$this->assertEquals(array(), $results['error']);
159
$this->assertEquals('remote.GalleryRemote', $results['delegate']['view']);
161
$response = $results['status']['controllerResponse'];
162
$this->assertEquals($this->_code['LOGIN_MISSING'], (int)$response->getProperty('status'));
163
$this->assertEquals(sprintf('%d.%d', $this->_version['MAJ'], $this->_version['MIN']),
164
$response->getProperty('server_version'));
165
$this->assert($response->hasProperty('status_text'));
167
$user = $gallery->getActiveUser();
168
$this->assertEquals($response->getProperty('debug_user'), $user->getuserName());
171
function testFetchAlbums() {
174
/* Create some albums */
175
$parentId = $this->_rootAlbum->getId();
176
for ($i = 0; $i < 2; $i++) {
177
list ($ret, $album[$i]) = $this->_createRandomAlbum($parentId);
178
if ($ret->isError()) {
179
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
181
$parentId = $album[$i]->getId();
184
/* give the 2nd album only view permissions */
185
$ret = GalleryCoreApi::removeItemPermissions($album[1]->getId());
186
if ($ret->isError()) {
187
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
189
$ret = GalleryCoreApi::addUserPermission(
190
$album[1]->getId(), $gallery->getActiveUserId(), 'core.view');
191
if ($ret->isError()) {
192
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
195
GalleryUtilities::putRequestVariable('form[cmd]', 'fetch-albums-prune');
196
$results = $this->handleRequest();
197
$this->assertEquals(array(), $results['error']);
198
$this->assertEquals('remote.GalleryRemote', $results['delegate']['view']);
200
$response = $results['status']['controllerResponse'];
201
$this->assertEquals($this->_code['SUCCESS'], (int)$response->getProperty('status'));
203
/* Make sure the albums we created are in the list -- Ignore the rest of the results */
204
$this->assert($response->getProperty('album_count') >= 4);
205
$this->assert($response->hasProperty('status_text'));
208
for ($i = 1; $i <= $response->getProperty('album_count'); $i++) {
209
for ($j = 0; $j < sizeof($album); $j++) {
210
if ($response->getProperty('album.name.' . $i) == $album[$j]->getId()) {
213
$album[$j]->getTitle(), $response->getProperty('album.title.' . $i));
215
$album[$j]->getSummary(), $response->getProperty('album.summary.' . $i));
216
$this->assertEquals($album[$j]->getParentId(),
217
(int)$response->getProperty('album.parent.' . $i));
219
$expected = $j == 0 ? 'true' : 'false';
220
$this->assertEquals($expected, $response->getProperty('album.perms.add.' . $i));
222
$expected, $response->getProperty('album.perms.write.' . $i));
224
$expected, $response->getProperty('album.perms.del_alb.' . $i));
226
$expected, $response->getProperty('album.perms.create_sub.' . $i));
230
$this->assert($seen[0]);
231
$this->assert($seen[1]);
234
function testAddItem() {
237
$platform = $gallery->getPlatform();
238
GalleryUtilities::putRequestVariable('form[cmd]', 'add-item');
239
GalleryUtilities::putRequestVariable('form[set_albumName]', $this->_rootAlbum->getId());
240
GalleryUtilities::putRequestVariable('form[caption]', 'this is a caption');
242
$this->_controller->setOptionInstances(array());
244
$_FILES[GalleryUtilities::prefixFormVariable('userfile')] =
245
array('name' => 'testfile.txt',
246
'type' => 'text/plain',
247
'tmp_name' => __FILE__,
249
'size' => $platform->filesize(__FILE__));
251
$results = $this->handleRequest();
252
$this->assertEquals(array(), $results['error']);
253
$this->assertEquals('remote.GalleryRemote', $results['delegate']['view']);
255
$response = $results['status']['controllerResponse'];
256
$this->assertEquals($this->_code['SUCCESS'], (int)$response->getProperty('status'));
258
/* Make sure the item was added */
259
list ($ret, $childIds) = GalleryCoreApi::fetchChildItemIds($this->_rootAlbum);
260
if ($ret->isError()) {
261
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
264
$this->assertEquals(1, sizeof($childIds));
266
list ($ret, $newItem) = GalleryCoreApi::loadEntitiesById($childIds[0]);
267
if ($ret->isError()) {
268
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
271
$this->assertEquals('testfile.txt', $newItem->getPathComponent());
272
$this->assertEquals('this is a caption', $newItem->getTitle());
273
$this->assertEquals($platform->filesize(__FILE__), $newItem->getSize());
276
function testAddItemWithOption() {
279
$platform = $gallery->getPlatform();
280
GalleryUtilities::putRequestVariable('form[cmd]', 'add-item');
281
GalleryUtilities::putRequestVariable('form[set_albumName]', $this->_rootAlbum->getId());
282
GalleryUtilities::putRequestVariable('form[caption]', 'this is a caption');
283
GalleryUtilities::putRequestVariable('form[extrafield.Summary]', 'this is a summary');
284
GalleryUtilities::putRequestVariable('form[extrafield.Description]', 'this is a description');
285
GalleryUtilities::putRequestVariable('form[caption]', 'this is a caption');
287
$this->_controller->setOptionInstances(
288
array('instance' => new GalleryRemoteControllerTestAddOption()));
290
$_FILES[GalleryUtilities::prefixFormVariable('userfile')] =
291
array('name' => 'testfile.txt',
292
'type' => 'text/plain',
293
'tmp_name' => __FILE__,
295
'size' => $platform->filesize(__FILE__));
297
$results = $this->handleRequest();
298
$this->assertEquals(array(), $results['error']);
299
$this->assertEquals('remote.GalleryRemote', $results['delegate']['view']);
301
$response = $results['status']['controllerResponse'];
302
$this->assertEquals($this->_code['SUCCESS'], (int)$response->getProperty('status'));
304
/* Make sure the item was added */
305
list ($ret, $childIds) = GalleryCoreApi::fetchChildItemIds($this->_rootAlbum);
306
if ($ret->isError()) {
307
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
310
$this->assertEquals(1, sizeof($childIds));
312
list ($ret, $newItem) = GalleryCoreApi::loadEntitiesById($childIds[0]);
313
if ($ret->isError()) {
314
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
317
/* Make sure our option was called */
318
$this->assertEquals($newItem->getId(),
319
GalleryRemoteControllerTestAddOption::getCalledWithId());
321
$this->assertEquals('testfile.txt', $newItem->getPathComponent());
322
$this->assertEquals('this is a caption', $newItem->getTitle());
323
$this->assertEquals('this is a description', $newItem->getDescription());
324
$this->assertEquals('this is a summary', $newItem->getSummary());
325
$this->assertEquals($platform->filesize(__FILE__), $newItem->getSize());
328
function testNewAlbum() {
331
$platform = $gallery->getPlatform();
332
GalleryUtilities::putRequestVariable('form[cmd]', 'new-album');
333
GalleryUtilities::putRequestVariable('form[set_albumName]', $this->_rootAlbum->getId());
334
GalleryUtilities::putRequestVariable('form[newAlbumName]', 'name'.rand());
335
GalleryUtilities::putRequestVariable('form[newAlbumTitle]', 'title');
336
GalleryUtilities::putRequestVariable('form[newAlbumDesc]', 'desc');
338
$results = $this->handleRequest();
339
$this->assertEquals(array(), $results['error']);
340
$this->assertEquals('remote.GalleryRemote', $results['delegate']['view']);
342
$response = $results['status']['controllerResponse'];
343
$this->assertEquals($this->_code['SUCCESS'], (int)$response->getProperty('status'));
345
$newAlbumId = (int)$response->getProperty('album_name');
347
/* Make sure the item was added */
348
list ($ret, $newAlbum) = GalleryCoreApi::loadEntitiesById($newAlbumId);
349
if ($ret->isError()) {
350
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
353
$this->assertEquals('title', $newAlbum->getTitle());
354
$this->assertEquals('desc', $newAlbum->getDescription());
357
function testFetchAlbumImagesEmpty() {
360
GalleryUtilities::putRequestVariable('form[cmd]', 'fetch-album-images');
361
GalleryUtilities::putRequestVariable('form[set_albumName]', $this->_rootAlbum->getId());
362
$results = $this->handleRequest();
363
$this->assertEquals(array(), $results['error']);
364
$this->assertEquals('remote.GalleryRemote', $results['delegate']['view']);
366
$response = $results['status']['controllerResponse'];
367
$this->assertEquals($this->_code['SUCCESS'], (int)$response->getProperty('status'));
369
/* Make sure the there are no images */
370
$this->assert($response->getProperty('image_count') == 0);
371
$this->assert($response->hasProperty('status_text'));
374
function testFetchAlbumImages() {
377
/* Create some images */
378
$parentId = $this->_rootAlbum->getId();
379
for ($i = 0; $i < 2; $i++) {
380
list ($ret, $image[$i]) = $this->_createRandomDataItem($parentId, 'image/jpg');
381
if ($ret->isError()) {
382
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
385
list ($ret, $derivative[$i]) = $this->_createDerivative(
386
$image[$i], $image[$i]->getId(), DERIVATIVE_TYPE_IMAGE_THUMBNAIL);
387
if ($ret->isError()) {
388
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
391
GalleryUtilities::putRequestVariable('form[cmd]', 'fetch-album-images');
392
GalleryUtilities::putRequestVariable('form[set_albumName]', $this->_rootAlbum->getId());
393
$results = $this->handleRequest();
394
$this->assertEquals(array(), $results['error']);
395
$this->assertEquals('remote.GalleryRemote', $results['delegate']['view']);
397
$response = $results['status']['controllerResponse'];
398
$this->assertEquals($this->_code['SUCCESS'], (int)$response->getProperty('status'));
400
/* Make sure the there are 2 images */
401
$this->assertEquals('2', $response->getProperty('image_count'));
402
$this->assert($response->hasProperty('status_text'));
405
function testFetchAlbumImagesAlbumsToo() {
408
/* Create some images */
409
$parentId = $this->_rootAlbum->getId();
410
for ($i = 0; $i < 2; $i++) {
411
list ($ret, $image[$i]) = $this->_createRandomDataItem($parentId, 'image/jpg');
412
if ($ret->isError()) {
413
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
416
list ($ret, $derivative[$i]) = $this->_createDerivative(
417
$image[$i], $image[$i]->getId(), DERIVATIVE_TYPE_IMAGE_THUMBNAIL);
418
if ($ret->isError()) {
419
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
422
list ($ret, $album) = $this->_createRandomAlbum($parentId);
423
if ($ret->isError()) {
424
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
427
GalleryUtilities::putRequestVariable('form[cmd]', 'fetch-album-images');
428
GalleryUtilities::putRequestVariable('form[set_albumName]', $this->_rootAlbum->getId());
429
GalleryUtilities::putRequestVariable('form[albums_too]', 'yes');
430
$results = $this->handleRequest();
431
$this->assertEquals(array(), $results['error']);
432
$this->assertEquals('remote.GalleryRemote', $results['delegate']['view']);
434
$response = $results['status']['controllerResponse'];
435
$this->assertEquals($this->_code['SUCCESS'], (int)$response->getProperty('status'));
437
/* Make sure the there are 2 images and 1 album */
438
$this->assertEquals('3', $response->getProperty('image_count'));
439
$this->assert($response->hasProperty('status_text'));
442
function testNoop() {
445
$platform = $gallery->getPlatform();
446
GalleryUtilities::putRequestVariable('form[cmd]', 'no-op');
448
$results = $this->handleRequest();
449
$this->assertEquals(array(), $results['error']);
450
$this->assertEquals('remote.GalleryRemote', $results['delegate']['view']);
452
$response = $results['status']['controllerResponse'];
453
$this->assertEquals($this->_code['SUCCESS'], (int)$response->getProperty('status'));
454
$this->assert($response->hasProperty('status_text'));
457
function testAlbumProperties() {
460
/* Set the preferences */
461
$ret = GalleryCoreApi::addDerivativePreference(
462
1, $this->_rootAlbum->getId(), DERIVATIVE_TYPE_IMAGE_RESIZE, 'scale|640,640');
463
if ($ret->isError()) {
464
return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
467
GalleryUtilities::putRequestVariable('form[cmd]', 'album-properties');
468
GalleryUtilities::putRequestVariable('form[set_albumName]', $this->_rootAlbum->getId());
469
$results = $this->handleRequest();
470
$this->assertEquals(array(), $results['error']);
471
$this->assertEquals('remote.GalleryRemote', $results['delegate']['view']);
473
$response = $results['status']['controllerResponse'];
474
$this->assertEquals($this->_code['SUCCESS'], (int)$response->getProperty('status'));
476
$this->assertEquals('640', $response->getProperty('auto_resize'));
477
$this->assert($response->hasProperty('status_text'));
481
class GalleryRemoteControllerTestAddOption {
483
function handleRequestAfterAdd($form, $items) {
484
GalleryUtilities::putRequestVariable('calledWith', $items[0]->getId());
485
return array(GalleryStatus::success(), array(), array());
488
function getCalledWithId() {
489
return (int)GalleryUtilities::getRequestVariables('calledWith');