1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Inventory\Test\Extension; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Extension\Purchasable; |
6
|
|
|
use Dynamic\Foxy\Extension\PurchasableExtension; |
7
|
|
|
use Dynamic\Foxy\Inventory\Extension\ProductExpirationManager; |
8
|
|
|
use Dynamic\Foxy\Inventory\Extension\ProductInventoryManager; |
9
|
|
|
use Dynamic\Foxy\Inventory\Test\TestOnly\Extension\TestVariationDataExtension; |
10
|
|
|
use Dynamic\Foxy\Inventory\Test\TestOnly\Page\TestProduct; |
11
|
|
|
use Dynamic\Foxy\Inventory\Test\TestOnly\Page\TestProductController; |
12
|
|
|
use Dynamic\Foxy\Model\Variation; |
13
|
|
|
use Dynamic\Foxy\Orders\Model\Order; |
14
|
|
|
use Dynamic\Foxy\Orders\Model\OrderDetail; |
15
|
|
|
use SilverStripe\Core\Config\Config; |
16
|
|
|
use SilverStripe\Core\Injector\Injector; |
17
|
|
|
use SilverStripe\Dev\SapphireTest; |
18
|
|
|
use SilverStripe\Forms\FieldList; |
19
|
|
|
use SilverStripe\Forms\Form; |
20
|
|
|
|
21
|
|
|
class ProductInventoryManagerTest extends SapphireTest |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected static $fixture_file = [ |
27
|
|
|
'../fixtures.yml', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected static $extra_dataobjects = [ |
34
|
|
|
TestProduct::class, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected static $required_extensions = [ |
41
|
|
|
TestProduct::class => [ |
42
|
|
|
Purchasable::class, |
43
|
|
|
ProductInventoryManager::class, |
44
|
|
|
ProductExpirationManager::class, |
45
|
|
|
], |
46
|
|
|
TestProductController::class => [ |
47
|
|
|
PurchasableExtension::class, |
48
|
|
|
], |
49
|
|
|
Variation::class => [ |
50
|
|
|
TestVariationDataExtension::class, |
51
|
|
|
], |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected static $extra_controllers = [ |
58
|
|
|
TestProductController::class, |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* |
63
|
|
|
*/ |
64
|
|
|
protected function setUp() |
65
|
|
|
{ |
66
|
|
|
parent::setUp(); |
67
|
|
|
|
68
|
|
|
if (class_exists('Dynamic\\Foxy\\SingleSignOn\\Client\\CustomerClient')) { |
69
|
|
|
Config::modify() |
70
|
|
|
->set( |
71
|
|
|
'Dynamic\\Foxy\\SingleSignOn\\Client\\CustomerClient', |
72
|
|
|
'foxy_sso_enabled', |
73
|
|
|
false |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
*/ |
81
|
|
|
public function testUpdateCMSFields() |
82
|
|
|
{ |
83
|
|
|
$object = Injector::inst()->create(TestProduct::class); |
84
|
|
|
$fields = $object->getCMSFields(); |
85
|
|
|
$this->assertInstanceOf(FieldList::class, $fields); |
86
|
|
|
$this->assertNotNull($fields->dataFieldByName('ControlInventory')); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* |
91
|
|
|
*/ |
92
|
|
|
public function testFoxyStripePurchaseForm() |
93
|
|
|
{ |
94
|
|
|
/** @var TestProduct $object */ |
95
|
|
|
$object = Injector::inst()->create(TestProduct::class); |
96
|
|
|
/** @var TestProductController $controller */ |
97
|
|
|
$controller = TestProductController::create($object); |
98
|
|
|
$form = $controller->AddToCartForm(); |
|
|
|
|
99
|
|
|
$this->assertInstanceOf(Form::class, $form); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* |
104
|
|
|
*/ |
105
|
|
|
public function testGetHasInventory() |
106
|
|
|
{ |
107
|
|
|
/** @var TestProduct $product */ |
108
|
|
|
$product = Injector::inst()->create(TestProduct::class); |
109
|
|
|
$product->ControlInventory = false; |
|
|
|
|
110
|
|
|
$product->PurchaseLimit = 0; |
|
|
|
|
111
|
|
|
$this->assertFalse($product->getHasInventory()); |
|
|
|
|
112
|
|
|
$product->ControlInventory = true; |
113
|
|
|
$product->PurchaseLimit = 0; |
114
|
|
|
$this->assertFalse($product->getHasInventory()); |
115
|
|
|
$product->ControlInventory = false; |
116
|
|
|
$product->PurchaseLimit = 10; |
117
|
|
|
$this->assertFalse($product->getHasInventory()); |
118
|
|
|
$product->ControlInventory = true; |
119
|
|
|
$product->PurchaseLimit = 10; |
120
|
|
|
$this->assertTrue($product->getHasInventory()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* |
125
|
|
|
*/ |
126
|
|
|
public function testGetIsProductAvailable() |
127
|
|
|
{ |
128
|
|
|
/** @var TestProduct $product */ |
129
|
|
|
$product = Injector::inst()->create(TestProduct::class); |
130
|
|
|
// no inventory control |
131
|
|
|
$product->ControlInventory = false; |
|
|
|
|
132
|
|
|
$product->PurchaseLimit = 0; |
|
|
|
|
133
|
|
|
$product->write(); |
134
|
|
|
|
135
|
|
|
$product = TestProduct::get()->byID($product->ID); |
136
|
|
|
|
137
|
|
|
$this->assertTrue($product->getIsProductAvailable()); |
138
|
|
|
|
139
|
|
|
// inventory control, with limit |
140
|
|
|
$product->ControlInventory = true; |
141
|
|
|
$product->PurchaseLimit = 10; |
142
|
|
|
$product->write(); |
143
|
|
|
$product = TestProduct::get()->byID($product->ID); |
144
|
|
|
|
145
|
|
|
$this->assertTrue($product->getIsProductAvailable()); |
146
|
|
|
|
147
|
|
|
/** @var OrderDetail $detail */ |
148
|
|
|
$detail = OrderDetail::create(); |
149
|
|
|
$detail->OrderID = $this->objFromFixture(Order::class, 'one')->ID; |
150
|
|
|
$detail->Quantity = 10; |
151
|
|
|
$detail->ProductID = $product->ID; |
152
|
|
|
$detail->write(); |
153
|
|
|
|
154
|
|
|
$product = TestProduct::get()->byID($product->ID); |
155
|
|
|
|
156
|
|
|
// inventory control, no inventory left |
157
|
|
|
$this->assertFalse($product->getIsProductAvailable()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* |
162
|
|
|
*/ |
163
|
|
|
public function testGetNumberPurchased() |
164
|
|
|
{ |
165
|
|
|
/** @var TestProduct $product */ |
166
|
|
|
$product = Injector::inst()->create(TestProduct::class); |
167
|
|
|
$product->write(); |
168
|
|
|
$this->assertEquals(0, $product->getNumberPurchased()); |
|
|
|
|
169
|
|
|
|
170
|
|
|
/** @var Order $order */ |
171
|
|
|
$order = $this->objFromFixture(Order::class, 'one'); |
172
|
|
|
|
173
|
|
|
/** @var OrderDetail $detail */ |
174
|
|
|
$detail = OrderDetail::create(); |
175
|
|
|
$detail->OrderID = $order->ID; |
176
|
|
|
$detail->Quantity = 10; |
177
|
|
|
$detail->ProductID = $product->ID; |
178
|
|
|
$detail->write(); |
179
|
|
|
|
180
|
|
|
$this->assertEquals(10, $product->getNumberPurchased()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* |
185
|
|
|
*/ |
186
|
|
|
public function testGetOrders() |
187
|
|
|
{ |
188
|
|
|
/** @var TestProduct $product */ |
189
|
|
|
$product = Injector::inst()->create(TestProduct::class); |
190
|
|
|
$product->write(); |
191
|
|
|
$this->assertEquals(0, $product->getOrders()->count()); |
|
|
|
|
192
|
|
|
/** @var OrderDetail $detail */ |
193
|
|
|
$detail = OrderDetail::create(); |
194
|
|
|
$detail->OrderID = $this->objFromFixture(Order::class, 'one')->ID; |
195
|
|
|
$detail->Quantity = 10; |
196
|
|
|
$detail->ProductID = $product->ID; |
197
|
|
|
$detail->write(); |
198
|
|
|
$this->assertEquals(1, $product->getOrders()->count()); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|