Passed
Push — master ( eb8b5a...80a24d )
by Nic
02:40
created

PurchasableTest::testCanCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Foxy\Test\Extension;
4
5
use Dynamic\Foxy\Extension\Purchasable;
6
use Dynamic\Foxy\Model\FoxyCategory;
7
use Dynamic\Foxy\Model\OptionType;
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Model\OptionType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Dynamic\Foxy\Model\ProductOption;
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Model\ProductOption was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Dynamic\Foxy\Model\Setting;
10
use Dynamic\Foxy\Model\Variation;
11
use Dynamic\Foxy\Test\TestOnly\TestProduct;
12
use Dynamic\Foxy\Test\TestOnly\TestVariationDataExtension;
13
use SilverStripe\Core\Config\Config;
14
use SilverStripe\Core\Injector\Injector;
15
use SilverStripe\Dev\Debug;
16
use SilverStripe\Dev\SapphireTest;
17
use SilverStripe\Forms\FieldList;
18
use SilverStripe\Forms\GridField\GridField;
19
use SilverStripe\i18n\i18n;
20
use SilverStripe\ORM\ValidationException;
21
use SilverStripe\Security\Member;
22
use SilverStripe\Versioned\Versioned;
23
24
/**
25
 * Class PurchasableTest
26
 * @package Dynamic\Foxy\Test\Extension
27
 */
28
class PurchasableTest extends SapphireTest
29
{
30
    /**
31
     * @var string
32
     */
33
    protected static $fixture_file = [
34
        '../fixtures.yml',
35
        '../purchasableproducts.yml',
36
    ];
37
38
    /**
39
     * @var array
40
     */
41
    protected static $extra_dataobjects = [
42
        TestProduct::class,
43
    ];
44
45
    /**
46
     * @var \string[][]
47
     */
48
    protected static $required_extensions = [
49
        TestProduct::class => [
50
            Purchasable::class,
51
        ],
52
        Variation::class => [
53
            TestVariationDataExtension::class,
54
        ],
55
    ];
56
57
    /**
58
     *
59
     */
60
    public function testUpdateCMSFields()
61
    {
62
        $newProduct = TestProduct::singleton();
63
        $fields = $newProduct->getCMSFields();
64
65
        $this->assertInstanceOf(FieldList::class, $fields);
66
        $this->assertNull($fields->dataFieldByName('SKU'));
67
68
        $existingProduct = $this->objFromFixture(TestProduct::class, 'productone');
69
        $existingFields = $existingProduct->getCMSFields();
70
71
        $this->assertNull($existingFields->dataFieldByName('SKU'));
72
        $this->assertInstanceOf(GridField::class, $existingFields->dataFieldByName('Variations'));
73
    }
74
75
    /**
76
     *
77
     */
78
    public function testGetIsAvailable()
79
    {
80
        /** @var TestProduct $availableProduct */
81
        $availableProduct = $this->objFromFixture(TestProduct::class, 'productone');
82
        $this->assertTrue($availableProduct->getIsAvailable());
0 ignored issues
show
Bug introduced by
The method getIsAvailable() does not exist on Dynamic\Foxy\Test\TestOnly\TestProduct. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        $this->assertTrue($availableProduct->/** @scrutinizer ignore-call */ getIsAvailable());
Loading history...
83
84
        /** @var TestProduct $unavailableProduct */
85
        $unavailableProduct = $this->objFromFixture(TestProduct::class, 'productwo');
86
        $this->assertFalse($unavailableProduct->getIsAvailable());
87
88
        /** @var TestProduct $availableVariations */
89
        $availableVariations = $this->objFromFixture(TestProduct::class, 'productfour');
90
        $this->assertTrue($availableVariations->getIsAvailable());
91
92
        /** @var TestProduct $unavailableVariations */
93
        $unavailableVariations = $this->objFromFixture(TestProduct::class, 'productthree');
94
        $this->assertFalse($unavailableVariations->getIsAvailable());
95
    }
96
97
    /**
98
     *
99
     */
100
    public function testIsProduct()
101
    {
102
        /** @var TestProduct $object */
103
        $object = TestProduct::singleton();
104
105
        $this->assertTrue($object->isProduct());
0 ignored issues
show
Bug introduced by
The method isProduct() does not exist on Dynamic\Foxy\Test\TestOnly\TestProduct. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
        $this->assertTrue($object->/** @scrutinizer ignore-call */ isProduct());
Loading history...
106
    }
107
108
    /**
109
     *
110
     */
111
    public function testProvidePermissions()
112
    {
113
        /** @var Purchasable $object */
114
        $object = singleton(Purchasable::class);
115
116
        i18n::set_locale('en');
117
        $expected = [
118
            'MANAGE_FOXY_PRODUCTS' => [
119
                'name' => 'Manage products',
120
                'category' => 'Foxy',
121
                'help' => 'Manage products and related settings',
122
                'sort' => 400,
123
            ],
124
        ];
125
        $this->assertEquals($expected, $object->providePermissions());
126
    }
127
128
    /**
129
     *
130
     */
131
    public function testCanCreate()
132
    {
133
        /** @var TestProduct $object */
134
        $object = TestProduct::singleton();
135
        /** @var Member $admin */
136
        $admin = $this->objFromFixture(Member::class, 'admin');
137
        /** @var Member $siteOwner */
138
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
139
        /** @var Member $default */
140
        $default = $this->objFromFixture(Member::class, 'default');
141
142
        $this->assertFalse($object->canCreate($default));
143
        $this->assertTrue($object->canCreate($admin));
144
        $this->assertTrue($object->canCreate($siteOwner));
145
    }
146
147
    /**
148
     *
149
     */
150
    public function testCanEdit()
151
    {
152
        /** @var TestProduct $object */
153
        $object = TestProduct::singleton();
154
        /** @var Member $admin */
155
        $admin = $this->objFromFixture(Member::class, 'admin');
156
        /** @var Member $siteOwner */
157
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
158
        /** @var Member $default */
159
        $default = $this->objFromFixture(Member::class, 'default');
160
161
        $this->assertFalse($object->canEdit($default));
162
        $this->assertTrue($object->canEdit($admin));
163
        $this->assertTrue($object->canEdit($siteOwner));
164
    }
165
166
    /**
167
     *
168
     */
169
    public function testCanDelete()
170
    {
171
        /** @var TestProduct $object */
172
        $object = TestProduct::singleton();
173
        /** @var Member $admin */
174
        $admin = $this->objFromFixture(Member::class, 'admin');
175
        /** @var Member $siteOwner */
176
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
177
        /** @var Member $default */
178
        $default = $this->objFromFixture(Member::class, 'default');
179
180
        $this->assertFalse($object->canDelete($default));
181
        $this->assertTrue($object->canDelete($admin));
182
        $this->assertTrue($object->canDelete($siteOwner));
183
    }
184
185
    /**
186
     *
187
     */
188
    public function testCanUnpublish()
189
    {
190
        /** @var TestProduct $object */
191
        $object = TestProduct::singleton();
192
        /** @var Member $admin */
193
        $admin = $this->objFromFixture(Member::class, 'admin');
194
        /** @var Member $siteOwner */
195
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
196
        /** @var Member $default */
197
        $default = $this->objFromFixture(Member::class, 'default');
198
199
        $this->assertFalse($object->canUnpublish($default));
0 ignored issues
show
Bug introduced by
The method canUnpublish() does not exist on Dynamic\Foxy\Test\TestOnly\TestProduct. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

199
        $this->assertFalse($object->/** @scrutinizer ignore-call */ canUnpublish($default));
Loading history...
200
        $this->assertTrue($object->canUnpublish($admin));
201
        $this->assertTrue($object->canUnpublish($siteOwner));
202
    }
203
204
    /**
205
     *
206
     */
207
    public function testCanArchive()
208
    {
209
        /** @var TestProduct $object */
210
        $object = TestProduct::singleton();
211
        /** @var Member $admin */
212
        $admin = $this->objFromFixture(Member::class, 'admin');
213
        /** @var Member $siteOwner */
214
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
215
        /** @var Member $default */
216
        $default = $this->objFromFixture(Member::class, 'default');
217
218
        $this->assertFalse($object->canArchive($default));
0 ignored issues
show
Bug introduced by
The method canArchive() does not exist on Dynamic\Foxy\Test\TestOnly\TestProduct. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

218
        $this->assertFalse($object->/** @scrutinizer ignore-call */ canArchive($default));
Loading history...
219
        $this->assertTrue($object->canArchive($admin));
220
        $this->assertTrue($object->canArchive($siteOwner));
221
    }
222
223
    /**
224
     *
225
     */
226
    public function testBeforeWrite()
227
    {
228
        $product = TestProduct::create();
229
        $product->Title = 'My New Product';
230
        $product->Code = ' foo- bar  -baz ';
231
232
        $this->assertEquals(' foo- bar  -baz ', $product->Code);
233
        $product->writeToStage(Versioned::DRAFT);
234
235
        $product = TestProduct::get()->byID($product->ID);
236
        $this->assertEquals('foo- bar -baz', $product->Code);
237
    }
238
}
239