|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Test\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Extension\Purchasable; |
|
6
|
|
|
use Dynamic\Foxy\Extension\Shippable; |
|
7
|
|
|
use Dynamic\Foxy\Model\Variation; |
|
8
|
|
|
use Dynamic\Foxy\Test\TestOnly\TestProduct; |
|
9
|
|
|
use Dynamic\Foxy\Test\TestOnly\TestVariationDataExtension; |
|
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
11
|
|
|
use SilverStripe\Forms\FieldList; |
|
12
|
|
|
use SilverStripe\ORM\DataObject; |
|
13
|
|
|
use SilverStripe\ORM\ValidationException; |
|
14
|
|
|
use SilverStripe\Security\Member; |
|
15
|
|
|
use SilverStripe\Versioned\Versioned; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class ProductOptionTest |
|
19
|
|
|
* @package Dynamic\Foxy\Test\Model |
|
20
|
|
|
*/ |
|
21
|
|
|
class VariationTest extends SapphireTest |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected static $fixture_file = [ |
|
27
|
|
|
'../fixtures.yml', |
|
28
|
|
|
'../shippableproducts.yml', |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
public static $extra_dataobjects = [ |
|
35
|
|
|
TestProduct::class, |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var \string[][] |
|
40
|
|
|
*/ |
|
41
|
|
|
protected static $required_extensions = [ |
|
42
|
|
|
TestProduct::class => [ |
|
43
|
|
|
Purchasable::class, |
|
44
|
|
|
Shippable::class, |
|
45
|
|
|
], |
|
46
|
|
|
Variation::class => [ |
|
47
|
|
|
TestVariationDataExtension::class, |
|
48
|
|
|
], |
|
49
|
|
|
]; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testVariationHasExtension() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->assertTrue(Variation::has_extension(TestVariationDataExtension::class)); |
|
57
|
|
|
$this->assertTrue(Variation::singleton()->hasDatabaseField('ProductID')); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* |
|
62
|
|
|
*/ |
|
63
|
|
|
public function testGetCMSFields() |
|
64
|
|
|
{ |
|
65
|
|
|
$object = Variation::singleton(); |
|
66
|
|
|
$fields = $object->getCMSFields(); |
|
67
|
|
|
$this->assertInstanceOf(FieldList::class, $fields); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* |
|
72
|
|
|
*/ |
|
73
|
|
|
public function testCanCreate() |
|
74
|
|
|
{ |
|
75
|
|
|
/** @var Variation $object */ |
|
76
|
|
|
$object = Variation::singleton(); |
|
77
|
|
|
/** @var Member $admin */ |
|
78
|
|
|
$admin = $this->objFromFixture(Member::class, 'admin'); |
|
79
|
|
|
/** @var Member $siteOwner */ |
|
80
|
|
|
$siteOwner = $this->objFromFixture(Member::class, 'site-owner'); |
|
81
|
|
|
/** @var Member $default */ |
|
82
|
|
|
$default = $this->objFromFixture(Member::class, 'default'); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertFalse($object->canCreate($default)); |
|
85
|
|
|
$this->assertTrue($object->canCreate($admin)); |
|
86
|
|
|
$this->assertTrue($object->canCreate($siteOwner)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testCanEdit() |
|
93
|
|
|
{ |
|
94
|
|
|
/** @var Variation $object */ |
|
95
|
|
|
$object = Variation::singleton(); |
|
96
|
|
|
/** @var Member $admin */ |
|
97
|
|
|
$admin = $this->objFromFixture(Member::class, 'admin'); |
|
98
|
|
|
/** @var Member $siteOwner */ |
|
99
|
|
|
$siteOwner = $this->objFromFixture(Member::class, 'site-owner'); |
|
100
|
|
|
/** @var Member $default */ |
|
101
|
|
|
$default = $this->objFromFixture(Member::class, 'default'); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertFalse($object->canEdit($default)); |
|
104
|
|
|
$this->assertTrue($object->canEdit($admin)); |
|
105
|
|
|
$this->assertTrue($object->canEdit($siteOwner)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* |
|
110
|
|
|
*/ |
|
111
|
|
|
public function testCanDelete() |
|
112
|
|
|
{ |
|
113
|
|
|
/** @var Variation $object */ |
|
114
|
|
|
$object = Variation::singleton(); |
|
115
|
|
|
/** @var Member $admin */ |
|
116
|
|
|
$admin = $this->objFromFixture(Member::class, 'admin'); |
|
117
|
|
|
/** @var Member $siteOwner */ |
|
118
|
|
|
$siteOwner = $this->objFromFixture(Member::class, 'site-owner'); |
|
119
|
|
|
/** @var Member $default */ |
|
120
|
|
|
$default = $this->objFromFixture(Member::class, 'default'); |
|
121
|
|
|
|
|
122
|
|
|
$this->assertFalse($object->canDelete($default)); |
|
123
|
|
|
$this->assertTrue($object->canDelete($admin)); |
|
124
|
|
|
$this->assertTrue($object->canDelete($siteOwner)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @throws ValidationException |
|
129
|
|
|
*/ |
|
130
|
|
|
public function testOnBeforeWrite() |
|
131
|
|
|
{ |
|
132
|
|
|
$productID = $this->idFromFixture(TestProduct::class, 'productfiveandvariations'); |
|
133
|
|
|
|
|
134
|
|
|
$newVariation = Variation::create(); |
|
135
|
|
|
$newVariation->Title = 'My Variation - Before Write Test'; |
|
136
|
|
|
$newVariation->ProductID = $productID; |
|
137
|
|
|
|
|
138
|
|
|
$newVariation->PriceModifierAction = 'Add'; |
|
139
|
|
|
$newVariation->PriceModifier = 10; |
|
140
|
|
|
|
|
141
|
|
|
$newVariation->WeightModifier = .5; |
|
142
|
|
|
$newVariation->WeightModifierAction = 'Subtract'; |
|
143
|
|
|
|
|
144
|
|
|
$newVariation->CodeModifier = 'my-foo-code-modifier'; |
|
145
|
|
|
$newVariation->CodeModifierAction = 'Set'; |
|
146
|
|
|
|
|
147
|
|
|
$this->assertNull($newVariation->FinalPrice); |
|
148
|
|
|
$this->assertNull($newVariation->FinalWeight); |
|
149
|
|
|
$this->assertNull($newVariation->FinalCode); |
|
150
|
|
|
|
|
151
|
|
|
$newVariation->write(); |
|
152
|
|
|
/** @var Variation $newVariation */ |
|
153
|
|
|
$newVariation = Variation::get()->byID($newVariation->ID); |
|
154
|
|
|
|
|
155
|
|
|
$this->assertEquals(110, $newVariation->FinalPrice); |
|
156
|
|
|
$this->assertEquals(9.5, $newVariation->FinalWeight); |
|
157
|
|
|
$this->assertEquals('my-foo-code-modifier', $newVariation->FinalCode); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* |
|
162
|
|
|
*/ |
|
163
|
|
|
public function testBeforeWriteRetainWhiteSpace() |
|
164
|
|
|
{ |
|
165
|
|
|
Variation::config()->update('code_trim_right_space', false); |
|
166
|
|
|
Variation::config()->update('code_trim_left_spaces', false); |
|
167
|
|
|
$productID = $this->idFromFixture(TestProduct::class, 'productfiveandvariations'); |
|
168
|
|
|
|
|
169
|
|
|
$newVariation = Variation::create(); |
|
170
|
|
|
$newVariation->Title = 'My Variation - Before Write Whitespace Test'; |
|
171
|
|
|
$newVariation->ProductID = $productID; |
|
172
|
|
|
|
|
173
|
|
|
$newVariation->CodeModifier = ' my-foo-code- modifier '; |
|
174
|
|
|
$newVariation->CodeModifierAction = 'Add'; |
|
175
|
|
|
|
|
176
|
|
|
$newVariation->write(); |
|
177
|
|
|
$newVariation = Variation::get()->byID($newVariation->ID); |
|
178
|
|
|
|
|
179
|
|
|
$this->assertEquals(' my-foo-code- modifier ', $newVariation->CodeModifier); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @throws ValidationException |
|
184
|
|
|
*/ |
|
185
|
|
|
public function testBeforeWriteTrimRight() |
|
186
|
|
|
{ |
|
187
|
|
|
Variation::config()->update('code_trim_right_space', true); |
|
188
|
|
|
$productID = $this->idFromFixture(TestProduct::class, 'productfiveandvariations'); |
|
189
|
|
|
|
|
190
|
|
|
$newVariation = Variation::create(); |
|
191
|
|
|
$newVariation->Title = 'My Variation - Before Write Whitespace Test'; |
|
192
|
|
|
$newVariation->ProductID = $productID; |
|
193
|
|
|
|
|
194
|
|
|
$newVariation->CodeModifier = ' my-foo-code- modifier '; |
|
195
|
|
|
$newVariation->CodeModifierAction = 'Add'; |
|
196
|
|
|
|
|
197
|
|
|
$newVariation->write(); |
|
198
|
|
|
$newVariation = Variation::get()->byID($newVariation->ID); |
|
199
|
|
|
|
|
200
|
|
|
$this->assertEquals(' my-foo-code- modifier', $newVariation->CodeModifier); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @throws ValidationException |
|
205
|
|
|
*/ |
|
206
|
|
|
public function testBeforeWriteSingleSpace() |
|
207
|
|
|
{ |
|
208
|
|
|
Variation::config()->update('code_trim_left_spaces', false); |
|
209
|
|
|
Variation::config()->update('code_trim_right_space', false); |
|
210
|
|
|
Variation::config()->update('code_enforce_single_spaces', true); |
|
211
|
|
|
$productID = $this->idFromFixture(TestProduct::class, 'productfiveandvariations'); |
|
212
|
|
|
|
|
213
|
|
|
$newVariation = Variation::create(); |
|
214
|
|
|
$newVariation->Title = 'My Variation - Before Write Whitespace Test'; |
|
215
|
|
|
$newVariation->ProductID = $productID; |
|
216
|
|
|
|
|
217
|
|
|
$newVariation->CodeModifier = ' my-foo-code- modifier '; |
|
218
|
|
|
$newVariation->CodeModifierAction = 'Add'; |
|
219
|
|
|
|
|
220
|
|
|
$newVariation->write(); |
|
221
|
|
|
$newVariation = Variation::get()->byID($newVariation->ID); |
|
222
|
|
|
|
|
223
|
|
|
$this->assertEquals(' my-foo-code- modifier ', $newVariation->CodeModifier); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @throws ValidationException |
|
228
|
|
|
*/ |
|
229
|
|
|
public function testBeforeWriteRemoveSpaces() |
|
230
|
|
|
{ |
|
231
|
|
|
Variation::config()->update('code_enforce_single_spaces', true); |
|
232
|
|
|
Variation::config()->update('code_remove_spaces', true); |
|
233
|
|
|
$productID = $this->idFromFixture(TestProduct::class, 'productfiveandvariations'); |
|
234
|
|
|
|
|
235
|
|
|
$newVariation = Variation::create(); |
|
236
|
|
|
$newVariation->Title = 'My Variation - Before Write Whitespace Test'; |
|
237
|
|
|
$newVariation->ProductID = $productID; |
|
238
|
|
|
|
|
239
|
|
|
$newVariation->CodeModifier = ' my-foo-code- modifier '; |
|
240
|
|
|
$newVariation->CodeModifierAction = 'Add'; |
|
241
|
|
|
|
|
242
|
|
|
$newVariation->write(); |
|
243
|
|
|
$newVariation = Variation::get()->byID($newVariation->ID); |
|
244
|
|
|
|
|
245
|
|
|
$this->assertEquals('my-foo-code-modifier', $newVariation->CodeModifier); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @throws ValidationException |
|
250
|
|
|
*/ |
|
251
|
|
|
public function testBeforeWriteSetSpaces() |
|
252
|
|
|
{ |
|
253
|
|
|
$productID = $this->idFromFixture(TestProduct::class, 'productfiveandvariations'); |
|
254
|
|
|
|
|
255
|
|
|
$newVariation = Variation::create(); |
|
256
|
|
|
$newVariation->Title = 'My Variation - Before Write Whitespace Test'; |
|
257
|
|
|
$newVariation->ProductID = $productID; |
|
258
|
|
|
|
|
259
|
|
|
$newVariation->CodeModifier = ' my-foo-code- modifier '; |
|
260
|
|
|
$newVariation->CodeModifierAction = 'Add'; |
|
261
|
|
|
|
|
262
|
|
|
$newVariation->write(); |
|
263
|
|
|
$newVariation = Variation::get()->byID($newVariation->ID); |
|
264
|
|
|
|
|
265
|
|
|
$this->assertEquals(' my-foo-code- modifier', $newVariation->CodeModifier); |
|
266
|
|
|
|
|
267
|
|
|
Variation::config()->update('code_trim_left_spaces', true); |
|
268
|
|
|
|
|
269
|
|
|
$newVariation->CodeModifier = ' my-foo-code- modifier '; |
|
270
|
|
|
$newVariation->write(); |
|
271
|
|
|
|
|
272
|
|
|
$this->assertEquals('my-foo-code- modifier', $newVariation->CodeModifier); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @throws ValidationException |
|
277
|
|
|
*/ |
|
278
|
|
|
public function testBeforeWriteNoSpaces() |
|
279
|
|
|
{ |
|
280
|
|
|
Variation::config()->update('code_remove_spaces', true); |
|
281
|
|
|
$productID = $this->idFromFixture(TestProduct::class, 'productfiveandvariations'); |
|
282
|
|
|
|
|
283
|
|
|
$newVariation = Variation::create(); |
|
284
|
|
|
$newVariation->Title = 'My Variation - Before Write Whitespace Test'; |
|
285
|
|
|
$newVariation->ProductID = $productID; |
|
286
|
|
|
|
|
287
|
|
|
$newVariation->CodeModifier = ' my-foo-code- modifier '; |
|
288
|
|
|
$newVariation->CodeModifierAction = 'Add'; |
|
289
|
|
|
|
|
290
|
|
|
$newVariation->write(); |
|
291
|
|
|
$newVariation = Variation::get()->byID($newVariation->ID); |
|
292
|
|
|
|
|
293
|
|
|
$this->assertEquals('my-foo-code-modifier', $newVariation->CodeModifier); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* |
|
298
|
|
|
*/ |
|
299
|
|
|
public function testGetGeneratedValue() |
|
300
|
|
|
{ |
|
301
|
|
|
$variation = $this->objFromFixture(Variation::class, 'variationone'); |
|
302
|
|
|
$variation2 = $this->objFromFixture(Variation::class, 'variationtwo'); |
|
303
|
|
|
$variation3 = $this->objFromFixture(Variation::class, 'variationthree'); |
|
304
|
|
|
|
|
305
|
|
|
//Add |
|
306
|
|
|
$expected = sprintf( |
|
307
|
|
|
'%s{p%s|w%s|c%s}', |
|
308
|
|
|
$variation->Title, |
|
309
|
|
|
'+10', |
|
310
|
|
|
'+0', |
|
311
|
|
|
'+' |
|
312
|
|
|
); |
|
313
|
|
|
|
|
314
|
|
|
//Set |
|
315
|
|
|
$expected2 = sprintf( |
|
316
|
|
|
'%s{p%s|w%s|c%s}', |
|
317
|
|
|
$variation2->Title, |
|
318
|
|
|
':150', |
|
319
|
|
|
'+0', |
|
320
|
|
|
'+' |
|
321
|
|
|
); |
|
322
|
|
|
|
|
323
|
|
|
//Subtract |
|
324
|
|
|
$expected3 = sprintf( |
|
325
|
|
|
'%s{p%s|w%s|c%s}', |
|
326
|
|
|
$variation3->Title, |
|
327
|
|
|
'-20', |
|
328
|
|
|
'+0', |
|
329
|
|
|
'+' |
|
330
|
|
|
); |
|
331
|
|
|
|
|
332
|
|
|
$this->assertEquals($expected, $variation->getGeneratedValue()); |
|
333
|
|
|
$this->assertEquals($expected2, $variation2->getGeneratedValue()); |
|
334
|
|
|
$this->assertEquals($expected3, $variation3->getGeneratedValue()); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* @return TestProduct|DataObject |
|
339
|
|
|
*/ |
|
340
|
|
|
protected function findOrMakeProduct() |
|
341
|
|
|
{ |
|
342
|
|
|
if (!$product = TestProduct::get()->first()) { |
|
343
|
|
|
$product = TestProduct::create(); |
|
344
|
|
|
$product->Title = 'My Product'; |
|
345
|
|
|
$product->writeToStage(Versioned::DRAFT); |
|
346
|
|
|
$product->publishSingle(); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
return $product; |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|