|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\UserForms\Tests\Model\EditableFormField; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Assets\Filesystem; |
|
6
|
|
|
use SilverStripe\Assets\Folder; |
|
7
|
|
|
use SilverStripe\Core\Config\Config; |
|
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
9
|
|
|
use SilverStripe\UserForms\Model\EditableFormField\EditableFileField; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Tests integration of EditableFileField with the securefiles module |
|
13
|
|
|
* |
|
14
|
|
|
* @todo |
|
15
|
|
|
* @author dmooyman |
|
16
|
|
|
*/ |
|
17
|
|
|
class SecureEditableFileFieldTest extends SapphireTest |
|
18
|
|
|
{ |
|
19
|
|
|
protected $usesDatabase = true; |
|
20
|
|
|
|
|
21
|
|
|
protected function setUp() |
|
22
|
|
|
{ |
|
23
|
|
|
parent::setUp(); |
|
24
|
|
|
|
|
25
|
|
|
if (!class_exists('SecureFileExtension')) { |
|
26
|
|
|
$this->skipTest = true; |
|
|
|
|
|
|
27
|
|
|
$this->markTestSkipped(get_class() . ' skipped unless running with securefiles'); |
|
28
|
|
|
} |
|
29
|
|
|
Config::modify()->set(EditableFileField::class, 'secure_folder_name', 'SecureEditableFileFieldTest/SecureUploads'); |
|
30
|
|
|
$this->clearPath(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function tearDown() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->clearPath(); |
|
36
|
|
|
parent::tearDown(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function clearPath() |
|
40
|
|
|
{ |
|
41
|
|
|
if (file_exists(ASSETS_PATH . '/SecureEditableFileFieldTest')) { |
|
42
|
|
|
Filesystem::removeFolder(ASSETS_PATH . '/SecureEditableFileFieldTest'); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Test that newly created folders are secure |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testCreateFolder() |
|
50
|
|
|
{ |
|
51
|
|
|
$field = new EditableFileField(); |
|
52
|
|
|
$field->write(); |
|
53
|
|
|
$this->assertTrue($field->getIsSecure()); |
|
|
|
|
|
|
54
|
|
|
$this->assertTrue($field->Folder()->exists()); |
|
|
|
|
|
|
55
|
|
|
$this->assertEquals('assets/SecureEditableFileFieldTest/SecureUploads/', $field->Folder()->Filename); |
|
|
|
|
|
|
56
|
|
|
$this->assertEquals('OnlyTheseUsers', $field->Folder()->CanViewType); |
|
|
|
|
|
|
57
|
|
|
$this->assertEquals(1, $field->Folder()->ViewerGroups()->first()->Permissions()->filter('code', 'ADMIN')->count()); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Test new folders that are created without security enabled |
|
62
|
|
|
*/ |
|
63
|
|
|
public function testCreateInsecure() |
|
64
|
|
|
{ |
|
65
|
|
|
Config::modify()->set(EditableFileField::class, 'disable_security', true); |
|
66
|
|
|
|
|
67
|
|
|
// Esure folder is created without a folder |
|
68
|
|
|
$field = new EditableFileField(); |
|
69
|
|
|
$field->write(); |
|
70
|
|
|
$this->assertFalse($field->getIsSecure()); |
|
|
|
|
|
|
71
|
|
|
$this->assertFalse($field->Folder()->exists()); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
// Assigning a non-secure folder doesn't secure this |
|
74
|
|
|
$folder = Folder::find_or_make('SecureEditableFileFieldTest/PublicFolder'); |
|
75
|
|
|
$field->FolderID = $folder->ID; |
|
|
|
|
|
|
76
|
|
|
$field->write(); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertFalse($field->getIsSecure()); |
|
|
|
|
|
|
79
|
|
|
$this->assertTrue($field->Folder()->exists()); |
|
|
|
|
|
|
80
|
|
|
$this->assertEquals('assets/SecureEditableFileFieldTest/PublicFolder/', $field->Folder()->Filename); |
|
|
|
|
|
|
81
|
|
|
$this->assertEquals('Inherit', $field->Folder()->CanViewType); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
// Enabling security and re-saving will force this field to be made secure (but not changed) |
|
84
|
|
|
Config::modify()->set(EditableFileField::class, 'disable_security', false); |
|
85
|
|
|
singleton(EditableFileField::class)->requireDefaultRecords(); |
|
86
|
|
|
|
|
87
|
|
|
// Reload record from DB |
|
88
|
|
|
$field = EditableFileField::get()->byID($field->ID); |
|
89
|
|
|
|
|
90
|
|
|
// Existing folder is now secured (retro-actively secures any old uploads) |
|
91
|
|
|
$this->assertTrue($field->getIsSecure()); |
|
92
|
|
|
$this->assertTrue($field->Folder()->exists()); |
|
93
|
|
|
$this->assertEquals('assets/SecureEditableFileFieldTest/PublicFolder/', $field->Folder()->Filename); |
|
94
|
|
|
$this->assertEquals('OnlyTheseUsers', $field->Folder()->CanViewType); |
|
95
|
|
|
$this->assertEquals(1, $field->Folder()->ViewerGroups()->first()->Permissions()->filter('code', 'ADMIN')->count()); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: