|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverCommerce\Postage\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Director; |
|
6
|
|
|
use SilverStripe\Control\Controller; |
|
7
|
|
|
use SilverStripe\Core\Config\Config; |
|
8
|
|
|
use SilverStripe\Dev\FunctionalTest; |
|
9
|
|
|
use SilverStripe\Forms\DropdownField; |
|
10
|
|
|
use SilverStripe\Forms\ReadonlyField; |
|
11
|
|
|
use SilverStripe\Forms\OptionsetField; |
|
12
|
|
|
use SilverCommerce\GeoZones\Model\Zone; |
|
13
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
|
14
|
|
|
use SilverCommerce\GeoZones\Model\Region; |
|
15
|
|
|
use SilverCommerce\Postage\Forms\PostageForm; |
|
16
|
|
|
use SilverCommerce\GeoZones\Forms\RegionSelectionField; |
|
17
|
|
|
use SilverCommerce\Postage\Tests\Model\ExtendableObject; |
|
18
|
|
|
use SilverCommerce\Postage\Tests\Control\PostageController; |
|
19
|
|
|
use SilverCommerce\Postage\Helpers\Parcel; |
|
20
|
|
|
use SilverCommerce\Postage\Helpers\PostageOption; |
|
21
|
|
|
|
|
22
|
|
|
class PostageFormTest extends FunctionalTest |
|
23
|
|
|
{ |
|
24
|
|
|
protected static $fixture_file = 'PostageFormTest.yml'; |
|
25
|
|
|
|
|
26
|
|
|
protected static $extra_dataobjects = [ |
|
27
|
|
|
ExtendableObject::class |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
public function setUp() |
|
31
|
|
|
{ |
|
32
|
|
|
parent::setUp(); |
|
33
|
|
|
Config::inst()->set(Region::class, "create_on_build", false); |
|
|
|
|
|
|
34
|
|
|
Director::config()->set( |
|
35
|
|
|
"rules", |
|
36
|
|
|
[ |
|
37
|
|
|
'postagetest//$Action/$ID/$OtherID' => PostageController::class |
|
38
|
|
|
] |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Test that the form generates dropdowns when no country provided |
|
44
|
|
|
*/ |
|
45
|
|
|
public function testDropdowns() |
|
46
|
|
|
{ |
|
47
|
|
|
$object = $this->objFromFixture(ExtendableObject::class, "uk"); |
|
48
|
|
|
|
|
49
|
|
|
$form = PostageForm::create( |
|
50
|
|
|
Controller::curr(), |
|
51
|
|
|
"PostageForm", |
|
52
|
|
|
$object, |
|
53
|
|
|
$object->SubTotal, |
|
54
|
|
|
$object->TotalWeight, |
|
55
|
|
|
$object->TotalItems |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
$this->assertInstanceOf( |
|
59
|
|
|
DropdownField::class, |
|
60
|
|
|
$form->Fields()->dataFieldByName("Country") |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertInstanceOf( |
|
64
|
|
|
RegionSelectionField::class, |
|
65
|
|
|
$form->Fields()->dataFieldByName("Region") |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Test that the form generates a correct list of postage |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testFixed() |
|
73
|
|
|
{ |
|
74
|
|
|
$object = $this->objFromFixture(ExtendableObject::class, "uk"); |
|
75
|
|
|
|
|
76
|
|
|
$form = PostageForm::create( |
|
77
|
|
|
Controller::curr(), |
|
78
|
|
|
"PostageForm", |
|
79
|
|
|
$object, |
|
80
|
|
|
$object->SubTotal, |
|
81
|
|
|
$object->TotalWeight, |
|
82
|
|
|
$object->TotalItems, |
|
83
|
|
|
$object->DeliveryCountry, |
|
84
|
|
|
$object->DeliveryCounty |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertInstanceOf( |
|
88
|
|
|
OptionsetField::class, |
|
89
|
|
|
$form->Fields()->dataFieldByName("PostageKey") |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Test that the form generates a no postage error |
|
96
|
|
|
*/ |
|
97
|
|
|
public function testNoPostage() |
|
98
|
|
|
{ |
|
99
|
|
|
$object = $this->objFromFixture(ExtendableObject::class, "us"); |
|
100
|
|
|
|
|
101
|
|
|
$form = PostageForm::create( |
|
102
|
|
|
Controller::curr(), |
|
103
|
|
|
"PostageForm", |
|
104
|
|
|
$object, |
|
105
|
|
|
$object->SubTotal, |
|
106
|
|
|
$object->TotalWeight, |
|
107
|
|
|
$object->TotalItems, |
|
108
|
|
|
$object->DeliveryCountry, |
|
109
|
|
|
$object->DeliveryCounty |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
$field = $form->Fields()->dataFieldByName("NoPostage"); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertInstanceOf(ReadonlyField::class, $field); |
|
115
|
|
|
$this->assertEquals( |
|
116
|
|
|
"Unfortunatley we cannot post to this location", |
|
117
|
|
|
$field->Value() |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Test that the form updated the object with the new postage |
|
123
|
|
|
* |
|
124
|
|
|
*/ |
|
125
|
|
|
public function testObjectChanged() |
|
126
|
|
|
{ |
|
127
|
|
|
$object = $this->objFromFixture(ExtendableObject::class, "uk"); |
|
128
|
|
|
$id = $object->ID; |
|
129
|
|
|
$page = $this->get('postagetest/index/' . $id); |
|
130
|
|
|
$postage_key = $object->getPostage()->getKey(); |
|
131
|
|
|
|
|
132
|
|
|
// Page should load.. |
|
133
|
|
|
$this->assertEquals(200, $page->getStatusCode()); |
|
134
|
|
|
|
|
135
|
|
|
// Submit the postage form |
|
136
|
|
|
$result = $this->submitForm( |
|
|
|
|
|
|
137
|
|
|
"PostageForm_PostageForm", |
|
138
|
|
|
null, |
|
139
|
|
|
["PostageKey" => $postage_key] |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
// Reload the page and check result |
|
143
|
|
|
$page = $this->get('postagetest/index/' . $id); |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
// Check the rendered key |
|
146
|
|
|
$this->assertExactMatchBySelector("p.postage-key", [ |
|
147
|
|
|
$postage_key |
|
148
|
|
|
]); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|