ProductWishListTest::testCanEdit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Dynamic\Wishlist\Test\Model;
4
5
use Dynamic\AdditionalFormFields\Form\CancelFormAction;
6
use Dynamic\Wishlist\Model\ProductWishList;
7
use Dynamic\Wishlist\Test\Extra\TestProductWishList;
8
use Dynamic\Wishlist\Test\Extra\TestWishListPage;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\Forms\FormAction;
13
use SilverStripe\Forms\RequiredFields;
14
use SilverStripe\Forms\TextareaField;
15
use SilverStripe\Security\Member;
16
17
/**
18
 * Class TestProductWishListTest
19
 */
20
class ProductWishListTest extends SapphireTest
21
{
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
        TestWishListPage::class,
33
        TestProductWishList::class,
34
    ];
35
36
    /**
37
     *
38
     */
39
    public function testCanCreate()
40
    {
41
        $wishList = $this->objFromFixture(TestProductWishList::class, 'one');
42
        $canAccess = $this->objFromFixture(Member::class, 'cancreate');
43
        $cantAccess = $this->objFromFixture(Member::class, 'cantcreate');
44
45
        $this->assertTrue($wishList->canCreate($canAccess));
46
        $this->assertFalse($wishList->canCreate($cantAccess));
47
    }
48
49
    /**
50
     *
51
     */
52
    public function testCanEdit()
53
    {
54
        $wishList = $this->objFromFixture(TestProductWishList::class, 'one');
55
        $canAccess = $this->objFromFixture(Member::class, 'cancreate');
56
        $cantAccess = $this->objFromFixture(Member::class, 'cantcreate');
0 ignored issues
show
Unused Code introduced by
The assignment to $cantAccess is dead and can be removed.
Loading history...
57
58
        $this->assertTrue($wishList->canEdit($canAccess));
59
        //$this->assertFalse($wishList->canEdit($cantAccess));
60
    }
61
62
    /**
63
     *
64
     */
65
    public function testCanDelete()
66
    {
67
        $wishList = $this->objFromFixture(TestProductWishList::class, 'one');
68
        $canAccess = $this->objFromFixture(Member::class, 'cancreate');
69
        $cantAccess = $this->objFromFixture(Member::class, 'cantcreate');
0 ignored issues
show
Unused Code introduced by
The assignment to $cantAccess is dead and can be removed.
Loading history...
70
71
        $this->assertTrue($wishList->canDelete($canAccess));
72
        //$this->assertFalse($wishList->canDelete($cantAccess));
73
    }
74
75
    /**
76
     *
77
     */
78
    public function testCanView()
79
    {
80
        $wishList = $this->objFromFixture(TestProductWishList::class, 'one');
81
        $canAccess = $this->objFromFixture(Member::class, 'cancreate');
82
        $cantAccess = $this->objFromFixture(Member::class, 'cantcreate');
83
84
        $this->assertTrue($wishList->canView($canAccess));
85
        //todo fix following assertion
86
        //$this->assertFalse($wishList->canView($cantAccess));
87
88
        $viewableWishList = $this->objFromFixture(TestProductWishList::class, 'three');
89
90
        $this->assertTrue($viewableWishList->canView($cantAccess));
91
    }
92
93
    /**
94
     *
95
     */
96
    public function testProvidePermissions()
97
    {
98
        $expected = [
99
            'WishList_EDIT' => [
100
                'name' => 'Edit a Wish List',
101
                'category' => 'Wish List Permissions',
102
            ],
103
            'WishList_DELETE' => [
104
                'name' => 'Delete a Wish List',
105
                'category' => 'Wish List Permissions',
106
            ],
107
            'WishList_CREATE' => [
108
                'name' => 'Create a Wish List',
109
                'category' => 'Wish List Permissions',
110
            ],
111
            'WishList_VIEW' => [
112
                'name' => 'View a Wish List',
113
                'category' => 'Wish List Permissions',
114
            ],
115
        ];
116
117
        $this->assertEquals($expected, Injector::inst()->get(ProductWishList::class)->providePermissions());
118
    }
119
120
    /**
121
     *
122
     */
123
    public function testGetViewAction()
124
    {
125
        $this->assertEquals('view', Injector::inst()->get(ProductWishList::class)->getViewAction());
126
    }
127
128
    /**
129
     *
130
     */
131
    public function testGetFrontEndRequiredFields()
132
    {
133
        $this->assertInstanceOf(
134
            RequiredFields::class,
135
            Injector::inst()->get(ProductWishList::class)->getFrontEndRequiredFields()
136
        );
137
138
        /** @var TestProductWishList $wishList */
139
        $wishList = $this->objFromFixture(TestProductWishList::class, 'one');
140
        $this->assertTrue($wishList->getFrontEndRequiredFields()->fieldIsRequired('OtherField'));
141
    }
142
143
    /**
144
     *
145
     */
146
    public function testGetFrontEndActions()
147
    {
148
        $this->assertInstanceOf(
149
            FieldList::class,
150
            Injector::inst()->get(ProductWishList::class)->getFrontEndActions()
151
        );
152
153
        /** @var TestProductWishList $wishList */
154
        $wishList = $this->objFromFixture(TestProductWishList::class, 'one');
155
        $this->assertNull($wishList->getFrontEndActions()->dataFieldByName('action_CancelFormAction'));
156
        $this->assertInstanceOf(CancelFormAction::class,
157
            $wishList->getFrontEndActions(true)->dataFieldByName('action_CancelFormAction'));
158
159
        $this->assertInstanceOf(FormAction::class, $wishList->getFrontEndActions()->dataFieldByName('action_OtherAction'));
160
    }
161
162
    /**
163
     *
164
     */
165
    public function testGetFrontEndFields()
166
    {
167
        /** @var ProductWishList $baseWishList */
168
        $baseWishList = Injector::inst()->get(ProductWishList::class);
169
        $baseFields = $baseWishList->getFrontEndFields();
170
        $this->assertInstanceOf(FieldList::class, $baseFields);
171
        $this->assertNull($baseFields->fieldByName('MemberID'));
172
173
        /** @var TestProductWishList $wishList */
174
        $wishList = $this->objFromFixture(TestProductWishList::class, 'four');
175
        $updatedFields = $wishList->getFrontEndFields();
176
        $this->assertInstanceOf(FieldList::class, $updatedFields);
177
        $this->assertInstanceOf(TextareaField::class, $updatedFields->dataFieldByName('OtherField'));
178
    }
179
180
}
181