Passed
Pull Request — master (#111)
by Jason
02:35
created

FoxyCategoryTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Foxy\Test\Model;
4
5
use Dynamic\Foxy\Extension\Purchasable;
6
use Dynamic\Foxy\Model\FoxyCategory;
7
use Dynamic\Foxy\Model\Variation;
8
use Dynamic\Foxy\Test\TestOnly\TestProduct;
9
use Dynamic\Foxy\Test\TestOnly\TestVariation;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\Forms\FieldList;
14
use SilverStripe\ORM\ValidationException;
15
use SilverStripe\Security\Member;
16
17
/**
18
 * Class FoxyCategoryTest
19
 * @package Dynamic\Foxy\Test\Model
20
 */
21
class FoxyCategoryTest extends SapphireTest
22
{
23
    /**
24
     * @var string
25
     */
26
    protected static $fixture_file = '../fixtures.yml';
27
28
    /**
29
     * @var array
30
     */
31
    protected static $extra_dataobjects = [
32
        TestProduct::class,
33
        TestVariation::class,
34
    ];
35
36
    /**
37
     * @var \string[][]
38
     */
39
    protected static $required_extensions = [
40
        TestProduct::class => [
41
            Purchasable::class,
42
        ],
43
    ];
44
45
    /**
46
     *
47
     */
48
    public function setUp()
49
    {
50
        Config::modify()->set(TestVariation::class, 'has_one', ['Product' => TestProduct::class]);
51
52
        return parent::setUp();
53
    }
54
55
    /**
56
     *
57
     */
58
    public function testGetCMSFields()
59
    {
60
        $object = $this->objFromFixture(FoxyCategory::class, 'one');
61
        $fields = $object->getCMSFields();
62
        $this->assertInstanceOf(FieldList::class, $fields);
63
64
        $object = Injector::inst()->create(FoxyCategory::class);
65
        $object->Code = 'DEFAULT';
66
        $object->write();
67
        $fields = $object->getCMSFields();
68
        $this->assertInstanceOf(FieldList::class, $fields);
69
    }
70
71
    /**
72
     *
73
     */
74
    public function testValidateCode()
75
    {
76
        $object = $this->objFromFixture(FoxyCategory::class, 'one');
77
        $object->Code = '';
78
        $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

78
        /** @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...
79
        $object->write();
80
81
        $object = $this->objFromFixture(FoxyCategory::class, 'one');
82
        $object->Code = 'DEFAULT';
83
        $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

83
        /** @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...
84
        $object->write();
85
    }
86
87
    /**
88
     *
89
     */
90
    public function testCanCreate()
91
    {
92
        /** @var FoxyCategory $object */
93
        $object = singleton(FoxyCategory::class);
94
        /** @var \SilverStripe\Security\Member $admin */
95
        $admin = $this->objFromFixture(Member::class, 'admin');
96
        /** @var \SilverStripe\Security\Member $siteOwner */
97
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
98
        /** @var \SilverStripe\Security\Member $default */
99
        $default = $this->objFromFixture(Member::class, 'default');
100
101
        $this->assertFalse($object->canCreate($default));
102
        $this->assertTrue($object->canCreate($admin));
103
        $this->assertTrue($object->canCreate($siteOwner));
104
    }
105
106
    /**
107
     *
108
     */
109
    public function testCanEdit()
110
    {
111
        /** @var FoxyCategory $object */
112
        $object = singleton(FoxyCategory::class);
113
        /** @var \SilverStripe\Security\Member $admin */
114
        $admin = $this->objFromFixture(Member::class, 'admin');
115
        /** @var \SilverStripe\Security\Member $siteOwner */
116
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
117
        /** @var \SilverStripe\Security\Member $default */
118
        $default = $this->objFromFixture(Member::class, 'default');
119
120
        $this->assertFalse($object->canEdit($default));
121
        $this->assertTrue($object->canEdit($admin));
122
        $this->assertTrue($object->canEdit($siteOwner));
123
    }
124
125
    /**
126
     *
127
     */
128
    public function testCanDelete()
129
    {
130
        /** @var FoxyCategory $object */
131
        $object = singleton(FoxyCategory::class);
132
        /** @var \SilverStripe\Security\Member $admin */
133
        $admin = $this->objFromFixture(Member::class, 'admin');
134
        /** @var \SilverStripe\Security\Member $siteOwner */
135
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
136
        /** @var \SilverStripe\Security\Member $default */
137
        $default = $this->objFromFixture(Member::class, 'default');
138
139
        $this->assertFalse($object->canDelete($default));
140
        $this->assertTrue($object->canDelete($admin));
141
        $this->assertTrue($object->canDelete($siteOwner));
142
    }
143
}
144