1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Discounts\Tests; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Discounts\DiscountHelper; |
6
|
|
|
use Dynamic\Foxy\Discounts\Extension\ProductDataExtension; |
7
|
|
|
use Dynamic\Foxy\Discounts\Model\Discount; |
8
|
|
|
use Dynamic\Foxy\Discounts\Tests\TestOnly\Extension\TestDiscountExtension; |
9
|
|
|
use Dynamic\Foxy\Discounts\Tests\TestOnly\Extension\VariationDataExtension; |
10
|
|
|
use Dynamic\Foxy\Discounts\Tests\TestOnly\Page\ProductPage; |
11
|
|
|
use Dynamic\Foxy\Extension\Purchasable; |
12
|
|
|
use Dynamic\Foxy\Model\Variation; |
13
|
|
|
use Dynamic\Foxy\Model\VariationType; |
14
|
|
|
use SilverStripe\Core\Config\Config; |
15
|
|
|
use SilverStripe\Dev\SapphireTest; |
16
|
|
|
use SilverStripe\Versioned\Versioned; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class DiscountHelperTest |
20
|
|
|
* @package Dynamic\Foxy\Discounts\Tests |
21
|
|
|
*/ |
22
|
|
|
class DiscountHelperTest extends SapphireTest |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string[] |
26
|
|
|
*/ |
27
|
|
|
protected static $fixture_file = [ |
28
|
|
|
'products.yml', |
29
|
|
|
'discounts.yml', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string[] |
34
|
|
|
*/ |
35
|
|
|
protected static $extra_dataobjects = [ |
36
|
|
|
ProductPage::class, |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \string[][] |
41
|
|
|
*/ |
42
|
|
|
protected static $required_extensions = [ |
43
|
|
|
ProductPage::class => [ |
44
|
|
|
Purchasable::class, |
45
|
|
|
ProductDataExtension::class, |
46
|
|
|
], |
47
|
|
|
Discount::class => [ |
48
|
|
|
TestDiscountExtension::class, |
49
|
|
|
], |
50
|
|
|
Variation::class => [ |
51
|
|
|
VariationDataExtension::class, |
52
|
|
|
], |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string[] |
57
|
|
|
*/ |
58
|
|
|
protected static $illegal_extensions = [ |
59
|
|
|
/*Discount::class => [ |
60
|
|
|
'Dynamic\\FoxyRecipe\\Extension\\DiscountDataExtension', |
61
|
|
|
],//*/ |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
*/ |
67
|
|
|
protected function setUp() |
68
|
|
|
{ |
69
|
|
|
if (class_exists('Dynamic\\Foxy\\API\\Client\\APIClient')) { |
70
|
|
|
Config::modify()->set('Dynamic\\Foxy\\API\\Client\\APIClient', 'enable_api', false); |
71
|
|
|
} |
72
|
|
|
if (class_exists('Dynamic\\Foxy\\SingleSignOn\\Client\\CustomerClient')) { |
73
|
|
|
Config::modify()->set('Dynamic\\Foxy\\SingleSignOn\\Client\\CustomerClient', 'foxy_sso_enabled', false); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
parent::setUp(); |
77
|
|
|
|
78
|
|
|
$product = $this->objFromFixture(ProductPage::class, 'productthree'); |
79
|
|
|
$product->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
80
|
|
|
$productTwo = $this->objFromFixture(ProductPage::class, 'productfiveandvariations'); |
81
|
|
|
$productTwo->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
82
|
|
|
$discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage'); |
83
|
|
|
$discountOne->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
84
|
|
|
$discountTwo = $this->objFromFixture(Discount::class, 'tierdiscountpercentage'); |
85
|
|
|
$discountTwo->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
86
|
|
|
$discountThree = $this->objFromFixture(Discount::class, 'tierdiscountamount'); |
87
|
|
|
$discountThree->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
88
|
|
|
|
89
|
|
|
Versioned::set_stage(Versioned::LIVE); |
90
|
|
|
|
91
|
|
|
$variationOne = Variation::create(); |
92
|
|
|
$variationOne->Title = 'Variation One'; |
93
|
|
|
$variationOne->PriceModifier = 10; |
94
|
|
|
$variationOne->PriceModifierAction = 'Add'; |
95
|
|
|
$variationOne->Available = true; |
96
|
|
|
$variationOne->VariationTypeID = VariationType::get()->first()->ID; |
97
|
|
|
$variationOne->ProductID = $productTwo->ID; |
98
|
|
|
$variationOne->write(); |
99
|
|
|
|
100
|
|
|
$variationTwo = Variation::create(); |
101
|
|
|
$variationTwo->Title = 'Variation Two'; |
102
|
|
|
$variationTwo->PriceModifier = 150; |
103
|
|
|
$variationTwo->PriceModifierAction = 'Set'; |
104
|
|
|
$variationTwo->Available = true; |
105
|
|
|
$variationTwo->VariationTypeID = VariationType::get()->first()->ID; |
106
|
|
|
$variationTwo->ProductID = $productTwo->ID; |
107
|
|
|
$variationTwo->write(); |
108
|
|
|
|
109
|
|
|
$variationThree = Variation::create(); |
110
|
|
|
$variationThree->Title = 'Variation Three'; |
111
|
|
|
$variationThree->PriceModifier = 20; |
112
|
|
|
$variationThree->PriceModifierAction = 'Subtract'; |
113
|
|
|
$variationThree->Available = true; |
114
|
|
|
$variationThree->VariationTypeID = VariationType::get()->first()->ID; |
115
|
|
|
$variationThree->ProductID = $productTwo->ID; |
116
|
|
|
$variationThree->write(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testGetProduct() |
120
|
|
|
{ |
121
|
|
|
$product = $this->objFromFixture(ProductPage::class, 'productthree'); |
122
|
|
|
$helper = DiscountHelper::create($product); |
123
|
|
|
|
124
|
|
|
$this->assertEquals($product->ID, $helper->getProduct()->ID); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* |
129
|
|
|
*/ |
130
|
|
|
public function testGetDiscountedPrice() |
131
|
|
|
{ |
132
|
|
|
$product = $this->objFromFixture(ProductPage::class, 'productthree'); |
133
|
|
|
$helper = DiscountHelper::create($product); |
134
|
|
|
|
135
|
|
|
$this->assertEquals(75, $helper->getDiscountedPrice()->getValue()); |
136
|
|
|
|
137
|
|
|
$helper->setQuantity(25); |
138
|
|
|
$this->assertEquals(70, $helper->getDiscountedPrice()->getValue()); |
139
|
|
|
|
140
|
|
|
$discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage'); |
141
|
|
|
$discountOne->doUnpublish(); |
142
|
|
|
|
143
|
|
|
$helper->setDiscountTier(); |
144
|
|
|
$helper->setQuantity(1); |
145
|
|
|
$this->assertEquals(95, $helper->getDiscountedPrice()->getValue()); |
146
|
|
|
|
147
|
|
|
$helper->setQuantity(6); |
148
|
|
|
$this->assertEquals(88, $helper->getDiscountedPrice()->getValue()); |
149
|
|
|
|
150
|
|
|
$helper->setQuantity(23); |
151
|
|
|
$this->assertEquals(70, $helper->getDiscountedPrice()->getValue()); |
152
|
|
|
|
153
|
|
|
$product->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* |
158
|
|
|
*/ |
159
|
|
|
public function testGetDiscountedPriceVariation() |
160
|
|
|
{ |
161
|
|
|
$product = $this->objFromFixture(ProductPage::class, 'productfiveandvariations'); |
162
|
|
|
|
163
|
|
|
$variationOne = Variation::get()->filter('PriceModifierAction', 'Add')->first(); |
164
|
|
|
$variationTwo = Variation::get()->filter('PriceModifierAction', 'Set')->first(); |
165
|
|
|
$variationThree = Variation::get()->filter('PriceModifierAction', 'Subtract')->first(); |
166
|
|
|
|
167
|
|
|
//$110 less 25% |
168
|
|
|
$this->assertEquals( |
169
|
|
|
82.5, |
170
|
|
|
DiscountHelper::create($product, 1, $variationOne)->getDiscountedPrice()->getValue() |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
//$150 less 25% |
174
|
|
|
$this->assertEquals( |
175
|
|
|
112.5, |
176
|
|
|
DiscountHelper::create($product, 1, $variationTwo)->getDiscountedPrice()->getValue() |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
//$80 less 25% |
180
|
|
|
$this->assertEquals( |
181
|
|
|
60, |
182
|
|
|
DiscountHelper::create($product, 1, $variationThree)->getDiscountedPrice()->getValue() |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* |
188
|
|
|
*/ |
189
|
|
|
public function testGetFoxyDiscountType() |
190
|
|
|
{ |
191
|
|
|
$product = $this->objFromFixture(ProductPage::class, 'productthree'); |
192
|
|
|
$helper = DiscountHelper::create($product); |
193
|
|
|
|
194
|
|
|
$this->assertEquals('discount_quantity_percentage', $helper->getFoxyDiscountType()); |
195
|
|
|
|
196
|
|
|
$discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage'); |
197
|
|
|
$discountTwo = $this->objFromFixture(Discount::class, 'tierdiscountpercentage'); |
198
|
|
|
|
199
|
|
|
$discountOne->doUnpublish(); |
200
|
|
|
$discountTwo->doUnpublish(); |
201
|
|
|
|
202
|
|
|
$newHelper = DiscountHelper::create($product); |
203
|
|
|
|
204
|
|
|
$this->assertEquals('discount_quantity_amount', $newHelper->getFoxyDiscountType()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* |
209
|
|
|
*/ |
210
|
|
|
public function testGetDiscountFieldValue() |
211
|
|
|
{ |
212
|
|
|
$product = $this->objFromFixture(ProductPage::class, 'productthree'); |
213
|
|
|
$helper = DiscountHelper::create($product); |
214
|
|
|
|
215
|
|
|
$this->assertEquals("Unrestricted Discount{allunits|1-25}", $helper->getDiscountFieldValue()); |
216
|
|
|
|
217
|
|
|
$discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage'); |
218
|
|
|
$discountOne->doUnpublish(); |
219
|
|
|
|
220
|
|
|
$newHelper = DiscountHelper::create($product); |
221
|
|
|
|
222
|
|
|
$this->assertEquals( |
223
|
|
|
'Unrestricted Discount Tiered Percentage{allunits|1-5|5-10|20-30}', |
224
|
|
|
$newHelper->getDiscountFieldValue() |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|