Passed
Pull Request — master (#41)
by Jason
03:42
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;
8
use Dynamic\Foxy\Model\ProductOption;
9
use Dynamic\Foxy\Model\Setting;
10
use Dynamic\Foxy\Test\TestOnly\TestProduct;
11
use SilverStripe\Core\Config\Config;
12
use SilverStripe\Core\Injector\Injector;
13
use SilverStripe\Dev\Debug;
14
use SilverStripe\Dev\SapphireTest;
15
use SilverStripe\Forms\FieldList;
16
use SilverStripe\i18n\i18n;
17
use SilverStripe\ORM\ValidationException;
18
use SilverStripe\Security\Member;
19
20
class PurchasableTest extends SapphireTest
21
{
22
    /**
23
     * @var string
24
     */
25
    protected static $fixture_file = '../fixtures.yml';
26
27
    /**
28
     * @var array
29
     */
30
    protected static $extra_dataobjects = [
31
        TestProduct::class,
32
    ];
33
34
    /**
35
     *
36
     */
37
    public function setUp()
38
    {
39
        Config::modify()->set(OptionType::class, 'has_one', ['TestProduct' => TestProduct::class]);
40
41
        return parent::setUp();
42
    }
43
44
    /**
45
     *
46
     */
47
    public function testUpdateCMSFields()
48
    {
49
        $object = Injector::inst()->create(TestProduct::class);
50
        $fields = $object->getCMSFields();
51
        $this->assertInstanceOf(FieldList::class, $fields);
52
53
        $object->Price = 10.00;
54
        $object->Code = 000123;
55
        $object->FoxyCategoryID = $this->objFromFixture(FoxyCategory::class, 'one')->ID;
56
57
        $object->write();
58
        $fields = $object->getCMSFields();
59
        $this->assertInstanceOf(FieldList::class, $fields);
60
    }
61
62
    /**
63
     *
64
     */
65
    public function testValidate()
66
    {
67
        $object = Injector::inst()->create(TestProduct::class);
68
        $object->Price = '';
69
        $this->setExpectedException(ValidationException::class);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit_Framework_TestCase::setExpectedException() has been deprecated: Method deprecated since Release 5.2.0; use expectException() instead ( Ignorable by Annotation )

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

69
        /** @scrutinizer ignore-deprecated */ $this->setExpectedException(ValidationException::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
70
        $object->write();
71
72
        $object->Price = '10.00';
73
        $object->Code = '';
74
        $this->setExpectedException(ValidationException::class);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit_Framework_TestCase::setExpectedException() has been deprecated: Method deprecated since Release 5.2.0; use expectException() instead ( Ignorable by Annotation )

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

74
        /** @scrutinizer ignore-deprecated */ $this->setExpectedException(ValidationException::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
75
        $object->write();
76
77
        $object->Code = '123';
78
        $object->FoxyCategoryID = '';
79
        $this->setExpectedException(ValidationException::class);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit_Framework_TestCase::setExpectedException() has been deprecated: Method deprecated since Release 5.2.0; use expectException() instead ( Ignorable by Annotation )

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

79
        /** @scrutinizer ignore-deprecated */ $this->setExpectedException(ValidationException::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
80
        $object->write();
81
    }
82
83
    /**
84
     *
85
     */
86
    public function testIsAvailable()
87
    {
88
        $object = Injector::inst()->create(TestProduct::class);
89
        $this->assertTrue($object->isAvailable());
90
91
        $object->Available = 0;
92
        $this->assertFalse($object->isAvailable());
93
94
        $object->Available = 1;
95
        $type = Injector::inst()->create(OptionType::class);
96
        $type->Title = 'Size';
97
        $type->Options()->add($this->objFromFixture(ProductOption::class, 'small'));
98
        $type->Options()->add($this->objFromFixture(ProductOption::class, 'large'));
99
        $this->assertTrue($object->isAvailable());
100
    }
101
102
    /**
103
     *
104
     */
105
    public function testIsProduct()
106
    {
107
        $object = Injector::inst()->create(TestProduct::class);
108
        $this->assertTrue($object->isProduct());
109
    }
110
111
    /**
112
     *
113
     */
114
    public function testProvidePermissions()
115
    {
116
        /** @var TestProduct $object */
117
        $object = singleton(Purchasable::class);
118
119
        i18n::set_locale('en');
120
        $expected = [
121
            'MANAGE_PRODUCTS' => [
122
                'name' => 'Manage products',
123
                'category' => 'Foxy',
124
                'help' => 'Manage products and related settings',
125
                'sort' => 400
126
            ]
127
        ];
128
        $this->assertEquals($expected, $object->providePermissions());
129
    }
130
131
    /**
132
     *
133
     */
134
    public function testCanCreate()
135
    {
136
        /** @var TestProduct $object */
137
        $object = singleton(TestProduct::class);
138
        /** @var \SilverStripe\Security\Member $admin */
139
        $admin = $this->objFromFixture(Member::class, 'admin');
140
        /** @var \SilverStripe\Security\Member $siteOwner */
141
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
142
        /** @var \SilverStripe\Security\Member $default */
143
        $default = $this->objFromFixture(Member::class, 'default');
144
145
        $this->assertFalse($object->canCreate($default));
146
        $this->assertTrue($object->canCreate($admin));
147
        $this->assertTrue($object->canCreate($siteOwner));
148
    }
149
150
    /**
151
     *
152
     */
153
    public function testCanEdit()
154
    {
155
        /** @var TestProduct $object */
156
        $object = singleton(TestProduct::class);
157
        /** @var \SilverStripe\Security\Member $admin */
158
        $admin = $this->objFromFixture(Member::class, 'admin');
159
        /** @var \SilverStripe\Security\Member $siteOwner */
160
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
161
        /** @var \SilverStripe\Security\Member $default */
162
        $default = $this->objFromFixture(Member::class, 'default');
163
164
        $this->assertFalse($object->canEdit($default));
165
        $this->assertTrue($object->canEdit($admin));
166
        $this->assertTrue($object->canEdit($siteOwner));
167
    }
168
169
    /**
170
     *
171
     */
172
    public function testCanDelete()
173
    {
174
        /** @var TestProduct $object */
175
        $object = singleton(TestProduct::class);
176
        /** @var \SilverStripe\Security\Member $admin */
177
        $admin = $this->objFromFixture(Member::class, 'admin');
178
        /** @var \SilverStripe\Security\Member $siteOwner */
179
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
180
        /** @var \SilverStripe\Security\Member $default */
181
        $default = $this->objFromFixture(Member::class, 'default');
182
183
        $this->assertFalse($object->canDelete($default));
184
        $this->assertTrue($object->canDelete($admin));
185
        $this->assertTrue($object->canDelete($siteOwner));
186
    }
187
188
    /**
189
     *
190
     */
191
    public function testCanUnpublish()
192
    {
193
        /** @var TestProduct $object */
194
        $object = singleton(TestProduct::class);
195
        /** @var \SilverStripe\Security\Member $admin */
196
        $admin = $this->objFromFixture(Member::class, 'admin');
197
        /** @var \SilverStripe\Security\Member $siteOwner */
198
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
199
        /** @var \SilverStripe\Security\Member $default */
200
        $default = $this->objFromFixture(Member::class, 'default');
201
202
        $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

202
        $this->assertFalse($object->/** @scrutinizer ignore-call */ canUnpublish($default));
Loading history...
203
        $this->assertTrue($object->canUnpublish($admin));
204
        $this->assertTrue($object->canUnpublish($siteOwner));
205
    }
206
207
    /**
208
     *
209
     */
210
    public function testCanArchive()
211
    {
212
        /** @var TestProduct $object */
213
        $object = singleton(TestProduct::class);
214
        /** @var \SilverStripe\Security\Member $admin */
215
        $admin = $this->objFromFixture(Member::class, 'admin');
216
        /** @var \SilverStripe\Security\Member $siteOwner */
217
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
218
        /** @var \SilverStripe\Security\Member $default */
219
        $default = $this->objFromFixture(Member::class, 'default');
220
221
        $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

221
        $this->assertFalse($object->/** @scrutinizer ignore-call */ canArchive($default));
Loading history...
222
        $this->assertTrue($object->canArchive($admin));
223
        $this->assertTrue($object->canArchive($siteOwner));
224
    }
225
}
226