adrema/tests/FileshareTestCase.php

82 lines
2.3 KiB
PHP
Raw Normal View History

2024-06-27 00:00:29 +02:00
<?php
namespace Tests;
2024-06-28 22:26:38 +02:00
use Illuminate\Filesystem\FilesystemAdapter;
2024-06-27 00:00:29 +02:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
2024-06-28 22:26:38 +02:00
use Illuminate\Support\Facades\Storage;
2024-06-28 23:56:34 +02:00
use League\Flysystem\DirectoryAttributes;
2024-06-28 22:26:38 +02:00
use League\Flysystem\Filesystem;
use League\Flysystem\WebDAV\WebDAVAdapter;
use Sabre\DAV\Client;
2024-06-28 23:56:34 +02:00
use Throwable;
2024-06-27 00:00:29 +02:00
abstract class FileshareTestCase extends TestCase
{
use DatabaseTransactions;
2024-06-27 10:52:45 +02:00
protected string $adminUser = 'admin';
protected string $adminPassword = 'admin';
2024-06-27 00:00:29 +02:00
2024-06-28 22:26:38 +02:00
/**
* @var array<string, string>
*/
protected array $passwords = [];
2024-06-27 00:00:29 +02:00
public function setUp(): void
{
parent::setUp();
foreach ($this->http()->get('/ocs/v1.php/cloud/users?format=json')->json('ocs.data.users') as $user) {
if ($user === $this->adminUser) {
continue;
}
$this->http()->delete('/ocs/v1.php/cloud/users/' . $user);
}
}
public function withOwncloudUser(string $username, string $password): self
{
2024-06-28 22:26:38 +02:00
$this->passwords[$username] = $password;
2024-06-27 00:00:29 +02:00
$this->http()->asForm()->post('/ocs/v1.php/cloud/users?format=json', ['password' => $password, 'userid' => $username]);
return $this;
}
private function http(): PendingRequest
{
return Http::withOptions(['base_uri' => env('TEST_OWNCLOUD_DOMAIN')])->withBasicAuth($this->adminUser, $this->adminPassword)->acceptJson();
}
2024-06-28 22:26:38 +02:00
/**
* @param array<int, string> $dirs
*/
protected function withDirs(string $username, array $dirs): self
{
$adapter = $this->adapter($username);
2024-06-28 23:56:34 +02:00
foreach ($adapter->directories('/') as $directory) {
2024-06-28 22:26:38 +02:00
$adapter->deleteDirectory($directory);
}
2024-06-28 23:56:34 +02:00
foreach ($dirs as $dir) {
$adapter->makeDirectory($dir);
}
return $this;
2024-06-28 22:26:38 +02:00
}
private function adapter(string $username): FilesystemAdapter
{
$adapter = new WebDAVAdapter(new Client([
'baseUri' => env('TEST_OWNCLOUD_DOMAIN') . '/remote.php/dav/files/' . $username,
'userName' => $username,
'password' => $this->passwords[$username],
]), '/remote.php/dav/files/' . $username);
return new FilesystemAdapter(new Filesystem($adapter), $adapter);
}
2024-06-27 00:00:29 +02:00
}