1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AntonyThorpe\SilverShopJsonResponse\Tests; |
4
|
|
|
|
5
|
|
|
use SilverShop\Cart\ShoppingCartController; |
6
|
|
|
use SilverShop\Forms\VariationForm; |
7
|
|
|
use SilverShop\Tests\ShopTest; |
8
|
|
|
use SilverShop\Model\Order; |
9
|
|
|
use SilverShop\Model\Modifiers\Tax\FlatTax; |
10
|
|
|
use SilverShop\Page\Product; |
11
|
|
|
use SilverShop\Page\ProductCategory; |
12
|
|
|
use SilverShop\Cart\ShoppingCart; |
13
|
|
|
use SilverShop\Model\Variation\Variation; |
14
|
|
|
use SilverStripe\Dev\FunctionalTest; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Functional tests of json responses for shopping cart of Silverstripe Shop |
18
|
|
|
* |
19
|
|
|
* @package shop |
20
|
|
|
* @subpackage tests |
21
|
|
|
*/ |
22
|
|
|
class SilvershopVariationsJsonResponseTest extends FunctionalTest |
23
|
|
|
{ |
24
|
|
|
protected static $fixture_file = [ |
25
|
|
|
'vendor/silvershop/core/tests/php/Fixtures/shop.yml', |
26
|
|
|
'vendor/silvershop/core/tests/php/Fixtures/variations.yml' |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
public $mp3player; |
30
|
|
|
public $socks; |
31
|
|
|
public $cart; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
public function setUpOnce(): void |
35
|
|
|
{ |
36
|
|
|
if (!ShoppingCartController::has_extension('SilvershopJsonResponse')) { |
37
|
|
|
ShoppingCartController::add_extension('SilvershopJsonResponse'); |
38
|
|
|
} |
39
|
|
|
if (!VariationForm::has_extension('SilvershopJsonResponse')) { |
40
|
|
|
VariationForm::add_extension('SilvershopJsonResponse'); |
41
|
|
|
} |
42
|
|
|
parent::setUpOnce(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function setUp(): void |
46
|
|
|
{ |
47
|
|
|
parent::setUp(); |
48
|
|
|
ShopTest::setConfiguration(); //reset config |
49
|
|
|
Order::config()->modifiers = [ FlatTax::class ]; |
50
|
|
|
|
51
|
|
|
// Needed, so that products can be published |
52
|
|
|
$this->logInWithPermission('ADMIN'); |
53
|
|
|
|
54
|
|
|
$this->mp3player = $this->objFromFixture(Product::class, 'mp3player'); |
55
|
|
|
$this->socks = $this->objFromFixture(Product::class, 'socks'); |
56
|
|
|
|
57
|
|
|
//publish some product categories and products |
58
|
|
|
$this->objFromFixture(ProductCategory::class, 'products')->copyVersionToStage('Stage', 'Live'); |
59
|
|
|
$this->objFromFixture(ProductCategory::class, 'clothing')->copyVersionToStage('Stage', 'Live'); |
60
|
|
|
$this->objFromFixture(ProductCategory::class, 'clearance')->copyVersionToStage('Stage', 'Live'); |
61
|
|
|
|
62
|
|
|
$this->mp3player->copyVersionToStage('Stage', 'Live'); |
63
|
|
|
$this->socks->copyVersionToStage('Stage', 'Live'); |
64
|
|
|
|
65
|
|
|
$this->cart = ShoppingCart::singleton(); |
66
|
|
|
$this->cart->clear(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testVariations(): void |
70
|
|
|
{ |
71
|
|
|
$ballRoot = $this->objFromFixture(Product::class, 'ball'); |
72
|
|
|
// add parent for categories in JSON response |
73
|
|
|
$parent = $this->objFromFixture(ProductCategory::class, 'products'); |
74
|
|
|
$ballRoot->ParentID = $parent->ID; |
75
|
|
|
$ballRoot->write(); |
76
|
|
|
|
77
|
|
|
$ballRoot->copyVersionToStage('Stage', 'Live'); |
78
|
|
|
$ball1 = $this->objFromFixture(Variation::class, 'redLarge'); |
79
|
|
|
$ball2 = $this->objFromFixture(Variation::class, 'redSmall'); |
80
|
|
|
$this->logInWithPermission('ADMIN'); |
81
|
|
|
$ball1->publishSingle(); |
82
|
|
|
$ball2->publishSingle(); |
83
|
|
|
|
84
|
|
|
// Add the two variation items |
85
|
|
|
$response = $this->get(ShoppingCartController::add_item_link($ball1) . "?ajax=1"); |
|
|
|
|
86
|
|
|
$this->assertEquals( |
87
|
|
|
200, |
88
|
|
|
$response->getStatusCode(), |
89
|
|
|
"Response status code is 200" |
90
|
|
|
); |
91
|
|
|
$this->assertEquals( |
92
|
|
|
"application/json; charset=utf-8", |
93
|
|
|
$response->getHeader("Content-Type"), |
94
|
|
|
"Json response header" |
95
|
|
|
); |
96
|
|
|
$this->assertJson( |
97
|
|
|
$response->getBody(), |
98
|
|
|
"Contains json in the body of the response" |
99
|
|
|
); |
100
|
|
|
$this->assertStringContainsString( |
101
|
|
|
"Size:Large, Color:Red", |
102
|
|
|
$response->getBody(), |
103
|
|
|
"Contains json in the body of the cart id" |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$response = $this->get(ShoppingCartController::add_item_link($ball2) . "?ajax=1"); |
|
|
|
|
107
|
|
|
$items = ShoppingCart::curr()->Items(); |
108
|
|
|
$this->assertEquals( |
109
|
|
|
$items->Count(), |
110
|
|
|
2, |
111
|
|
|
"There are 2 items in the cart" |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
// Remove one and see what happens |
115
|
|
|
$this->get(ShoppingCartController::remove_all_item_link($ball1) . "?ajax=1"); |
|
|
|
|
116
|
|
|
$this->assertEquals( |
117
|
|
|
$items->Count(), |
118
|
|
|
1, |
119
|
|
|
"There is 1 item in the cart" |
120
|
|
|
); |
121
|
|
|
$this->assertNull( |
122
|
|
|
$this->cart->get($ball1), |
123
|
|
|
"first item not in cart" |
124
|
|
|
); |
125
|
|
|
$this->assertNotNull( |
126
|
|
|
$this->cart->get($ball2), |
127
|
|
|
"second item is still in cart" |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|