1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Discounts\Tests\Page; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Discounts\DiscountHelper; |
6
|
|
|
use Dynamic\Foxy\Discounts\Extension\ProductDataExtension; |
7
|
|
|
use Dynamic\Foxy\Discounts\Model\Discount; |
8
|
|
|
use Dynamic\Foxy\Discounts\Tests\TestOnly\Extension\TestDiscountExtension; |
9
|
|
|
use Dynamic\Foxy\Discounts\Tests\TestOnly\Extension\VariationDataExtension; |
10
|
|
|
use Dynamic\Foxy\Discounts\Tests\TestOnly\Page\ProductPage; |
11
|
|
|
use Dynamic\Foxy\Extension\Purchasable; |
12
|
|
|
use Dynamic\Foxy\Model\Variation; |
13
|
|
|
use SilverStripe\Core\Config\Config; |
14
|
|
|
use SilverStripe\Dev\SapphireTest; |
15
|
|
|
use SilverStripe\Versioned\Versioned; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ProductPageDiscountTest |
19
|
|
|
* @package Dynamic\Foxy\Discounts\Tests\Page |
20
|
|
|
*/ |
21
|
|
|
class ProductDataExtensionTest extends SapphireTest |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string[] |
25
|
|
|
*/ |
26
|
|
|
protected static $fixture_file = [ |
27
|
|
|
'../products.yml', |
28
|
|
|
'../discounts.yml', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string[] |
33
|
|
|
*/ |
34
|
|
|
protected static $extra_dataobjects = [ |
35
|
|
|
ProductPage::class, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \string[][] |
40
|
|
|
*/ |
41
|
|
|
protected static $required_extensions = [ |
42
|
|
|
ProductPage::class => [ |
43
|
|
|
Purchasable::class, |
44
|
|
|
ProductDataExtension::class, |
45
|
|
|
], |
46
|
|
|
Discount::class => [ |
47
|
|
|
TestDiscountExtension::class, |
48
|
|
|
], |
49
|
|
|
Variation::class => [ |
50
|
|
|
VariationDataExtension::class, |
51
|
|
|
], |
52
|
|
|
|
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string[] |
57
|
|
|
*/ |
58
|
|
|
protected static $illegal_extensions = [ |
59
|
|
|
/*Discount::class => [ |
60
|
|
|
'Dynamic\\FoxyRecipe\\Extension\\DiscountDataExtension', |
61
|
|
|
],//*/ |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
*/ |
67
|
|
|
protected function setUp() |
68
|
|
|
{ |
69
|
|
|
if (class_exists('Dynamic\\Foxy\\API\\Client\\APIClient')) { |
70
|
|
|
Config::modify()->set('Dynamic\\Foxy\\API\\Client\\APIClient', 'enable_api', false); |
71
|
|
|
} |
72
|
|
|
if (class_exists('Dynamic\\Foxy\\SingleSignOn\\Client\\CustomerClient')) { |
73
|
|
|
Config::modify()->set('Dynamic\\Foxy\\SingleSignOn\\Client\\CustomerClient', 'foxy_sso_enabled', false); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
parent::setUp(); |
77
|
|
|
|
78
|
|
|
$product = $this->objFromFixture(ProductPage::class, 'productthree'); |
79
|
|
|
$product->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
80
|
|
|
$discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage'); |
81
|
|
|
$discountOne->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
82
|
|
|
$discountTwo = $this->objFromFixture(Discount::class, 'tierdiscountpercentage'); |
83
|
|
|
$discountTwo->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
84
|
|
|
|
85
|
|
|
Versioned::set_stage(Versioned::LIVE); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* |
90
|
|
|
*/ |
91
|
|
|
public function testGetBestDiscount() |
92
|
|
|
{ |
93
|
|
|
$product = $product = $this->objFromFixture(ProductPage::class, 'productthree'); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$this->assertInstanceOf(DiscountHelper::class, $product->getBestDiscount()); |
96
|
|
|
|
97
|
|
|
//TODO test user_error for a 0 value |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* |
102
|
|
|
*/ |
103
|
|
|
public function testGetDiscountPrice() |
104
|
|
|
{ |
105
|
|
|
$product = $this->objFromFixture(ProductPage::class, 'productthree'); |
106
|
|
|
|
107
|
|
|
$this->assertEquals(75, $product->getDiscountPrice()->getValue()); |
108
|
|
|
|
109
|
|
|
$discountOne = $this->objFromFixture(Discount::class, 'simplediscountpercentage'); |
110
|
|
|
$discountOne->doUnpublish(); |
111
|
|
|
|
112
|
|
|
$this->assertEquals(70, $product->getDiscountPrice(23)->getValue()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Expected failure in local tests of a foxy-recipe-installer |
117
|
|
|
*/ |
118
|
|
|
public function testGetHasDiscount() |
119
|
|
|
{ |
120
|
|
|
$product = $this->objFromFixture(ProductPage::class, 'productthree'); |
121
|
|
|
|
122
|
|
|
$this->assertTrue($product->getHasDiscount()); |
123
|
|
|
|
124
|
|
|
$newProduct = ProductPage::create(); |
125
|
|
|
$newProduct->Title = 'No Discount Product'; |
126
|
|
|
$newProduct->Code = 'no-discount-product'; |
127
|
|
|
$newProduct->Price = 1000; |
128
|
|
|
$newProduct->writeToStage(Versioned::DRAFT); |
129
|
|
|
$newProduct->publishSingle(); |
130
|
|
|
|
131
|
|
|
/** Expected failure in local tests of a foxy-recipe-installer */ |
132
|
|
|
Discount::get()->each(function (Discount $discount) use ($newProduct) { |
133
|
|
|
$discount->ExcludeProducts()->add($newProduct); |
|
|
|
|
134
|
|
|
}); |
135
|
|
|
|
136
|
|
|
$this->assertFalse($newProduct->getHasDiscount()); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|