1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\AssetAdmin\Tests\GraphQL; |
4
|
|
|
|
5
|
|
|
use SilverStripe\AssetAdmin\GraphQL\ReadFileQueryCreator; |
6
|
|
|
use SilverStripe\AssetAdmin\Tests\Controller\AssetAdminTest\FileExtension; |
7
|
|
|
use SilverStripe\AssetAdmin\Tests\Controller\AssetAdminTest\FolderExtension; |
8
|
|
|
use SilverStripe\Assets\File; |
9
|
|
|
use SilverStripe\Assets\Folder; |
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
11
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
12
|
|
|
use SilverStripe\GraphQL\Manager; |
13
|
|
|
use Silverstripe\Assets\Dev\TestAssetStore; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Most of the search functionality is covered in {@link FileFilterInputTypeCreatorTest} |
17
|
|
|
*/ |
18
|
|
|
class ReadFileQueryCreatorTest extends SapphireTest |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
protected $usesDatabase = true; |
22
|
|
|
|
23
|
|
|
protected function setUp() : void |
24
|
|
|
{ |
25
|
|
|
parent::setUp(); |
26
|
|
|
|
27
|
|
|
TestAssetStore::activate('AssetAdminTest'); |
28
|
|
|
|
29
|
|
|
File::add_extension(FileExtension::class); |
30
|
|
|
Folder::add_extension(FolderExtension::class); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function tearDown() : void |
34
|
|
|
{ |
35
|
|
|
File::remove_extension(FileExtension::class); |
36
|
|
|
Folder::remove_extension(FolderExtension::class); |
37
|
|
|
|
38
|
|
|
TestAssetStore::reset(); |
39
|
|
|
parent::tearDown(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testItRestrictsParentByCanView() |
43
|
|
|
{ |
44
|
|
|
$this->expectException(\InvalidArgumentException::class); |
45
|
|
|
$this->expectExceptionMessage('view access not permitted'); |
46
|
|
|
$folder = new Folder(['Name' => 'disallowCanView']); |
47
|
|
|
$folder->write(); |
48
|
|
|
|
49
|
|
|
$this->getResultsForSearch([ |
50
|
|
|
'filter' => ['parentId' => $folder->ID], |
51
|
|
|
]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testItFiltersResultsByCanView() |
55
|
|
|
{ |
56
|
|
|
$allowedFolder = new Folder(['Name' => 'allowedFolder']); |
57
|
|
|
$allowedFolder->write(); |
58
|
|
|
|
59
|
|
|
$disallowedFolder = new Folder(['Name' => 'disallowCanView']); |
60
|
|
|
$disallowedFolder->write(); |
61
|
|
|
|
62
|
|
|
$allowedFile = new File(['Name' => 'allowedFile']); |
63
|
|
|
$allowedFile->write(); |
64
|
|
|
|
65
|
|
|
$disallowedFile = new File(['Name' => 'disallowCanView.txt']); |
66
|
|
|
$disallowedFile->write(); |
67
|
|
|
|
68
|
|
|
$list = $this->getResultsForSearch([ |
69
|
|
|
'filter' => ['parentId' => 0], |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
$this->assertEquals( |
73
|
|
|
[ |
74
|
|
|
$allowedFile->Name, |
75
|
|
|
$allowedFolder->Name, |
76
|
|
|
], |
77
|
|
|
$list->column('Name') |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array $args |
83
|
|
|
* @param array $context |
84
|
|
|
* @return \SilverStripe\ORM\DataList|\SilverStripe\ORM\Filterable |
85
|
|
|
*/ |
86
|
|
|
protected function getResultsForSearch($args, $context = null) |
87
|
|
|
{ |
88
|
|
|
$context = $context ? $context : ['currentUser' => null]; |
89
|
|
|
$creator = new ReadFileQueryCreator(new Manager()); |
90
|
|
|
return $creator->resolveConnection(null, $args, $context, new ResolveInfo([])); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|