Completed
Push — master ( 660d11...e1d670 )
by Franco
01:58
created

testMultipleValidationErrorsReturned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
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
    }
27
28
    /**
29
     * Test the items method of the controller
30
     */
31
    public function testItems()
32
    {
33
        /** @var DMSDocument $doc1 */
34
        $doc1 = $this->objFromFixture('DMSDocument', 'doc1');
35
        /** @var DMSDocument $doc2 */
36
        $doc2 = $this->objFromFixture('DMSDocument', 'doc2');
37
        /** @var DMSRequestItem $item */
38
        $item1 = DMSRequestItem::create()->setDocument($doc1)->setQuantity(2);
39
        $item2 = DMSRequestItem::create()->setDocument($doc2)->setQuantity(5);
40
        $this->cart->addItem($item1);
41
        $this->cart->addItem($item2);
42
        $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...
43
            'ArrayList',
44
            $this->controller->items(),
45
            'DMSDocumentCartController->Items() returned an ArrayList'
46
        );
47
        $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...
48
            2,
49
            $this->controller->items(),
50
            'DMSDocumentCartController->Items()->count() returned the requisite number of items'
51
        );
52
    }
53
54
    public function testReceiverInfo()
55
    {
56
        // Now add some info
57
        $this->cart->setReceiverInfo(array('Name' => 'Joe', 'Surname' => 'Soap'));
58
        $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...
59
    }
60
61
    public function testIsCartEmpty()
62
    {
63
        $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...
64
        /** @var DMSDocument $doc */
65
        $doc1 = $this->objFromFixture('DMSDocument', 'doc1');
66
        /** @var DMSRequestItem $item */
67
        $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...
68
        $this->cart->addItem($item1);
69
        $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...
70
    }
71
72
    public function testAdd()
73
    {
74
        $this->logInWithPermission();
75
        $doc1 = $this->objFromFixture('DMSDocument', 'doc1');
76
        // Check cart is initially empty
77
        $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...
78
        // Now call controller add
79
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 5));
80
        $request->setRouteParams(array('ID' => $doc1->ID));
81
        $this->controller->add($request);
82
        $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...
83
84
        // Do it again and assert quantity was updated
85
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7));
86
        $request->setRouteParams(array('ID' => $doc1->ID));
87
        $this->controller->add($request);
88
89
        // Test ajax
90
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1));
91
        $request->setRouteParams(array('ID' => $doc1->ID));
92
        $response = $this->controller->add($request);
93
        $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...
94
        $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...
95
96
        $item = $this->cart->getItem($doc1->ID);
97
        $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...
98
    }
99
100
101
102
    public function testDeduct()
103
    {
104
        $doc1 = $this->objFromFixture('DMSDocument', 'doc1');
105
        // Check catty is initially empty
106
        $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...
107
        // Now call controller add
108
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity'=>5));
109
        $request->setRouteParams(array('ID' => $doc1->ID));
110
        $this->controller->deduct($request);
111
        // Assert cart still empty because item doesn't exist
112
        $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...
113
        // Now add item
114
        $this->controller->add($request);
115
        $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...
116
117
118
        // Now try and deduct 2 from the items
119
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => -2));
120
        $request->setRouteParams(array('ID' => $doc1->ID));
121
        $this->controller->deduct($request);
122
123
        $item = $this->cart->getItem($doc1->ID);
124
        $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...
125
126
        // Test ajax
127
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1));
128
        $request->setRouteParams(array('ID' => $doc1->ID));
129
        $response = $this->controller->deduct($request);
130
        $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...
131
        $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...
132
    }
133
134
    public function testRemove()
135
    {
136
        $doc1 = $this->objFromFixture('DMSDocument', 'doc1');
137
        // Check catty is initially empty
138
        $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...
139
        // Now call controller add
140
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity'=>5));
141
        $request->setRouteParams(array('ID' => $doc1->ID));
142
        // Now add item
143
        $this->controller->add($request);
144
        $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...
145
        $this->controller->remove($request);
146
        $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...
147
148
        // Test ajax
149
        $request = new SS_HTTPRequest('POST', '', array(), array('quantity' => 7, 'ajax' => 1));
150
        $request->setRouteParams(array('ID' => $doc1->ID));
151
        $response = $this->controller->remove($request);
152
        $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...
153
        $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...
154
    }
155
156
    /**
157
     * Ensure that a validation error is shown when requesting to add more of a document that is allowed
158
     */
159
    public function testCannotAddMoreThanSuggestedQuantityOfItem()
160
    {
161
        $document = $this->objFromFixture('DMSDocument', 'limited_supply');
162
        $result = $this->get('/documentcart/add/' . $document->ID . '?quantity=5&ajax=1');
163
        $this->assertContains(
164
            'Maximum of 3 documents exceeded for \"Doc3\", please select a lower quantity.',
165
            (string) $result->getBody()
166
        );
167
    }
168
169
    /**
170
     * Ensure that multiple validation errors are returned in the failure message, if any
171
     */
172
    public function testMultipleValidationErrorsReturned()
173
    {
174
        $document1 = $this->objFromFixture('DMSDocument', 'limited_supply');
175
        $document2 = $this->objFromFixture('DMSDocument', 'very_limited_supply');
176
177
        $this->cart
178
            ->addItem(DMSRequestItem::create($document1))
179
            ->addItem(DMSRequestItem::create($document2));
180
181
        $input = array('ItemQuantity' => array(
182
            $document1->ID => 15000,
183
            $document2->ID => 12000
184
        ));
185
186
        $form = Form::create($this->controller, '', new FieldList, new FieldList);
187
        $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...
188
189
        $this->assertContains('Maximum of 3 documents exceeded for "Doc3"', $form->Message());
190
        $this->assertContains('Maximum of 2 documents exceeded for "Doc5"', $form->Message());
191
    }
192
193
    /**
194
     * Ensure that when a document that cannot be added to the cart is added to the cart, a validation error is
195
     * returned
196
     */
197
    public function testValidationErrorReturnedOnInvalidAdd()
198
    {
199
        $document = $this->objFromFixture('DMSDocument', 'not_allowed_in_cart');
200
        $result = $this->get('/documentcart/add/' . $document->ID . '?ajax=1');
201
        $this->assertContains('You are not allowed to add this document', (string) $result->getBody());
202
    }
203
204
    /**
205
     * Tests whether the cart items are updated from the controller
206
     */
207
    public function testUpdateCartItems()
208
    {
209
        $doc = $this->objFromFixture('DMSDocument', 'doc1');
210
        /** @var DMSRequestItem $item */
211
        $item = DMSRequestItem::create()->setDocument($doc)->setQuantity(2);
0 ignored issues
show
Documentation introduced by
$doc 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...
212
        $this->cart->addItem($item);
213
214
        $invalidQuantities = array(
215
            'ItemQuantity' => array($doc->ID => 'non-numeric')
216
        );
217
218
        $sameQuantities = array(
219
            'ItemQuantity' => array($doc->ID => 2)
220
        );
221
222
        $updatedQuantities = array(
223
            'ItemQuantity' => array($doc->ID => 5),
224
        );
225
        $form = Form::create(
226
            $this->controller,
227
            'Test',
228
            FieldList::create(),
229
            FieldList::create()
230
        );
231
        $request = new SS_HTTPRequest('POST', '');
232
        // Test invalids leave it unchanged
233
        $response = $this->controller->updateCartItems($invalidQuantities, $form, $request);
234
        $this->assertEquals(2, $this->cart->getItem($item->getItemId())->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...
235
236
        // Test quantity remains the same
237
        $response->removeHeader('Location');
238
        $response =$this->controller->updateCartItems($sameQuantities, $form, $request);
239
        $this->assertEquals(2, $this->cart->getItem($item->getItemId())->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...
240
241
        $response->removeHeader('Location');
242
        $response = $this->controller->updateCartItems($updatedQuantities, $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...
243
        $this->assertEquals(5, $this->cart->getItem($item->getItemId())->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...
244
    }
245
246
    /**
247
     * Tests DMSCartEditForm form has a FieldList
248
     */
249
    public function testDMSCartEditForm()
250
    {
251
        $form = $this->controller->DMSCartEditForm();
252
        $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...
253
    }
254
255
    /**
256
     * Tests if the DMSCartEditForm is extensible
257
     */
258
    public function testDMSCartEditFormIsExtensible()
259
    {
260
        $controller = $this->controller;
261
        $form = $controller->DMSCartEditForm();
262
        $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...
263
            $form->Fields()->fieldByName('NewTextField'),
264
            'DMSDocumentRequestForm() is extensible as it included the field from the extension'
265
        );
266
    }
267
268
    /**
269
     * Tests that the cart summary view is returned.
270
     */
271
    public function testView()
272
    {
273
        $result = $this->get('documentcart/view');
274
        $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...
275
        $this->assertContains('Updating cart items', $result->getBody());
276
    }
277
278
    /**
279
     * Ensure the link is "friendly", not a class name
280
     */
281
    public function testLink()
282
    {
283
        $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...
284
        $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...
285
    }
286
}
287