Completed
Push — master ( 3c661d...2a57f9 )
by Will
26s queued 12s
created

php/Extension/SteppedCheckoutExtensionTest.php (5 issues)

1
<?php
2
3
namespace SilverShop\Tests\Extension;
4
5
use SilverShop\Cart\ShoppingCart;
6
use SilverShop\Checkout\Checkout;
7
use SilverShop\Extension\MemberExtension;
8
use SilverShop\Extension\SteppedCheckoutExtension;
9
use SilverShop\Forms\PaymentForm;
10
use SilverShop\Model\Order;
11
use SilverShop\Page\CheckoutPage;
12
use SilverShop\Page\CheckoutPageController;
13
use SilverShop\Page\Product;
14
use SilverShop\Tests\Model\Product\CustomProduct_OrderItem;
15
use SilverShop\Tests\ShopTest;
16
use SilverStripe\Control\Controller;
17
use SilverStripe\Control\Director;
18
use SilverStripe\Control\HTTPRequest;
19
use SilverStripe\Core\Config\Config;
20
use SilverStripe\Dev\FunctionalTest;
21
use SilverStripe\Omnipay\GatewayInfo;
22
use SilverStripe\Security\Member;
23
use SilverStripe\Security\Security;
24
25
class SteppedCheckoutExtensionTest extends FunctionalTest
26
{
27
    protected static $fixture_file = [
28
        __DIR__ . '/../Fixtures/Pages.yml',
29
        __DIR__ . '/../Fixtures/shop.yml',
30
    ];
31
32
    // This seems to be required, because we query the OrderItem table and thus this gets included…
33
    // TODO: Remove once we figure out how to circumvent that…
34
    protected static $extra_dataobjects = [
35
        CustomProduct_OrderItem::class,
36
    ];
37
38
    protected static $use_draft_site = true; //so we don't need to publish
39
    protected $autoFollowRedirection = false;
40
41
    /**
42
     * @var CheckoutPageController
43
     */
44
    protected $checkout;
45
46
    /**
47
     * @var Product
48
     */
49
    protected $socks;
50
51
    /**
52
     * @var Order
53
     */
54
    protected $cart;
55
56
    public function setUp()
57
    {
58
        parent::setUp();
59
        $this->logInWithPermission('ADMIN');
60
61
        Config::modify()->merge(GatewayInfo::class, 'Dummy', [
62
            'is_offsite' => false
63
        ]);
64
65
        ShopTest::setConfiguration();
66
        ShoppingCart::singleton()->clear();
67
        //set up steps
68
        SteppedCheckoutExtension::setupSteps(); //use default steps
69
70
        $this->socks = $this->objFromFixture(Product::class, "socks");
71
        $this->socks->publishSingle();
72
73
        /**
74
         * @var CheckoutPage $checkoutpage
75
         */
76
        $checkoutpage = $this->objFromFixture(CheckoutPage::class, "checkout");
77
        $checkoutpage->publishSingle();
78
        $this->checkout = CheckoutPageController::create($checkoutpage);
79
80
        $this->cart = $this->objFromFixture(Order::class, "cart");
81
        ShoppingCart::singleton()->setCurrent($this->cart);
82
    }
83
84
    public function testTemplateFunctionsForFirstStep()
85
    {
86
        //put us at the first step index == membership
87
        $this->checkout->handleRequest($this->buildTestRequest(''));
88
89
        $this->assertTrue($this->checkout->StepExists('membership'));
90
        $this->assertFalse($this->checkout->IsPastStep('membership'));
91
        $this->assertTrue($this->checkout->IsCurrentStep('membership'));
92
        $this->assertFalse($this->checkout->IsFutureStep('membership'));
93
94
        $this->checkout->NextStepLink();
95
96
        $this->assertTrue($this->checkout->StepExists('contactdetails'));
97
        $this->assertFalse($this->checkout->IsPastStep('contactdetails'));
98
        $this->assertFalse($this->checkout->IsCurrentStep('contactdetails'));
99
        $this->assertTrue($this->checkout->IsFutureStep('contactdetails'));
100
    }
101
102
    public function testTemplateFunctionsForOtherSteps()
103
    {
104
        $this->checkout->handleRequest($this->buildTestRequest('summary')); //change to summary step
105
        $this->assertTrue($this->checkout->StepExists('summary'));
106
        $this->assertFalse($this->checkout->IsPastStep('summary'));
107
        $this->assertTrue($this->checkout->IsCurrentStep('summary'));
108
        $this->assertFalse($this->checkout->IsFutureStep('summary'));
109
110
        $this->assertFalse($this->checkout->StepExists('nosuchstep'));
111
    }
112
113
    public function testMembershipStep()
114
    {
115
        $this->logOut();
116
        //this should still work if there is no cart
117
        ShoppingCart::singleton()->clear();
118
        /*
119
        $this->checkout->index();
120
        $this->checkout->membership();
121
        $this->post('/checkout/guestcontinue', array()); //redirect to next step
122
        $this->checkout->handleRequest($this->buildTestRequest('checkout/createaccount'));
123
        */
124
        $form = $this->checkout->MembershipForm();
125
        $data = array();
126
        $form->loadDataFrom($data);
127
128
        $data = array(
129
            'FirstName' => 'Michael',
130
            'Surname' => 'Black',
131
            'Email' => '[email protected]',
132
            'Password' => array(
133
                '_Password' => 'pass1234',
134
                '_ConfirmPassword' => 'pass1234',
135
            ),
136
            'action_docreateaccount' => 'Create New Account',
137
        );
138
        $response = $this->post('/checkout/CreateAccountForm', $data); //redirect to next step
0 ignored issues
show
The assignment to $response is dead and can be removed.
Loading history...
139
140
        $member = MemberExtension::get_by_identifier("[email protected]");
141
        $this->assertTrue((boolean)$member, "Check new account was created");
142
        $this->assertEquals('Michael', $member->FirstName);
143
        $this->assertEquals('Black', $member->Surname);
144
    }
145
146
    public function testContactDetails()
147
    {
148
        $user = $this->objFromFixture(Member::class, "joebloggs");
149
        Security::setCurrentUser($user);
150
        $this->checkout->handleRequest($this->buildTestRequest('contactdetails'));
151
        $data = array(
152
            'FirstName' => 'Pauline',
153
            'Surname' => 'Richardson',
154
            'Email' => '[email protected]',
155
            'action_setcontactdetails' => 1,
156
        );
157
        $response = $this->post('/checkout/ContactDetailsForm', $data);
0 ignored issues
show
The assignment to $response is dead and can be removed.
Loading history...
158
159
        $this->markTestIncomplete('check order has been updated');
160
    }
161
162
    public function testShippingAddress()
163
    {
164
        $user = $this->objFromFixture(Member::class, "joebloggs");
165
        Security::setCurrentUser($user);
166
        $this->checkout->handleRequest($this->buildTestRequest('shippingaddress'));
167
        $data = array(
168
            'Address' => '2b Baba place',
169
            'AddressLine2' => 'Level 2',
170
            'City' => 'Newton',
171
            'State' => 'Wellington',
172
            'Country' => 'NZ',
173
            'action_setaddress' => 1,
174
        );
175
        $response = $this->post('/checkout/AddressForm', $data);
0 ignored issues
show
The assignment to $response is dead and can be removed.
Loading history...
176
177
        $this->markTestIncomplete('assertions!');
178
    }
179
180
    public function testBillingAddress()
181
    {
182
        $user = $this->objFromFixture(Member::class, "joebloggs");
183
        Security::setCurrentUser($user);
184
        $this->checkout->handleRequest($this->buildTestRequest('billingaddress'));
185
        $data = array(
186
            'Address' => '3 Art Cresent',
187
            'AddressLine2' => '',
188
            'City' => 'Walkworth',
189
            'State' => 'New Caliphoneya',
190
            'Country' => 'ZA',
191
            'action_setbillingaddress' => 1,
192
        );
193
        $response = $this->post('/checkout/AddressForm', $data);
0 ignored issues
show
The assignment to $response is dead and can be removed.
Loading history...
194
195
        $this->markTestIncomplete('assertions!');
196
    }
197
198
    public function testPaymentMethod()
199
    {
200
        $data = array(
201
            'PaymentMethod' => 'Dummy',
202
            'action_setpaymentmethod' => 1,
203
        );
204
        $response = $this->post('/checkout/PaymentMethodForm', $data);
0 ignored issues
show
The assignment to $response is dead and can be removed.
Loading history...
205
        $this->assertEquals('Dummy', Checkout::get($this->cart)->getSelectedPaymentMethod());
206
    }
207
208
    public function testSummary()
209
    {
210
        $this->checkout->handleRequest($this->buildTestRequest('summary'));
211
        /**
212
         * @var PaymentForm $form
213
         */
214
        $form = $this->checkout->ConfirmationForm();
215
        $data = array(
216
            'Notes' => 'Leave it around the back',
217
            'ReadTermsAndConditions' => 1,
218
        );
219
        $member = $this->objFromFixture(Member::class, "joebloggs");
220
        Security::setCurrentUser($member);
221
222
        Checkout::get($this->cart)->setPaymentMethod("Dummy"); //a selected payment method is required
223
        $form->loadDataFrom($data);
224
        $this->assertTrue($form->validationResult()->isValid(), "Checkout data is valid");
225
        $response = $this->post('/checkout/ConfirmationForm', $data);
226
        $this->assertEquals('Cart', $this->cart->Status, "Order is still in cart");
227
228
        $order = Order::get()->byID($this->cart->ID);
229
230
        $this->assertEquals("Leave it around the back", $order->Notes);
231
232
        //redirect to make payment
233
        $this->assertEquals(302, $response->getStatusCode());
234
        $this->assertContains(
235
            '/checkout/payment',
236
            $response->getHeader('location')
237
        );
238
    }
239
240
    protected function buildTestRequest($url, $method = 'GET')
241
    {
242
        $request = new HTTPRequest($method, $url);
243
        $request->setSession($this->mainSession->session());
244
        return $request;
245
    }
246
}
247