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

« back to all changes in this revision

Viewing changes to modules/comment/test/phpunit/CommentTest.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: CommentTest.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.10 $ $Date: 2005/08/23 03:49:02 $
 
23
 * @version $Revision: 1.15 $ $Date: 2006/01/13 03:39:31 $
24
24
 * @package Comment
25
25
 * @subpackage PHPUnit
26
26
 * @author Bharat Mediratta <bharat@menalto.com>
29
29
/**
30
30
 * Test Comment functionality
31
31
 *
32
 
 * @package GalleryCore
 
32
 * @package Comment
33
33
 * @subpackage PHPUnit
34
34
 *
35
35
 */
39
39
        $this->GalleryTestCase($methodName);
40
40
    }
41
41
 
42
 
    /**
43
 
     *
44
 
     */
45
42
    function setUp() {
46
43
        global $gallery;
47
 
 
48
44
        parent::setUp();
49
45
 
50
 
        list ($ret, $this->_item) = $this->_createRandomDataItem($this->_getRootId());
51
 
        if ($ret->isError()) {
 
46
        list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
 
47
        if ($ret) {
 
48
            print $ret->getAsHtml();
52
49
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
53
50
        }
 
51
        $this->_markForCleanup($this->_album);
54
52
 
55
 
        $this->_markForCleanup($this->_item);
 
53
        list ($ret, $this->_item) = $this->_createRandomDataItem($this->_album->getId());
 
54
        if ($ret) {
 
55
            print $ret->getAsHtml();
 
56
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
57
        }
56
58
 
57
59
        /* Create a few comments */
58
60
        for ($i = 0; $i < 3; $i++) {
59
61
            list ($ret, $this->_comment[$i]) =
60
62
                GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryComment');
61
 
            if ($ret->isError()) {
 
63
            if ($ret) {
62
64
                print $ret->getAsHtml();
63
65
                return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
64
66
            }
70
72
            $this->_comment[$i]->setComment('this is a test comment #' . $i);
71
73
            $this->_comment[$i]->setDate(time()+$i);
72
74
            $ret = $this->_comment[$i]->save();
73
 
            if ($ret->isError()) {
 
75
            if ($ret) {
74
76
                print $ret->getAsHtml();
75
77
                return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
76
78
            }
80
82
    function testFetchComments() {
81
83
        list ($ret, $comments) =
82
84
            GalleryCommentHelper::fetchComments($this->_item->getId(), 2, ORDER_ASCENDING);
83
 
        if ($ret->isError()) {
 
85
        if ($ret) {
84
86
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
85
87
        }
86
88
 
89
91
    }
90
92
 
91
93
    function testFetchCommentCounts() {
92
 
        list ($ret, $counts) = GalleryCommentHelper::fetchCommentCounts(array($this->_item->getId()));
93
 
        if ($ret->isError()) {
 
94
        list ($ret, $counts) =
 
95
            GalleryCommentHelper::fetchCommentCounts(array($this->_item->getId()));
 
96
        if ($ret) {
94
97
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
95
98
        }
96
99
 
97
100
        $this->assertEquals(3, $counts[$this->_item->getId()], 'Count is wrong');
98
101
    }
99
102
 
 
103
    function testFetchAllComments() {
 
104
        global $gallery;
 
105
        list ($ret, $item2) = $this->_createRandomDataItem($this->_album->getId());
 
106
        if ($ret) {
 
107
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
108
        }
 
109
        $ret = GalleryCoreApi::removeItemPermissions($item2->getId());
 
110
        if ($ret) {
 
111
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
112
        }
 
113
        $ret = GalleryCoreApi::addUserPermission(
 
114
            $item2->getId(), $gallery->getActiveUserId(), 'core.all');
 
115
        if ($ret) {
 
116
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
117
        }
 
118
        list ($ret, $everybodyGroupId) =
 
119
            GalleryCoreApi::getPluginParameter('module', 'core', 'id.everybodyGroup');
 
120
        if ($ret) {
 
121
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
122
        }
 
123
        $ret = GalleryCoreApi::addGroupPermission(
 
124
            $this->_item->getId(), $everybodyGroupId, 'comment.view');
 
125
        if ($ret) {
 
126
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
127
        }
 
128
 
 
129
        list ($ret, $comment) =
 
130
            GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryComment');
 
131
        if ($ret) {
 
132
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
133
        }
 
134
        $this->assert(isset($comment), 'Missing comment instance');
 
135
        $comment->create($item2->getId());
 
136
        $comment->setCommenterId($gallery->getActiveUserId());
 
137
        $comment->setHost('127.0.0.1');
 
138
        $comment->setComment('this is a test comment');
 
139
        $comment->setDate(time()+5);
 
140
        $ret = $comment->save();
 
141
        if ($ret) {
 
142
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
143
        }
 
144
 
 
145
        list ($ret, $comments) = GalleryCommentHelper::fetchAllComments(
 
146
                                 $this->_album->getId(), 3, null, ORDER_DESCENDING);
 
147
        if ($ret) {
 
148
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
149
        }
 
150
        $this->assertEquals(array($comment, $this->_comment[2], $this->_comment[1]), $comments);
 
151
 
 
152
        $this->_becomeGuestUser();
 
153
 
 
154
        list ($ret, $comments) = GalleryCommentHelper::fetchAllComments($this->_album->getId());
 
155
        if ($ret) {
 
156
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
157
        }
 
158
        $this->assertEquals(array($this->_comment[0], $this->_comment[1], $this->_comment[2]),
 
159
                            $comments, 'guest');
 
160
 
 
161
        list ($ret, $comments) =
 
162
            GalleryCommentHelper::fetchAllComments($this->_album->getId(), 2, 2, ORDER_DESCENDING);
 
163
        if ($ret) {
 
164
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
165
        }
 
166
        $this->assertEquals(array($this->_comment[0]), $comments, 'guest/offset');
 
167
    }
 
168
 
100
169
    function testGetItemSummaries() {
101
170
        list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'comment');
102
 
        if ($ret->isError()) {
 
171
        if ($ret) {
103
172
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
104
173
        }
105
174
        $itemId = $this->_item->getId();
106
175
        /* Verify we get a non empty item summary for the item */
107
176
        $permissions[$itemId]['comment.view'] = 1;
108
177
        list ($ret, $content) = $module->getItemSummaries(array($this->_item), $permissions);
109
 
        if ($ret->isError()) {
 
178
        if ($ret) {
110
179
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
111
180
        }
112
181
        $this->assert(isset($content[$itemId]) && !empty($content[$itemId]),
113
182
                      'did not get item summary');
114
 
        
 
183
 
115
184
        /* Get the item summary again, but without having the permissions */
116
185
        unset($content);
117
186
        list ($ret, $content) = $module->getItemSummaries(array($this->_item),
118
187
                                                          array($itemId => array()));
119
 
        if ($ret->isError()) {
 
188
        if ($ret) {
120
189
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
121
190
        }
122
191
        $this->assert(!isset($content[$itemId]), 'got an item summary');
126
195
        global $gallery;
127
196
 
128
197
        /* Create a comment */
129
 
        list ($ret, $comment) = GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryComment');
130
 
        if ($ret->isError()) {
 
198
        list ($ret, $comment) =
 
199
            GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryComment');
 
200
        if ($ret) {
131
201
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
132
202
        }
133
203
        $this->assert(isset($comment), 'Missing comment instance');
137
207
        $comment->setComment('this is a test comment');
138
208
        $comment->setDate(time());
139
209
        $ret = $comment->save();
140
 
        if ($ret->isError()) {
 
210
        if ($ret) {
141
211
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
142
212
        }
143
213
 
144
214
        /* Modify it */
145
215
        list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($comment->getId());
146
 
        if ($ret->isError()) {
 
216
        if ($ret) {
147
217
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
148
218
        }
149
219
 
150
220
        $comment->setComment('foo bar');
151
221
        $ret = $comment->save();
152
 
        if ($ret->isError()) {
 
222
        if ($ret) {
153
223
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
154
224
        }
155
225
 
156
226
        $ret = GalleryCoreApi::releaseLocks($lockId);
157
 
        if ($ret->isError()) {
 
227
        if ($ret) {
158
228
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
159
229
        }
160
230
 
161
231
        /* Delete it */
162
232
        $ret = GalleryCoreApi::deleteEntityById($comment->getId());
163
 
        if ($ret->isError()) {
164
 
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
233
        if ($ret) {
 
234
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
235
        }
 
236
    }
 
237
 
 
238
    /* Verify that the comments are deleted if the commenter is deleted */
 
239
    function testDeleteCommentsOnDeleteUserEvent() {
 
240
        global $gallery;
 
241
 
 
242
        /* Create a user that we can delete */
 
243
        list ($ret, $user) = $this->_createRandomUser();
 
244
        if ($ret) {
 
245
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
246
        }
 
247
 
 
248
        /* Create a few comments */
 
249
        for ($i = 0; $i < 3; $i++) {
 
250
            list ($ret, $myComment[$i]) =
 
251
                GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryComment');
 
252
            if ($ret) {
 
253
                return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
254
            }
 
255
 
 
256
            $this->assert(isset($myComment[$i]), 'Missing comment instance');
 
257
            $myComment[$i]->create($this->_item->getId());
 
258
            $myComment[$i]->setCommenterId($user->getId());
 
259
            $myComment[$i]->setHost('127.0.0.1');
 
260
            $myComment[$i]->setComment('my comment #' . $i);
 
261
            $myComment[$i]->setDate(time()+$i);
 
262
            $ret =$myComment[$i]->save();
 
263
            if ($ret) {
 
264
                return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
265
            }
 
266
        }
 
267
 
 
268
        /* Assert that everything is as expected before executing the test */
 
269
        list ($ret, $comments) = GalleryCommentHelper::fetchComments($this->_item->getId());
 
270
        if ($ret) {
 
271
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
272
        }
 
273
        $otherCommentsCount = count($this->_comment);
 
274
        $this->assertEquals($otherCommentsCount + 3, count($comments),
 
275
                            'nr of comments before the test');
 
276
        foreach ($comments as $comment) {
 
277
            $commentsById[$comment->getId()] = $comment;
 
278
        }
 
279
        foreach ($myComment as $comment) {
 
280
            $this->assertEquals($commentsById[$comment->getId()]->getCommenterId(), $user->getId(),
 
281
                                'commenterId (user) wrong before the test');
 
282
        }
 
283
        foreach ($this->_comment as $comment) {
 
284
            $this->assertEquals($commentsById[$comment->getId()]->getCommenterId(),
 
285
                                $this->_item->getOwnerId(),
 
286
                                'commenterId (admin) wrong before the test');
 
287
        }
 
288
 
 
289
        /* The event listener is deactivated during unit tests, register it */
 
290
        $listener = new GalleryCommentHelper();
 
291
        $this->_registerTestEventListener('GalleryEntity::delete', $listener);
 
292
 
 
293
        /* Delete the user (execute the test) */
 
294
        $ret = GalleryCoreApi::deleteEntityById($user->getId());
 
295
        if ($ret) {
 
296
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
297
        }
 
298
 
 
299
        /* Verify that all comments by this user are gone */
 
300
        list ($ret, $comments) = GalleryCommentHelper::fetchComments($this->_item->getId());
 
301
        if ($ret) {
 
302
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
303
        }
 
304
        $this->assertEquals(3, count($comments), 'nr of comments after the test');
 
305
        foreach ($comments as $comment) {
 
306
            $this->assertEquals($this->_item->getOwnerId(), $comment->getCommenterId(),
 
307
                                'commenterId wrong after the test');
165
308
        }
166
309
    }
167
310
}
168
 
 
169
311
?>