ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
LegacyPathHelperTestTBD.php
Go to the documentation of this file.
1 <?php
2 
19 namespace ILIAS\Filesystem\Util;
20 
30 use Mockery;
33 
38 #[BackupGlobals(false)]
39 #[BackupStaticProperties(false)]
40 #[PreserveGlobalState(false)]
41 #[RunTestsInSeparateProcesses]
42 class LegacyPathHelperTest extends TestCase
43 {
44  public $libsPath;
45  private string $vendorPath;
46  private string $storagePath;
47  private string $webPath;
48  private MockInterface|Filesystems $filesystemsMock;
49 
50 
54  protected function setUp(): void
55  {
56  parent::setUp();
57 
58  $iliasAbsolutePath = '/dummy/var/www/html/ilias';
59  $dataDir = '/dummy/var/www/ildata';
60  $webDir = 'public/data';
61  $clientId = 'default';
62 
63  //constants needed for test subject
64  define("CLIENT_DATA_DIR", $dataDir . '/' . $clientId);
65  define("CLIENT_WEB_DIR", $iliasAbsolutePath . '/' . $webDir . '/' . $clientId);
66  define("ILIAS_ABSOLUTE_PATH", $iliasAbsolutePath);
67  define("ILIAS_WEB_DIR", $webDir);
68  define("CLIENT_ID", 'default');
69  $this->libsPath = $iliasAbsolutePath . '/' . 'vendor';
70  $this->webPath = CLIENT_WEB_DIR;
71  $this->storagePath = CLIENT_DATA_DIR;
72 
73  //create mock DI container
74  $this->filesystemsMock = \Mockery::mock(Filesystems::class);
75 
76  $containerMock = Mockery::mock(Container::class);
77  $containerMock->shouldReceive('filesystem')
78  ->withNoArgs()
79  ->zeroOrMoreTimes()
80  ->andReturn($this->filesystemsMock);
81 
82  $GLOBALS['DIC'] = $containerMock;
83  }
84 
85 
86  #[Test]
87 
89  {
90  $target = $this->webPath . '/testtarget';
91 
92  $this->filesystemsMock
93  ->shouldReceive('web')
94  ->once()
95  ->andReturn(Mockery::mock(Filesystem::class));
96 
97  $filesystem = LegacyPathHelper::deriveFilesystemFrom($target);
98  $this->assertTrue($filesystem instanceof Filesystem, 'Expecting filesystem instance.');
99  }
100 
101 
102  #[Test]
103 
105  {
106  $target = $this->storagePath . '/testtarget';
107 
108  $this->filesystemsMock
109  ->shouldReceive('storage')
110  ->once()
111  ->andReturn(Mockery::mock(Filesystem::class));
112 
113  $filesystem = LegacyPathHelper::deriveFilesystemFrom($target);
114  $this->assertTrue($filesystem instanceof Filesystem, 'Expecting filesystem instance.');
115  }
116 
117 
118  #[Test]
119 
121  {
122  $target = './vendor/bower/bower_components/mediaelement/build';
123 
124  $this->filesystemsMock
125  ->shouldReceive('libs')
126  ->once()
127  ->andReturn(Mockery::mock(Filesystem::class));
128 
129  $filesystem = LegacyPathHelper::deriveFilesystemFrom($target);
130  $this->assertTrue($filesystem instanceof Filesystem, 'Expecting filesystem instance.');
131  }
132 
133  #[Test]
134 
136  {
137  $target = $this->libsPath . 'vendor/bower/bower_components/mediaelement/build';
138 
139  $this->filesystemsMock
140  ->shouldReceive('libs')
141  ->once()
142  ->andReturn(Mockery::mock(Filesystem::class));
143 
144  $filesystem = LegacyPathHelper::deriveFilesystemFrom($target);
145  $this->assertTrue($filesystem instanceof Filesystem, 'Expecting filesystem instance.');
146  }
147 
148 
149  #[Test]
150 
152  {
153  $target = '/invalid/path/to/testtarget';
154 
155  $this->expectException(\InvalidArgumentException::class);
156  $this->expectExceptionMessage("Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$target}'");
157 
159  }
160 
161 
162  #[Test]
163 
165  {
166  $expectedPath = 'testtarget/subdir';
167  $target = $this->webPath . '/' . $expectedPath;
168 
169  $result = LegacyPathHelper::createRelativePath($target);
170  $this->assertEquals($expectedPath, $result);
171  }
172 
173 
174  #[Test]
175 
177  {
178  $expectedPath = 'testtarget/subdir';
179  $target = $this->storagePath . '/' . $expectedPath;
180 
181  $result = LegacyPathHelper::createRelativePath($target);
182  $this->assertEquals($expectedPath, $result);
183  }
184 
185 
186  #[Test]
187 
189  {
190  $target = '/invalid/path/to/target';
191 
192  $this->expectException(\InvalidArgumentException::class);
193  $this->expectExceptionMessage("Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$target}'");
194 
196  }
197 }
static createRelativePath(string $absolute_path)
Creates a relative path from an absolute path which starts with a valid storage location.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$clientId
Definition: ltiregend.php:25
const CLIENT_DATA_DIR
Definition: constants.php:46
$GLOBALS["DIC"]
Definition: wac.php:53
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const CLIENT_WEB_DIR
Definition: constants.php:47
static deriveFilesystemFrom(string $absolute_path)
Tries to fetch the filesystem responsible for the absolute path.