|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverCommerce\TaxAdmin\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use NumberFormatter; |
|
6
|
|
|
use SilverStripe\i18n\i18n; |
|
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
8
|
|
|
use SilverStripe\Security\Security; |
|
9
|
|
|
use SilverStripe\Core\Config\Config; |
|
10
|
|
|
use SilverCommerce\GeoZones\Model\Region; |
|
11
|
|
|
use SilverCommerce\TaxAdmin\Tests\Model\TestProduct; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Test functionality of postage extension |
|
15
|
|
|
* |
|
16
|
|
|
*/ |
|
17
|
|
|
class TaxableTest extends SapphireTest |
|
18
|
|
|
{ |
|
19
|
|
|
protected static $fixture_file = 'TaxData.yml'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Setup test only objects |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected static $extra_dataobjects = [ |
|
27
|
|
|
TestProduct::class |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
public function setUp() |
|
31
|
|
|
{ |
|
32
|
|
|
parent::setUp(); |
|
33
|
|
|
Config::inst()->set(Region::class, "create_on_build", false); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
// Setup default locale |
|
36
|
|
|
i18n::set_locale("en_GB"); |
|
37
|
|
|
$member = Security::getCurrentUser(); |
|
38
|
|
|
$member->Locale = "en_GB"; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testFilterTaxableExtensionResults() |
|
42
|
|
|
{ |
|
43
|
|
|
$product = $this->objFromFixture(TestProduct::class, 'product1'); |
|
44
|
|
|
|
|
45
|
|
|
$null = $product->filterTaxableExtensionResults(null); |
|
46
|
|
|
$string = $product->filterTaxableExtensionResults("One"); |
|
47
|
|
|
$list = $product->filterTaxableExtensionResults(["One", "Two", "Three"]); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertNull($null); |
|
50
|
|
|
$this->assertNull($string); |
|
51
|
|
|
$this->assertEquals("One", $list); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testGetShowPriceWithTax() |
|
55
|
|
|
{ |
|
56
|
|
|
$curr = TestProduct::config()->get('show_price_with_tax'); |
|
57
|
|
|
$product = $this->objFromFixture(TestProduct::class, 'product1'); |
|
58
|
|
|
|
|
59
|
|
|
TestProduct::config()->set('show_price_with_tax', false); |
|
60
|
|
|
$this->assertFalse($product->getShowPriceWithTax()); |
|
61
|
|
|
|
|
62
|
|
|
TestProduct::config()->set('show_price_with_tax', true); |
|
63
|
|
|
$this->assertTrue($product->getShowPriceWithTax()); |
|
64
|
|
|
|
|
65
|
|
|
TestProduct::config()->set('show_price_with_tax', $curr); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testGetShowTaxString() |
|
69
|
|
|
{ |
|
70
|
|
|
$curr = TestProduct::config()->get('show_tax_string'); |
|
71
|
|
|
$product = $this->objFromFixture(TestProduct::class, 'product1'); |
|
72
|
|
|
|
|
73
|
|
|
TestProduct::config()->set('show_tax_string', false); |
|
74
|
|
|
$this->assertFalse($product->getShowTaxString()); |
|
75
|
|
|
|
|
76
|
|
|
TestProduct::config()->set('show_tax_string', true); |
|
77
|
|
|
$this->assertTrue($product->getShowTaxString()); |
|
78
|
|
|
|
|
79
|
|
|
TestProduct::config()->set('show_tax_string', $curr); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testGetFormatter() |
|
83
|
|
|
{ |
|
84
|
|
|
$product = $this->objFromFixture(TestProduct::class, 'product1'); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertInstanceOf(NumberFormatter::class, $product->getFormatter()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testGetCurrencySymbol() |
|
90
|
|
|
{ |
|
91
|
|
|
$locale = i18n::get_locale(); |
|
92
|
|
|
$product = $this->objFromFixture(TestProduct::class, 'product1'); |
|
93
|
|
|
|
|
94
|
|
|
i18n::set_locale('en_GB'); |
|
95
|
|
|
$this->assertEquals("£", $product->getCurrencySymbol()); |
|
96
|
|
|
|
|
97
|
|
|
i18n::set_locale('en_US'); |
|
98
|
|
|
$this->assertEquals("$", $product->getCurrencySymbol()); |
|
99
|
|
|
|
|
100
|
|
|
i18n::set_locale('en_DE'); |
|
101
|
|
|
$this->assertEquals("€", $product->getCurrencySymbol()); |
|
102
|
|
|
|
|
103
|
|
|
i18n::set_locale($locale); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testGetCurrency() |
|
107
|
|
|
{ |
|
108
|
|
|
$locale = i18n::get_locale(); |
|
109
|
|
|
$product = $this->objFromFixture(TestProduct::class, 'product1'); |
|
110
|
|
|
|
|
111
|
|
|
i18n::set_locale('en_GB'); |
|
112
|
|
|
$this->assertEquals("GBP", $product->getCurrency()); |
|
113
|
|
|
|
|
114
|
|
|
i18n::set_locale('en_US'); |
|
115
|
|
|
$this->assertEquals("USD", $product->getCurrency()); |
|
116
|
|
|
|
|
117
|
|
|
i18n::set_locale('en_DE'); |
|
118
|
|
|
$this->assertEquals("EUR", $product->getCurrency()); |
|
119
|
|
|
|
|
120
|
|
|
i18n::set_locale($locale); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testGetNoTaxPrice() |
|
124
|
|
|
{ |
|
125
|
|
|
$p_one = $this->objFromFixture(TestProduct::class, 'product1'); |
|
126
|
|
|
$p_two = $this->objFromFixture(TestProduct::class, 'product2'); |
|
127
|
|
|
$p_three = $this->objFromFixture(TestProduct::class, 'product5'); |
|
128
|
|
|
$p_four = $this->objFromFixture(TestProduct::class, 'product8'); |
|
129
|
|
|
|
|
130
|
|
|
$this->assertEquals('16.66', $p_one->NoTaxPrice); |
|
131
|
|
|
$this->assertEquals('83.29', $p_two->NoTaxPrice); |
|
132
|
|
|
$this->assertEquals('49.99', $p_three->NoTaxPrice); |
|
133
|
|
|
$this->assertEquals('16.625', $p_four->NoTaxPrice); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function testGetTaxRate() |
|
137
|
|
|
{ |
|
138
|
|
|
$p_one = $this->objFromFixture(TestProduct::class, 'product1'); |
|
139
|
|
|
$p_two = $this->objFromFixture(TestProduct::class, 'product3'); |
|
140
|
|
|
|
|
141
|
|
|
$this->assertEquals(20, $p_one->getTaxRate()->Rate); |
|
142
|
|
|
$this->assertEquals(5, $p_two->getTaxRate()->Rate); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function testGetTaxPercentage() |
|
146
|
|
|
{ |
|
147
|
|
|
$p_one = $this->objFromFixture(TestProduct::class, 'product1'); |
|
148
|
|
|
$p_two = $this->objFromFixture(TestProduct::class, 'product3'); |
|
149
|
|
|
$p_three = $this->objFromFixture(TestProduct::class, 'product4'); |
|
150
|
|
|
|
|
151
|
|
|
$this->assertEquals(20, $p_one->getTaxPercentage()); |
|
152
|
|
|
$this->assertEquals(5, $p_two->getTaxPercentage()); |
|
153
|
|
|
$this->assertEquals(0, $p_three->getTaxPercentage()); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function testGetTaxAmount() |
|
157
|
|
|
{ |
|
158
|
|
|
$p_one = $this->objFromFixture(TestProduct::class, 'product1'); |
|
159
|
|
|
$p_two = $this->objFromFixture(TestProduct::class, 'product2'); |
|
160
|
|
|
$p_three = $this->objFromFixture(TestProduct::class, 'product3'); |
|
161
|
|
|
$p_four = $this->objFromFixture(TestProduct::class, 'product4'); |
|
162
|
|
|
$p_five = $this->objFromFixture(TestProduct::class, 'product5'); |
|
163
|
|
|
$p_six = $this->objFromFixture(TestProduct::class, 'product6'); |
|
164
|
|
|
$p_seven = $this->objFromFixture(TestProduct::class, 'product7'); |
|
165
|
|
|
$p_eight = $this->objFromFixture(TestProduct::class, 'product8'); |
|
166
|
|
|
$p_nine = $this->objFromFixture(TestProduct::class, 'product9'); |
|
167
|
|
|
$p_ten = $this->objFromFixture(TestProduct::class, 'product10'); |
|
168
|
|
|
|
|
169
|
|
|
$this->assertEquals(3.332, $p_one->getTaxAmount()); |
|
170
|
|
|
$this->assertEquals(16.658, $p_two->getTaxAmount()); |
|
171
|
|
|
$this->assertEquals(0.625, $p_three->getTaxAmount()); |
|
172
|
|
|
$this->assertEquals(0, $p_four->getTaxAmount()); |
|
173
|
|
|
$this->assertEquals(9.998, $p_five->getTaxAmount()); |
|
174
|
|
|
$this->assertEquals(24.834, $p_six->getTaxAmount()); |
|
175
|
|
|
$this->assertEquals(2.498, $p_seven->getTaxAmount()); |
|
176
|
|
|
$this->assertEquals(3.325, $p_eight->getTaxAmount()); |
|
177
|
|
|
$this->assertEquals(2.825, $p_nine->getTaxAmount()); |
|
178
|
|
|
$this->assertEquals(24.998, $p_ten->getTaxAmount()); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function testGetPriceAndTax() |
|
182
|
|
|
{ |
|
183
|
|
|
$p_one = $this->objFromFixture(TestProduct::class, 'product1'); |
|
184
|
|
|
$p_two = $this->objFromFixture(TestProduct::class, 'product2'); |
|
185
|
|
|
$p_three = $this->objFromFixture(TestProduct::class, 'product3'); |
|
186
|
|
|
$p_four = $this->objFromFixture(TestProduct::class, 'product4'); |
|
187
|
|
|
$p_five = $this->objFromFixture(TestProduct::class, 'product5'); |
|
188
|
|
|
$p_six = $this->objFromFixture(TestProduct::class, 'product6'); |
|
189
|
|
|
$p_seven = $this->objFromFixture(TestProduct::class, 'product7'); |
|
190
|
|
|
$p_eight = $this->objFromFixture(TestProduct::class, 'product8'); |
|
191
|
|
|
$p_nine = $this->objFromFixture(TestProduct::class, 'product9'); |
|
192
|
|
|
$p_ten = $this->objFromFixture(TestProduct::class, 'product10'); |
|
193
|
|
|
|
|
194
|
|
|
$this->assertEquals(19.992, $p_one->getPriceAndTax()); |
|
195
|
|
|
$this->assertEquals(99.948, $p_two->getPriceAndTax()); |
|
196
|
|
|
$this->assertEquals(13.125, $p_three->getPriceAndTax()); |
|
197
|
|
|
$this->assertEquals(12.5, $p_four->getPriceAndTax()); |
|
198
|
|
|
$this->assertEquals(59.988, $p_five->getPriceAndTax()); |
|
199
|
|
|
$this->assertEquals(149.004, $p_six->getPriceAndTax()); |
|
200
|
|
|
$this->assertEquals(14.988, $p_seven->getPriceAndTax()); |
|
201
|
|
|
$this->assertEquals(19.95, $p_eight->getPriceAndTax()); |
|
202
|
|
|
$this->assertEquals(16.95, $p_nine->getPriceAndTax()); |
|
203
|
|
|
$this->assertEquals(149.988, $p_ten->getPriceAndTax()); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
public function testGetFormattedPrice() |
|
207
|
|
|
{ |
|
208
|
|
|
$whitespace = "\xc2\xa0"; |
|
209
|
|
|
$locale = i18n::get_locale(); |
|
210
|
|
|
|
|
211
|
|
|
$p_one = $this->objFromFixture(TestProduct::class, 'product1'); |
|
212
|
|
|
$p_two = $this->objFromFixture(TestProduct::class, 'product6'); |
|
213
|
|
|
$p_three = $this->objFromFixture(TestProduct::class, 'product8'); |
|
214
|
|
|
$p_four = $this->objFromFixture(TestProduct::class, 'product9'); |
|
215
|
|
|
$p_five = $this->objFromFixture(TestProduct::class, 'product10'); |
|
216
|
|
|
|
|
217
|
|
|
i18n::set_locale('en_GB'); |
|
218
|
|
|
$this->assertEquals("£16.66", $p_one->getFormattedPrice()); |
|
219
|
|
|
$this->assertEquals("£124.17", $p_two->getFormattedPrice()); |
|
220
|
|
|
$this->assertEquals("£16.62", $p_three->getFormattedPrice()); |
|
221
|
|
|
$this->assertEquals("£14.12", $p_four->getFormattedPrice()); |
|
222
|
|
|
$this->assertEquals("£124.99", $p_five->getFormattedPrice()); |
|
223
|
|
|
|
|
224
|
|
|
i18n::set_locale('en_US'); |
|
225
|
|
|
$this->assertEquals("$16.66", $p_one->getFormattedPrice()); |
|
226
|
|
|
|
|
227
|
|
|
i18n::set_locale('de_DE'); |
|
228
|
|
|
$this->assertEquals( |
|
229
|
|
|
'16,66 €', |
|
230
|
|
|
str_replace($whitespace, ' ', $p_one->getFormattedPrice()) |
|
231
|
|
|
); |
|
232
|
|
|
|
|
233
|
|
|
i18n::set_locale('en_GB'); |
|
234
|
|
|
$this->assertEquals("£19.99", $p_one->getFormattedPrice(true)); |
|
235
|
|
|
$this->assertEquals("£149.00", $p_two->getFormattedPrice(true)); |
|
236
|
|
|
$this->assertEquals("£19.95", $p_three->getFormattedPrice(true)); |
|
237
|
|
|
$this->assertEquals("£16.95", $p_four->getFormattedPrice(true)); |
|
238
|
|
|
$this->assertEquals("£149.99", $p_five->getFormattedPrice(true)); |
|
239
|
|
|
|
|
240
|
|
|
i18n::set_locale('en_US'); |
|
241
|
|
|
$this->assertEquals("$19.99", $p_one->getFormattedPrice(true)); |
|
242
|
|
|
|
|
243
|
|
|
i18n::set_locale('de_DE'); |
|
244
|
|
|
$this->assertEquals( |
|
245
|
|
|
'19,99 €', |
|
246
|
|
|
str_replace($whitespace, ' ', $p_one->getFormattedPrice(true)) |
|
247
|
|
|
); |
|
248
|
|
|
|
|
249
|
|
|
i18n::set_locale($locale); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|