~ubuntu-branches/ubuntu/hardy/gallery2/hardy-security

« back to all changes in this revision

Viewing changes to modules/quotas/test/phpunit/QuotasHelperTest.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2006-04-16 16:42:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060416164235-8uy0u4bfjdxpge2o
Tags: 2.1.1-1
* New upstream release (Closes: #362936)
  + Bugfixes for Postgres7 (Closes: #359000, #362152)

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 * $RCSfile: QuotasHelperTest.class,v $
4
4
 *
5
5
 * Gallery - a web based photo album viewer and editor
6
 
 * Copyright (C) 2000-2005 Bharat Mediratta
 
6
 * Copyright (C) 2000-2006 Bharat Mediratta
7
7
 *
8
8
 * This program is free software; you can redistribute it and/or modify
9
9
 * it under the terms of the GNU General Public License as published by
20
20
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
21
21
 */
22
22
/**
23
 
 * @version $Revision: 1.5 $ $Date: 2005/08/23 03:49:50 $
 
23
 * @version $Revision: 1.6 $ $Date: 2006/01/10 04:41:49 $
24
24
 * @package Quotas
25
25
 * @subpackage PHPUnit
26
26
 * @author Robert Balousek <volksport@users.sf.net>
29
29
/**
30
30
 * Helper class
31
31
 */
32
 
GalleryCoreApi::relativeRequireOnce('modules/quotas/classes/GalleryQuotasHelper.class');
33
 
 
34
 
/**
35
 
 * Map class
36
 
 */
37
 
GalleryCoreApi::relativeRequireOnce('modules/quotas/classes/QuotasMap.class');
 
32
GalleryCoreApi::requireOnce('modules/quotas/classes/GalleryQuotasHelper.class');
38
33
 
39
34
/**
40
35
 * Test Quotas functionality
56
51
 
57
52
        /* Create a container album for any data items we create */
58
53
        list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
59
 
        if ($ret->isError()) {
 
54
        if ($ret) {
60
55
            print $ret->getAsHtml();
61
56
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
62
57
        }
64
59
 
65
60
        /* Create a dummy user */
66
61
        list ($ret, $this->_user) = $this->_createRandomUser();
67
 
        if ($ret->isError()) {
 
62
        if ($ret) {
68
63
            print $ret->getAsHtml();
69
64
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
70
65
        }
72
67
 
73
68
        /* Create dummy group */
74
69
        list ($ret, $this->_group) = $this->_createRandomGroup();
75
 
        if ($ret->isError()) {
 
70
        if ($ret) {
76
71
            print $ret->getAsHtml();
77
72
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
78
73
        }
83
78
        foreach (array('allUserGroup', 'everybodyGroup') as $groupName) {
84
79
            list ($ret, $groupId) =
85
80
                GalleryCoreApi::getPluginParameter('module', 'core', 'id.' . $groupName);
86
 
            if ($ret->isError()) {
 
81
            if ($ret) {
87
82
                print $ret->getAsHtml();
88
83
                return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
89
84
            }
90
85
            list ($ret, $hasQuota, $quota) = GalleryQuotasHelper::fetchGroupQuota($groupId);
91
 
            if ($ret->isError()) {
 
86
            if ($ret) {
92
87
                print $ret->getAsHtml();
93
88
                return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
94
89
            }
95
90
            if ($hasQuota) {
96
91
                $this->_saveQuota[$groupId] = $quota;
97
 
                $ret = QuotasMap::removeMapEntry(array('userOrGroupId' => $groupId));
98
 
                if ($ret->isError()) {
 
92
                $ret = GalleryCoreApi::removeMapEntry(
 
93
                    'QuotasMap', array('userOrGroupId' => $groupId));
 
94
                if ($ret) {
99
95
                    print $ret->getAsHtml();
100
96
                    return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
101
97
                }
105
101
 
106
102
    function tearDown() {
107
103
        foreach ($this->_saveQuota as $groupId => $quota) {
108
 
            $ret = QuotasMap::addMapEntry(
 
104
            $ret = GalleryCoreApi::addMapEntry(
 
105
                'QuotasMap',
109
106
                array('userOrGroupId' => $groupId, 'quotaSize' => $quota['quota']));
110
 
            if ($ret->isError()) {
 
107
            if ($ret) {
111
108
                $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
112
109
            }
113
110
        }
118
115
 
119
116
        /* check the user's usage, it should be zero */
120
117
        list ($ret, $userDiskUsage) = GalleryQuotasHelper::getUserDiskUsage($this->_user->getId());
121
 
        if ($ret->isError()) {
 
118
        if ($ret) {
122
119
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
123
120
        }
124
121
 
130
127
            list ($ret, $this->_item[$j]) =
131
128
                $this->_createRandomDataItem($this->_album->getId(),
132
129
                                             'application/unknown',
133
 
                                             array('OwnerId' => $this->_user->getId(),
134
 
                                                   'Size' => 100000 + $j));
135
 
            if ($ret->isError()) {
 
130
                                             array('ownerId' => $this->_user->getId(),
 
131
                                                   'size' => 100000 + $j));
 
132
            if ($ret) {
136
133
                return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
137
134
            }
138
135
        }
147
144
            list ($ret, $userDiskUsage) =
148
145
                GalleryQuotasHelper::getUserDiskUsage($this->_user->getId(),
149
146
                                                      $excludeIds);
150
 
            if ($ret->isError()) {
 
147
            if ($ret) {
151
148
                return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
152
149
            }
153
150
            $expectedUsage += $j + 100000;
160
157
        /* user doesn't have a quota yet */
161
158
        list ($ret, $quotaExists, $userDiskQuota) =
162
159
            GalleryQuotasHelper::getUserDiskQuota($this->_user->getId());
163
 
        if ($ret->isError()) {
 
160
        if ($ret) {
164
161
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
165
162
        }
166
163
        $this->assert($quotaExists == false, 'A quota should not exist');
172
169
        /* user has a user quota */
173
170
        list ($ret, $quotaExists, $userDiskQuota) =
174
171
            GalleryQuotasHelper::getUserDiskQuota($this->_user->getId());
175
 
        if ($ret->isError()) {
 
172
        if ($ret) {
176
173
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
177
174
        }
178
175
        $this->assert($quotaExists == true, 'A user quota should exist for this user');
180
177
 
181
178
        /* Add our dummy user to our dummy group, dummy */
182
179
        $ret = GalleryCoreApi::addUserToGroup($this->_user->getId(), $this->_group->getId());
183
 
        if ($ret->isError()) {
 
180
        if ($ret) {
184
181
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
185
182
        }
186
183
 
189
186
        /* user has a user quota, and is in a group with no quota */
190
187
        list ($ret, $quotaExists, $userDiskQuota) =
191
188
            GalleryQuotasHelper::getUserDiskQuota($this->_user->getId());
192
 
        if ($ret->isError()) {
 
189
        if ($ret) {
193
190
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
194
191
        }
195
192
        $this->assert($quotaExists == true, 'A user quota should exist for this user');
202
199
        /* user has a user & group quota */
203
200
        list ($ret, $quotaExists, $userDiskQuota) =
204
201
            GalleryQuotasHelper::getUserDiskQuota($this->_user->getId());
205
 
        if ($ret->isError()) {
 
202
        if ($ret) {
206
203
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
207
204
        }
208
205
        $this->assert($quotaExists == true, 'A user quota should exist for this user');
214
211
        /* user has a group quota(s) and no user quota */
215
212
        list ($ret, $quotaExists, $userDiskQuota) =
216
213
            GalleryQuotasHelper::getUserDiskQuota($this->_user->getId());
217
 
        if ($ret->isError()) {
 
214
        if ($ret) {
218
215
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
219
216
        }
220
217
 
227
224
        /* user doesn't have any quotas anymore */
228
225
        list ($ret, $quotaExists, $userDiskQuota) =
229
226
            GalleryQuotasHelper::getUserDiskQuota($this->_user->getId());
230
 
        if ($ret->isError()) {
 
227
        if ($ret) {
231
228
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
232
229
        }
233
230
        $this->assert($quotaExists == false, 'A quota should not exist');
236
233
    function testFetchHighestGroupQuota () {
237
234
        /* Add our dummy user to our dummy group, dummy */
238
235
        $ret = GalleryCoreApi::addUserToGroup($this->_user->getId(), $this->_group->getId());
239
 
        if ($ret->isError()) {
 
236
        if ($ret) {
240
237
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
241
238
        }
242
239
 
245
242
        /* get the highest group quota */
246
243
        list ($ret, $groupQuotaExists, $groupQuotaInfo) =
247
244
            GalleryQuotasHelper::fetchHighestGroupQuota($this->_user->getId());
248
 
        if ($ret->isError()) {
 
245
        if ($ret) {
249
246
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
250
247
        }
251
248
 
258
255
        /* get the highest group quota perhaps our new one is highest? */
259
256
        list ($ret, $groupQuotaExists, $groupQuotaInfo) =
260
257
            GalleryQuotasHelper::fetchHighestGroupQuota($this->_user->getId());
261
 
        if ($ret->isError()) {
 
258
        if ($ret) {
262
259
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
263
260
        }
264
261
        $this->assert($groupQuotaExists == true, 'Quota existance failure');
279
276
        foreach ($conversion as $key => $value) {
280
277
            list ($ret, $humanReadable, $units) =
281
278
                GalleryQuotasHelper::humanReadableFromKilobytes($key);
282
 
            if ($ret->isError()) {
 
279
            if ($ret) {
283
280
                return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
284
281
            }
285
282
            $this->assertEquals((string)$value[0], $humanReadable, 'Incorrect value');
290
287
    function testFetchQuotaGroupIdList() {
291
288
        /* get the actual number of group quotas there is */
292
289
        list ($ret, $groupCount) = GalleryQuotasHelper::fetchQuotaGroupCount();
293
 
        if ($ret->isError()) {
 
290
        if ($ret) {
294
291
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
295
292
        }
296
293
        /* now we use that as the count (+1 for good measure) */
297
294
        list ($ret, $groupIdList) = GalleryQuotasHelper::fetchQuotaGroupIdList($groupCount + 1);
298
 
        if ($ret->isError()) {
 
295
        if ($ret) {
299
296
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
300
297
        }
301
298
        $this->assert($groupCount == count($groupIdList), 'Group count is incorrect');
304
301
                                  'quotaSize' => 105));
305
302
        /* now we use that as the count (+1 for good measure) */
306
303
        list ($ret, $groupIdList) = GalleryQuotasHelper::fetchQuotaGroupIdList($groupCount + 1);
307
 
        if ($ret->isError()) {
 
304
        if ($ret) {
308
305
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
309
306
        }
310
307
        $this->assertEquals($groupCount + 1, count($groupIdList), 'New group count is incorrect');
316
313
 
317
314
    function testFetchQuotaGroupCount() {
318
315
        list ($ret, $preGroupCount) = GalleryQuotasHelper::fetchQuotaGroupCount();
319
 
        if ($ret->isError()) {
 
316
        if ($ret) {
320
317
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
321
318
        }
322
319
        /* add a quota of 99 KB to this group */
323
320
        $this->_addMapEntry(array('userOrGroupId' => $this->_group->getId(),
324
321
                                  'quotaSize' => 99));
325
322
        list ($ret, $postGroupCount) = GalleryQuotasHelper::fetchQuotaGroupCount();
326
 
        if ($ret->isError()) {
 
323
        if ($ret) {
327
324
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
328
325
        }
329
326
        $this->assert($postGroupCount = $preGroupCount + 1, 'Group Counts do not match up');
333
330
    function testFetchQuotaUserIdList() {
334
331
        /* get the actual number of User quotas there is */
335
332
        list ($ret, $userCount) = GalleryQuotasHelper::fetchQuotaUserCount();
336
 
        if ($ret->isError()) {
 
333
        if ($ret) {
337
334
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
338
335
        }
339
336
        /* now we use that as the count (+1 for good measure) */
340
337
        list ($ret, $userIdList) = GalleryQuotasHelper::fetchQuotaUserIdList($userCount + 1);
341
 
        if ($ret->isError()) {
 
338
        if ($ret) {
342
339
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
343
340
        }
344
341
        $this->assert($userCount == count($userIdList), 'User count is incorrect');
347
344
                                  'quotaSize' => 467));
348
345
        /* now we use that as the count (+1 for good measure) */
349
346
        list ($ret, $userIdList) = GalleryQuotasHelper::fetchQuotaUserIdList($userCount + 1);
350
 
        if ($ret->isError()) {
 
347
        if ($ret) {
351
348
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
352
349
        }
353
350
        $this->assertEquals($userCount + 1, count($userIdList), 'New User count is incorrect');
359
356
 
360
357
    function testFetchQuotaUserCount() {
361
358
        list ($ret, $preUserCount) = GalleryQuotasHelper::fetchQuotaUserCount();
362
 
        if ($ret->isError()) {
 
359
        if ($ret) {
363
360
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
364
361
        }
365
362
        /* add a quota of 86 KB to this user */
366
363
        $this->_addMapEntry(array('userOrGroupId' => $this->_user->getId(),
367
364
                                  'quotaSize' => 86));
368
365
        list ($ret, $postUserCount) = GalleryQuotasHelper::fetchQuotaUserCount();
369
 
        if ($ret->isError()) {
 
366
        if ($ret) {
370
367
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
371
368
        }
372
369
        $this->assert($postUserCount = $preUserCount + 1, 'User Counts do not match up');
376
373
    function testFetchGroupQuota() {
377
374
        list ($ret, $quotaExists, $groupQuotaInfo) =
378
375
            GalleryQuotasHelper::FetchGroupQuota($this->_group->getId());
379
 
        if ($ret->isError()) {
 
376
        if ($ret) {
380
377
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
381
378
        }
382
379
        $this->assert($quotaExists == false, 'Quota should not exist for this group');
388
385
 
389
386
        list ($ret, $quotaExists, $groupQuotaInfo) =
390
387
            GalleryQuotasHelper::FetchGroupQuota($this->_group->getId());
391
 
        if ($ret->isError()) {
 
388
        if ($ret) {
392
389
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
393
390
        }
394
391
        $this->assert($quotaExists == true, 'Quota should exist for this group');
402
399
    function testFetchUserQuota() {
403
400
        list ($ret, $quotaExists, $userQuotaInfo) =
404
401
            GalleryQuotasHelper::FetchUserQuota($this->_user->getId());
405
 
        if ($ret->isError()) {
 
402
        if ($ret) {
406
403
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
407
404
        }
408
405
        $this->assert($quotaExists == false, 'Quota should not exist for this user');
414
411
 
415
412
        list ($ret, $quotaExists, $userQuotaInfo) =
416
413
            GalleryQuotasHelper::fetchUserQuota($this->_user->getId());
417
 
        if ($ret->isError()) {
 
414
        if ($ret) {
418
415
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
419
416
        }
420
417
        $this->assert($quotaExists == true, 'Quota should exist for this user');
426
423
    }
427
424
 
428
425
    function _removeMapEntry($remove) {
429
 
        $ret = QuotasMap::removeMapEntry($remove);
430
 
        if ($ret->isError()) {
 
426
        $ret = GalleryCoreApi::removeMapEntry('QuotasMap', $remove);
 
427
        if ($ret) {
431
428
            $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
432
429
        }
433
430
    }
434
431
 
435
432
    function _addMapEntry($add) {
436
 
        $ret = QuotasMap::addMapEntry($add);
437
 
        if ($ret->isError()) {
 
433
        $ret = GalleryCoreApi::addMapEntry('QuotasMap', $add);
 
434
        if ($ret) {
438
435
            $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
439
436
        }
440
437
    }
441
438
 
442
439
    function _deleteEntity($entity) {
443
 
        global $gallery;
444
440
        $ret = GalleryCoreApi::deleteEntityById($entity->getId());
445
 
        if ($ret->isError()) {
 
441
        if ($ret) {
446
442
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
447
443
        }
448
444
    }