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