1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Test\Model; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Extension\Purchasable; |
6
|
|
|
use Dynamic\Foxy\Model\OptionType; |
7
|
|
|
use Dynamic\Foxy\Model\ProductOption; |
8
|
|
|
use Dynamic\Foxy\Test\TestOnly\TestProduct; |
9
|
|
|
use SilverStripe\Core\Injector\Injector; |
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
use SilverStripe\Security\Member; |
13
|
|
|
use SilverStripe\Versioned\Versioned; |
14
|
|
|
|
15
|
|
|
class ProductOptionTest extends SapphireTest |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected static $fixture_file = '../fixtures.yml'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
public static $extra_dataobjects = [ |
26
|
|
|
TestProduct::class, |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
protected static $required_extensions = [ |
30
|
|
|
TestProduct::class => [ |
31
|
|
|
Purchasable::class, |
32
|
|
|
], |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* |
37
|
|
|
*/ |
38
|
|
|
public function testGetCMSFields() |
39
|
|
|
{ |
40
|
|
|
$object = $this->objFromFixture(ProductOption::class, 'small'); |
41
|
|
|
$fields = $object->getCMSFields(); |
42
|
|
|
$this->assertInstanceOf(FieldList::class, $fields); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
*/ |
48
|
|
|
public function testCanCreate() |
49
|
|
|
{ |
50
|
|
|
/** @var ProductOption $object */ |
51
|
|
|
$object = singleton(ProductOption::class); |
52
|
|
|
/** @var \SilverStripe\Security\Member $admin */ |
53
|
|
|
$admin = $this->objFromFixture(Member::class, 'admin'); |
54
|
|
|
/** @var \SilverStripe\Security\Member $siteOwner */ |
55
|
|
|
$siteOwner = $this->objFromFixture(Member::class, 'site-owner'); |
56
|
|
|
/** @var \SilverStripe\Security\Member $default */ |
57
|
|
|
$default = $this->objFromFixture(Member::class, 'default'); |
58
|
|
|
|
59
|
|
|
$this->assertFalse($object->canCreate($default)); |
60
|
|
|
$this->assertTrue($object->canCreate($admin)); |
61
|
|
|
$this->assertTrue($object->canCreate($siteOwner)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
*/ |
67
|
|
|
public function testCanEdit() |
68
|
|
|
{ |
69
|
|
|
/** @var ProductOption $object */ |
70
|
|
|
$object = singleton(ProductOption::class); |
71
|
|
|
/** @var \SilverStripe\Security\Member $admin */ |
72
|
|
|
$admin = $this->objFromFixture(Member::class, 'admin'); |
73
|
|
|
/** @var \SilverStripe\Security\Member $siteOwner */ |
74
|
|
|
$siteOwner = $this->objFromFixture(Member::class, 'site-owner'); |
75
|
|
|
/** @var \SilverStripe\Security\Member $default */ |
76
|
|
|
$default = $this->objFromFixture(Member::class, 'default'); |
77
|
|
|
|
78
|
|
|
$this->assertFalse($object->canEdit($default)); |
79
|
|
|
$this->assertTrue($object->canEdit($admin)); |
80
|
|
|
$this->assertTrue($object->canEdit($siteOwner)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* |
85
|
|
|
*/ |
86
|
|
|
public function testCanDelete() |
87
|
|
|
{ |
88
|
|
|
/** @var ProductOption $object */ |
89
|
|
|
$object = singleton(ProductOption::class); |
90
|
|
|
/** @var \SilverStripe\Security\Member $admin */ |
91
|
|
|
$admin = $this->objFromFixture(Member::class, 'admin'); |
92
|
|
|
/** @var \SilverStripe\Security\Member $siteOwner */ |
93
|
|
|
$siteOwner = $this->objFromFixture(Member::class, 'site-owner'); |
94
|
|
|
/** @var \SilverStripe\Security\Member $default */ |
95
|
|
|
$default = $this->objFromFixture(Member::class, 'default'); |
96
|
|
|
|
97
|
|
|
$this->assertFalse($object->canDelete($default)); |
98
|
|
|
$this->assertTrue($object->canDelete($admin)); |
99
|
|
|
$this->assertTrue($object->canDelete($siteOwner)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
104
|
|
|
*/ |
105
|
|
|
public function testGenerateKey() |
106
|
|
|
{ |
107
|
|
|
$this->markTestSkipped(); |
108
|
|
|
/*$product = $this->findOrMakeProduct(); |
109
|
|
|
$option = ProductOption::create(); |
110
|
|
|
//$option->write(); |
111
|
|
|
|
112
|
|
|
$option->Title = $title = 'My Title'; |
113
|
|
|
$price = 150; |
114
|
|
|
$action = 'Set'; |
115
|
|
|
|
116
|
|
|
$product->Options()->add( |
117
|
|
|
$option, |
118
|
|
|
[ |
119
|
|
|
'Available' => true, |
120
|
|
|
'PriceModifier' => $price, |
121
|
|
|
'PriceModifierAction' => $action, |
122
|
|
|
] |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$actionSymbol = ProductOption::getOptionModifierActionSymbol($action); |
126
|
|
|
|
127
|
|
|
$expected = "{$title}{p{$actionSymbol}{$price}|w+0|c+0}"; |
128
|
|
|
|
129
|
|
|
$option = $product->Options()->filter('ProductOptionID', $option->ID)->first(); |
130
|
|
|
|
131
|
|
|
$this->assertEquals( |
132
|
|
|
$expected, |
133
|
|
|
$option->OptionModifierKey |
134
|
|
|
);//*/ |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return TestProduct|\SilverStripe\ORM\DataObject |
139
|
|
|
*/ |
140
|
|
|
protected function findOrMakeProduct() |
141
|
|
|
{ |
142
|
|
|
if (!$product = TestProduct::get()->first()) { |
143
|
|
|
$product = TestProduct::create(); |
144
|
|
|
$product->Title = 'My Product'; |
145
|
|
|
$product->writeToStage(Versioned::DRAFT); |
146
|
|
|
$product->publishSingle(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $product; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|