Skip to content

Commit

Permalink
Added translation helper with code reformatting over copied files
Browse files Browse the repository at this point in the history
Signed-off-by: Kiran Parajuli <[email protected]>
  • Loading branch information
kiranparajuli589 committed Dec 27, 2022
1 parent 1cc2b64 commit 025a07d
Show file tree
Hide file tree
Showing 29 changed files with 13,421 additions and 13,032 deletions.
407 changes: 207 additions & 200 deletions tests/TestHelpers/HttpRequestHelper.php

Large diffs are not rendered by default.

226 changes: 122 additions & 104 deletions tests/TestHelpers/OcisHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@

use Exception;
use GuzzleHttp\Exception\GuzzleException;
use function array_diff;
use function closedir;
use function copy;
use function exec;
use function file_exists;
use function getenv;
use function is_dir;
use function mkdir;
use function opendir;
use function readdir;
use function rmdir;
use function scandir;
use function sprintf;
use function str_replace;
use function strtoupper;
use function trim;
use function unlink;

/**
* Class OcisHelper
Expand All @@ -36,51 +53,77 @@ class OcisHelper {
/**
* @return bool
*/
public static function isTestingOnOcis():bool {
return (\getenv("TEST_OCIS") === "true");
public static function isTestingOnOc10(): bool {
return (!self::isTestingOnOcisOrReva());
}

/**
* @return bool
*/
public static function isTestingOnReva():bool {
return (\getenv("TEST_REVA") === "true");
public static function isTestingOnOcisOrReva(): bool {
return (self::isTestingOnOcis() || self::isTestingOnReva());
}

/**
* @return bool
*/
public static function isTestingOnOcisOrReva():bool {
return (self::isTestingOnOcis() || self::isTestingOnReva());
public static function isTestingOnOcis(): bool {
return (getenv("TEST_OCIS") === "true");
}

/**
* @return bool
*/
public static function isTestingOnOc10():bool {
return (!self::isTestingOnOcisOrReva());
public static function isTestingOnReva(): bool {
return (getenv("TEST_REVA") === "true");
}

/**
* @return bool
*/
public static function isTestingParallelDeployment(): bool {
return (\getenv("TEST_PARALLEL_DEPLOYMENT") === "true");
return (getenv("TEST_PARALLEL_DEPLOYMENT") === "true");
}

/**
* @return bool
*/
public static function isTestingWithGraphApi(): bool {
return \getenv('TEST_WITH_GRAPH_API') === 'true';
return getenv('TEST_WITH_GRAPH_API') === 'true';
}

/**
* @param string|null $user
*
* @return void
* @throws Exception
*/
public static function deleteRevaUserData(?string $user = ""): void {
$deleteCmd = self::getDeleteUserDataCommand();
if ($deleteCmd === false) {
if (self::getStorageDriver() === "OWNCLOUD") {
self::recurseRmdir(self::getOcisRevaDataRoot() . $user);
}
return;
}
if (self::getStorageDriver() === "EOS") {
$deleteCmd = str_replace(
"%s",
$user[0] . '/' . $user,
$deleteCmd
);
} else {
$deleteCmd = sprintf($deleteCmd, $user);
}
exec($deleteCmd);
}

/**
* @return bool|string false if no command given or the command as string
*/
public static function getDeleteUserDataCommand() {
$cmd = \getenv("DELETE_USER_DATA_CMD");
if ($cmd === false || \trim($cmd) === "") {
$cmd = getenv("DELETE_USER_DATA_CMD");
if ($cmd === false || trim($cmd) === "") {
return false;
}
return $cmd;
Expand All @@ -90,12 +133,12 @@ public static function getDeleteUserDataCommand() {
* @return string
* @throws Exception
*/
public static function getStorageDriver():string {
$storageDriver = (\getenv("STORAGE_DRIVER"));
public static function getStorageDriver(): string {
$storageDriver = (getenv("STORAGE_DRIVER"));
if ($storageDriver === false) {
return "OWNCLOUD";
}
$storageDriver = \strtoupper($storageDriver);
$storageDriver = strtoupper($storageDriver);
if ($storageDriver !== "OCIS" && $storageDriver !== "EOS" && $storageDriver !== "OWNCLOUD" && $storageDriver !== "S3NG") {
throw new Exception(
"Invalid storage driver. " .
Expand All @@ -106,29 +149,37 @@ public static function getStorageDriver():string {
}

/**
* @param string|null $user
* @param string|null $dir
*
* @return void
* @throws Exception
* @return bool
*/
public static function deleteRevaUserData(?string $user = ""):void {
$deleteCmd = self::getDeleteUserDataCommand();
if ($deleteCmd === false) {
if (self::getStorageDriver() === "OWNCLOUD") {
self::recurseRmdir(self::getOcisRevaDataRoot() . $user);
private static function recurseRmdir(?string $dir): bool {
if (file_exists($dir) === true) {
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
if (is_dir("$dir/$file")) {
self::recurseRmdir("$dir/$file");
} else {
unlink("$dir/$file");
}
}
return;
return rmdir($dir);
}
if (self::getStorageDriver() === "EOS") {
$deleteCmd = \str_replace(
"%s",
$user[0] . '/' . $user,
$deleteCmd
);
} else {
$deleteCmd = \sprintf($deleteCmd, $user);
return true;
}

/**
* @return string
*/
private static function getOcisRevaDataRoot(): string {
$root = getenv("OCIS_REVA_DATA_ROOT");
if (($root === false || $root === "") && self::isTestingOnOcisOrReva()) {
$root = "/var/tmp/ocis/owncloud/";
}
\exec($deleteCmd);
if (!file_exists($root)) {
echo "WARNING: reva data root folder ($root) does not exist\n";
}
return $root;
}

/**
Expand All @@ -140,19 +191,19 @@ public static function deleteRevaUserData(?string $user = ""):void {
*
* @return void
*/
public static function recurseCopy(?string $source, ?string $destination):void {
$dir = \opendir($source);
@\mkdir($destination);
while (($file = \readdir($dir)) !== false) {
public static function recurseCopy(?string $source, ?string $destination): void {
$dir = opendir($source);
@mkdir($destination);
while (($file = readdir($dir)) !== false) {
if (($file != '.') && ($file != '..')) {
if (\is_dir($source . '/' . $file)) {
if (is_dir($source . '/' . $file)) {
self::recurseCopy($source . '/' . $file, $destination . '/' . $file);
} else {
\copy($source . '/' . $file, $destination . '/' . $file);
copy($source . '/' . $file, $destination . '/' . $file);
}
}
}
\closedir($dir);
closedir($dir);
}

/**
Expand All @@ -175,7 +226,7 @@ public static function recurseUpload(
?string $password,
?string $xRequestId = '',
?string $destination = ''
):void {
): void {
if ($destination !== '') {
$response = WebDavHelper::makeDavRequest(
$baseUrl,
Expand All @@ -191,12 +242,12 @@ public static function recurseUpload(
}
}

$dir = \opendir($source);
while (($file = \readdir($dir)) !== false) {
$dir = opendir($source);
while (($file = readdir($dir)) !== false) {
if (($file != '.') && ($file != '..')) {
$sourcePath = $source . '/' . $file;
$destinationPath = $destination . '/' . $file;
if (\is_dir($sourcePath)) {
if (is_dir($sourcePath)) {
self::recurseUpload(
$baseUrl,
$sourcePath,
Expand Down Expand Up @@ -224,118 +275,85 @@ public static function recurseUpload(
}
}
}
\closedir($dir);
}

/**
* @return int
*/
public static function getLdapPort():int {
$port = \getenv("REVA_LDAP_PORT");
return $port ? (int)$port : 636;
closedir($dir);
}

/**
* @return bool
*/
public static function useSsl():bool {
$useSsl = \getenv("REVA_LDAP_USESSL");
public static function useSsl(): bool {
$useSsl = getenv("REVA_LDAP_USESSL");
if ($useSsl === false) {
return (self::getLdapPort() === 636);
} else {
return $useSsl === "true";
}
}

/**
* @return int
*/
public static function getLdapPort(): int {
$port = getenv("REVA_LDAP_PORT");
return $port ? (int)$port : 636;
}

/**
* @return string
*/
public static function getBaseDN():string {
$dn = \getenv("REVA_LDAP_BASE_DN");
public static function getBaseDN(): string {
$dn = getenv("REVA_LDAP_BASE_DN");
return $dn ? $dn : "dc=owncloud,dc=com";
}

/**
* @return string
*/
public static function getGroupsOU():string {
$ou = \getenv("REVA_LDAP_GROUPS_OU");
public static function getGroupsOU(): string {
$ou = getenv("REVA_LDAP_GROUPS_OU");
return $ou ? $ou : "TestGroups";
}

/**
* @return string
*/
public static function getUsersOU():string {
$ou = \getenv("REVA_LDAP_USERS_OU");
public static function getUsersOU(): string {
$ou = getenv("REVA_LDAP_USERS_OU");
return $ou ? $ou : "TestUsers";
}

/**
* @return string
*/
public static function getGroupSchema():string {
$schema = \getenv("REVA_LDAP_GROUP_SCHEMA");
public static function getGroupSchema(): string {
$schema = getenv("REVA_LDAP_GROUP_SCHEMA");
return $schema ? $schema : "rfc2307";
}

/**
* @return string
*/
public static function getHostname():string {
$hostname = \getenv("REVA_LDAP_HOSTNAME");
public static function getHostname(): string {
$hostname = getenv("REVA_LDAP_HOSTNAME");
return $hostname ? $hostname : "localhost";
}

/**
* @return string
*/
public static function getBindDN():string {
$dn = \getenv("REVA_LDAP_BIND_DN");
public static function getBindDN(): string {
$dn = getenv("REVA_LDAP_BIND_DN");
return $dn ? $dn : "cn=admin,dc=owncloud,dc=com";
}

/**
* @return string
*/
public static function getBindPassword():string {
$pw = \getenv("REVA_LDAP_BIND_PASSWORD");
public static function getBindPassword(): string {
$pw = getenv("REVA_LDAP_BIND_PASSWORD");
return $pw ? $pw : "";
}

/**
* @return string
*/
private static function getOcisRevaDataRoot():string {
$root = \getenv("OCIS_REVA_DATA_ROOT");
if (($root === false || $root === "") && self::isTestingOnOcisOrReva()) {
$root = "/var/tmp/ocis/owncloud/";
}
if (!\file_exists($root)) {
echo "WARNING: reva data root folder ($root) does not exist\n";
}
return $root;
}

/**
* @param string|null $dir
*
* @return bool
*/
private static function recurseRmdir(?string $dir):bool {
if (\file_exists($dir) === true) {
$files = \array_diff(\scandir($dir), ['.', '..']);
foreach ($files as $file) {
if (\is_dir("$dir/$file")) {
self::recurseRmdir("$dir/$file");
} else {
\unlink("$dir/$file");
}
}
return \rmdir($dir);
}
return true;
}

/**
* On Eos storage backend when the user data is cleared after test run
* Running another test immediately fails. So Send this request to create user home directory
Expand All @@ -353,7 +371,7 @@ public static function createEOSStorageHome(
?string $user,
?string $password,
?string $xRequestId = ''
):void {
): void {
HttpRequestHelper::get(
$baseUrl . "/ocs/v2.php/apps/notifications/api/v1/notifications",
$xRequestId,
Expand Down
Loading

0 comments on commit 025a07d

Please sign in to comment.