ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
FileNamePolicyTest.php
Go to the documentation of this file.
1<?php
2
20
23
29{
30 protected function getResourceBuilder(string $denied_ending): ResourceBuilder
31 {
32 $policy = $this->getFileNamePolicy($denied_ending);
33 return new ResourceBuilder(
34 $this->storage_handler_factory,
35 $this->repositories,
36 $this->locking,
37 $this->stream_access,
38 $policy
39 );
40 }
41
42 protected function getFileNamePolicy(string $denied_ending): FileNamePolicy
43 {
44 return new class ($denied_ending) implements FileNamePolicy {
45 public function __construct(private string $denied_ending)
46 {
47 }
48
49 public function check(string $extension): bool
50 {
51 if ($this->denied_ending === $extension) {
52 throw new FileNamePolicyException('ERROR');
53 }
54 return true;
55 }
56
57 public function isValidExtension(string $extension): bool
58 {
59 return $this->denied_ending !== $extension;
60 }
61
62 public function isBlockedExtension(string $extension): bool
63 {
64 return $this->denied_ending === $extension;
65 }
66
67 public function prepareFileNameForConsumer(string $filename_with_extension): string
68 {
69 return $filename_with_extension;
70 }
71 };
72 }
73
74 public function testDeniedFileEnding(): void
75 {
76 $denied_ending = 'xml';
77 $resource_builder = $this->getResourceBuilder($denied_ending);
78
79 // EXPECTED VALUES
80 $expected_file_name = 'info.' . $denied_ending;
81
82 // MOCK
83 [$upload_result, $info_resolver, $identification] = $this->mockResourceAndRevision(
84 $expected_file_name,
85 "",
86 0,
87 1,
88 0
89 );
90
91 // RUN
92 $resource = $resource_builder->new(
94 $info_resolver
95 );
96
97 $this->expectException(FileNamePolicyException::class);
98 $resource_builder->store($resource);
99 }
100
101 public function testValidFileEnding(): void
102 {
103 $denied_ending = 'xml';
104 $resource_builder = $this->getResourceBuilder($denied_ending);
105
106 // EXPECTED VALUES
107 $expected_file_name = 'info.pdf';
108
109 // MOCK
110 [$upload_result, $info_resolver, $identification] = $this->mockResourceAndRevision(
111 $expected_file_name,
112 "",
113 0,
114 1,
115 0
116 );
117
118 // RUN
119 $resource = $resource_builder->new(
121 $info_resolver
122 );
123
124 $resource_builder->store($resource);
125 }
126}
mockResourceAndRevision(string $expected_file_name, string $expected_mime_type, int $expected_size, int $expected_version_number, int $expected_owner_id)
new(UploadResult $result, InfoResolver $info_resolver, ResourceType $type=ResourceType::SINGLE_FILE)
@inheritDoc
store(StorableResource $resource)
@description after you have modified a resource, you can store it here
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...