~ubuntu-branches/ubuntu/quantal/gallery2/quantal

« back to all changes in this revision

Viewing changes to modules/cart/test/phpunit/AddToCartControllerTest.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2007-09-10 20:22:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070910202219-0jsuntvqge4ade6b
Tags: 2.2.3-2
Add Slovak translation of Debconf templates.  (Thanks to 
Ivan Masá.  Closes: #441671)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Gallery - a web based photo album viewer and editor
4
 
 * Copyright (C) 2000-2007 Bharat Mediratta
5
 
 *
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.
10
 
 *
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.
15
 
 *
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.
19
 
 */
20
 
 
21
 
/**
22
 
 * AddToCart controller tests
23
 
 * @package Cart
24
 
 * @subpackage PHPUnit
25
 
 * @author Bharat Mediratta <bharat@menalto.com>
26
 
 * @version $Revision: 15513 $
27
 
 */
28
 
class AddToCartControllerTest extends GalleryControllerTestCase {
29
 
 
30
 
    function AddToCartControllerTest($methodName) {
31
 
        $this->GalleryControllerTestCase($methodName, 'cart.AddToCart');
32
 
    }
33
 
 
34
 
    function setUp() {
35
 
        global $gallery;
36
 
        parent::setUp();
37
 
 
38
 
        /* create an album to hold all of the test items */
39
 
        list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
40
 
        if ($ret) {
41
 
            print $ret->getAsHtml();
42
 
            return $this->failWithStatus($ret);
43
 
        }
44
 
        $this->_markForCleanup($this->_album);
45
 
 
46
 
        $ret = GalleryCoreApi::removeItemPermissions($this->_album->getId());
47
 
        if ($ret) {
48
 
            print $ret->getAsHtml();
49
 
            return $this->failWithStatus($ret);
50
 
        }
51
 
        $ret = GalleryCoreApi::addUserPermission(
52
 
            $this->_album->getId(), $this->_userId = $gallery->getActiveUserId(), 'core.all');
53
 
        if ($ret) {
54
 
            print $ret->getAsHtml();
55
 
            return $this->failWithStatus($ret);
56
 
        }
57
 
 
58
 
        /* Save the existing cart */
59
 
        list ($ret, $this->_saveCartItemCounts) = CartHelper::fetchCartItemCounts();
60
 
        if ($ret) {
61
 
            print $ret->getAsHtml();
62
 
            return $this->failWithStatus($ret);
63
 
        }
64
 
 
65
 
        $cartItemCounts = array(1 => 3,
66
 
                                2 => 3);
67
 
        $ret = CartHelper::setCartItemCounts($cartItemCounts);
68
 
        if ($ret) {
69
 
            print $ret->getAsHtml();
70
 
            return $this->failWithStatus($ret);
71
 
        }
72
 
    }
73
 
 
74
 
    function tearDown() {
75
 
        /* Restore the existing cart */
76
 
        $ret = CartHelper::setCartItemCounts($this->_saveCartItemCounts);
77
 
        if ($ret) {
78
 
            $this->failWithStatus($ret);
79
 
        }
80
 
 
81
 
        parent::tearDown();
82
 
    }
83
 
 
84
 
    function testAddItem() {
85
 
        /* Create a test item */
86
 
        list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId());
87
 
        if ($ret) {
88
 
            return $this->failWithStatus($ret);
89
 
        }
90
 
 
91
 
        GalleryUtilities::putRequestVariable('itemId', $item->getId());
92
 
        /* Perform the request and verify that we succeeded */
93
 
        $results = $this->handleRequest();
94
 
        $this->assertEquals(array('redirect' => array('view' => 'cart.ViewCart'),
95
 
                                  'status' => array(),
96
 
                                  'error' => array()),
97
 
                            $results);
98
 
 
99
 
        /* Verify our cart */
100
 
        list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
101
 
        if ($ret) {
102
 
            return $this->failWithStatus($ret);
103
 
        }
104
 
 
105
 
        $this->assertEquals(array(1 => 3,
106
 
                                  2 => 3,
107
 
                                  $item->getId() => 1),
108
 
                            $cartItemCounts);
109
 
    }
110
 
 
111
 
    function testAddAlbum() {
112
 
        /* Create two test items */
113
 
        for ($i = 0; $i < 2; $i++) {
114
 
            list ($ret, $item[$i]) = $this->_createRandomDataItem($this->_album->getId());
115
 
            if ($ret) {
116
 
                return $this->failWithStatus($ret);
117
 
            }
118
 
        }
119
 
 
120
 
        GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
121
 
        /* Perform the request and verify that we succeeded */
122
 
        $results = $this->handleRequest();
123
 
        $this->assertEquals(array('redirect' => array('view' => 'cart.ViewCart'),
124
 
                                  'status' => array(),
125
 
                                  'error' => array()),
126
 
                            $results);
127
 
 
128
 
        /* Verify our cart */
129
 
        list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
130
 
        if ($ret) {
131
 
            return $this->failWithStatus($ret);
132
 
        }
133
 
 
134
 
        $this->assertEquals(array(1 => 3,
135
 
                                  2 => 3,
136
 
                                  $item[0]->getId() => 1,
137
 
                                  $item[1]->getId() => 1),
138
 
                            $cartItemCounts);
139
 
    }
140
 
 
141
 
    function testAddDuplicateItem() {
142
 
        /* Create a test item */
143
 
        list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId());
144
 
        if ($ret) {
145
 
            return $this->failWithStatus($ret);
146
 
        }
147
 
 
148
 
        /* Put the item in the cart */
149
 
        $ret = CartHelper::setCartItemCounts(array($item->getId() => 1));
150
 
        if ($ret) {
151
 
            return $this->failWithStatus($ret);
152
 
        }
153
 
 
154
 
        /* Perform the request and verify that we succeeded */
155
 
        GalleryUtilities::putRequestVariable('itemId', $item->getId());
156
 
        $results = $this->handleRequest();
157
 
        $this->assertEquals(array('redirect' => array('view' => 'cart.ViewCart'),
158
 
                                  'status' => array(),
159
 
                                  'error' => array()),
160
 
                            $results);
161
 
 
162
 
        /* Verify our cart */
163
 
        list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
164
 
        if ($ret) {
165
 
            return $this->failWithStatus($ret);
166
 
        }
167
 
 
168
 
        $this->assertEquals(array($item->getId() => 2), $cartItemCounts);
169
 
    }
170
 
 
171
 
    function testAddDuplicateAlbum() {
172
 
        /* Create two test items */
173
 
        for ($i = 0; $i < 2; $i++) {
174
 
            list ($ret, $item[$i]) = $this->_createRandomDataItem($this->_album->getId());
175
 
            if ($ret) {
176
 
                return $this->failWithStatus($ret);
177
 
            }
178
 
        }
179
 
 
180
 
        /* Put the items in the cart */
181
 
        $ret = CartHelper::setCartItemCounts(array($item[0]->getId() => 1, $item[1]->getId() => 1));
182
 
        if ($ret) {
183
 
            return $this->failWithStatus($ret);
184
 
        }
185
 
 
186
 
        GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
187
 
        /* Perform the request and verify that we succeeded */
188
 
        $results = $this->handleRequest();
189
 
        $this->assertEquals(array('redirect' => array('view' => 'cart.ViewCart'),
190
 
                                  'status' => array(),
191
 
                                  'error' => array()),
192
 
                            $results);
193
 
 
194
 
        /* Verify our cart */
195
 
        list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
196
 
        if ($ret) {
197
 
            return $this->failWithStatus($ret);
198
 
        }
199
 
 
200
 
        $this->assertEquals(array($item[0]->getId() => 2,
201
 
                                  $item[1]->getId() => 2),
202
 
                            $cartItemCounts);
203
 
    }
204
 
 
205
 
    function testAddNoViewPermissionItem() {
206
 
        /* Create a test item */
207
 
        list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId());
208
 
        if ($ret) {
209
 
            return $this->failWithStatus($ret);
210
 
        }
211
 
        $ret = GalleryCoreApi::removeUserPermission($item->getId(), $this->_userId, 'core.viewAll');
212
 
        if ($ret) {
213
 
            return $this->failWithStatus($ret);
214
 
        }
215
 
 
216
 
        GalleryUtilities::putRequestVariable('itemId', $item->getId());
217
 
        /* Perform the request and verify that we failed */
218
 
        $results = $this->handleRequest(ERROR_PERMISSION_DENIED);
219
 
 
220
 
        /* Verify our cart */
221
 
        list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
222
 
        if ($ret) {
223
 
            return $this->failWithStatus($ret);
224
 
        }
225
 
        $this->assertEquals(array(1 => 3, 2 => 3), $cartItemCounts);
226
 
    }
227
 
 
228
 
    function testAddNoCartPermissionItem() {
229
 
        /* Create a test item */
230
 
        list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId());
231
 
        if ($ret) {
232
 
            return $this->failWithStatus($ret);
233
 
        }
234
 
        $ret = GalleryCoreApi::removeUserPermission($item->getId(), $this->_userId, 'cart.add');
235
 
        if ($ret) {
236
 
            return $this->failWithStatus($ret);
237
 
        }
238
 
 
239
 
        GalleryUtilities::putRequestVariable('itemId', $item->getId());
240
 
        $results = $this->handleRequest();
241
 
        $this->assertEquals(array('redirect' => array('view' => 'cart.ViewCart'),
242
 
                                  'status' => array(),
243
 
                                  'error' => array()),
244
 
                            $results);
245
 
 
246
 
        /* Verify our cart */
247
 
        list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
248
 
        if ($ret) {
249
 
            return $this->failWithStatus($ret);
250
 
        }
251
 
        $this->assertEquals(array(1 => 3, 2 => 3), $cartItemCounts);
252
 
    }
253
 
 
254
 
    function testAddAlbumWithNoViewPermissionItem() {
255
 
        /* Create test items */
256
 
        list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId());
257
 
        if ($ret) {
258
 
            return $this->failWithStatus($ret);
259
 
        }
260
 
        $ret = GalleryCoreApi::removeUserPermission($item->getId(), $this->_userId, 'core.viewAll');
261
 
        if ($ret) {
262
 
            return $this->failWithStatus($ret);
263
 
        }
264
 
        list ($ret, $item2) = $this->_createRandomDataItem($this->_album->getId());
265
 
        if ($ret) {
266
 
            return $this->failWithStatus($ret);
267
 
        }
268
 
 
269
 
        GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
270
 
        $results = $this->handleRequest();
271
 
        $this->assertEquals(array('redirect' => array('view' => 'cart.ViewCart'),
272
 
                                  'status' => array(),
273
 
                                  'error' => array()),
274
 
                            $results);
275
 
 
276
 
        /* Verify our cart - one item added, no permission item skipped */
277
 
        list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
278
 
        if ($ret) {
279
 
            return $this->failWithStatus($ret);
280
 
        }
281
 
        $this->assertEquals(array(1 => 3, 2 => 3, $item2->getId() => 1), $cartItemCounts);
282
 
    }
283
 
 
284
 
    function testAddAlbumWithNoCartPermissionItem() {
285
 
        /* Create test items */
286
 
        list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId());
287
 
        if ($ret) {
288
 
            return $this->failWithStatus($ret);
289
 
        }
290
 
        $ret = GalleryCoreApi::removeUserPermission($item->getId(), $this->_userId, 'cart.add');
291
 
        if ($ret) {
292
 
            return $this->failWithStatus($ret);
293
 
        }
294
 
        list ($ret, $item2) = $this->_createRandomDataItem($this->_album->getId());
295
 
        if ($ret) {
296
 
            return $this->failWithStatus($ret);
297
 
        }
298
 
 
299
 
        GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
300
 
        $results = $this->handleRequest();
301
 
        $this->assertEquals(array('redirect' => array('view' => 'cart.ViewCart'),
302
 
                                  'status' => array(),
303
 
                                  'error' => array()),
304
 
                            $results);
305
 
 
306
 
        /* Verify our cart - one item added, no permission item skipped */
307
 
        list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
308
 
        if ($ret) {
309
 
            return $this->failWithStatus($ret);
310
 
        }
311
 
        $this->assertEquals(array(1 => 3, 2 => 3, $item2->getId() => 1), $cartItemCounts);
312
 
    }
313
 
}
314
 
?>