Completed
Push — master ( d30bf9...31aec5 )
by Sam
02:25
created

AssetAdminTest   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 462
Duplicated Lines 19.48 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 22
c 5
b 2
f 0
lcom 2
cbo 2
dl 90
loc 462
rs 10

19 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 29 3
A tearDown() 0 8 1
A testItCreatesFolder() 0 21 1
A testItRestrictsCreateFolderByCanCreate() 17 17 1
A testItRestrictsCreateFolderByCanAddChildren() 19 17 1
B testItCreatesFile() 2 42 1
A testItRestrictsCreateFileOnCanCreate() 21 21 1
A testItRestrictsCreateFileOnCanAddChildren() 25 22 1
B testItRestrictsCreateFileOnExtension() 2 30 1
A testItFiltersByDateInSearch() 0 51 1
A testItDoesNotFilterByDefaultInSearch() 0 17 1
A testItFiltersByParentInSearch() 0 18 1
A testItFiltersByNameInSearch() 0 16 1
A testItRestrictsViewInSearch() 0 12 1
A testItRestrictsViewInReadFolder() 0 17 1
B testItRestrictsUpdateFile() 0 30 1
B testItRestrictsDelete() 0 30 1
A getResultsForSearch() 0 7 1
A getUploadFile() 0 18 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SilverStripe\AssetAdmin\Tests;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Assets\Folder;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Control\Session;
9
use SilverStripe\Dev\FunctionalTest;
10
use SilverStripe\Dev\TestOnly;
11
use SilverStripe\ORM\DataExtension;
12
use SilverStripe\ORM\Versioning\Versioned;
13
use SilverStripe\Security\SecurityToken;
14
use AssetStoreTest_SpyStore;
15
16
/**
17
 * Tests {@see AssetAdmin}
18
 */
19
class AssetAdminTest extends FunctionalTest
20
{
21
22
    protected static $fixture_file = 'AssetAdminTest.yml';
23
24
    /**
25
     * @var Session
26
     */
27
    protected $session = null;
28
29
    public function setUp()
30
    {
31
        parent::setUp();
32
33
        AssetStoreTest_SpyStore::activate('AssetAdminTest');
34
        $memberID = $this->logInWithPermission('ADMIN');
35
        $this->session = Session::create(array('loggedInAs' => $memberID));
36
37
        File::add_extension('SilverStripe\\AssetAdmin\\Tests\\AssetAdminTest_File');
38
        Folder::add_extension('SilverStripe\\AssetAdmin\\Tests\\AssetAdminTest_Folder');
39
40
        // Create a test folders for each of the fixture references
41
        foreach (File::get()->filter('ClassName', 'SilverStripe\\Assets\\Folder') as $folder) {
42
            /** @var Folder $folder */
43
            $folder->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
44
        }
45
46
        // Create a test files for each of the fixture references
47
        $content = str_repeat('x', 1000000);
48
        foreach (File::get()->exclude('ClassName', 'SilverStripe\\Assets\\Folder') as $file) {
49
            /** @var File $file */
50
            $file->setFromString($content, $file->generateFilename());
51
            $file->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
52
        }
53
54
        // Override FunctionalTest defaults
55
        SecurityToken::enable();
56
        $this->session->inst_set('SecurityID', SecurityToken::inst()->getValue());
57
    }
58
59
    public function tearDown()
60
    {
61
        File::remove_extension('SilverStripe\\AssetAdmin\\Tests\\AssetAdminTest_File');
62
        Folder::remove_extension('SilverStripe\\AssetAdmin\\Tests\\AssetAdminTest_Folder');
63
64
        AssetStoreTest_SpyStore::reset();
65
        parent::tearDown();
66
    }
67
68
    public function testItCreatesFolder()
69
    {
70
        $folder1 = $this->objFromFixture('SilverStripe\\Assets\\Folder', 'folder1');
71
72
        $response = Director::test(
73
            'admin/assets/api/createFolder',
74
            [
75
                'ParentID' => $folder1->ID,
76
                'Name' => 'testItCreatesFolder',
77
                'SecurityID' => SecurityToken::inst()->getValue(),
78
            ],
79
            $this->session,
80
            'POST'
81
        );
82
        $this->assertFalse($response->isError());
83
        $responseData = json_decode($response->getBody(), true);
84
        $newFolder = Folder::get()->byID($responseData['id']);
85
        $this->assertNotNull($newFolder);
86
        $this->assertEquals($folder1->ID, $newFolder->ParentID);
87
        $this->assertEquals('testItCreatesFolder', $newFolder->Name);
88
    }
89
90 View Code Duplication
    public function testItRestrictsCreateFolderByCanCreate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $folder = $this->objFromFixture('SilverStripe\\Assets\\Folder', 'folder1');
93
94
        $response = Director::test(
95
            'admin/assets/api/createFolder',
96
            [
97
                'ParentID' => $folder->ID,
98
                'Name' => 'disallowCanCreate',
99
                'SecurityID' => SecurityToken::inst()->getValue(),
100
            ],
101
            $this->session,
102
            'POST'
103
        );
104
        $this->assertTrue($response->isError());
105
        $this->assertEquals(403, $response->getStatusCode());
106
    }
107
108 View Code Duplication
    public function testItRestrictsCreateFolderByCanAddChildren()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110
        $folder = $this->objFromFixture('SilverStripe\\Assets\\Folder', 'disallowCanAddChildren');
111
112
        $response = Director::test(
113
            'admin/assets/api/createFolder',
114
            [
115
                'ParentID' => $folder->ID,
116
                'Name' => 'testItRestrictsCreateFolderByCanAddChildren',
117
                'SecurityID' => SecurityToken::inst()->getValue(),
118
            ],
119
            $this->session,
120
            'POST'
121
        );
122
        $this->assertTrue($response->isError());
123
        $this->assertEquals(403, $response->getStatusCode());
124
    }
125
126
    public function testItCreatesFile()
0 ignored issues
show
Coding Style introduced by
testItCreatesFile uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
127
    {
128
        $folder1 = $this->objFromFixture('SilverStripe\\Assets\\Folder', 'folder1');
129
130
        /** @skipUpgrade */
131
        $fileData = array('Upload' => $this->getUploadFile('Upload', 'testItCreatesFile.txt'));
132
        $_FILES = $fileData;
133
        $postedData = array_merge(
134
                $fileData,
135
                [
136
                    'ParentID' => $folder1->ID,
137
                    'SecurityID' => SecurityToken::inst()->getValue(),
138
                ]
139
        );
140
        $response = Director::test(
141
            'admin/assets/api/createFile',
142
            $postedData,
143
            $this->session,
144
            'POST'
145
        );
146
        $this->assertFalse($response->isError());
147
        $responseData = json_decode($response->getBody(), true);
148
        $newFile = File::get()->byID($responseData[0]['id']);
149
        $this->assertNotNull($newFile);
150
        $this->assertEquals($folder1->ID, $newFile->ParentID);
151
        $this->assertEquals('testItCreatesFile.txt', $newFile->Name);
152
153
        // Test that duplicate uploads are renamed
154
        $response = Director::test(
155
            'admin/assets/api/createFile',
156
            $postedData,
157
            $this->session,
158
            'POST'
159
        );
160
        $this->assertFalse($response->isError());
161
        $responseData = json_decode($response->getBody(), true);
162
        $newFile2 = File::get()->byID($responseData[0]['id']);
163
        $this->assertNotNull($newFile2);
164
        $this->assertEquals($folder1->ID, $newFile2->ParentID);
165
        $this->assertNotEquals($newFile->ID, $newFile2->ID);
166
        $this->assertEquals('testItCreatesFile-v2.txt', $newFile2->Name);
167
    }
168
169 View Code Duplication
    public function testItRestrictsCreateFileOnCanCreate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
testItRestrictsCreateFileOnCanCreate uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
170
    {
171
        $folder = $this->objFromFixture('SilverStripe\\Assets\\Folder', 'folder1');
172
173
        $fileData = array('Upload' => $this->getUploadFile('Upload', 'disallowCanCreate.txt'));
174
        $_FILES = $fileData;
175
        $response = Director::test(
176
            'admin/assets/api/createFile',
177
            array_merge(
178
                $fileData,
179
                [
180
                    'ParentID' => $folder->ID,
181
                    'SecurityID' => SecurityToken::inst()->getValue(),
182
                ]
183
            ),
184
            $this->session,
185
            'POST'
186
        );
187
        $this->assertTrue($response->isError());
188
        $this->assertEquals(403, $response->getStatusCode());
189
    }
190
191 View Code Duplication
    public function testItRestrictsCreateFileOnCanAddChildren()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
testItRestrictsCreateFileOnCanAddChildren uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
192
    {
193
        $folder = $this->objFromFixture('SilverStripe\\Assets\\Folder', 'disallowCanAddChildren');
194
195
        /** @skipUpgrade */
196
        $fileData = array('Upload' => $this->getUploadFile('Upload', 'test.txt'));
197
        $_FILES = $fileData;
198
        $response = Director::test(
199
            'admin/assets/api/createFile',
200
            array_merge(
201
                $fileData,
202
                [
203
                    'ParentID' => $folder->ID,
204
                    'SecurityID' => SecurityToken::inst()->getValue(),
205
                ]
206
            ),
207
            $this->session,
208
            'POST'
209
        );
210
        $this->assertTrue($response->isError());
211
        $this->assertEquals(403, $response->getStatusCode());
212
    }
213
214
    public function testItRestrictsCreateFileOnExtension()
0 ignored issues
show
Coding Style introduced by
testItRestrictsCreateFileOnExtension uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
215
    {
216
        $folder1 = $this->objFromFixture(
217
            'SilverStripe\\Assets\\Folder',
218
            'folder1'
219
        );
220
221
        /** @skipUpgrade */
222
        $fileData = array('Upload' => $this->getUploadFile('Upload', 'disallowed.php'));
223
        $_FILES = $fileData;
224
        $response = Director::test(
225
            'admin/assets/api/createFile',
226
            array_merge(
227
                $fileData,
228
                [
229
                    'ParentID' => $folder1->ID,
230
                    'SecurityID' => SecurityToken::inst()->getValue(),
231
                ]
232
            ),
233
            $this->session,
234
            'POST'
235
        );
236
        $this->assertTrue($response->isError());
237
        $this->assertEquals(400, $response->getStatusCode());
238
        $responseData = json_decode($response->getBody(), true);
239
        $this->assertContains(
240
            'Extension is not allowed',
241
            $responseData['error'][0]
242
        );
243
    }
244
245
    public function testItFiltersByDateInSearch()
246
    {
247
        $file1 = $this->objFromFixture('SilverStripe\\Assets\\File', 'file1');
248
        $file2 = $this->objFromFixture('SilverStripe\\Assets\\File', 'file2');
249
250
        // Force creation times
251
        $file1->Created = '2014-01-05 23:11:39';
252
        $file1->write();
253
        $file2->Created = '2014-01-06 12:00:00';
254
        $file2->write();
255
256
        // Mock searches for 4th Jan
257
        $results = $this->getResultsForSearch([
258
            'CreatedFrom' => '2014-01-04',
259
            'CreatedTo' => '2014-01-04'
260
        ]);
261
        $this->assertEquals(count($results['files']), 0);
262
263
        // Mock searches for 5th Jan
264
        $results = $this->getResultsForSearch([
265
            'CreatedFrom' => '2014-01-05',
266
            'CreatedTo' => '2014-01-05'
267
        ]);
268
        $this->assertEquals(count($results['files']), 1);
269
        $this->assertContains($file1->ID, array_column($results['files'], 'id'));
270
271
272
        // Mock searches for 5th-6th Jan
273
        $results = $this->getResultsForSearch([
274
            'CreatedFrom' => '2014-01-05',
275
            'CreatedTo' => '2014-01-06'
276
        ]);
277
        $this->assertEquals(count($results['files']), 2);
278
        $this->assertContains($file1->ID, array_column($results['files'], 'id'));
279
        $this->assertContains($file2->ID, array_column($results['files'], 'id'));
280
281
        // Mock searches for 6th Jan
282
        $results = $this->getResultsForSearch([
283
            'CreatedFrom' => '2014-01-06',
284
            'CreatedTo' => '2014-01-06'
285
        ]);
286
        $this->assertEquals(count($results['files']), 1);
287
        $this->assertContains($file2->ID, array_column($results['files'], 'id'));
288
289
        // Mock searches for 7th Jan
290
        $results = $this->getResultsForSearch([
291
            'CreatedFrom' => '2014-01-07',
292
            'CreatedTo' => '2014-01-07'
293
        ]);
294
        $this->assertEquals(count($results['files']), 0);
295
    }
296
297
298
    public function testItDoesNotFilterByDefaultInSearch()
299
    {
300
        $rootfile = $this->objFromFixture('SilverStripe\\Assets\\File', 'rootfile');
301
        $file1 = $this->objFromFixture('SilverStripe\\Assets\\File', 'file1');
302
        $folder1 = $this->objFromFixture('SilverStripe\\Assets\\Folder', 'folder1');
303
304
        $results = $this->getResultsForSearch();
305
        $this->assertContains($rootfile->ID, array_column($results['files'], 'id'),
306
            'Contains top level file'
307
        );
308
        $this->assertContains($folder1->ID, array_column($results['files'], 'id'),
309
            'Contains top level folder'
310
        );
311
        $this->assertContains($file1->ID, array_column($results['files'], 'id'),
312
            'Contains files in subfolder'
313
        );
314
    }
315
316
    public function testItFiltersByParentInSearch()
317
    {
318
        $file1 = $this->objFromFixture('SilverStripe\\Assets\\File', 'file1');
319
        $file2 = $this->objFromFixture('SilverStripe\\Assets\\File', 'file2');
320
        $file1Folder = $file1->Parent();
321
        $file2Folder = $file2->Parent();
322
323
        $results = $this->getResultsForSearch(['Name' => $file1->Name, 'ParentID' => $file1Folder->ID]);
324
        $this->assertEquals(count($results['files']), 1);
325
        $this->assertContains($file1->ID, array_column($results['files'], 'id'),
326
            'Returns file when contained in correct folder'
327
        );
328
329
        $results = $this->getResultsForSearch(['Name' => $file1->Name, 'ParentID' => $file2Folder->ID]);
330
        $this->assertEquals(count($results['files']), 0,
331
            'Does not return file when contained in different folder'
332
        );
333
    }
334
335
    public function testItFiltersByNameInSearch()
336
    {
337
        $file1 = $this->objFromFixture('SilverStripe\\Assets\\File', 'file1');
338
339
        $results = $this->getResultsForSearch(['Name' => $file1->Name]);
340
        $this->assertEquals(count($results['files']), 1,
341
            'Finds by Name property'
342
        );
343
        $this->assertContains($file1->ID, array_column($results['files'], 'id'));
344
345
        $results = $this->getResultsForSearch(['Name' => 'First']);
346
        $this->assertEquals(count($results['files']), 1,
347
            'Finds by Title property'
348
        );
349
        $this->assertContains($file1->ID, array_column($results['files'], 'id'));
350
    }
351
352
    public function testItRestrictsViewInSearch()
353
    {
354
        $allowedFile = $this->objFromFixture('SilverStripe\\Assets\\File', 'file1');
355
        $disallowedFile = $this->objFromFixture('SilverStripe\\Assets\\File', 'disallowCanView');
356
357
        $results = $this->getResultsForSearch(['Name' => $allowedFile->Name]);
358
        $this->assertEquals(count($results['files']), 1);
359
        $this->assertContains($allowedFile->ID, array_column($results['files'], 'id'));
360
361
        $results = $this->getResultsForSearch(['Name' => $disallowedFile->Name]);
362
        $this->assertEquals(count($results['files']), 0);
363
    }
364
365
    public function testItRestrictsViewInReadFolder()
366
    {
367
        $folder1 = $this->objFromFixture('SilverStripe\\Assets\\Folder', 'folder1');
368
        $allowedFile = $this->objFromFixture('SilverStripe\\Assets\\File', 'file1');
369
        $disallowedFile = $this->objFromFixture('SilverStripe\\Assets\\File', 'disallowCanView');
370
371
        $response = $this->get('admin/assets/api/readFolder?' . http_build_query(['id' => $folder1->ID]));
372
        $files = json_decode($response->getBody(), true);
373
        $this->assertArrayHasKey('files', $files);
374
        $ids = array_map(function ($file) {
375
            return $file['id'];
376
        }, $files['files']);
377
        $this->assertContains($allowedFile->ID, $ids);
378
        $this->assertEquals($allowedFile->ParentID, $folder1->ID);
379
        $this->assertNotContains($disallowedFile->ID, $ids);
380
        $this->assertEquals($disallowedFile->ParentID, $folder1->ID);
381
    }
382
383
    public function testItRestrictsUpdateFile()
384
    {
385
        $allowedFile = $this->objFromFixture('SilverStripe\\Assets\\File', 'file1');
386
        $disallowedFile = $this->objFromFixture('SilverStripe\\Assets\\File', 'disallowCanEdit');
387
388
        $response = Director::test(
389
            'admin/assets/FileEditForm',
390
            [
391
                'action_save' => 1,
392
                'ID' => $allowedFile->ID,
393
                'Name' => 'disallowCanEdit.txt',
394
                'Title' => 'new',
395
                'SecurityID' => SecurityToken::inst()->getValue(),
396
            ],
397
            $this->session
398
        );
399
        $this->assertFalse($response->isError());
400
401
        $response = Director::test(
402
            'admin/assets/FileEditForm',
403
            [
404
                'action_save' => 1,
405
                'ID' => $disallowedFile->ID,
406
                'Title' => 'new',
407
                'SecurityID' => SecurityToken::inst()->getValue(),
408
            ],
409
            $this->session
410
        );
411
        $this->assertTrue($response->isError());
412
    }
413
414
    public function testItRestrictsDelete()
415
    {
416
        $allowedFile = $this->objFromFixture('SilverStripe\\Assets\\File', 'file1');
417
        $disallowedFile = $this->objFromFixture('SilverStripe\\Assets\\File',
418
            'disallowCanDelete');
419
420
        $response = Director::test(
421
            'admin/assets/api/delete',
422
            null,
423
            $this->session,
424
            'DELETE',
425
            http_build_query([
426
                'ids' => [$allowedFile->ID, $disallowedFile->ID],
427
                'SecurityID' => SecurityToken::inst()->getValue(),
428
            ])
429
        );
430
        $this->assertTrue($response->isError());
431
432
        $response = Director::test(
433
            'admin/assets/api/delete',
434
            null,
435
            $this->session,
436
            'DELETE',
437
            http_build_query([
438
                'ids' => [$allowedFile->ID],
439
                'SecurityID' => SecurityToken::inst()->getValue(),
440
            ])
441
        );
442
        $this->assertFalse($response->isError());
443
    }
444
445
    /**
446
     * @param array $params
447
     * @return array
448
     */
449
    protected function getResultsForSearch($params = array())
450
    {
451
        $response = $this->get('admin/assets/api/search?' . http_build_query($params));
452
        $this->assertFalse($response->isError());
453
454
        return json_decode($response->getBody(), true);
455
    }
456
457
    /**
458
     * @param string $paramName
459
     * @param string $tmpFileName
460
     * @return array Emulating an entry in the $_FILES superglobal
461
     */
462
    protected function getUploadFile($paramName, $tmpFileName = 'AssetAdminTest.txt')
463
    {
464
        $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
465
        $tmpFileContent = '';
466
        for ($i = 0; $i < 10000; $i++) {
467
            $tmpFileContent .= '0';
468
        }
469
        file_put_contents($tmpFilePath, $tmpFileContent);
470
471
        // emulates the $_FILES array
472
        return array(
473
            'name' => $tmpFileName,
474
            'type' => 'text/plaintext',
475
            'size' => filesize($tmpFilePath),
476
            'tmp_name' => $tmpFilePath,
477
            'error' => UPLOAD_ERR_OK,
478
        );
479
    }
480
}
481
482
class AssetAdminTest_File extends DataExtension implements TestOnly
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
483
{
484
    public function canView($member = null)
485
    {
486
        if ($this->owner->Name === 'disallowCanView.txt') {
487
            return false;
488
        }
489
    }
490
491
    public function canEdit($member = null)
492
    {
493
        if ($this->owner->Name === 'disallowCanEdit.txt') {
494
            return false;
495
        }
496
    }
497
498
    public function canDelete($member = null)
499
    {
500
        if ($this->owner->Name === 'disallowCanDelete.txt') {
501
            return false;
502
        }
503
    }
504
505
    public function canCreate($member = null, $context = [])
506
    {
507
        if (isset($context['Parent']) && $context['Parent']->Name === 'disallowCanAddChildren') {
508
            return false;
509
        }
510
        if (isset($context['Upload']['name']) && $context['Upload']['name'] === 'disallowCanCreate.txt') {
511
            return false;
512
        }
513
    }
514
}
515
516
class AssetAdminTest_Folder extends DataExtension implements TestOnly
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
517
{
518
    public function canView($member = null, $context = array())
519
    {
520
        if ($this->owner->Name === 'disallowCanView') {
521
            return false;
522
        }
523
    }
524
525
    public function canEdit($member = null, $context = array())
526
    {
527
        if ($this->owner->Name === 'disallowCanEdit') {
528
            return false;
529
        }
530
    }
531
532
    public function canDelete($member = null, $context = array())
533
    {
534
        if ($this->owner->Name === 'disallowCanDelete') {
535
            return false;
536
        }
537
    }
538
539
    public function canCreate($member = null, $context = array())
540
    {
541
        if (isset($context['Name']) && $context['Name'] === 'disallowCanCreate') {
542
            return false;
543
    }
544
    }
545
}
546