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

SettingsTest::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\Setting;
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\Dev\SapphireTest;
12
use SilverStripe\Forms\FieldList;
13
use SilverStripe\Security\Member;
14
15
/**
16
 * Class SettingsTest
17
 * @package Dynamic\Foxy\Test\Model
18
 */
19
class SettingsTest extends SapphireTest
20
{
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
        TestVariation::class,
33
    ];
34
35
    /**
36
     * @var \string[][]
37
     */
38
    protected static $required_extensions = [
39
        TestProduct::class => [
40
            Purchasable::class,
41
        ],
42
    ];
43
44
    /**
45
     *
46
     */
47
    public function setUp()
48
    {
49
        Config::modify()->set(TestVariation::class, 'has_one', ['Product' => TestProduct::class]);
50
51
        return parent::setUp();
52
    }
53
54
    /**
55
     *
56
     */
57
    public function testGetCMSFields()
58
    {
59
        /** @var Setting $object */
60
        $object = singleton(Setting::class);
61
        $fields = $object->getCMSFields();
62
        $this->assertInstanceOf(FieldList::class, $fields);
63
    }
64
65
    /**
66
     *
67
     */
68
    public function testGetCMSActions()
69
    {
70
        /** @var Setting $object */
71
        $object = singleton(Setting::class);
72
        $fields = $object->getCMSActions();
73
        $this->assertInstanceOf(FieldList::class, $fields);
74
    }
75
76
    /**
77
     *
78
     */
79
    public function testOnBeforeWrite()
80
    {
81
        /** @var Setting $object */
82
        $object = Setting::create();
83
        $this->assertNull($object->StoreKey);
84
85
        $object->write();
86
        $this->assertInternalType('string', $object->StoreKey);
87
    }
88
89
    /**
90
     *
91
     */
92
    public function testGenerateStoreKey()
93
    {
94
        /** @var Setting $object */
95
        $object = singleton(Setting::class);
96
        $key = $object->generateStoreKey();
97
        $this->assertEquals(60, strlen($key));
98
        $this->assertEquals('dYnm1c', substr($key, 0, 6));
99
    }
100
101
    /**
102
     *
103
     */
104
    public function testCanEdit()
105
    {
106
        /** @var Setting $object */
107
        $object = singleton(Setting::class);
108
        /** @var \SilverStripe\Security\Member $admin */
109
        $admin = $this->objFromFixture(Member::class, 'admin');
110
        /** @var \SilverStripe\Security\Member $siteOwner */
111
        $siteOwner = $this->objFromFixture(Member::class, 'site-owner');
112
        /** @var \SilverStripe\Security\Member $default */
113
        $default = $this->objFromFixture(Member::class, 'default');
114
115
        $this->assertFalse($object->canEdit($default));
116
        $this->assertTrue($object->canEdit($admin));
117
        $this->assertTrue($object->canEdit($siteOwner));
118
    }
119
120
    /**
121
     *
122
     */
123
    public function testProvidePermissions()
124
    {
125
        /** @var Setting $object */
126
        $object = singleton(Setting::class);
127
        $this->assertInternalType('array', $object->providePermissions());
128
        $this->assertArrayHasKey('EDIT_FOXY_SETTING', $object->providePermissions());
129
    }
130
131
    /**
132
     *
133
     */
134
    public function testCurrentFoxySetting()
135
    {
136
        $this->assertInstanceOf(Setting::class, Setting::current_foxy_setting());
137
    }
138
139
    /**
140
     *
141
     */
142
    public function testMakeFoxySetting()
143
    {
144
        $this->assertInstanceOf(Setting::class, Setting::make_foxy_setting());
145
    }
146
147
    /**
148
     *
149
     */
150
    public function testGetTemplateGlobalVariables()
151
    {
152
        $this->assertInternalType('array', Setting::get_template_global_variables());
153
        $this->assertArrayHasKey('FoxyStripe', Setting::get_template_global_variables());
154
    }
155
}
156