aboutsummaryrefslogtreecommitdiffstats
path: root/build/integration/features/bootstrap/Trashbin.php
blob: 6c8fd5e4fb6fb6590ebbe0a4a45d9fa45d6cfd00 (plain)
1
2
3
4
5
6
7
8
9
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-
<?php
/**
 * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2017 ownCloud GmbH
 * SPDX-License-Identifier: AGPL-3.0-only
 */
use DMS\PHPUnitExtensions\ArraySubset\Assert as AssertArraySubset;
use PHPUnit\Framework\Assert;

require __DIR__ . '/../../vendor/autoload.php';

/**
 * Trashbin functions
 */
trait Trashbin {
	// WebDav trait is expected to be used in the class that uses this trait.

	/**
	 * @When User :user empties trashbin
	 * @param string $user user
	 */
	public function emptyTrashbin($user) {
		$client = $this->getSabreClient($user);
		$response = $client->request('DELETE', $this->makeSabrePath($user, 'trash', 'trashbin'));
		Assert::assertEquals(204, $response['statusCode']);
	}

	private function findFullTrashname($user, $name) {
		$rootListing = $this->listTrashbinFolder($user, '/');

		foreach ($rootListing as $href => $rootItem) {
			if ($rootItem['{http://nextcloud.org/ns}trashbin-filename'] === $name) {
				return basename($href);
			}
		}

		return null;
	}

	/**
	 * Get the full /startofpath.dxxxx/rest/of/path from /startofpath/rest/of/path
	 */
	private function getFullTrashPath($user, $path) {
		if ($path !== '' && $path !== '/') {
			$parts = explode('/', $path);
			$fullName = $this->findFullTrashname($user, $parts[1]);
			if ($fullName === null) {
				Assert::fail("cant find $path in trash");
				return '/dummy_full_path_not_found';
			}
			$parts[1] = $fullName;

			$path = implode('/', $parts);
		}
		return $path;
	}

	/**
	 * List trashbin folder
	 *
	 * @param string $user user
	 * @param string $path path
	 * @return array response
	 */
	public function listTrashbinFolder($user, $path) {
		$path = $this->getFullTrashPath($user, $path);
		$client = $this->getSabreClient($user);

		$results = $client->propfind($this->makeSabrePath($user, 'trash' . $path, 'trashbin'), [
			'{http://nextcloud.org/ns}trashbin-filename',
			'{http://nextcloud.org/ns}trashbin-original-location',
			'{http://nextcloud.org/ns}trashbin-deletion-time'
		], 1);
		$results = array_filter($results, function (array $item) {
			return isset($item['{http://nextcloud.org/ns}trashbin-filename']);
		});
		if ($path !== '' && $path !== '/') {
			array_shift($results);
		}
		return $results;
	}

	/**
	 * @Then /^user "([^"]*)" in trash folder "([^"]*)" should have the following elements$/
	 * @param string $user
	 * @param string $folder
	 * @param \Behat\Gherkin\Node\TableNode|null $expectedElements
	 */
	public function checkTrashContents($user, $folder, $expectedElements) {
		$elementList = $this->listTrashbinFolder($user, $folder);
		$trashContent = array_filter(array_map(function (array $item) {
			return $item['{http://nextcloud.org/ns}trashbin-filename'];
		}, $elementList));
		if ($expectedElements instanceof \Behat\Gherkin\Node\TableNode) {
			$elementRows = $expectedElements->getRows();
			$elementsSimplified = $this->simplifyArray($elementRows);
			foreach ($elementsSimplified as $expectedElement) {
				$expectedElement = ltrim($expectedElement, '/');
				if (array_search($expectedElement, $trashContent) === false) {
					Assert::fail("$expectedElement" . ' is not in trash listing');
				}
			}
		}
	}

	/**
	 * @Then /^as "([^"]*)" the (file|folder) "([^"]*)" exists in trash$/
	 * @param string $user
	 * @param string $type
	 * @param string $file
	 */
	public function checkTrashContains($user, $type, $file) {
		$parent = dirname($file);
		if ($parent === '.') {
			$parent = '/';
		}
		$name = basename($file);
		$elementList = $this->listTrashbinFolder($user, $parent);
		$trashContent = array_filter(array_map(function (array $item) {
			return $item['{http://nextcloud.org/ns}trashbin-filename'];
		}, $elementList));

		AssertArraySubset::assertArraySubset([$name], array_values($trashContent));
	}

	/**
	 * @Then /^user "([^"]*)" in trash folder "([^"]*)" should have (\d+) elements?$/
	 * @param string $user
	 * @param string $folder
	 * @param \Behat\Gherkin\Node\TableNode|null $expectedElements
	 */
	public function checkTrashSize($user, $folder, $expectedCount) {
		$elementList = $this->listTrashbinFolder($user, $folder);
		Assert::assertEquals($expectedCount, count($elementList));
	}

	/**
	 * @When /^user "([^"]*)" in restores "([^"]*)" from trash$/
	 * @param string $user
	 * @param string $file
	 */
	public function restoreFromTrash($user, $file) {
		$file = $this->getFullTrashPath($user, $file);
		$url = $this->makeSabrePath($user, 'trash' . $file, 'trashbin');
		$client = $this->getSabreClient($user);
		$response = $client->request('MOVE', $url, null, [
			'Destination' => $this->makeSabrePath($user, 'restore/' . basename($file), 'trashbin'),
		]);
		Assert::assertEquals(201, $response['statusCode']);
		return;
	}
}