adrema/tests/FileshareTestCase.php

114 lines
4.0 KiB
PHP
Raw Normal View History

2024-06-27 00:00:29 +02:00
<?php
namespace Tests;
2024-07-13 22:30:34 +02:00
use App\Fileshare\ConnectionTypes\ConnectionType;
use App\Fileshare\ConnectionTypes\NextcloudConnection;
use App\Fileshare\ConnectionTypes\OwncloudConnection;
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 League\Flysystem\Filesystem;
use League\Flysystem\WebDAV\WebDAVAdapter;
use Sabre\DAV\Client;
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-12-22 18:32:05 +01:00
protected function setUp(): void
2024-06-27 00:00:29 +02:00
{
parent::setUp();
2024-07-13 22:30:34 +02:00
foreach ($this->http(OwncloudConnection::class)->get('/ocs/v1.php/cloud/users?format=json')->json('ocs.data.users') as $user) {
2024-06-27 00:00:29 +02:00
if ($user === $this->adminUser) {
continue;
}
2024-07-13 22:30:34 +02:00
$this->http(OwncloudConnection::class)->delete('/ocs/v1.php/cloud/users/' . $user);
}
foreach ($this->http(NextcloudConnection::class)->get('/ocs/v2.php/cloud/users')->json('ocs.data.users') as $user) {
if ($user === $this->adminUser) {
continue;
}
$this->http(NextcloudConnection::class)->delete('/ocs/v2.php/cloud/users/' . $user);
2024-06-27 00:00:29 +02:00
}
}
2024-07-13 22:30:34 +02:00
public function withUser(string $username, string $password): self
2024-06-27 00:00:29 +02:00
{
2024-06-28 22:26:38 +02:00
$this->passwords[$username] = $password;
2024-07-13 22:30:34 +02:00
$this->http(OwncloudConnection::class)->asForm()->post('/ocs/v1.php/cloud/users?format=json', ['password' => $password, 'userid' => $username]);
$this->http(NextcloudConnection::class)->post('/ocs/v2.php/cloud/users', ['password' => $password, 'userid' => $username]);
2024-06-27 00:00:29 +02:00
return $this;
}
2024-07-13 22:30:34 +02:00
/**
* @param class-string<ConnectionType> $connection
*/
private function http(string $connection): PendingRequest
2024-06-27 00:00:29 +02:00
{
2024-07-13 22:30:34 +02:00
if ($connection === OwncloudConnection::class) {
return Http::withOptions(['base_uri' => env('TEST_OWNCLOUD_DOMAIN')])->withBasicAuth($this->adminUser, $this->adminPassword)->acceptJson();
}
2024-07-14 00:11:18 +02:00
return Http::withOptions(['base_uri' => env('TEST_NEXTCLOUD_DOMAIN')])->withHeaders(['OCS-APIRequest' => 'true'])->withBasicAuth($this->adminUser, $this->adminPassword)->acceptJson();
2024-06-27 00:00:29 +02:00
}
2024-06-28 22:26:38 +02:00
/**
* @param array<int, string> $dirs
*/
protected function withDirs(string $username, array $dirs): self
{
2024-07-13 22:30:34 +02:00
foreach ([NextcloudConnection::class, OwncloudConnection::class] as $connection) {
$adapter = $this->adapter($username, $connection);
2024-06-28 22:26:38 +02:00
2024-07-13 22:30:34 +02:00
foreach ($adapter->directories('/') as $directory) {
$adapter->deleteDirectory($directory);
}
2024-06-28 22:26:38 +02:00
2024-07-13 22:30:34 +02:00
foreach ($adapter->files('/') as $file) {
$adapter->delete($file);
}
2024-07-13 22:30:34 +02:00
foreach ($dirs as $dir) {
$adapter->makeDirectory($dir);
}
2024-06-28 23:56:34 +02:00
}
return $this;
2024-06-28 22:26:38 +02:00
}
2024-07-13 22:30:34 +02:00
/**
* @param class-string<ConnectionType> $connection
*/
private function adapter(string $username, string $connection): FilesystemAdapter
2024-06-28 22:26:38 +02:00
{
2024-07-13 22:30:34 +02:00
if ($connection === OwncloudConnection::class) {
$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);
2024-07-14 00:11:18 +02:00
} else {
2024-07-13 22:30:34 +02:00
$adapter = new WebDAVAdapter(new Client([
'baseUri' => env('TEST_NEXTCLOUD_DOMAIN') . '/remote.php/dav/files/' . $username,
'userName' => $username,
'password' => $this->passwords[$username],
]), '/remote.php/dav/files/' . $username);
}
2024-06-28 22:26:38 +02:00
return new FilesystemAdapter(new Filesystem($adapter), $adapter);
}
2024-06-27 00:00:29 +02:00
}