testAddressBookWithReadonlyFieldForCountry()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 37
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 51
rs 9.328

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverShop\Tests\Page;
4
5
use SilverShop\Model\Address;
6
use SilverShop\Page\AccountPage;
7
use SilverShop\Page\AccountPageController;
8
use SilverShop\Tests\ShopTestControllerExtension;
9
use SilverStripe\Control\Controller;
10
use SilverStripe\Control\Director;
11
use SilverStripe\Control\HTTPRequest;
12
use SilverStripe\Dev\FunctionalTest;
13
use SilverStripe\ORM\DataObject;
14
use SilverStripe\Security\Member;
15
use SilverStripe\Security\Security;
16
use SilverStripe\SiteConfig\SiteConfig;
17
use SilverStripe\View\SSViewer;
18
19
class AccountPageTest extends FunctionalTest
20
{
21
    protected static $fixture_file = [
22
        __DIR__ . '/../Fixtures/Pages.yml',
23
        __DIR__ . '/../Fixtures/shop.yml',
24
    ];
25
    protected static $disable_theme = true;
26
    protected static $use_draft_site = true;
27
28
    /**
29
     * @var AccountPage
30
     */
31
    protected $accountpage;
32
33
    /**
34
     * @var AccountPageController
35
     */
36
    protected $controller;
37
38
    public function setUp(): void
39
    {
40
        parent::setUp();
41
42
        Controller::add_extension(ShopTestControllerExtension::class);
43
        $this->accountpage = $this->objFromFixture(AccountPage::class, "accountpage");
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->objFromFixture(Si...::class, 'accountpage') of type SilverStripe\ORM\DataObject is incompatible with the declared type SilverShop\Page\AccountPage of property $accountpage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
45
        $this->controller = new AccountPageController($this->accountpage);
46
47
        $r = new HTTPRequest('GET', '/');
48
        $r->setSession($this->session());
49
50
        $this->controller->setRequest($r);
51
    }
52
53
    public function testCanViewAccountPage()
54
    {
55
        $page = $this->get("account/");  // attempt to access the Account Page
56
        $this->assertEquals(200, $page->getStatusCode(), "a page should load");
57
        $this->assertTrue(
58
            $page->getHeader('X-TestPageClass') == Security::class && $page->getHeader('X-TestPageAction') == 'login',
59
            'Need to login before accessing the account page'
60
        );
61
62
        // login using form
63
        $this->submitForm(
64
            "MemberLoginForm_LoginForm",
65
            "action_doLogin",
66
            [
67
                'Email' => '[email protected]',
68
                'Password' => '23u90oijlJKsa',
69
            ]
70
        );
71
72
        $page = $this->get("account/");  // try accessing the account page again
73
        $this->assertEquals(200, $page->getStatusCode(), "a page should load");
74
75
        $this->assertEquals(AccountPageController::class, $page->getHeader('X-TestPageClass'), "Account Page should open");
76
    }
77
78
    public function testGlobals()
79
    {
80
        $this->assertFalse($this->accountpage->canCreate(), "account page exists");
81
        $this->assertEquals(Director::baseURL() . "account/", AccountPage::find_link());
82
        $this->assertEquals(Director::baseURL() . "account/order/10", AccountPage::get_order_link(10));
83
    }
84
85
    public function testAddressBook()
86
    {
87
        $member = $this->objFromFixture(Member::class, "joebloggs");
88
        $this->logInAs($member);
89
90
        $address = $this->objFromFixture(Address::class, "foobar");
91
        $address->MemberID = $member->ID;
92
        $address->write();
93
94
        $this->controller->init();
95
        $forms = $this->controller->addressbook();
96
        $createform = $forms['CreateAddressForm'];
97
        $defaultform = $forms['DefaultAddressForm'];
98
        $this->assertTrue($member->AddressBook()->exists());
99
100
        $this->assertTrue((boolean)$createform, "Create form exists");
101
        $this->assertTrue((boolean)$defaultform, "Default form exists");
102
103
        //$this->controller->saveaddresses($data, $createform);
104
        //$this->controller->savedefaultaddresses($data, $defaultform);
105
        $this->markTestIncomplete("save address and save default");
106
    }
107
108
    public function testAddressBookWithDropdownFieldToSelectCountry()
109
    {
110
        $this->useTestTheme(realpath(__DIR__ . '/../'), 'shoptest', function () {
111
            $member = $this->objFromFixture(Member::class, "joebloggs");
112
            $this->logInAs($member);
113
114
            // Open Address Book page
115
            $page = $this->get("account/addressbook/"); // goto address book page
116
            $this->assertEquals(200, $page->getStatusCode(), "a page should load");
117
            $this->assertEquals(AccountPageController::class, $page->getHeader('X-TestPageClass'), "Account page should open");
118
            $this->assertEquals('addressbook', $page->getHeader('X-TestPageAction'), "Account addressbook should open");
119
120
            // Create an address
121
            $data = [
122
                "Country" => "AU",
123
                "Address" => "Sydney Opera House",
124
                "AddressLine2" => "Bennelong Point",
125
                "City" => "Sydney",
126
                "State" => "NSW",
127
                "PostalCode" => "2000",
128
                "Phone" => "1234 5678",
129
            ];
130
            $this->submitForm("Form_CreateAddressForm", "action_saveaddress", $data);
131
            $this->assertEquals(200, $page->getStatusCode(), "a page should load");
132
133
            $au_address = Address::get()->filter('PostalCode', '2000')->sort('ID')->last();
134
            $this->assertEquals(
135
                "AU",
136
                $au_address->Country,
137
                "New address successfully saved, using dropdown to select the country"
138
            );
139
            $this->assertEquals(
140
                "Sydney Opera House",
141
                $au_address->Address,
142
                "Ensure that the Address is the Sydney Opera House"
143
            );
144
        });
145
    }
146
147
    public function testAddressBookWithReadonlyFieldForCountry()
148
    {
149
        $this->useTestTheme(realpath(__DIR__ . '/../'), 'shoptest', function () {
150
            $member = $this->objFromFixture(Member::class, "joebloggs");
151
            $this->logInAs($member);
152
153
            // setup a single-country site
154
            $siteconfig = DataObject::get_one(SiteConfig::class);
155
            $siteconfig->AllowedCountries = '["NZ"]';
156
            $siteconfig->write();
157
            $singlecountry = SiteConfig::current_site_config();
158
            $this->assertEquals(
159
                "NZ",
160
                $singlecountry->getSingleCountry(),
161
                "Confirm that the website is setup as a single country site"
162
            );
163
164
            // Open the Address Book page to test form submission with a readonly field
165
            $page = $this->get("account/addressbook/"); // goto address book page
166
            $this->assertEquals(200, $page->getStatusCode(), "a page should load");
167
            $this->assertStringContainsString(
168
                "Form_CreateAddressForm_Country_readonly",
169
                $page->getBody(),
170
                "The Country field is readonly"
171
            );
172
            $this->assertStringNotContainsString(
173
                "<option value=\"NZ\">New Zealand</option>",
174
                $page->getBody(),
175
                "Dropdown field is not shown"
176
            );
177
178
            // Create an address
179
            $data = [
180
                "Address" => "234 Hereford Street",
181
                "City" => "Christchurch",
182
                "State" => "Canterbury",
183
                "PostalCode" => "8011",
184
            ];
185
            $this->submitForm("Form_CreateAddressForm", "action_saveaddress", $data);
186
            $this->assertEquals(200, $page->getStatusCode(), "a page should load");
187
188
            $nz_address = Address::get()->filter('PostalCode', '8011')->sort('ID')->last();
189
            $this->assertEquals(
190
                "NZ",
191
                $nz_address->Country,
192
                "New address successfully saved; even with a Country readonly field in the form"
193
            );
194
            $this->assertEquals(
195
                "234 Hereford Street",
196
                $nz_address->Address,
197
                "Ensure that the Address is 234 Hereford Street"
198
            );
199
        });
200
    }
201
202
    public function testEditProfile()
203
    {
204
        //$this->controller->editprofile();
205
        //$this->controller->EditAccountForm();
206
        //$this->controller->ChangePasswordForm();
207
        $this->markTestIncomplete("Add some assertions");
208
    }
209
}
210