3
* Gallery - a web based photo album viewer and editor
4
* Copyright (C) 2000-2007 Bharat Mediratta
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.
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.
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.
22
* AddToCart controller tests
25
* @author Bharat Mediratta <bharat@menalto.com>
26
* @version $Revision: 15513 $
28
class AddToCartControllerTest extends GalleryControllerTestCase {
30
function AddToCartControllerTest($methodName) {
31
$this->GalleryControllerTestCase($methodName, 'cart.AddToCart');
38
/* create an album to hold all of the test items */
39
list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
41
print $ret->getAsHtml();
42
return $this->failWithStatus($ret);
44
$this->_markForCleanup($this->_album);
46
$ret = GalleryCoreApi::removeItemPermissions($this->_album->getId());
48
print $ret->getAsHtml();
49
return $this->failWithStatus($ret);
51
$ret = GalleryCoreApi::addUserPermission(
52
$this->_album->getId(), $this->_userId = $gallery->getActiveUserId(), 'core.all');
54
print $ret->getAsHtml();
55
return $this->failWithStatus($ret);
58
/* Save the existing cart */
59
list ($ret, $this->_saveCartItemCounts) = CartHelper::fetchCartItemCounts();
61
print $ret->getAsHtml();
62
return $this->failWithStatus($ret);
65
$cartItemCounts = array(1 => 3,
67
$ret = CartHelper::setCartItemCounts($cartItemCounts);
69
print $ret->getAsHtml();
70
return $this->failWithStatus($ret);
75
/* Restore the existing cart */
76
$ret = CartHelper::setCartItemCounts($this->_saveCartItemCounts);
78
$this->failWithStatus($ret);
84
function testAddItem() {
85
/* Create a test item */
86
list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId());
88
return $this->failWithStatus($ret);
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'),
100
list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
102
return $this->failWithStatus($ret);
105
$this->assertEquals(array(1 => 3,
107
$item->getId() => 1),
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());
116
return $this->failWithStatus($ret);
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'),
128
/* Verify our cart */
129
list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
131
return $this->failWithStatus($ret);
134
$this->assertEquals(array(1 => 3,
136
$item[0]->getId() => 1,
137
$item[1]->getId() => 1),
141
function testAddDuplicateItem() {
142
/* Create a test item */
143
list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId());
145
return $this->failWithStatus($ret);
148
/* Put the item in the cart */
149
$ret = CartHelper::setCartItemCounts(array($item->getId() => 1));
151
return $this->failWithStatus($ret);
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'),
162
/* Verify our cart */
163
list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
165
return $this->failWithStatus($ret);
168
$this->assertEquals(array($item->getId() => 2), $cartItemCounts);
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());
176
return $this->failWithStatus($ret);
180
/* Put the items in the cart */
181
$ret = CartHelper::setCartItemCounts(array($item[0]->getId() => 1, $item[1]->getId() => 1));
183
return $this->failWithStatus($ret);
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'),
194
/* Verify our cart */
195
list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
197
return $this->failWithStatus($ret);
200
$this->assertEquals(array($item[0]->getId() => 2,
201
$item[1]->getId() => 2),
205
function testAddNoViewPermissionItem() {
206
/* Create a test item */
207
list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId());
209
return $this->failWithStatus($ret);
211
$ret = GalleryCoreApi::removeUserPermission($item->getId(), $this->_userId, 'core.viewAll');
213
return $this->failWithStatus($ret);
216
GalleryUtilities::putRequestVariable('itemId', $item->getId());
217
/* Perform the request and verify that we failed */
218
$results = $this->handleRequest(ERROR_PERMISSION_DENIED);
220
/* Verify our cart */
221
list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
223
return $this->failWithStatus($ret);
225
$this->assertEquals(array(1 => 3, 2 => 3), $cartItemCounts);
228
function testAddNoCartPermissionItem() {
229
/* Create a test item */
230
list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId());
232
return $this->failWithStatus($ret);
234
$ret = GalleryCoreApi::removeUserPermission($item->getId(), $this->_userId, 'cart.add');
236
return $this->failWithStatus($ret);
239
GalleryUtilities::putRequestVariable('itemId', $item->getId());
240
$results = $this->handleRequest();
241
$this->assertEquals(array('redirect' => array('view' => 'cart.ViewCart'),
246
/* Verify our cart */
247
list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
249
return $this->failWithStatus($ret);
251
$this->assertEquals(array(1 => 3, 2 => 3), $cartItemCounts);
254
function testAddAlbumWithNoViewPermissionItem() {
255
/* Create test items */
256
list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId());
258
return $this->failWithStatus($ret);
260
$ret = GalleryCoreApi::removeUserPermission($item->getId(), $this->_userId, 'core.viewAll');
262
return $this->failWithStatus($ret);
264
list ($ret, $item2) = $this->_createRandomDataItem($this->_album->getId());
266
return $this->failWithStatus($ret);
269
GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
270
$results = $this->handleRequest();
271
$this->assertEquals(array('redirect' => array('view' => 'cart.ViewCart'),
276
/* Verify our cart - one item added, no permission item skipped */
277
list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
279
return $this->failWithStatus($ret);
281
$this->assertEquals(array(1 => 3, 2 => 3, $item2->getId() => 1), $cartItemCounts);
284
function testAddAlbumWithNoCartPermissionItem() {
285
/* Create test items */
286
list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId());
288
return $this->failWithStatus($ret);
290
$ret = GalleryCoreApi::removeUserPermission($item->getId(), $this->_userId, 'cart.add');
292
return $this->failWithStatus($ret);
294
list ($ret, $item2) = $this->_createRandomDataItem($this->_album->getId());
296
return $this->failWithStatus($ret);
299
GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
300
$results = $this->handleRequest();
301
$this->assertEquals(array('redirect' => array('view' => 'cart.ViewCart'),
306
/* Verify our cart - one item added, no permission item skipped */
307
list ($ret, $cartItemCounts) = CartHelper::fetchCartItemCounts();
309
return $this->failWithStatus($ret);
311
$this->assertEquals(array(1 => 3, 2 => 3, $item2->getId() => 1), $cartItemCounts);