1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Test; |
4
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Model\OptionGroup; |
6
|
|
|
use Dynamic\FoxyStripe\Model\OptionItem; |
7
|
|
|
use Dynamic\FoxyStripe\Model\ProductCategory; |
8
|
|
|
use Dynamic\FoxyStripe\Page\ProductHolder; |
9
|
|
|
use Dynamic\FoxyStripe\Page\ProductPage; |
10
|
|
|
use SilverStripe\ORM\DB; |
11
|
|
|
|
12
|
|
|
class ProductPageTest extends FS_Test |
13
|
|
|
{ |
14
|
|
|
protected static $use_draft_site = true; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
18
|
|
|
*/ |
19
|
|
|
public function setUp() |
20
|
|
|
{ |
21
|
|
|
parent::setUp(); |
22
|
|
|
|
23
|
|
|
$groupForItem = OptionGroup::create(); |
24
|
|
|
$groupForItem->Title = 'Sample-Group'; |
25
|
|
|
$groupForItem->write(); |
26
|
|
|
|
27
|
|
|
/*$productHolder = ProductHolder::create(); |
28
|
|
|
$productHolder->Title = 'Product Holder'; |
29
|
|
|
$productHolder->write();*/ |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testProductCreation() |
33
|
|
|
{ |
34
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
35
|
|
|
$default = $this->objFromFixture(ProductCategory::class, 'default'); |
|
|
|
|
36
|
|
|
$holder = $this->objFromFixture(ProductHolder::class, 'default'); |
|
|
|
|
37
|
|
|
$product1 = $this->objFromFixture(ProductPage::class, 'product1'); |
38
|
|
|
|
39
|
|
|
$product1->doPublish(); |
40
|
|
|
$this->assertTrue($product1->isPublished()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testProductDeletion() |
44
|
|
|
{ |
45
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
46
|
|
|
$holder = $this->objFromFixture(ProductHolder::class, 'default'); |
47
|
|
|
$holder->write(); |
48
|
|
|
$product2 = $this->objFromFixture(ProductPage::class, 'product2'); |
49
|
|
|
$productID = $product2->ID; |
50
|
|
|
|
51
|
|
|
$product2->write(); |
52
|
|
|
$this->assertTrue($product2->exists()); |
53
|
|
|
|
54
|
|
|
$versions = DB::query('Select * FROM "ProductPage_Versions" WHERE "RecordID" = '.$productID); |
55
|
|
|
$versionsPostPublish = array(); |
56
|
|
|
foreach ($versions as $versionRow) { |
57
|
|
|
$versionsPostPublish[] = $versionRow; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$product2->delete(); |
61
|
|
|
$this->assertTrue(!$product2->exists()); |
62
|
|
|
|
63
|
|
|
$versions = DB::query('Select * FROM "ProductPage_Versions" WHERE "RecordID" = '.$productID); |
64
|
|
|
$versionsPostDelete = array(); |
65
|
|
|
foreach ($versions as $versionRow) { |
66
|
|
|
$versionsPostDelete[] = $versionRow; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->assertTrue($versionsPostPublish == $versionsPostDelete); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
74
|
|
|
*/ |
75
|
|
|
public function testProductTitleLeadingWhiteSpace() |
76
|
|
|
{ |
77
|
|
|
$this->logInWithPermission('ADMIN'); |
78
|
|
|
|
79
|
|
|
$holder = $this->objFromFixture(ProductHolder::class, 'default'); |
80
|
|
|
$holder->write(); |
81
|
|
|
|
82
|
|
|
$product = $this->objFromFixture(ProductPage::class, 'product1'); |
83
|
|
|
$product->Title = ' Test with leading space'; |
84
|
|
|
$product->write(); |
85
|
|
|
|
86
|
|
|
$this->assertTrue($product->Title == 'Test with leading space'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
91
|
|
|
*/ |
92
|
|
|
public function testProductTitleTrailingWhiteSpace() |
93
|
|
|
{ |
94
|
|
|
$this->logInWithPermission('ADMIN'); |
95
|
|
|
|
96
|
|
|
$holder = $this->objFromFixture(ProductHolder::class, 'default'); |
97
|
|
|
$holder->write(); |
98
|
|
|
|
99
|
|
|
$product = $this->objFromFixture(ProductPage::class, 'product1'); |
100
|
|
|
$product->Title = 'Test with trailing space '; |
101
|
|
|
$product->write(); |
102
|
|
|
|
103
|
|
|
$this->assertTrue($product->Title == 'Test with trailing space'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testProductCategoryCreation() |
107
|
|
|
{ |
108
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
109
|
|
|
$category = $this->objFromFixture(ProductCategory::class, 'apparel'); |
110
|
|
|
$categoryID = $category->ID; |
111
|
|
|
|
112
|
|
|
$productCategory = ProductCategory::get()->filter(array('Code' => 'APPAREL'))->first(); |
113
|
|
|
|
114
|
|
|
$this->assertTrue($categoryID == $productCategory->ID); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testProductCategoryDeletion() |
118
|
|
|
{ |
119
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
120
|
|
|
|
121
|
|
|
$category = $this->objFromFixture(ProductCategory::class, 'default'); |
122
|
|
|
|
123
|
|
|
$this->assertFalse($category->canDelete()); |
124
|
|
|
|
125
|
|
|
$category2 = $this->objFromFixture(ProductCategory::class, 'apparel'); |
126
|
|
|
$category2ID = $category2->ID; |
127
|
|
|
|
128
|
|
|
$this->assertTrue($category2->canDelete()); |
129
|
|
|
|
130
|
|
|
$this->logOut(); |
131
|
|
|
|
132
|
|
|
$this->logInWithPermission('ADMIN'); |
133
|
|
|
|
134
|
|
|
$this->assertFalse($category->canDelete()); |
135
|
|
|
$this->assertTrue($category2->canDelete()); |
136
|
|
|
|
137
|
|
|
$this->logOut(); |
138
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
139
|
|
|
|
140
|
|
|
$category2->delete(); |
141
|
|
|
|
142
|
|
|
$this->assertFalse(in_array($category2ID, ProductCategory::get()->column('ID'))); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testOptionGroupCreation() |
146
|
|
|
{ |
147
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
148
|
|
|
|
149
|
|
|
$group = $this->objFromFixture(OptionGroup::class, 'size'); |
150
|
|
|
$group->write(); |
151
|
|
|
|
152
|
|
|
$this->assertNotNull(OptionGroup::get()->first()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testOptionGroupDeletion() |
156
|
|
|
{ |
157
|
|
|
$this->logInWithPermission('ADMIN'); |
158
|
|
|
$group = $this->objFromFixture(OptionGroup::class, 'color'); |
159
|
|
|
$group->write(); |
160
|
|
|
$groupID = $group->ID; |
161
|
|
|
|
162
|
|
|
$this->assertTrue($group->canDelete()); |
163
|
|
|
|
164
|
|
|
$this->logOut(); |
165
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
166
|
|
|
|
167
|
|
|
$this->assertTrue($group->canDelete()); |
168
|
|
|
$group->delete(); |
169
|
|
|
|
170
|
|
|
$this->assertFalse(in_array($groupID, OptionGroup::get()->column('ID'))); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testOptionItemCreation() |
174
|
|
|
{ |
175
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
176
|
|
|
|
177
|
|
|
$optionGroup = OptionGroup::get()->filter(array('Title' => 'Sample-Group'))->first(); |
178
|
|
|
|
179
|
|
|
$option = $this->objFromFixture(OptionItem::class, 'large'); |
180
|
|
|
$option->ProductOptionGroupID = $optionGroup->ID; |
181
|
|
|
$option->write(); |
182
|
|
|
|
183
|
|
|
$optionID = $option->ID; |
184
|
|
|
|
185
|
|
|
$optionItem = OptionItem::get()->filter(array('ProductOptionGroupID' => $optionGroup->ID))->first(); |
186
|
|
|
|
187
|
|
|
$this->assertEquals($optionID, $optionItem->ID); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testOptionItemDeletion() |
191
|
|
|
{ |
192
|
|
|
$this->logInWithPermission('ADMIN'); |
193
|
|
|
|
194
|
|
|
$optionGroup = $this->objFromFixture(OptionGroup::class, 'size'); |
195
|
|
|
$optionGroup->write(); |
196
|
|
|
|
197
|
|
|
$option = $this->objFromFixture(OptionItem::class, 'small'); |
198
|
|
|
$option->write(); |
199
|
|
|
|
200
|
|
|
$optionID = $option->ID; |
201
|
|
|
|
202
|
|
|
$this->assertTrue($option->canDelete()); |
203
|
|
|
|
204
|
|
|
$this->logOut(); |
205
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
206
|
|
|
|
207
|
|
|
$this->assertTrue($option->canDelete()); |
208
|
|
|
$option->delete(); |
209
|
|
|
|
210
|
|
|
$this->assertFalse(in_array($optionID, OptionItem::get()->column('ID'))); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testProductDraftOptionDeletion() |
214
|
|
|
{ |
215
|
|
|
self::$use_draft_site = false;//make sure we can publish |
216
|
|
|
|
217
|
|
|
$this->logInWithPermission('ADMIN'); |
218
|
|
|
|
219
|
|
|
$holder = $this->objFromFixture(ProductHolder::class, 'default'); |
220
|
|
|
//build holder page, ProductPage can't be on root level |
221
|
|
|
$holder->doPublish(); |
222
|
|
|
|
223
|
|
|
$product = $this->objFromFixture(ProductPage::class, 'product1');//build product page |
224
|
|
|
$product->doPublish(); |
225
|
|
|
|
226
|
|
|
$productID = $product->ID; |
227
|
|
|
|
228
|
|
|
$optionGroup = $this->objFromFixture(OptionGroup::class, 'size'); |
229
|
|
|
//build the group for the options |
230
|
|
|
$optionGroup->write(); |
231
|
|
|
$option = $this->objFromFixture(OptionItem::class, 'small');//build first option |
232
|
|
|
$option->write(); |
233
|
|
|
$option2 = $this->objFromFixture(OptionItem::class, 'large');//build second option |
234
|
|
|
$option2->write(); |
235
|
|
|
|
236
|
|
|
$this->assertTrue($product->isPublished());//check that product is published |
237
|
|
|
|
238
|
|
|
$product->deleteFromStage('Stage');//remove product from draft site |
239
|
|
|
|
240
|
|
|
$this->assertTrue($product->isPublished());//check product is still published |
241
|
|
|
|
242
|
|
|
$testOption = $this->objFromFixture(OptionItem::class, 'large'); |
243
|
|
|
|
244
|
|
|
$this->assertThat($testOption->ID, $this->logicalNot($this->equalTo(0))); |
245
|
|
|
//make sure the first option still exists |
246
|
|
|
|
247
|
|
|
$product->doRestoreToStage();//restore page to draft site |
248
|
|
|
$product->doUnpublish();//unpublish page |
249
|
|
|
$product->deleteFromStage('Stage');//remove product from draft site |
250
|
|
|
|
251
|
|
|
$checkDeleted = OptionItem::get()->filter(array('Title' => 'Large', 'ProductID' => $productID))->first(); |
252
|
|
|
//query same option as above |
253
|
|
|
|
254
|
|
|
$this->assertEquals($checkDeleted->ID, 0);//check that the ID is 0 (empty object/non-existent) |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|