ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
LegacyPathHelperTest.php
Go to the documentation of this file.
1 <?php
2 
19 namespace ILIAS\Filesystem\Util;
20 
24 use Mockery;
27 
37 class LegacyPathHelperTest extends TestCase
38 {
39  private string $libsPath;
40  private string $storagePath;
41  private string $webPath;
42  private \Mockery\MockInterface|\ILIAS\Filesystem\Filesystems $filesystemsMock;
43 
44 
48  protected function setUp(): void
49  {
50  parent::setUp();
51 
52  $iliasAbsolutePath = '/dummy/var/www/html/ilias';
53  $dataDir = '/dummy/var/www/ildata';
54  $webDir = 'data';
55  $clientId = 'default';
56 
57  //constants needed for test subject
58  define("CLIENT_DATA_DIR", $dataDir . '/' . $clientId);
59  define("CLIENT_WEB_DIR", $iliasAbsolutePath . '/' . $webDir . '/' . $clientId);
60  define("ILIAS_ABSOLUTE_PATH", $iliasAbsolutePath);
61  define("ILIAS_WEB_DIR", $webDir);
62  define("CLIENT_ID", 'default');
63  $this->libsPath = $iliasAbsolutePath . '/' . 'libs';
64  $this->webPath = CLIENT_WEB_DIR;
65  $this->storagePath = CLIENT_DATA_DIR;
66 
67  //create mock DI container
68  $this->filesystemsMock = \Mockery::mock(Filesystems::class);
69 
70  $containerMock = Mockery::mock(Container::class);
71  $containerMock->shouldReceive('filesystem')
72  ->withNoArgs()
73  ->zeroOrMoreTimes()
74  ->andReturn($this->filesystemsMock);
75 
76  $GLOBALS['DIC'] = $containerMock;
77  }
78 
79 
85  {
86  $target = $this->webPath . '/testtarget';
87 
88  $this->filesystemsMock
89  ->shouldReceive('web')
90  ->once()
91  ->andReturn(Mockery::mock(Filesystem::class));
92 
93  $filesystem = LegacyPathHelper::deriveFilesystemFrom($target);
94  $this->assertTrue($filesystem instanceof Filesystem, 'Expecting filesystem instance.');
95  }
96 
97 
103  {
104  $target = $this->storagePath . '/testtarget';
105 
106  $this->filesystemsMock
107  ->shouldReceive('storage')
108  ->once()
109  ->andReturn(Mockery::mock(Filesystem::class));
110 
111  $filesystem = LegacyPathHelper::deriveFilesystemFrom($target);
112  $this->assertTrue($filesystem instanceof Filesystem, 'Expecting filesystem instance.');
113  }
114 
115 
121  {
122  $target = './libs/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 
138  {
139  $target = $this->libsPath . 'libs/bower/bower_components/mediaelement/build';
140 
141  $this->filesystemsMock
142  ->shouldReceive('libs')
143  ->once()
144  ->andReturn(Mockery::mock(Filesystem::class));
145 
146  $filesystem = LegacyPathHelper::deriveFilesystemFrom($target);
147  $this->assertTrue($filesystem instanceof Filesystem, 'Expecting filesystem instance.');
148  }
149 
150 
156  {
157  $target = '/invalid/path/to/testtarget';
158 
159  $this->expectException(\InvalidArgumentException::class);
160  $this->expectExceptionMessage("Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$target}'");
161 
163  }
164 
165 
171  {
172  $expectedPath = 'testtarget/subdir';
173  $target = $this->webPath . '/' . $expectedPath;
174 
175  $result = LegacyPathHelper::createRelativePath($target);
176  $this->assertEquals($expectedPath, $result);
177  }
178 
179 
185  {
186  $expectedPath = 'testtarget/subdir';
187  $target = $this->storagePath . '/' . $expectedPath;
188 
189  $result = LegacyPathHelper::createRelativePath($target);
190  $this->assertEquals($expectedPath, $result);
191  }
192 
193 
199  {
200  $target = '/invalid/path/to/target';
201 
202  $this->expectException(\InvalidArgumentException::class);
203  $this->expectExceptionMessage("Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$target}'");
204 
206  }
207 }
static createRelativePath(string $absolute_path)
Creates a relative path from an absolute path which starts with a valid storage location.
$clientId
Definition: ltiregend.php:27
const CLIENT_DATA_DIR
Definition: constants.php:46
$GLOBALS["DIC"]
Definition: wac.php:31
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.
Mockery MockInterface ILIAS Filesystem Filesystems $filesystemsMock
The Filesystems interface defines the access methods which can be used to fetch the different filesys...
Definition: Filesystems.php:29