Completed
Push — master ( e1d670...b78fb6 )
by Franco
15s
created

testEmptyCartShowsNoticeOnCheckout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * Class DMSDocumentCartControllerTest contains all the tests for {@link DMSDocumentCartController}
5
 */
6
class DMSDocumentCartControllerTest extends FunctionalTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    protected static $fixture_file = 'dms-cart/tests/DMSDocumentCartTest.yml';
9
10
    /**
11
     * @var DMSDocumentCartController
12
     */
13
    protected $controller;
14
15
    /**
16
     * @var DMSDocumentCart
17
     */
18
    protected $cart;
19
20
    public function setUp()
21
    {
22
        parent::setUp();
23
        DMSDocumentCartController::add_extension('StubDMSDocumentCheckoutPageExtension');
24
        $this->controller = DMSDocumentCartController::create();
25
        $this->cart = $this->controller->getCart();
26
        $this->cart->emptyCart();
27
    }
28
29
    /**
30
     * Test the items method of the controller
31
     */
32
    public function testItems()
33
    {
34
        /** @var DMSDocument $doc1 */
35
        $doc1 = $this->objFromFixture('DMSDocument', 'doc1');
36
        /** @var DMSDocument $doc2 */
37
        $doc2 = $this->objFromFixture('DMSDocument', 'doc2');
38
        /** @var DMSRequestItem $item */
39
        $item1 = DMSRequestItem::create()->setDocument($doc1)->setQuantity(2);
40
        $item2 = DMSRequestItem::create()->setDocument($doc2)->setQuantity(5);
41
        $this->cart->addItem($item1);
42
        $this->cart->addItem($item2);
43
        $this->assertInstanceOf(
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
            'ArrayList',
45
            $this->controller->items(),
46
            'DMSDocumentCartController->Items() returned an ArrayList'
47
        );
48
        $this->assertCount(
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
            2,
50
            $this->controller->items(),
51
            'DMSDocumentCartController->Items()->count() returned the requisite number of items'
52
        );
53
    }
54
55
    public function testReceiverInfo()
56
    {
57
        // Now add some info
58
        $this->cart->setReceiverInfo(array('Name' => 'Joe', 'Surname' => 'Soap'));
59
        $this->assertCount(2, $this->controller->getReceiverInfo());
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
    }
61
62
    public function testIsCartEmpty()
63
    {
64
        $this->assertTrue($this->controller->getIsCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
        /** @var DMSDocument $doc */
66
        $doc1 = $this->objFromFixture('DMSDocument', 'doc1');
67
        /** @var DMSRequestItem $item */
68
        $item1 = DMSRequestItem::create()->setDocument($doc1)->setQuantity(2);
0 ignored issues
show
Documentation introduced by
$doc1 is of type object<DataObject>|null, but the function expects a object<DMSDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
        $this->cart->addItem($item1);
70
        $this->assertFalse($this->controller->getIsCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
    }
72
73
    public function testAdd()
74
    {
75
        $this->logInWithPermission();
76
        $doc1 = $this->objFromFixture('DMSDocument', 'doc1');
77
        // Check cart is initially empty
78
        $this->assertTrue($this->controller->getIsCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
        // Now call controller add
80
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 5));
81
        $request->setRouteParams(array('ID' => $doc1->ID));
82
        $this->controller->add($request);
83
        $this->assertFalse($this->controller->getIsCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
85
        // Do it again and assert quantity was updated
86
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7));
87
        $request->setRouteParams(array('ID' => $doc1->ID));
88
        $this->controller->add($request);
89
90
        // Test ajax
91
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1));
92
        $request->setRouteParams(array('ID' => $doc1->ID));
93
        $response = $this->controller->add($request);
94
        $this->assertTrue($request->isAjax());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
        $this->assertJson($response, 'Confirmed that an ajax call to add() responded with json JSON');
0 ignored issues
show
Bug introduced by
The method assertJson() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
97
        $item = $this->cart->getItem($doc1->ID);
98
        $this->assertEquals(19, $item->getQuantity());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
    }
100
101
102
103
    public function testDeduct()
104
    {
105
        $doc1 = $this->objFromFixture('DMSDocument', 'doc1');
106
        // Check catty is initially empty
107
        $this->assertTrue($this->controller->getIsCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
        // Now call controller add
109
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity'=>5));
110
        $request->setRouteParams(array('ID' => $doc1->ID));
111
        $this->controller->deduct($request);
112
        // Assert cart still empty because item doesn't exist
113
        $this->assertTrue($this->controller->getIsCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114
        // Now add item
115
        $this->controller->add($request);
116
        $this->assertFalse($this->controller->getIsCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
118
119
        // Now try and deduct 2 from the items
120
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => -2));
121
        $request->setRouteParams(array('ID' => $doc1->ID));
122
        $this->controller->deduct($request);
123
124
        $item = $this->cart->getItem($doc1->ID);
125
        $this->assertEquals(3, $item->getQuantity());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
127
        // Test ajax
128
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1));
129
        $request->setRouteParams(array('ID' => $doc1->ID));
130
        $response = $this->controller->deduct($request);
131
        $this->assertTrue($request->isAjax());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
        $this->assertJson($response, 'Confirmed that an ajax call to deduct() method responded with JSON');
0 ignored issues
show
Bug introduced by
The method assertJson() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
    }
134
135
    public function testRemove()
136
    {
137
        $doc1 = $this->objFromFixture('DMSDocument', 'doc1');
138
        // Check catty is initially empty
139
        $this->assertTrue($this->controller->getIsCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
140
        // Now call controller add
141
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity'=>5));
142
        $request->setRouteParams(array('ID' => $doc1->ID));
143
        // Now add item
144
        $this->controller->add($request);
145
        $this->assertFalse($this->controller->getIsCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
        $this->controller->remove($request);
147
        $this->assertTrue($this->controller->getIsCartEmpty());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
149
        // Test ajax
150
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1));
151
        $request->setRouteParams(array('ID' => $doc1->ID));
152
        $response = $this->controller->remove($request);
153
        $this->assertTrue($request->isAjax());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
154
        $this->assertJson($response, 'Confirmed that  an ajax call to remove() method responded with JSON');
0 ignored issues
show
Bug introduced by
The method assertJson() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
155
    }
156
157
    /**
158
     * Ensure that a validation error is shown when requesting to add more of a document that is allowed
159
     */
160
    public function testCannotAddMoreThanSuggestedQuantityOfItem()
161
    {
162
        $document = $this->objFromFixture('DMSDocument', 'limited_supply');
163
        $result = $this->get('/documentcart/add/' . $document->ID . '?quantity=5&ajax=1');
164
        $this->assertContains(
165
            'Maximum of 3 documents exceeded for \"Doc3\", please select a lower quantity.',
166
            (string) $result->getBody()
167
        );
168
    }
169
170
    /**
171
     * Ensure that multiple validation errors are returned in the failure message, if any
172
     */
173
    public function testMultipleValidationErrorsReturned()
174
    {
175
        $document1 = $this->objFromFixture('DMSDocument', 'limited_supply');
176
        $document2 = $this->objFromFixture('DMSDocument', 'very_limited_supply');
177
178
        $this->cart
179
            ->addItem(DMSRequestItem::create($document1))
180
            ->addItem(DMSRequestItem::create($document2));
181
182
        $input = array('ItemQuantity' => array(
183
            $document1->ID => 15000,
184
            $document2->ID => 12000
185
        ));
186
187
        $form = Form::create($this->controller, '', new FieldList, new FieldList);
188
        $result = $this->controller->updateCartItems($input, $form, new SS_HTTPRequest('POST', '/'));
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
189
190
        $this->assertContains('Maximum of 3 documents exceeded for "Doc3"', $form->Message());
191
        $this->assertContains('Maximum of 2 documents exceeded for "Doc5"', $form->Message());
192
    }
193
194
    /**
195
     * Ensure that when a document that cannot be added to the cart is added to the cart, a validation error is
196
     * returned
197
     */
198
    public function testValidationErrorReturnedOnInvalidAdd()
199
    {
200
        $document = $this->objFromFixture('DMSDocument', 'not_allowed_in_cart');
201
        $result = $this->get('/documentcart/add/' . $document->ID . '?ajax=1');
202
        $this->assertContains('You are not allowed to add this document', (string) $result->getBody());
203
    }
204
205
    /**
206
     * Tests whether the cart items are updated from the controller
207
     *
208
     * @param array $quantity
209
     * @param int|string $expectedCount
210
     * @dataProvider updateCartItemsProvider
211
     */
212
    public function testUpdateCartItems($quantity, $expectedCount)
213
    {
214
        $document = $this->objFromFixture('DMSDocument', 'doc1');
215
        $item = DMSRequestItem::create()->setDocument($document)->setQuantity(2);
0 ignored issues
show
Documentation introduced by
$document is of type object<DataObject>|null, but the function expects a object<DMSDocument>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
216
        $this->cart->addItem($item);
217
218
        $quantities = array('ItemQuantity' => array($document->ID => $quantity));
219
220
        $form = Form::create($this->controller, 'Test', FieldList::create(), FieldList::create());
221
        $request = new SS_HTTPRequest('POST', '');
222
        $response = $this->controller->updateCartItems($quantities, $form, $request);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
223
224
        $item = $this->cart->getItem($item->getItemId());
225
        if (is_numeric($quantity) && $quantity <= 0) {
226
            $this->assertFalse($item);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
227
        } else {
228
            $this->assertEquals($expectedCount, $item->getQuantity());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
229
        }
230
    }
231
232
    /**
233
     * @return array[]
234
     */
235
    public function updateCartItemsProvider()
236
    {
237
        return array(
238
            // Invalid quantity, leave it unchanged
239
            array('non-numeric', 2),
240
            // Existing quantity remains the same
241
            array(2, 2),
242
            // Valid quantity increase
243
            array(5, 5),
244
            // Zero, remove it
245
            array(0, 0),
246
            // Negative, treat as zero
247
            array(-10, 0)
248
        );
249
    }
250
251
    /**
252
     * Tests DMSCartEditForm form has a FieldList
253
     */
254
    public function testDMSCartEditForm()
255
    {
256
        $form = $this->controller->DMSCartEditForm();
257
        $this->assertInstanceOf('FieldList', $form->Fields());
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
258
    }
259
260
    /**
261
     * Tests if the DMSCartEditForm is extensible
262
     */
263
    public function testDMSCartEditFormIsExtensible()
264
    {
265
        $controller = $this->controller;
266
        $form = $controller->DMSCartEditForm();
267
        $this->assertNotNull(
0 ignored issues
show
Bug introduced by
The method assertNotNull() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
268
            $form->Fields()->fieldByName('NewTextField'),
269
            'DMSDocumentRequestForm() is extensible as it included the field from the extension'
270
        );
271
    }
272
273
    /**
274
     * Tests that the cart summary view is returned.
275
     */
276
    public function testView()
277
    {
278
        $result = $this->get('documentcart/view');
279
        $this->assertInstanceOf('SS_HTTPResponse', $result);
0 ignored issues
show
Bug introduced by
The method assertInstanceOf() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
280
        $this->assertContains('Updating cart items', $result->getBody());
281
    }
282
283
    /**
284
     * Ensure the link is "friendly", not a class name
285
     */
286
    public function testLink()
287
    {
288
        $this->assertSame('documentcart', $this->controller->Link());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
289
        $this->assertSame('documentcart/view', $this->controller->Link('view'));
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DMSDocumentCartControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
290
    }
291
292
    /**
293
     * Ensure that the user is notified when the cart is empty
294
     */
295
    public function testEmptyCartShowsNoticeOnCheckout()
296
    {
297
        $result = $this->get('checkout');
298
        $this->assertContains('Your cart is currently empty.', $result->getBody());
299
    }
300
}
301