Completed
Push — master ( a75fe8...9d0695 )
by
unknown
13s queued 11s
created

tests/unit/Converters/Models/UserGroupTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace NerdsAndCompany\Schematic\Converters\Models;
4
5
use Craft;
6
use craft\console\Application;
7
use craft\models\UserGroup as UserGroupModel;
8
use craft\models\Section as SectionModel;
9
use craft\models\CategoryGroup as CategoryGroupModel;
10
use craft\base\Volume as VolumeModel;
11
use Codeception\Test\Unit;
12
13
/**
14
 * Class UserGroupTest.
15
 *
16
 * @author    Nerds & Company
17
 * @copyright Copyright (c) 2015-2017, Nerds & Company
18
 * @license   MIT
19
 *
20
 * @see      http://www.nerds.company
21
 */
22
class UserGroupTest extends Unit
23
{
24
    /**
25
     * @var UserGroup
26
     */
27
    private $converter;
28
29
    /**
30
     * Set the converter.
31
     *
32
     * @SuppressWarnings(PHPMD.CamelCaseMethodName)
33
     * phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
34
     */
35
    protected function _before()
36
    {
37
        $this->converter = new UserGroup();
38
    }
39
40
    //==============================================================================================================
41
    //=================================================  TESTS  ====================================================
42
    //==============================================================================================================
43
44
    /**
45
     * @dataProvider provideUserGroups
46
     *
47
     * @param UserGroupModel $group
48
     * @param array          $definition
49
     * @param array          $groupPermissions
50
     * @param array          $allPermissions
51
     */
52
    public function testGetRecordDefinition(
53
        UserGroupModel $group,
54
        array $definition,
55
        array $groupPermissions,
56
        array $allPermissions
57
    ) {
58
        Craft::$app->userPermissions->expects($this->exactly(1))
59
                                    ->method('getPermissionsByGroupId')
60
                                    ->with($group->id)
61
                                    ->willReturn($groupPermissions);
62
63
        Craft::$app->userPermissions->expects($this->exactly(1))
64
                                    ->method('getAllPermissions')
65
                                    ->willReturn($allPermissions);
66
67
        Craft::$app->sections->expects($this->exactly(1))
68
                             ->method('getSectionById')
69
                             ->with(1)
70
                             ->willReturn($this->getMockSection(1));
71
72
        Craft::$app->categories->expects($this->exactly(1))
73
                               ->method('getGroupById')
74
                               ->with(2)
75
                               ->willReturn($this->getMockCategoryGroup(2));
76
77
        Craft::$app->volumes->expects($this->exactly(1))
78
                            ->method('getVolumeById')
79
                            ->with(3)
80
                            ->willReturn($this->getmockVolume(3));
81
82
        $result = $this->converter->getRecordDefinition($group);
83
84
        $this->assertSame($definition, $result);
85
    }
86
87
    /**
88
     * @dataProvider provideUserGroups
89
     *
90
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
91
     *
92
     * @param UserGroupModel $group
93
     * @param array          $definition
94
     * @param array          $groupPermissions
95
     * @param array          $allPermissions
96
     * @param bool           $valid
97
     */
98
    public function testSaveRecord(
99
        UserGroupModel $group,
100
        array $definition,
101
        array $groupPermissions,
0 ignored issues
show
The parameter $groupPermissions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
        array $allPermissions,
0 ignored issues
show
The parameter $allPermissions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
        bool $valid
104
    ) {
105
        Craft::$app->userGroups->expects($this->exactly(1))
106
                               ->method('saveGroup')
107
                               ->with($group)
108
                               ->willReturn($valid);
109
110
        $mappedPermissions = ['createEntries:1', 'editCategories:2', 'performUpdates', 'viewVolume:3'];
111
112
        if ($valid) {
113
            Craft::$app->userPermissions->expects($this->exactly(1))
114
                                        ->method('saveGroupPermissions')
115
                                        ->with($group->id, $mappedPermissions)
116
                                        ->willReturn(true);
117
118
            Craft::$app->sections->expects($this->exactly(1))
119
                                 ->method('getSectionByHandle')
120
                                 ->with('section1')
121
                                 ->willReturn($this->getMockSection(1));
122
123
            Craft::$app->categories->expects($this->exactly(1))
124
                                   ->method('getGroupByHandle')
125
                                   ->with('group2')
126
                                   ->willReturn($this->getMockCategoryGroup(2));
127
128
            Craft::$app->volumes->expects($this->exactly(1))
129
                                ->method('getVolumeByHandle')
130
                                ->with('volume3')
131
                                ->willReturn($this->getmockVolume(3));
132
        }
133
134
        $result = $this->converter->saveRecord($group, $definition);
135
136
        $this->assertSame($valid, $result);
137
    }
138
139
    /**
140
     * @dataProvider provideUserGroups
141
     *
142
     * @param UserGroupModel $group
143
     */
144
    public function testDeleteRecord(UserGroupModel $group)
145
    {
146
        Craft::$app->userGroups->expects($this->exactly(1))
147
                               ->method('deleteGroupById')
148
                               ->with($group->id);
149
150
        $this->converter->deleteRecord($group);
151
    }
152
153
    //==============================================================================================================
154
    //==============================================  PROVIDERS  ===================================================
155
    //==============================================================================================================
156
157
    /**
158
     * @return array
159
     */
160
    public function provideUserGroups()
161
    {
162
        $mockUserGroup = $this->getMockUserGroup(1);
163
164
        return [
165
            'valid user group' => [
166
                'group' => $mockUserGroup,
167
                'definition' => $this->getMockUserGroupDefinition($mockUserGroup),
168
                'groupPermissions' => [
169
                    'createentries:1',
170
                    'editcategories:2',
171
                    'performupdates',
172
                    'viewvolume:3',
173
                ],
174
                'allPermissions' => [
175
                    ['createEntries:1' => []],
176
                    ['editCategories:2' => []],
177
                    ['performUpdates' => []],
178
                    ['viewVolume:3' => []],
179
                ],
180
                'validSave' => true,
181
            ],
182
            'invalid user group' => [
183
                'group' => $mockUserGroup,
184
                'definition' => $this->getMockUserGroupDefinition($mockUserGroup),
185
                'groupPermissions' => [
186
                    'createentries:1',
187
                    'editcategories:2',
188
                    'performupdates',
189
                    'viewvolume:3',
190
                ],
191
                'allPermissions' => [
192
                    ['createEntries:1' => []],
193
                    ['editCategories:2' => []],
194
                    ['performUpdates' => []],
195
                    ['viewVolume:3' => []],
196
                ],
197
                'validSave' => false,
198
            ],
199
        ];
200
    }
201
202
    //==============================================================================================================
203
    //================================================  HELPERS  ===================================================
204
    //==============================================================================================================
205
206
    /**
207
     * @param UserGroupModel $userGroup
208
     *
209
     * @return array
210
     */
211
    private function getMockUserGroupDefinition(UserGroupModel $userGroup)
212
    {
213
        return [
214
          'class' => get_class($userGroup),
215
          'attributes' => [
216
              'name' => 'userGroupName'.$userGroup->id,
217
              'handle' => 'userGroupHandle'.$userGroup->id,
218
          ],
219
          'permissions' => [
220
              'createEntries:section1',
221
              'editCategories:group2',
222
              'performUpdates',
223
              'viewVolume:volume3',
224
          ],
225
        ];
226
    }
227
228
    /**
229
     * @param int $userGroupId
230
     *
231
     * @return UserGroupModel
232
     */
233
    private function getMockUserGroup(int $userGroupId)
234
    {
235
        $mockApp = $this->getMockBuilder(Application::class)
236
                        ->disableOriginalConstructor()
237
                        ->getMock();
238
239
        $mockApp->expects($this->exactly(1))
240
                ->method('requireEdition')
241
                ->with(Craft::Pro)
242
                ->willReturn(true);
243
244
        Craft::$app = $mockApp;
245
246
        $mockUserGroup = $this->getmockBuilder(UserGroupModel::class)
247
                              ->setMethods(['__toString'])
248
                              ->getMock();
249
250
        $mockUserGroup->id = $userGroupId;
251
        $mockUserGroup->handle = 'userGroupHandle'.$userGroupId;
252
        $mockUserGroup->name = 'userGroupName'.$userGroupId;
253
254
        return $mockUserGroup;
255
    }
256
257
    /**
258
     * @param int $sectionId
259
     *
260
     * @return Mock|SectionModel
261
     */
262
    private function getMockSection(int $sectionId)
263
    {
264
        $mockSection = $this->getMockBuilder(SectionModel::class)
265
                           ->disableOriginalConstructor()
266
                           ->getMock();
267
268
        $mockSection->id = $sectionId;
269
        $mockSection->handle = 'section'.$sectionId;
270
271
        return $mockSection;
272
    }
273
274
    /**
275
     * @param int $groupId
276
     *
277
     * @return Mock|CategoryGroupModel
278
     */
279
    private function getMockCategoryGroup(int $groupId)
280
    {
281
        $mockGroup = $this->getMockBuilder(CategoryGroupModel::class)
282
                          ->disableOriginalConstructor()
283
                          ->getMock();
284
285
        $mockGroup->id = $groupId;
286
        $mockGroup->handle = 'group'.$groupId;
287
288
        return $mockGroup;
289
    }
290
291
    /**
292
     * @param int $volumeId
293
     *
294
     * @return Mock|VolumeModel
295
     */
296
    private function getMockVolume(int $volumeId)
297
    {
298
        $mockVolume = $this->getMockBuilder(VolumeModel::class)
299
                          ->disableOriginalConstructor()
300
                          ->getMock();
301
302
        $mockVolume->id = $volumeId;
303
        $mockVolume->handle = 'volume'.$volumeId;
304
305
        return $mockVolume;
306
    }
307
}
308