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

« back to all changes in this revision

Viewing changes to modules/rating/test/phpunit/RatingCallbacksTest.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:
 
1
<?php
 
2
/*
 
3
 * $RCSfile: RatingCallbacksTest.class,v $
 
4
 *
 
5
 * Gallery - a web based photo album viewer and editor
 
6
 * Copyright (C) 2000-2006 Bharat Mediratta
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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.
 
21
 */
 
22
/**
 
23
 * @version $Revision: 1.3 $ $Date: 2006/01/11 20:12:01 $
 
24
 * @package Rating
 
25
 * @subpackage PHPUnit
 
26
 * @author Don Seiler <don@seiler.us>
 
27
 */
 
28
 
 
29
GalleryCoreApi::requireOnce('modules/core/classes/GallerySmarty.class');
 
30
GalleryCoreApi::requireOnce('modules/rating/Callbacks.inc');
 
31
 
 
32
/**
 
33
 * Rating callback tests
 
34
 * @package Rating
 
35
 * @subpackage PHPUnit
 
36
 */
 
37
class RatingCallbacksTest extends GalleryTestCase {
 
38
 
 
39
    function RatingCallbacksTest($methodName) {
 
40
        $this->GalleryTestCase($methodName);
 
41
    }
 
42
 
 
43
    /**
 
44
     * Create an album for testing
 
45
     */
 
46
    function setUp() {
 
47
        global $gallery;
 
48
        parent::setUp();
 
49
 
 
50
        $ret = $this->_markPluginParametersForCleanup('module', 'rating');
 
51
        if ($ret) {
 
52
            print $ret->getAsHtml();
 
53
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
54
        }
 
55
 
 
56
        $this->_userId = $gallery->getActiveUserId();
 
57
 
 
58
        list ($ret, $this->_module) = GalleryCoreApi::loadPlugin('module', 'rating');
 
59
        if ($ret || !isset($this->_module)) {
 
60
            print $ret->getAsHtml();
 
61
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
62
        }
 
63
 
 
64
        list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
 
65
        if ($ret) {
 
66
            print $ret->getAsHtml();
 
67
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
68
        }
 
69
        $this->_markForCleanup($this->_album);
 
70
 
 
71
        list ($ret, $this->_item) = $this->_createRandomDataItem($this->_album->getId(),
 
72
                'image/test', array(), __FILE__, 'GalleryPhotoItem');
 
73
        if ($ret) {
 
74
            print $ret->getAsHtml();
 
75
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
76
        }
 
77
    }
 
78
 
 
79
 
 
80
    function testRatingBlocks() {
 
81
        $smarty = new GallerySmarty();
 
82
        $callbacks = new RatingCallbacks();
 
83
        $ret = $callbacks->callback(array('itemId' => $this->_item->getId()),
 
84
                $smarty, 'LoadRating', $this->_userId);
 
85
        if ($ret) {
 
86
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
87
        }
 
88
 
 
89
        $data = $smarty->_tpl_vars['block']['rating']['RatingData'];
 
90
        $summary = $smarty->_tpl_vars['block']['rating']['RatingSummary'];
 
91
        $this->assertEquals(array('ratingValues' => array(1,2,3,4,5), 'firstCall' => true),
 
92
                            $summary, 'Rating summary array does not match.');
 
93
        $this->assert(isset($data['rating']), 'Missing rating');
 
94
        $this->assert(isset($data['userRating']), 'Missing userRating');
 
95
        $this->assert(isset($data['stars']), 'Missing stars');
 
96
        $this->assert(isset($data['userStars']), 'Missing userStars');
 
97
        $this->assert(isset($data['votes']), 'Missing votes');
 
98
        $this->assert(isset($data['averagePercent']), 'Missing average percent');
 
99
    }
 
100
 
 
101
 
 
102
 
 
103
    function testAllowAlbumRating() {
 
104
        $ret = GalleryCoreApi::setPluginParameter('module', 'rating', 'allowAlbumRating', 1);
 
105
        if ($ret) {
 
106
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
107
        }
 
108
 
 
109
        $smarty = new GallerySmarty();
 
110
        $callbacks = new RatingCallbacks();
 
111
        $ret = $callbacks->callback(array('itemId' => $this->_album->getId()),
 
112
                $smarty, 'LoadRating', $this->_userId);
 
113
        if ($ret) {
 
114
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
115
        }
 
116
 
 
117
        $data = $smarty->_tpl_vars['block']['rating']['RatingData'];
 
118
        $summary = $smarty->_tpl_vars['block']['rating']['RatingSummary'];
 
119
        $this->assertEquals(array('ratingValues' => array(1,2,3,4,5), 'firstCall' => true),
 
120
                            $summary, 'Rating summary array does not match.');
 
121
        $this->assert(isset($data['rating']), 'Missing rating');
 
122
        $this->assert(isset($data['userRating']), 'Missing userRating');
 
123
        $this->assert(isset($data['stars']), 'Missing stars');
 
124
        $this->assert(isset($data['userStars']), 'Missing userStars');
 
125
        $this->assert(isset($data['votes']), 'Missing votes');
 
126
        $this->assert(isset($data['averagePercent']), 'Missing average percent');
 
127
    }
 
128
 
 
129
    function testDoNotAllowAlbumRating() {
 
130
        $ret = GalleryCoreApi::setPluginParameter('module', 'rating', 'allowAlbumRating', 0);
 
131
        if ($ret) {
 
132
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
133
        }
 
134
 
 
135
        $smarty = new GallerySmarty();
 
136
        $callbacks = new RatingCallbacks();
 
137
        $ret = $callbacks->callback(array('itemId' => $this->_album->getId()),
 
138
                $smarty, 'LoadRating', $this->_userId);
 
139
        if ($ret) {
 
140
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
141
        }
 
142
 
 
143
        $this->assert(!isset($smarty->_tpl_vars['block']), 'Received rating block.');
 
144
    }
 
145
 
 
146
    function testRatingPermissionNone() {
 
147
        $ret = GalleryCoreApi::removeItemPermissions($this->_item->getId());
 
148
        if ($ret) {
 
149
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
150
        }
 
151
        $smarty = new GallerySmarty();
 
152
        $callbacks = new RatingCallbacks();
 
153
        $ret = $callbacks->callback(array('itemId' => $this->_item->getId()),
 
154
                $smarty, 'LoadRating', $this->_userId);
 
155
        if ($ret) {
 
156
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
157
        }
 
158
 
 
159
        $this->assert(!isset($smarty->_tpl_vars['block']), 'Received rating block.');
 
160
    }
 
161
 
 
162
    function testRatingPermissionViewOnly() {
 
163
        $ret = GalleryCoreApi::removeItemPermissions($this->_item->getId());
 
164
        if ($ret) {
 
165
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
166
        }
 
167
        $ret = GalleryCoreApi::addUserPermission(
 
168
            $this->_item->getId(), $this->_userId, 'rating.view');
 
169
        if ($ret) {
 
170
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
171
        }
 
172
        $smarty = new GallerySmarty();
 
173
        $callbacks = new RatingCallbacks();
 
174
        $ret = $callbacks->callback(array('itemId' => $this->_item->getId()),
 
175
                $smarty, 'LoadRating', $this->_userId);
 
176
        if ($ret) {
 
177
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
178
        }
 
179
 
 
180
        $data = $smarty->_tpl_vars['block']['rating']['RatingData'];
 
181
        $summary = $smarty->_tpl_vars['block']['rating']['RatingSummary'];
 
182
        $this->assertEquals(array('ratingValues' => array(1,2,3,4,5), 'firstCall' => true),
 
183
                            $summary, 'Rating summary array does not match.');
 
184
        $this->assert(isset($data['rating']), 'Missing rating');
 
185
        $this->assert(isset($data['userRating']), 'Missing userRating');
 
186
        $this->assert(isset($data['stars']), 'Missing stars');
 
187
        $this->assert(isset($data['userStars']), 'Missing userStars');
 
188
        $this->assert(isset($data['votes']), 'Missing votes');
 
189
        $this->assert(isset($data['averagePercent']), 'Missing average percent');
 
190
        $this->assertEquals(false, $data['canRate'], 'canRate is true.');
 
191
    }
 
192
 
 
193
    function testRatingPermissionFull() {
 
194
        $ret = GalleryCoreApi::removeItemPermissions($this->_item->getId());
 
195
        if ($ret) {
 
196
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
197
        }
 
198
        $ret = GalleryCoreApi::addUserPermission(
 
199
            $this->_item->getId(), $this->_userId, 'rating.view');
 
200
        if ($ret) {
 
201
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
202
        }
 
203
        $ret = GalleryCoreApi::addUserPermission(
 
204
            $this->_item->getId(), $this->_userId, 'rating.add');
 
205
        if ($ret) {
 
206
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
207
        }
 
208
        $smarty = new GallerySmarty();
 
209
        $callbacks = new RatingCallbacks();
 
210
        $ret = $callbacks->callback(array('itemId' => $this->_item->getId()),
 
211
                $smarty, 'LoadRating', $this->_userId);
 
212
        if ($ret) {
 
213
            return $this->failWithStatus($ret->wrap(__FILE__, __LINE__));
 
214
        }
 
215
 
 
216
        $data = $smarty->_tpl_vars['block']['rating']['RatingData'];
 
217
        $summary = $smarty->_tpl_vars['block']['rating']['RatingSummary'];
 
218
        $this->assertEquals(array('ratingValues' => array(1,2,3,4,5), 'firstCall' => true),
 
219
                            $summary, 'Rating summary array does not match.');
 
220
        $this->assert(isset($data['rating']), 'Missing rating');
 
221
        $this->assert(isset($data['userRating']), 'Missing userRating');
 
222
        $this->assert(isset($data['stars']), 'Missing stars');
 
223
        $this->assert(isset($data['userStars']), 'Missing userStars');
 
224
        $this->assert(isset($data['votes']), 'Missing votes');
 
225
        $this->assert(isset($data['averagePercent']), 'Missing average percent');
 
226
        $this->assertEquals(true, $data['canRate'], 'canRate is false.');
 
227
    }
 
228
}
 
229
?>