ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
FileNamePolicyTest.php
Go to the documentation of this file.
1 <?php
2 
4 
8 
14 {
19  protected function getResourceBuilder(string $denied_ending) : ResourceBuilder
20  {
21  $policy = $this->getFileNamePolicy($denied_ending);
23  $this->storage_handler_factory,
24  $this->revision_repository,
25  $this->resource_repository,
26  $this->information_repository,
27  $this->stakeholder_repository,
28  $this->locking,
29  $policy
30  );
31  return $resource_builder;
32  }
33 
37  protected function getFileNamePolicy(string $denied_ending)
38  {
39  return new class($denied_ending) implements FileNamePolicy {
40  public function __construct(string $denied_ending)
41  {
42  $this->denied_ending = $denied_ending;
43  }
44 
45  public function check(string $extension) : bool
46  {
47  if ($this->denied_ending === $extension) {
48  throw new FileNamePolicyException('ERROR');
49  }
50  return true;
51  }
52 
53  public function isValidExtension(string $extension) : bool
54  {
55  return $this->denied_ending !== $extension;
56  }
57 
58  public function isBlockedExtension(string $extension) : bool
59  {
60  return $this->denied_ending === $extension;
61  }
62 
63  public function prepareFileNameForConsumer(string $filename_with_extension) : string
64  {
65  return $filename_with_extension;
66  }
67 
68  };
69  }
70 
71  public function testDeniedFileEnding() : void
72  {
73  $denied_ending = 'xml';
74  $resource_builder = $this->getResourceBuilder($denied_ending);
75 
76  // EXPECTED VALUES
77  $expected_file_name = 'info.' . $denied_ending;
78 
79  // MOCK
80  list($upload_result, $info_resolver, $identification) = $this->mockResourceAndRevision(
81  $expected_file_name,
82  "",
83  0, 1, 0
84  );
85 
86  // RUN
87  $resource = $resource_builder->new(
89  $info_resolver
90  );
91 
92  $this->expectException(FileNamePolicyException::class);
93  $resource_builder->store($resource);
94  }
95 
96  public function testValidFileEnding() : void
97  {
98  $denied_ending = 'xml';
99  $resource_builder = $this->getResourceBuilder($denied_ending);
100 
101  // EXPECTED VALUES
102  $expected_file_name = 'info.pdf';
103 
104  // MOCK
105  list($upload_result, $info_resolver, $identification) = $this->mockResourceAndRevision(
106  $expected_file_name,
107  "",
108  0, 1, 0
109  );
110 
111  // RUN
112  $resource = $resource_builder->new(
114  $info_resolver
115  );
116 
117  $resource_builder->store($resource);
118  }
119 }
120 
mockResourceAndRevision(string $expected_file_name, string $expected_mime_type, int $expected_size, int $expected_version_number, int $expected_owner_id)
__construct($a_client_id=0)
Constructor setup ILIAS global object public.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...