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 "FS_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 "FS_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
|
|
|
$category->write(); |
111
|
|
|
$categoryID = $category->ID; |
112
|
|
|
|
113
|
|
|
$productCategory = ProductCategory::get()->filter(array('Code' => 'APPAREL'))->first(); |
114
|
|
|
|
115
|
|
|
$this->assertTrue($categoryID == $productCategory->ID); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testProductCategoryDeletion() |
119
|
|
|
{ |
120
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
121
|
|
|
|
122
|
|
|
$category = $this->objFromFixture(ProductCategory::class, 'default'); |
123
|
|
|
$category->write(); |
124
|
|
|
|
125
|
|
|
$this->assertFalse($category->canDelete()); |
126
|
|
|
|
127
|
|
|
$category2 = $this->objFromFixture(ProductCategory::class, 'apparel'); |
128
|
|
|
$category2->write(); |
129
|
|
|
$category2ID = $category2->ID; |
130
|
|
|
|
131
|
|
|
$this->assertTrue($category2->canDelete()); |
132
|
|
|
|
133
|
|
|
$this->logOut(); |
134
|
|
|
|
135
|
|
|
$this->logInWithPermission('ADMIN'); |
136
|
|
|
|
137
|
|
|
$this->assertFalse($category->canDelete()); |
138
|
|
|
$this->assertTrue($category2->canDelete()); |
139
|
|
|
|
140
|
|
|
$this->logOut(); |
141
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
142
|
|
|
|
143
|
|
|
$category2->delete(); |
144
|
|
|
|
145
|
|
|
$this->assertFalse(in_array($category2ID, ProductCategory::get()->column('ID'))); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testOptionGroupCreation() |
149
|
|
|
{ |
150
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
151
|
|
|
|
152
|
|
|
$group = $this->objFromFixture(OptionGroup::class, 'size'); |
153
|
|
|
$group->write(); |
154
|
|
|
|
155
|
|
|
$this->assertNotNull(OptionGroup::get()->first()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testOptionGroupDeletion() |
159
|
|
|
{ |
160
|
|
|
$this->logInWithPermission('ADMIN'); |
161
|
|
|
$group = $this->objFromFixture(OptionGroup::class, 'color'); |
162
|
|
|
$group->write(); |
163
|
|
|
$groupID = $group->ID; |
164
|
|
|
|
165
|
|
|
$this->assertTrue($group->canDelete()); |
166
|
|
|
|
167
|
|
|
$this->logOut(); |
168
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
169
|
|
|
|
170
|
|
|
$this->assertTrue($group->canDelete()); |
171
|
|
|
$group->delete(); |
172
|
|
|
|
173
|
|
|
$this->assertFalse(in_array($groupID, OptionGroup::get()->column('ID'))); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testOptionItemCreation() |
177
|
|
|
{ |
178
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
179
|
|
|
|
180
|
|
|
$optionGroup = OptionGroup::get()->filter(array('Title' => 'Sample-Group'))->first(); |
181
|
|
|
|
182
|
|
|
$option = $this->objFromFixture(OptionItem::class, 'large'); |
183
|
|
|
$option->ProductOptionGroupID = $optionGroup->ID; |
184
|
|
|
$option->write(); |
185
|
|
|
|
186
|
|
|
$optionID = $option->ID; |
187
|
|
|
|
188
|
|
|
$optionItem = OptionItem::get()->filter(array('ProductOptionGroupID' => $optionGroup->ID))->first(); |
189
|
|
|
|
190
|
|
|
$this->assertEquals($optionID, $optionItem->ID); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testOptionItemDeletion() |
194
|
|
|
{ |
195
|
|
|
$this->logInWithPermission('ADMIN'); |
196
|
|
|
|
197
|
|
|
$optionGroup = $this->objFromFixture(OptionGroup::class, 'size'); |
198
|
|
|
$optionGroup->write(); |
199
|
|
|
|
200
|
|
|
$option = $this->objFromFixture(OptionItem::class, 'small'); |
201
|
|
|
$option->write(); |
202
|
|
|
|
203
|
|
|
$optionID = $option->ID; |
204
|
|
|
|
205
|
|
|
$this->assertTrue($option->canDelete()); |
206
|
|
|
|
207
|
|
|
$this->logOut(); |
208
|
|
|
$this->logInWithPermission('Product_CANCRUD'); |
209
|
|
|
|
210
|
|
|
$this->assertTrue($option->canDelete()); |
211
|
|
|
$option->delete(); |
212
|
|
|
|
213
|
|
|
$this->assertFalse(in_array($optionID, OptionItem::get()->column('ID'))); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testProductDraftOptionDeletion() |
217
|
|
|
{ |
218
|
|
|
self::$use_draft_site = false;//make sure we can publish |
219
|
|
|
|
220
|
|
|
$this->logInWithPermission('ADMIN'); |
221
|
|
|
|
222
|
|
|
$holder = $this->objFromFixture(ProductHolder::class, 'default'); |
223
|
|
|
//build holder page, ProductPage can't be on root level |
224
|
|
|
$holder->doPublish(); |
225
|
|
|
|
226
|
|
|
$product = $this->objFromFixture(ProductPage::class, 'product1');//build product page |
227
|
|
|
$product->doPublish(); |
228
|
|
|
|
229
|
|
|
$productID = $product->ID; |
230
|
|
|
|
231
|
|
|
$optionGroup = $this->objFromFixture(OptionGroup::class, 'size'); |
232
|
|
|
//build the group for the options |
233
|
|
|
$optionGroup->write(); |
234
|
|
|
$option = $this->objFromFixture(OptionItem::class, 'small');//build first option |
235
|
|
|
$option->write(); |
236
|
|
|
$option2 = $this->objFromFixture(OptionItem::class, 'large');//build second option |
237
|
|
|
$option2->write(); |
238
|
|
|
|
239
|
|
|
$this->assertTrue($product->isPublished());//check that product is published |
240
|
|
|
|
241
|
|
|
$product->deleteFromStage('Stage');//remove product from draft site |
242
|
|
|
|
243
|
|
|
$this->assertTrue($product->isPublished());//check product is still published |
244
|
|
|
|
245
|
|
|
$testOption = $this->objFromFixture(OptionItem::class, 'large'); |
246
|
|
|
|
247
|
|
|
$this->assertThat($testOption->ID, $this->logicalNot($this->equalTo(0))); |
248
|
|
|
//make sure the first option still exists |
249
|
|
|
|
250
|
|
|
$product->doRestoreToStage();//restore page to draft site |
251
|
|
|
$product->doUnpublish();//unpublish page |
252
|
|
|
$product->deleteFromStage('Stage');//remove product from draft site |
253
|
|
|
|
254
|
|
|
$checkDeleted = OptionItem::get()->filter(array('Title' => 'Large', 'ProductID' => $productID))->first(); |
255
|
|
|
//query same option as above |
256
|
|
|
|
257
|
|
|
$this->assertEquals($checkDeleted->ID, 0);//check that the ID is 0 (empty object/non-existent) |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.